Skip to content

Commit 05cccdc

Browse files
committedAug 28, 2021
Auto merge of #88019 - inquisitivecrystal:macro-def, r=cjgillot
Treat macros as HIR items Macros have historically been treated differently from other items at the HIR level. This PR makes them just like normal items. There are a few special cases left over, which I've attempted to lay out below. By normalizing the treatment of macro items, this PR simplifies a fair bit of code and fixes some bugs at the same time. For more information, see #87406. r? `@cjgillot` ## Backwards incompatibility This is backwards incompatible in one small way. Due to a mistake, it was previously possible to apply stability attributes to an exported macro, even without enabling the `staged_api` feature. This never should have worked. After this PR, it will error, as it should. We could make it a warning instead, but that would require a special case for a feature that shouldn't ever have worked and is likely used by no or very few crates, so I'm not thrilled about the idea. ## Notes for reviewers ### Commit seperation I'd recommend reviewing this PR commit by commit. The commit chunking wasn't perfect, but it's better than looking at the combined diff, which is quite overwhelming. The compiler and standard library build after each commit, although tests do not necessarily pass and tools do not necessarily build till the end of the series. ### Special cases There are a few special cases that remain after this change. Here are the notable ones I remember: 1. Visibility works a bit differently for `macro_rules!` macros than other items, since they aren't generally marked with `pub` but instead with `#[macro_export]`. 2. Since `#[macro_export]` macros always have paths at the top level of the crate, some additional handling needs to be done on the reexport to top level. ### Performance impact I don't know for sure, but theses changes may slightly hurt performance. They create more work for the compiler in a few places. For instance, some operations that were previously run only for exported macros are now run for all macros. A perf run is probably advisable. For all I know we'll see performance improvements instead. :) ## Issues resolved This resolves #87406 (the tracking issue for this change). It also fixes several bugs: Fixes #59306. Fixes #73754. Fixes #87257.
2 parents 2031fd6 + b5a4141 commit 05cccdc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+338
-396
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4146,6 +4146,7 @@ dependencies = [
41464146
name = "rustc_privacy"
41474147
version = "0.0.0"
41484148
dependencies = [
4149+
"rustc_ast",
41494150
"rustc_attr",
41504151
"rustc_data_structures",
41514152
"rustc_errors",

‎compiler/rustc_ast_lowering/src/item.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
170170
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
171171
vec
172172
}
173-
ItemKind::MacroDef(..) => SmallVec::new(),
174173
ItemKind::Fn(..) | ItemKind::Impl(box ImplKind { of_trait: None, .. }) => {
175174
smallvec![i.id]
176175
}
@@ -212,28 +211,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
212211
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
213212
let mut ident = i.ident;
214213
let mut vis = self.lower_visibility(&i.vis, None);
215-
216-
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
217-
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
218-
let hir_id = self.lower_node_id(i.id);
219-
self.lower_attrs(hir_id, &i.attrs);
220-
let body = P(self.lower_mac_args(body));
221-
self.insert_macro_def(hir::MacroDef {
222-
ident,
223-
vis,
224-
def_id: hir_id.expect_owner(),
225-
span: i.span,
226-
ast: MacroDef { body, macro_rules },
227-
});
228-
} else {
229-
for a in i.attrs.iter() {
230-
let a = self.lower_attr(a);
231-
self.non_exported_macro_attrs.push(a);
232-
}
233-
}
234-
return None;
235-
}
236-
237214
let hir_id = self.lower_node_id(i.id);
238215
let attrs = self.lower_attrs(hir_id, &i.attrs);
239216
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
@@ -465,7 +442,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
465442
self.lower_generics(generics, ImplTraitContext::disallowed()),
466443
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
467444
),
468-
ItemKind::MacroDef(..) | ItemKind::MacCall(..) => {
445+
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
446+
let body = P(self.lower_mac_args(body));
447+
448+
hir::ItemKind::Macro(ast::MacroDef { body, macro_rules })
449+
}
450+
ItemKind::MacCall(..) => {
469451
panic!("`TyMac` should have been expanded by now")
470452
}
471453
}

0 commit comments

Comments
 (0)
Please sign in to comment.