Skip to content

Commit 9ecd36b

Browse files
committed
Don't return dummy MacroData in get_macro
1 parent a51f3a8 commit 9ecd36b

5 files changed

Lines changed: 98 additions & 26 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use crate::ref_mut::CmCell;
3737
use crate::{
3838
BindingKey, Decl, DeclData, DeclKind, DelayedVisResolutionError, ExternModule,
3939
ExternPreludeEntry, Finalize, IdentKey, LocalModule, MacroData, Module, ModuleKind,
40-
ModuleOrUniformRoot, ParentScope, PathResult, Res, Resolver, Segment, Used, VisResolutionError,
41-
errors,
40+
ModuleOrUniformRoot, ParentScope, PathResult, Res, Resolver, Segment, SyntaxExtension, Used,
41+
VisResolutionError, errors,
4242
};
4343

4444
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
@@ -201,10 +201,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
201201
}
202202
}
203203

204-
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra MacroData> {
204+
/// Gets the `SyntaxExtension` corresponding to `res`..
205+
pub(crate) fn get_macro(&self, res: Res) -> Option<&Arc<SyntaxExtension>> {
205206
match res {
206-
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
207-
Res::NonMacroAttr(_) => Some(self.non_macro_attr),
207+
Res::Def(DefKind::Macro(..), def_id) => Some(&self.get_macro_by_def_id(def_id).ext),
208+
Res::NonMacroAttr(_) => Some(&self.non_macro_attr),
208209
_ => None,
209210
}
210211
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ pub struct Resolver<'ra, 'tcx> {
13921392
extern_macro_map: CacheRefCell<FxHashMap<DefId, &'ra MacroData>>,
13931393
dummy_ext_bang: Arc<SyntaxExtension>,
13941394
dummy_ext_derive: Arc<SyntaxExtension>,
1395-
non_macro_attr: &'ra MacroData,
1395+
non_macro_attr: Arc<SyntaxExtension>,
13961396
local_macro_def_scopes: FxHashMap<LocalDefId, LocalModule<'ra>> = default::fx_hash_map(),
13971397
ast_transform_scopes: FxHashMap<LocalExpnId, LocalModule<'ra>> = default::fx_hash_map(),
13981398
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
@@ -1812,8 +1812,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18121812
extern_macro_map: Default::default(),
18131813
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
18141814
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
1815-
non_macro_attr: arenas
1816-
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
1815+
non_macro_attr: Arc::new(SyntaxExtension::non_macro_attr(edition)),
18171816
unused_macros: Default::default(),
18181817
unused_macro_rules: Default::default(),
18191818
single_segment_macro_resolutions: Default::default(),
@@ -1984,7 +1983,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19841983
match macro_kind {
19851984
MacroKind::Bang => Arc::clone(&self.dummy_ext_bang),
19861985
MacroKind::Derive => Arc::clone(&self.dummy_ext_derive),
1987-
MacroKind::Attr => Arc::clone(&self.non_macro_attr.ext),
1986+
MacroKind::Attr => Arc::clone(&self.non_macro_attr),
19881987
}
19891988
}
19901989

@@ -2013,11 +2012,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20132012
}
20142013

20152014
fn is_builtin_macro(&self, res: Res) -> bool {
2016-
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
2015+
self.get_macro(res).is_some_and(|ext| ext.builtin_name.is_some())
20172016
}
20182017

20192018
fn is_specific_builtin_macro(&self, res: Res, symbol: Symbol) -> bool {
2020-
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name == Some(symbol))
2019+
self.get_macro(res).is_some_and(|ext| ext.builtin_name == Some(symbol))
20212020
}
20222021

20232022
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {

compiler/rustc_resolve/src/macros.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -869,21 +869,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
869869
};
870870

