Skip to content

Commit 9b1da03

Browse files
Rollup merge of #118272 - petrochenkov:macrodata, r=cjgillot
resolve: Avoid clones of `MacroData` And move declarative macro compilation to an earlier point in def collector, which is required for #118188.
2 parents 8fb68fc + ad0770e commit 9b1da03

File tree

5 files changed

+52
-55
lines changed

5 files changed

+52
-55
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+21-34
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
156156
}
157157
}
158158

159-
pub(crate) fn get_macro(&mut self, res: Res) -> Option<MacroData> {
159+
pub(crate) fn get_macro(&mut self, res: Res) -> Option<&MacroData> {
160160
match res {
161161
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
162-
Res::NonMacroAttr(_) => {
163-
Some(MacroData { ext: self.non_macro_attr.clone(), macro_rules: false })
164-
}
162+
Res::NonMacroAttr(_) => Some(&self.non_macro_attr),
165163
_ => None,
166164
}
167165
}
168166

169-
pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> MacroData {
170-
if let Some(macro_data) = self.macro_map.get(&def_id) {
171-
return macro_data.clone();
167+
pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> &MacroData {
168+
if self.macro_map.contains_key(&def_id) {
169+
return &self.macro_map[&def_id];
172170
}
173171

174-
let load_macro_untracked = self.cstore().load_macro_untracked(def_id, self.tcx);
175-
let (ext, macro_rules) = match load_macro_untracked {
176-
LoadedMacro::MacroDef(item, edition) => (
177-
Lrc::new(self.compile_macro(&item, edition).0),
178-
matches!(item.kind, ItemKind::MacroDef(def) if def.macro_rules),
179-
),
180-
LoadedMacro::ProcMacro(extz) => (Lrc::new(extz), false),
172+
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
173+
let macro_data = match loaded_macro {
174+
LoadedMacro::MacroDef(item, edition) => self.compile_macro(&item, edition),
175+
LoadedMacro::ProcMacro(ext) => MacroData::new(Lrc::new(ext)),
181176
};
182177

183-
let macro_data = MacroData { ext, macro_rules };
184-
self.macro_map.insert(def_id, macro_data.clone());
185-
macro_data
178+
self.macro_map.entry(def_id).or_insert(macro_data)
186179
}
187180

188181
pub(crate) fn build_reduced_graph(
@@ -1175,16 +1168,10 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
11751168
// Mark the given macro as unused unless its name starts with `_`.
11761169
// Macro uses will remove items from this set, and the remaining
11771170
// items will be reported as `unused_macros`.
1178-
fn insert_unused_macro(
1179-
&mut self,
1180-
ident: Ident,
1181-
def_id: LocalDefId,
1182-
node_id: NodeId,
1183-
rule_spans: &[(usize, Span)],
1184-
) {
1171+
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
11851172
if !ident.as_str().starts_with('_') {
11861173
self.r.unused_macros.insert(def_id, (node_id, ident));
1187-
for (rule_i, rule_span) in rule_spans.iter() {
1174+
for (rule_i, rule_span) in &self.r.macro_map[&def_id.to_def_id()].rule_spans {
11881175
self.r.unused_macro_rules.insert((def_id, *rule_i), (ident, *rule_span));
11891176
}
11901177
}
@@ -1194,24 +1181,24 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
11941181
let parent_scope = self.parent_scope;
11951182
let expansion = parent_scope.expansion;
11961183
let def_id = self.r.local_def_id(item.id);
1197-
let (ext, ident, span, macro_rules, rule_spans) = match &item.kind {
1184+
let (macro_kind, ident, span, macro_rules) = match &item.kind {
11981185
ItemKind::MacroDef(def) => {
1199-
let (ext, rule_spans) = self.r.compile_macro(item, self.r.tcx.sess.edition());
1200-
let ext = Lrc::new(ext);
1201-
(ext, item.ident, item.span, def.macro_rules, rule_spans)
1186+
let macro_kind = self.r.macro_map[&def_id.to_def_id()].ext.macro_kind();
1187+
(macro_kind, item.ident, item.span, def.macro_rules)
12021188
}
12031189
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
12041190
Some((macro_kind, ident, span)) => {
1191+
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
1192+
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
12051193
self.r.proc_macro_stubs.insert(def_id);
1206-
(self.r.dummy_ext(macro_kind), ident, span, false, Vec::new())
1194+
(macro_kind, ident, span, false)
12071195
}
12081196
None => return parent_scope.macro_rules,
12091197
},
12101198
_ => unreachable!(),
12111199
};
12121200

1213-
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id());
1214-
self.r.macro_map.insert(def_id.to_def_id(), MacroData { ext, macro_rules });
1201+
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
12151202
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
12161203

12171204
if macro_rules {
@@ -1245,7 +1232,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12451232
self.r.define(self.r.graph_root, ident, MacroNS, import_binding);
12461233
} else {
12471234
self.r.check_reserved_macro_name(ident, res);
1248-
self.insert_unused_macro(ident, def_id, item.id, &rule_spans);
1235+
self.insert_unused_macro(ident, def_id, item.id);
12491236
}
12501237
self.r.visibilities.insert(def_id, vis);
12511238
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
@@ -1268,7 +1255,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12681255
_ => self.resolve_visibility(&item.vis),
12691256
};
12701257
if !vis.is_public() {
1271-
self.insert_unused_macro(ident, def_id, item.id, &rule_spans);
1258+
self.insert_unused_macro(ident, def_id, item.id);
12721259
}
12731260
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
12741261
self.r.visibilities.insert(def_id, vis);

compiler/rustc_resolve/src/def_collector.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
111111
return visit::walk_item(self, i);
112112
}
113113
};
114-
let def = self.create_def(i.id, def_data, i.span);
114+
let def_id = self.create_def(i.id, def_data, i.span);
115115

116-
self.with_parent(def, |this| {
116+
if let ItemKind::MacroDef(..) = i.kind {
117+
let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition());
118+
self.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
119+
}
120+
121+
self.with_parent(def_id, |this| {
117122
this.with_impl_trait(ImplTraitContext::Existential, |this| {
118123
match i.kind {
119124
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {

compiler/rustc_resolve/src/ident.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
240240
{
241241
// The macro is a proc macro derive
242242
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
243-
let ext = self.get_macro_by_def_id(def_id).ext;
243+
let ext = &self.get_macro_by_def_id(def_id).ext;
244244
if ext.builtin_name.is_none()
245245
&& ext.macro_kind() == MacroKind::Derive
246246
&& parent.expansion.outer_expn_is_descendant_of(*ctxt)

compiler/rustc_resolve/src/lib.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,16 @@ struct DeriveData {
927927
#[derive(Clone)]
928928
struct MacroData {
929929
ext: Lrc<SyntaxExtension>,
930+
rule_spans: Vec<(usize, Span)>,
930931
macro_rules: bool,
931932
}
932933

934+
impl MacroData {
935+
fn new(ext: Lrc<SyntaxExtension>) -> MacroData {
936+
MacroData { ext, rule_spans: Vec::new(), macro_rules: false }
937+
}
938+
}
939+
933940
/// The main resolver class.
934941
///
935942
/// This is the visitor that walks the whole crate.
@@ -1038,7 +1045,7 @@ pub struct Resolver<'a, 'tcx> {
10381045
macro_map: FxHashMap<DefId, MacroData>,
10391046
dummy_ext_bang: Lrc<SyntaxExtension>,
10401047
dummy_ext_derive: Lrc<SyntaxExtension>,
1041-
non_macro_attr: Lrc<SyntaxExtension>,
1048+
non_macro_attr: MacroData,
10421049
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
10431050
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'a>>,
10441051
unused_macros: FxHashMap<LocalDefId, (NodeId, Ident)>,
@@ -1321,6 +1328,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13211328

13221329
let features = tcx.features();
13231330
let pub_vis = ty::Visibility::<DefId>::Public;
1331+
let edition = tcx.sess.edition();
13241332

13251333
let mut resolver = Resolver {
13261334
tcx,
@@ -1402,9 +1410,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14021410
registered_tools,
14031411
macro_use_prelude: FxHashMap::default(),
14041412
macro_map: FxHashMap::default(),
1405-
dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(tcx.sess.edition())),
1406-
dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(tcx.sess.edition())),
1407-
non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(tcx.sess.edition())),
1413+
dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(edition)),
1414+
dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(edition)),
1415+
non_macro_attr: MacroData::new(Lrc::new(SyntaxExtension::non_macro_attr(edition))),
14081416
invocation_parent_scopes: Default::default(),
14091417
output_macro_rules_scopes: Default::default(),
14101418
macro_rules_scopes: Default::default(),
@@ -1564,7 +1572,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15641572
match macro_kind {
15651573
MacroKind::Bang => self.dummy_ext_bang.clone(),
15661574
MacroKind::Derive => self.dummy_ext_derive.clone(),
1567-
MacroKind::Attr => self.non_macro_attr.clone(),
1575+
MacroKind::Attr => self.non_macro_attr.ext.clone(),
15681576
}
15691577
}
15701578

compiler/rustc_resolve/src/macros.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::errors::{
66
MacroExpectedFound, RemoveSurroundingDerive,
77
};
88
use crate::Namespace::*;
9-
use crate::{BuiltinMacroState, Determinacy};
9+
use crate::{BuiltinMacroState, Determinacy, MacroData};
1010
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
1111
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding};
1212
use rustc_ast::expand::StrippedCfgItem;
@@ -695,7 +695,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
695695
res
696696
};
697697

698-
res.map(|res| (self.get_macro(res).map(|macro_data| macro_data.ext), res))
698+
res.map(|res| (self.get_macro(res).map(|macro_data| macro_data.ext.clone()), res))
699699
}
700700

701701
pub(crate) fn finalize_macro_resolutions(&mut self, krate: &Crate) {
@@ -936,27 +936,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
936936
/// Compile the macro into a `SyntaxExtension` and its rule spans.
937937
///
938938
/// Possibly replace its expander to a pre-defined one for built-in macros.
939-
pub(crate) fn compile_macro(
940-
&mut self,
941-
item: &ast::Item,
942-
edition: Edition,
943-
) -> (SyntaxExtension, Vec<(usize, Span)>) {
944-
let (mut result, mut rule_spans) =
939+
pub(crate) fn compile_macro(&mut self, item: &ast::Item, edition: Edition) -> MacroData {
940+
let (mut ext, mut rule_spans) =
945941
compile_declarative_macro(self.tcx.sess, self.tcx.features(), item, edition);
946942

947-
if let Some(builtin_name) = result.builtin_name {
943+
if let Some(builtin_name) = ext.builtin_name {
948944
// The macro was marked with `#[rustc_builtin_macro]`.
949945
if let Some(builtin_macro) = self.builtin_macros.get_mut(&builtin_name) {
950946
// The macro is a built-in, replace its expander function
951947
// while still taking everything else from the source code.
952948
// If we already loaded this builtin macro, give a better error message than 'no such builtin macro'.
953949
match mem::replace(builtin_macro, BuiltinMacroState::AlreadySeen(item.span)) {
954-
BuiltinMacroState::NotYetSeen(ext) => {
955-
result.kind = ext;
950+
BuiltinMacroState::NotYetSeen(builtin_ext) => {
951+
ext.kind = builtin_ext;
956952
rule_spans = Vec::new();
957953
if item.id != ast::DUMMY_NODE_ID {
958954
self.builtin_macro_kinds
959-
.insert(self.local_def_id(item.id), result.macro_kind());
955+
.insert(self.local_def_id(item.id), ext.macro_kind());
960956
}
961957
}
962958
BuiltinMacroState::AlreadySeen(span) => {
@@ -976,6 +972,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
976972
}
977973
}
978974

979-
(result, rule_spans)
975+
let ItemKind::MacroDef(def) = &item.kind else { unreachable!() };
976+
MacroData { ext: Lrc::new(ext), rule_spans, macro_rules: def.macro_rules }
980977
}
981978
}

0 commit comments

Comments
 (0)