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

+2-2
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

+3-1
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

+2-1
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

+1-1
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

+1-1
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

+28-10
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

+2-2
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

+2-2
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

+1-2
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

+2
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
}

compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
19511951
// Historically we've run more checks on non-exported than exported macros,
19521952
// so this lets us continue to run them while maintaining backwards compatibility.
19531953
// In the long run, the checks should be harmonized.
1954-
if let ItemKind::Macro(ref macro_def) = item.kind {
1954+
if let ItemKind::Macro(ref macro_def, _) = item.kind {
19551955
let def_id = item.def_id.to_def_id();
19561956
if macro_def.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) {
19571957
check_non_exported_macro_for_invalid_attrs(self.tcx, item);

compiler/rustc_privacy/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
564564
// privacy and mark them reachable.
565565
DefKind::Macro(_) => {
566566
let item = self.tcx.hir().expect_item(def_id);
567-
if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }) = item.kind {
567+
if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }, _) = item.kind {
568568
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
569569
self.update(def_id, level);
570570
}
@@ -686,7 +686,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
686686
}
687687
}
688688
}
689-
hir::ItemKind::Macro(ref macro_def) => {
689+
hir::ItemKind::Macro(ref macro_def, _) => {
690690
self.update_reachability_from_macro(item.def_id, macro_def);
691691
}
692692
hir::ItemKind::ForeignMod { items, .. } => {

compiler/rustc_resolve/src/access_levels.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
133133
ast::ItemKind::Impl(..) => return,
134134

135135
// Only exported `macro_rules!` items are public, but they always are
136-
ast::ItemKind::MacroDef(..) => {
136+
ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
137137
let is_macro_export =
138138
item.attrs.iter().any(|attr| attr.has_name(sym::macro_export));
139139
if is_macro_export { Some(AccessLevel::Public) } else { None }
@@ -155,7 +155,8 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
155155
| ast::ItemKind::Struct(..)
156156
| ast::ItemKind::Union(..)
157157
| ast::ItemKind::Trait(..)
158-
| ast::ItemKind::TraitAlias(..) => {
158+
| ast::ItemKind::TraitAlias(..)
159+
| ast::ItemKind::MacroDef(..) => {
159160
if item.vis.kind.is_pub() {
160161
self.prev_level
161162
} else {

compiler/rustc_resolve/src/build_reduced_graph.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
940940
/// Builds the reduced graph for a single item in an external crate.
941941
fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
942942
let parent = self.parent_scope.module;
943-
let ModChild { ident, res, vis, span } = child;
943+
let ModChild { ident, res, vis, span, macro_rules } = child;
944944
let res = res.expect_non_local();
945945
let expansion = self.parent_scope.expansion;
946946
// Record primary definitions.
@@ -972,7 +972,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
972972
_,
973973
) => self.r.define(parent, ident, ValueNS, (res, vis, span, expansion)),
974974
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
975-
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
975+
if !macro_rules {
976+
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
977+
}
976978
}
977979
Res::Def(
978980
DefKind::TyParam

compiler/rustc_resolve/src/imports.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1399,14 +1399,22 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13991399
let mut reexports = Vec::new();
14001400

14011401
module.for_each_child(self.r, |_, ident, _, binding| {
1402-
// Filter away ambiguous imports and anything that has def-site hygiene.
1403-
// FIXME: Implement actual cross-crate hygiene.
1404-
let is_good_import =
1405-
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
1406-
if is_good_import || binding.is_macro_def() {
1402+
// FIXME: Consider changing the binding inserted by `#[macro_export] macro_rules`
1403+
// into the crate root to actual `NameBindingKind::Import`.
1404+
if binding.is_import()
1405+
|| matches!(binding.kind, NameBindingKind::Res(_, _is_macro_export @ true))
1406+
{
14071407
let res = binding.res().expect_non_local();
1408-
if res != def::Res::Err {
1409-
reexports.push(ModChild { ident, res, vis: binding.vis, span: binding.span });
1408+
// Ambiguous imports are treated as errors at this point and are
1409+
// not exposed to other crates (see #36837 for more details).
1410+
if res != def::Res::Err && !binding.is_ambiguity() {
1411+
reexports.push(ModChild {
1412+
ident,
1413+
res,
1414+
vis: binding.vis,
1415+
span: binding.span,
1416+
macro_rules: false,
1417+
});
14101418
}
14111419
}
14121420
});

compiler/rustc_resolve/src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,6 @@ impl<'a> NameBinding<'a> {
845845
)
846846
}
847847

848-
fn is_macro_def(&self) -> bool {
849-
matches!(self.kind, NameBindingKind::Res(Res::Def(DefKind::Macro(..), _), _))
850-
}
851-
852848
fn macro_kind(&self) -> Option<MacroKind> {
853849
self.res().macro_kind()
854850
}
@@ -990,6 +986,9 @@ pub struct Resolver<'a> {
990986
crate_loader: CrateLoader<'a>,
991987
macro_names: FxHashSet<Ident>,
992988
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
989+
/// A small map keeping true kinds of built-in macros that appear to be fn-like on
990+
/// the surface (`macro` items in libcore), but are actually attributes or derives.
991+
builtin_macro_kinds: FxHashMap<LocalDefId, MacroKind>,
993992
registered_attrs: FxHashSet<Ident>,
994993
registered_tools: RegisteredTools,
995994
macro_use_prelude: FxHashMap<Symbol, &'a NameBinding<'a>>,
@@ -1261,6 +1260,10 @@ impl ResolverAstLowering for Resolver<'_> {
12611260

12621261
def_id
12631262
}
1263+
1264+
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
1265+
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
1266+
}
12641267
}
12651268

12661269
impl<'a> Resolver<'a> {
@@ -1381,6 +1384,7 @@ impl<'a> Resolver<'a> {
13811384
crate_loader: CrateLoader::new(session, metadata_loader, crate_name),
13821385
macro_names: FxHashSet::default(),
13831386
builtin_macros: Default::default(),
1387+
builtin_macro_kinds: Default::default(),
13841388
registered_attrs,
13851389
registered_tools,
13861390
macro_use_prelude: FxHashMap::default(),

compiler/rustc_resolve/src/macros.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,13 @@ impl<'a> Resolver<'a> {
12091209
// while still taking everything else from the source code.
12101210
// If we already loaded this builtin macro, give a better error message than 'no such builtin macro'.
12111211
match mem::replace(builtin_macro, BuiltinMacroState::AlreadySeen(item.span)) {
1212-
BuiltinMacroState::NotYetSeen(ext) => result.kind = ext,
1212+
BuiltinMacroState::NotYetSeen(ext) => {
1213+
result.kind = ext;
1214+
if item.id != ast::DUMMY_NODE_ID {
1215+
self.builtin_macro_kinds
1216+
.insert(self.local_def_id(item.id), result.macro_kind());
1217+
}
1218+
}
12131219
BuiltinMacroState::AlreadySeen(span) => {
12141220
struct_span_err!(
12151221
self.session,

compiler/rustc_save_analysis/src/sig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'hir> Sig for hir::Item<'hir> {
416416

417417
Ok(sig)
418418
}
419-
hir::ItemKind::Macro(_) => {
419+
hir::ItemKind::Macro(..) => {
420420
let mut text = "macro".to_owned();
421421
let name = self.ident.to_string();
422422
text.push_str(&name);

compiler/rustc_typeck/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
730730
// These don't define types.
731731
hir::ItemKind::ExternCrate(_)
732732
| hir::ItemKind::Use(..)
733-
| hir::ItemKind::Macro(_)
733+
| hir::ItemKind::Macro(..)
734734
| hir::ItemKind::Mod(_)
735735
| hir::ItemKind::GlobalAsm(_) => {}
736736
hir::ItemKind::ForeignMod { items, .. } => {

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ fn clean_maybe_renamed_item(
18551855
ItemKind::Fn(ref sig, ref generics, body_id) => {
18561856
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
18571857
}
1858-
ItemKind::Macro(ref macro_def) => {
1858+
ItemKind::Macro(ref macro_def, _) => {
18591859
let ty_vis = cx.tcx.visibility(def_id).clean(cx);
18601860
MacroItem(Macro {
18611861
source: display_macro_source(cx, name, macro_def, def_id, ty_vis),

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
11641164

11651165
fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
11661166
let name = match &item.kind {
1167-
hir::ItemKind::Macro(ref macro_def) => {
1167+
hir::ItemKind::Macro(ref macro_def, _) => {
11681168
// FIXME(#88038): Non exported macros have historically not been tested,
11691169
// but we really ought to start testing them.
11701170
let def_id = item.def_id.to_def_id();

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
325325

326326
om.items.push((item, renamed))
327327
}
328-
hir::ItemKind::Macro(ref macro_def) => {
328+
hir::ItemKind::Macro(ref macro_def, _) => {
329329
// `#[macro_export] macro_rules!` items are handled seperately in `visit()`,
330330
// above, since they need to be documented at the module top level. Accordingly,
331331
// we only want to handle macros if one of three conditions holds:

0 commit comments

Comments
 (0)