871871
let res = res?;
872-
let ext = match deleg_impl {
873-
Some((impl_def_id, star_span)) => match res {
874-
Res::Def(DefKind::Trait, def_id) => {
875-
let edition = self.tcx.sess.edition();
876-
Some(Arc::new(SyntaxExtension::glob_delegation(
877-
def_id,
878-
impl_def_id,
879-
star_span,
880-
edition,
881-
)))
882-
}
883-
_ => None,
884-
},
885-
None => self.get_macro(res).map(|macro_data| Arc::clone(&macro_data.ext)),
872+
let ext = match res {
873+
Res::Def(DefKind::Macro(..), def_id) => {
874+
let md = self.get_macro_by_def_id(def_id);
875+
Some(Arc::clone(&md.ext))
876+
}
877+
Res::Def(DefKind::Trait, def_id) if let Some((impl_def_id, star_span)) = deleg_impl => {
878+
let edition = self.tcx.sess.edition();
879+
Some(Arc::new(SyntaxExtension::glob_delegation(
880+
def_id,
881+
impl_def_id,
882+
star_span,
883+
edition,
884+
)))
885+
}
886+
// Avoid ICE-ing on tool attributes in weird positions like `rustfmt::skip!()`
887+
Res::NonMacroAttr(_) => Some(Arc::clone(&self.non_macro_attr)),
888+
_ => None,
886889
};
890+
887891
Ok((ext, res))
888892
}
889893

@@ -1212,7 +1216,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12121216
// Reserve some names that are not quite covered by the general check
12131217
// performed on `Resolver::builtin_attrs`.
12141218
if name == sym::cfg || name == sym::cfg_attr {
1215-
let macro_kinds = self.get_macro(res).map(|macro_data| macro_data.ext.macro_kinds());
1219+
let macro_kinds = res.macro_kinds();
12161220
if macro_kinds.is_some() && sub_namespace_match(macro_kinds, Some(MacroKind::Attr)) {
12171221
self.dcx().emit_err(errors::NameReservedInAttributeNamespace { span, ident: name });
12181222
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@edition:2018
2+
3+
#![crate_type = "lib"]
4+
5+
6+
mod a {
7+
use ignore as cfg;
8+
//~^ERROR name `cfg` is reserved in attribute namespace
9+
}
10+
11+
mod b {
12+
use cfg_attr as cfg;
13+
//~^ERROR name `cfg` is reserved in attribute namespace
14+
}
15+
16+
mod c {
17+
use cfg as cfg;
18+
//~^ERROR `cfg` is ambiguous
19+
}
20+
21+
mod d {
22+
use inline as cfg_attr;
23+
//~^ERROR name `cfg_attr` is reserved in attribute namespace
24+
}
25+
26+
mod e {
27+
use not_found as cfg; // trigger "unresolved import", not "cfg reserved".
28+
//~^ ERROR unresolved import `not_found`
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: name `cfg` is reserved in attribute namespace
2+
--> $DIR/reserved-macro-names-rename.rs:7:19
3+
|
4+
LL | use ignore as cfg;
5+
| ^^^
6+
7+
error: name `cfg` is reserved in attribute namespace
8+
--> $DIR/reserved-macro-names-rename.rs:12:21
9+
|
10+
LL | use cfg_attr as cfg;
11+
| ^^^
12+
13+
error: name `cfg_attr` is reserved in attribute namespace
14+
--> $DIR/reserved-macro-names-rename.rs:22:19
15+
|
16+
LL | use inline as cfg_attr;
17+
| ^^^^^^^^
18+
19+
error[E0432]: unresolved import `not_found`
20+
--> $DIR/reserved-macro-names-rename.rs:27:9
21+
|
22+
LL | use not_found as cfg; // trigger "unresolved import", not "cfg reserved".
23+
| ^^^^^^^^^^^^^^^^ no external crate `not_found`
24+
25+
error[E0659]: `cfg` is ambiguous
26+
--> $DIR/reserved-macro-names-rename.rs:17:9
27+
|
28+
LL | use cfg as cfg;
29+
| ^^^ ambiguous name
30+
|
31+
= note: ambiguous because of a name conflict with a builtin attribute
32+
= note: `cfg` could refer to a built-in attribute
33+
note: `cfg` could also refer to a macro from prelude
34+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
35+
36+
error: aborting due to 5 previous errors
37+
38+
Some errors have detailed explanations: E0432, E0659.
39+
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)