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

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

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

+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_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/lib.rs

+8
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(),

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:

src/tools/clippy/clippy_lints/src/utils/inspector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) {
373373
let item_ty = cx.tcx.type_of(did);
374374
println!("function of type {:#?}", item_ty);
375375
},
376-
hir::ItemKind::Macro(ref macro_def) => {
376+
hir::ItemKind::Macro(ref macro_def, _) => {
377377
if macro_def.macro_rules {
378378
println!("macro introduced by `macro_rules!`");
379379
} else {

0 commit comments

Comments
 (0)