Skip to content

Commit 6ba167a

Browse files
authored
Rollup merge of rust-lang#91795 - petrochenkov:nomacreexport, r=cjgillot
resolve/metadata: Stop encoding macros as reexports Supersedes rust-lang#88335. r? `@cjgillot`
2 parents 4b043fa + b91ec30 commit 6ba167a

File tree

25 files changed

+92
-49
lines changed

25 files changed

+92
-49
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
444444
),
445445
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
446446
let body = P(self.lower_mac_args(body));
447-
448-
hir::ItemKind::Macro(ast::MacroDef { body, macro_rules })
447+
let macro_kind = self.resolver.decl_macro_kind(self.resolver.local_def_id(id));
448+
hir::ItemKind::Macro(ast::MacroDef { body, macro_rules }, macro_kind)
449449
}
450450
ItemKind::MacCall(..) => {
451451
panic!("`TyMac` should have been expanded by now")

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_session::lint::LintBuffer;
6161
use rustc_session::parse::feature_err;
6262
use rustc_session::utils::{FlattenNonterminals, NtToTokenstream};
6363
use rustc_session::Session;
64-
use rustc_span::hygiene::ExpnId;
64+
use rustc_span::hygiene::{ExpnId, MacroKind};
6565
use rustc_span::source_map::{respan, DesugaringKind};
6666
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6767
use rustc_span::{Span, DUMMY_SP};
@@ -210,6 +210,8 @@ pub trait ResolverAstLowering {
210210
expn_id: ExpnId,
211211
span: Span,
212212
) -> LocalDefId;
213+
214+
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
213215
}
214216

215217
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_data_structures::fx::FxHashMap;
1515
use rustc_data_structures::sorted_map::SortedMap;
1616
use rustc_index::vec::IndexVec;
1717
use rustc_macros::HashStable_Generic;
18+
use rustc_span::hygiene::MacroKind;
1819
use rustc_span::source_map::Spanned;
1920
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2021
use rustc_span::{def_id::LocalDefId, BytePos, MultiSpan, Span, DUMMY_SP};
@@ -2803,7 +2804,7 @@ pub enum ItemKind<'hir> {
28032804
/// A function declaration.
28042805
Fn(FnSig<'hir>, Generics<'hir>, BodyId),
28052806
/// A MBE macro definition (`macro_rules!` or `macro`).
2806-
Macro(ast::MacroDef),
2807+
Macro(ast::MacroDef, MacroKind),
28072808
/// A module.
28082809
Mod(Mod<'hir>),
28092810
/// An external module, e.g. `extern { .. }`.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
575575
item.span,
576576
item.hir_id(),
577577
),
578-
ItemKind::Macro(_) => {
578+
ItemKind::Macro(..) => {
579579
visitor.visit_id(item.hir_id());
580580
}
581581
ItemKind::Mod(ref module) => {

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl<'a> State<'a> {
570570
self.end(); // need to close a box
571571
self.ann.nested(self, Nested::Body(body));
572572
}
573-
hir::ItemKind::Macro(ref macro_def) => {
573+
hir::ItemKind::Macro(ref macro_def, _) => {
574574
self.print_mac_def(macro_def, &item.ident, item.span, |state| {
575575
state.print_visibility(&item.vis)
576576
});

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::rmeta::table::{FixedSizeEncoding, Table};
55
use crate::rmeta::*;
66

77
use rustc_ast as ast;
8+
use rustc_ast::ptr::P;
89
use rustc_attr as attr;
910
use rustc_data_structures::captures::Captures;
1011
use rustc_data_structures::fx::FxHashMap;
@@ -1076,6 +1077,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10761077
res,
10771078
vis: ty::Visibility::Public,
10781079
span: ident.span,
1080+
macro_rules: false,
10791081
});
10801082
}
10811083
}
@@ -1087,17 +1089,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10871089
for child_index in children.decode((self, sess)) {
10881090
if let Some(ident) = self.opt_item_ident(child_index, sess) {
10891091
let kind = self.def_kind(child_index);
1090-
if matches!(kind, DefKind::Macro(..)) {
1091-
// FIXME: Macros are currently encoded twice, once as items and once as
1092-
// reexports. We ignore the items here and only use the reexports.
1093-
continue;
1094-
}
10951092
let def_id = self.local_def_id(child_index);
10961093
let res = Res::Def(kind, def_id);
10971094
let vis = self.get_visibility(child_index);
10981095
let span = self.get_span(child_index, sess);
1096+
let macro_rules = match kind {
1097+
DefKind::Macro(..) => match self.kind(child_index) {
1098+
EntryKind::MacroDef(_, macro_rules) => macro_rules,
1099+
_ => unreachable!(),
1100+
},
1101+
_ => false,
1102+
};
10991103

1100-
callback(ModChild { ident, res, vis, span });
1104+
callback(ModChild { ident, res, vis, span, macro_rules });
11011105

11021106
// For non-re-export structs and variants add their constructors to children.
11031107
// Re-export lists automatically contain constructors when necessary.
@@ -1109,7 +1113,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11091113
let ctor_res =
11101114
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
11111115
let vis = self.get_visibility(ctor_def_id.index);
1112-
callback(ModChild { ident, res: ctor_res, vis, span });
1116+
callback(ModChild {
1117+
ident,
1118+
res: ctor_res,
1119+
vis,
1120+
span,
1121+
macro_rules: false,
1122+
});
11131123
}
11141124
}
11151125
DefKind::Variant => {
@@ -1134,7 +1144,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11341144
vis = ty::Visibility::Restricted(crate_def_id);
11351145
}
11361146
}
1137-
callback(ModChild { ident, res: ctor_res, vis, span });
1147+
callback(ModChild {
1148+
ident,
1149+
res: ctor_res,
1150+
vis,
1151+
span,
1152+
macro_rules: false,
1153+
});
11381154
}
11391155
_ => {}
11401156
}
@@ -1402,9 +1418,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14021418
tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx)))
14031419
}
14041420

