Skip to content

Commit 17b1afd

Browse files
committed
resolve: Fix incorrect results of opt_def_kind query for some built-in macros
Previously it always returned `MacroKind::Bang` while some of those macros are actually attributes and derives
1 parent 4b043fa commit 17b1afd

File tree

17 files changed

+35
-19
lines changed

17 files changed

+35
-19
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/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14061406

14071407
EntryKind::Fn(self.lazy(data))
14081408
}
1409-
hir::ItemKind::Macro(ref macro_def) => {
1409+
hir::ItemKind::Macro(ref macro_def, _) => {
14101410
EntryKind::MacroDef(self.lazy(macro_def.clone()))
14111411
}
14121412
hir::ItemKind::Mod(ref m) => {

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_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,9 @@ pub struct Resolver<'a> {
990990
crate_loader: CrateLoader<'a>,
991991
macro_names: FxHashSet<Ident>,
992992
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
993+
/// A small map keeping true kinds of built-in macros that appear to be fn-like on
994+
/// the surface (`macro` items in libcore), but are actually attributes or derives.
995+
builtin_macro_kinds: FxHashMap<LocalDefId, MacroKind>,
993996
registered_attrs: FxHashSet<Ident>,
994997
registered_tools: RegisteredTools,
995998
macro_use_prelude: FxHashMap<Symbol, &'a NameBinding<'a>>,
@@ -1261,6 +1264,10 @@ impl ResolverAstLowering for Resolver<'_> {
12611264

12621265
def_id
12631266
}
1267+
1268+
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
1269+
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
1270+
}
12641271
}
12651272

12661273
impl<'a> Resolver<'a> {
@@ -1381,6 +1388,7 @@ impl<'a> Resolver<'a> {
13811388
crate_loader: CrateLoader::new(session, metadata_loader, crate_name),
13821389
macro_names: FxHashSet::default(),
13831390
builtin_macros: Default::default(),
1391+
builtin_macro_kinds: Default::default(),
13841392
registered_attrs,
13851393
registered_tools,
13861394
macro_use_prelude: FxHashMap::default(),

0 commit comments

Comments
 (0)