Skip to content

Commit 7084072

Browse files
address review
1 parent 8009764 commit 7084072

File tree

4 files changed

+17
-36
lines changed

4 files changed

+17
-36
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
162162
}
163163
}
164164

165-
pub(crate) fn get_macro(&mut self, res: Res) -> Option<Macro<'ra>> {
165+
pub(crate) fn get_macro(&self, res: Res) -> Option<Macro<'ra>> {
166166
match res {
167167
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
168168
Res::NonMacroAttr(_) => Some(self.non_macro_attr),
@@ -174,12 +174,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
174174
match def_id.as_local() {
175175
Some(local_def_id) => {
176176
// local macros are always compiled.
177-
self.local_macro_map
178-
.get(&local_def_id)
179-
.copied()
180-
.expect("Local Macros should be compiled and available.")
177+
self.local_macro_map[&local_def_id]
181178
}
182-
None => *self.external_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
179+
None => *self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
183180
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
184181
let macro_data = match loaded_macro {
185182
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16731673
.local_macro_map
16741674
.iter()
16751675
.map(|(local_id, data)| (local_id.to_def_id(), data))
1676-
.chain(self.external_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
1676+
.chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
16771677
{
16781678
for helper_attr in &data.ext.helper_attrs {
16791679
let item_name = self.tcx.item_name(def_id);

compiler/rustc_resolve/src/lib.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ struct DeriveData {
10131013
has_derive_copy: bool,
10141014
}
10151015

1016-
pub struct MacroData {
1016+
struct MacroData {
10171017
ext: Arc<SyntaxExtension>,
10181018
nrules: usize,
10191019
macro_rules: bool,
@@ -1025,20 +1025,7 @@ impl MacroData {
10251025
}
10261026
}
10271027

1028-
pub(crate) type Macro<'ra> = Interned<'ra, MacroData>;
1029-
1030-
// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
1031-
// contained data.
1032-
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
1033-
// are upheld.
1034-
impl std::hash::Hash for MacroData {
1035-
fn hash<H>(&self, _: &mut H)
1036-
where
1037-
H: std::hash::Hasher,
1038-
{
1039-
unreachable!()
1040-
}
1041-
}
1028+
type Macro<'ra> = &'ra MacroData;
10421029

10431030
/// The main resolver class.
10441031
///
@@ -1144,7 +1131,8 @@ pub struct Resolver<'ra, 'tcx> {
11441131
registered_tools: &'tcx RegisteredTools,
11451132
macro_use_prelude: FxIndexMap<Symbol, NameBinding<'ra>>,
11461133
local_macro_map: FxHashMap<LocalDefId, Macro<'ra>>,
1147-
external_macro_map: RefCell<FxHashMap<DefId, Macro<'ra>>>,
1134+
/// Lazily populated cache of macros loaded from external crates.
1135+
extern_macro_map: RefCell<FxHashMap<DefId, Macro<'ra>>>,
11481136
dummy_ext_bang: Arc<SyntaxExtension>,
11491137
dummy_ext_derive: Arc<SyntaxExtension>,
11501138
non_macro_attr: Macro<'ra>,
@@ -1316,7 +1304,7 @@ impl<'ra> ResolverArenas<'ra> {
13161304
self.ast_paths.alloc_from_iter(paths.iter().cloned())
13171305
}
13181306
fn alloc_macro(&'ra self, macro_data: MacroData) -> Macro<'ra> {
1319-
Interned::new_unchecked(self.macros.alloc(macro_data))
1307+
self.macros.alloc(macro_data)
13201308
}
13211309
fn alloc_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
13221310
self.dropless.alloc_from_iter(spans)
@@ -1560,8 +1548,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15601548
builtin_macros: Default::default(),
15611549
registered_tools,
15621550
macro_use_prelude: Default::default(),
1563-
local_macro_map: FxHashMap::default(),
1564-
external_macro_map: RefCell::default(),
1551+
local_macro_map: Default::default(),
1552+
extern_macro_map: Default::default(),
15651553
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
15661554
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
15671555
non_macro_attr: arenas
@@ -1638,13 +1626,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16381626
)
16391627
}
16401628

1641-
pub fn new_local_macro(
1642-
&mut self,
1643-
local_def_id: LocalDefId,
1644-
macro_data: MacroData,
1645-
) -> Macro<'ra> {
1629+
fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> Macro<'ra> {
16461630
let mac = self.arenas.alloc_macro(macro_data);
1647-
self.local_macro_map.insert(local_def_id, mac);
1631+
self.local_macro_map.insert(def_id, mac);
16481632
mac
16491633
}
16501634

@@ -1766,7 +1750,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17661750
f(self, MacroNS);
17671751
}
17681752

1769-
fn is_builtin_macro(&mut self, res: Res) -> bool {
1753+
fn is_builtin_macro(&self, res: Res) -> bool {
17701754
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
17711755
}
17721756

compiler/rustc_resolve/src/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
354354
if unused_arms.is_empty() {
355355
continue;
356356
}
357-
let local_def_id = self.local_def_id(node_id);
358-
let m = &self.local_macro_map[&local_def_id];
357+
let def_id = self.local_def_id(node_id);
358+
let m = &self.local_macro_map[&def_id];
359359
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
360360
continue;
361361
};
@@ -1132,7 +1132,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11321132
}
11331133
}
11341134

1135-
pub(crate) fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
1135+
pub(crate) fn check_reserved_macro_name(&self, ident: Ident, res: Res) {
11361136
// Reserve some names that are not quite covered by the general check
11371137
// performed on `Resolver::builtin_attrs`.
11381138
if ident.name == sym::cfg || ident.name == sym::cfg_attr {

0 commit comments

Comments
 (0)