1405-
fn get_macro(self, id: DefIndex, sess: &Session) -> MacroDef {
1421+
fn get_macro(self, id: DefIndex, sess: &Session) -> ast::MacroDef {
14061422
match self.kind(id) {
1407-
EntryKind::MacroDef(macro_def) => macro_def.decode((self, sess)),
1423+
EntryKind::MacroDef(mac_args, macro_rules) => {
1424+
ast::MacroDef { body: P(mac_args.decode((self, sess))), macro_rules }
1425+
}
14081426
_ => bug!(),
14091427
}
14101428
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,8 +1406,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14061406

14071407
EntryKind::Fn(self.lazy(data))
14081408
}
1409-
hir::ItemKind::Macro(ref macro_def) => {
1410-
EntryKind::MacroDef(self.lazy(macro_def.clone()))
1409+
hir::ItemKind::Macro(ref macro_def, _) => {
1410+
EntryKind::MacroDef(self.lazy(&*macro_def.body), macro_def.macro_rules)
14111411
}
14121412
hir::ItemKind::Mod(ref m) => {
14131413
return self.encode_info_for_mod(item.def_id, m);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use decoder::Metadata;
22
use def_path_hash_map::DefPathHashMapRef;
33
use table::{Table, TableBuilder};
44

5-
use rustc_ast::{self as ast, MacroDef};
5+
use rustc_ast as ast;
66
use rustc_attr as attr;
77
use rustc_data_structures::svh::Svh;
88
use rustc_data_structures::sync::MetadataRef;
@@ -350,7 +350,7 @@ enum EntryKind {
350350
Fn(Lazy<FnData>),
351351
ForeignFn(Lazy<FnData>),
352352
Mod(Lazy<[ModChild]>),
353-
MacroDef(Lazy<MacroDef>),
353+
MacroDef(Lazy<ast::MacArgs>, /*macro_rules*/ bool),
354354
ProcMacro(MacroKind),
355355
Closure,
356356
Generator,

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_hir::*;
1414
use rustc_index::vec::Idx;
1515
use rustc_middle::hir::nested_filter;
1616
use rustc_span::def_id::StableCrateId;
17-
use rustc_span::hygiene::MacroKind;
1817
use rustc_span::source_map::Spanned;
1918
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2019
use rustc_span::Span;
@@ -232,7 +231,7 @@ impl<'hir> Map<'hir> {
232231
ItemKind::Static(..) => DefKind::Static,
233232
ItemKind::Const(..) => DefKind::Const,
234233
ItemKind::Fn(..) => DefKind::Fn,
235-
ItemKind::Macro(..) => DefKind::Macro(MacroKind::Bang),
234+
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
236235
ItemKind::Mod(..) => DefKind::Mod,
237236
ItemKind::OpaqueTy(..) => DefKind::OpaqueTy,
238237
ItemKind::TyAlias(..) => DefKind::TyAlias,

compiler/rustc_middle/src/metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ pub struct ModChild {
2121
pub vis: ty::Visibility,
2222
/// Span of the item.
2323
pub span: Span,
24+
/// A proper `macro_rules` item (not a reexport).
25+
pub macro_rules: bool,
2426
}

0 commit comments

Comments
 (0)