Skip to content

Commit ea68bc8

Browse files
committed
resolve: Make ParentScope Copy
By allocating its derive paths on the resolver arena.
1 parent 59dd07a commit ea68bc8

File tree

5 files changed

+32
-33
lines changed

5 files changed

+32
-33
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
300300
root_id: NodeId,
301301
vis: ty::Visibility,
302302
) {
303-
let parent_scope = &self.parent_scope;
304-
let current_module = parent_scope.module;
303+
let current_module = self.parent_scope.module;
305304
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
306-
parent_scope: parent_scope.clone(),
305+
parent_scope: self.parent_scope,
307306
module_path,
308307
imported_module: Cell::new(None),
309308
subclass,
@@ -601,7 +600,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
601600
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
602601
root_id: item.id,
603602
id: item.id,
604-
parent_scope: self.parent_scope.clone(),
603+
parent_scope: self.parent_scope,
605604
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
606605
subclass: ImportDirectiveSubclass::ExternCrate {
607606
source: orig_name,
@@ -994,7 +993,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
994993
|this: &Self, span| this.r.arenas.alloc_import_directive(ImportDirective {
995994
root_id: item.id,
996995
id: item.id,
997-
parent_scope: this.parent_scope.clone(),
996+
parent_scope: this.parent_scope,
998997
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
999998
subclass: ImportDirectiveSubclass::MacroUse,
1000999
use_span_with_attributes: item.span_with_attributes(),
@@ -1066,11 +1065,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10661065
fn visit_invoc(&mut self, id: ast::NodeId) -> LegacyScope<'a> {
10671066
let invoc_id = id.placeholder_to_expn_id();
10681067

1069-
let parent_scope = self.parent_scope.clone();
1070-
parent_scope.module.unresolved_invocations.borrow_mut().insert(invoc_id);
1068+
self.parent_scope.module.unresolved_invocations.borrow_mut().insert(invoc_id);
10711069

1072-
let old_parent_scope =
1073-
self.r.invocation_parent_scopes.insert(invoc_id, parent_scope.clone());
1070+
let old_parent_scope = self.r.invocation_parent_scopes.insert(invoc_id, self.parent_scope);
10741071
assert!(old_parent_scope.is_none(), "invocation data is reset for an invocation");
10751072

10761073
LegacyScope::Invocation(invoc_id)
@@ -1261,7 +1258,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12611258

12621259
fn visit_attribute(&mut self, attr: &'b ast::Attribute) {
12631260
if !attr.is_sugared_doc && is_builtin_attr(attr) {
1264-
self.r.builtin_attrs.push((attr.path.segments[0].ident, self.parent_scope.clone()));
1261+
self.r.builtin_attrs.push((attr.path.segments[0].ident, self.parent_scope));
12651262
}
12661263
visit::walk_attribute(self, attr);
12671264
}

src/librustc_resolve/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ impl<'a> Resolver<'a> {
376376
Scope::DeriveHelpers => {
377377
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
378378
if filter_fn(res) {
379-
for derive in &parent_scope.derives {
379+
for derive in parent_scope.derives {
380380
let parent_scope =
381-
&ParentScope { derives: Vec::new(), ..*parent_scope };
381+
&ParentScope { derives: &[], ..*parent_scope };
382382
if let Ok((Some(ext), _)) = this.resolve_macro_path(
383383
derive, Some(MacroKind::Derive), parent_scope, false, false
384384
) {

src/librustc_resolve/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ enum ScopeSet {
122122
/// Serves as a starting point for the scope visitor.
123123
/// This struct is currently used only for early resolution (imports and macros),
124124
/// but not for late resolution yet.
125-
#[derive(Clone, Debug)]
125+
#[derive(Clone, Copy, Debug)]
126126
pub struct ParentScope<'a> {
127127
module: Module<'a>,
128128
expansion: ExpnId,
129129
legacy: LegacyScope<'a>,
130-
derives: Vec<ast::Path>,
130+
derives: &'a [ast::Path],
131131
}
132132

133133
impl<'a> ParentScope<'a> {
@@ -136,7 +136,7 @@ impl<'a> ParentScope<'a> {
136136
module,
137137
expansion: ExpnId::root(),
138138
legacy: LegacyScope::Empty,
139-
derives: Vec::new(),
139+
derives: &[],
140140
}
141141
}
142142
}
@@ -940,6 +940,7 @@ pub struct ResolverArenas<'a> {
940940
import_directives: arena::TypedArena<ImportDirective<'a>>,
941941
name_resolutions: arena::TypedArena<RefCell<NameResolution<'a>>>,
942942
legacy_bindings: arena::TypedArena<LegacyBinding<'a>>,
943+
ast_paths: arena::TypedArena<ast::Path>,
943944
}
944945

945946
impl<'a> ResolverArenas<'a> {
@@ -966,6 +967,9 @@ impl<'a> ResolverArenas<'a> {
966967
fn alloc_legacy_binding(&'a self, binding: LegacyBinding<'a>) -> &'a LegacyBinding<'a> {
967968
self.legacy_bindings.alloc(binding)
968969
}
970+
fn alloc_ast_paths(&'a self, paths: &[ast::Path]) -> &'a [ast::Path] {
971+
self.ast_paths.alloc_from_iter(paths.iter().cloned())
972+
}
969973
}
970974

971975
impl<'a, 'b> ty::DefIdTree for &'a Resolver<'b> {
@@ -1515,7 +1519,7 @@ impl<'a> Resolver<'a> {
15151519
self.hygienic_lexical_parent(module, &mut ident.span)
15161520
};
15171521
module = unwrap_or!(opt_module, break);
1518-
let adjusted_parent_scope = &ParentScope { module, ..parent_scope.clone() };
1522+
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
15191523
let result = self.resolve_ident_in_module_unadjusted(
15201524
ModuleOrUniformRoot::Module(module),
15211525
ident,
@@ -1651,7 +1655,7 @@ impl<'a> Resolver<'a> {
16511655
ModuleOrUniformRoot::Module(m) => {
16521656
if let Some(def) = ident.span.modernize_and_adjust(m.expansion) {
16531657
tmp_parent_scope =
1654-
ParentScope { module: self.macro_def_scope(def), ..parent_scope.clone() };
1658+
ParentScope { module: self.macro_def_scope(def), ..*parent_scope };
16551659
adjusted_parent_scope = &tmp_parent_scope;
16561660
}
16571661
}

src/librustc_resolve/macros.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,9 @@ impl<'a> base::Resolver for Resolver<'a> {
118118
&mut self, expansion: ExpnId, fragment: &AstFragment, derives: &[ExpnId]
119119
) {
120120
// Fill in some data for derives if the fragment is from a derive container.
121-
let parent_scope = self.invocation_parent_scopes[&expansion].clone();
121+
let parent_scope = self.invocation_parent_scopes[&expansion];
122122
let parent_def = self.definitions.invocation_parent(expansion);
123-
self.invocation_parent_scopes.extend(
124-
derives.iter().map(|&derive| (derive, parent_scope.clone()))
125-
);
123+
self.invocation_parent_scopes.extend(derives.iter().map(|&derive| (derive, parent_scope)));
126124
for &derive_invoc_id in derives {
127125
self.definitions.set_invocation_parent(derive_invoc_id, parent_def);
128126
}
@@ -152,14 +150,14 @@ impl<'a> base::Resolver for Resolver<'a> {
152150

153151
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: ExpnId, force: bool)
154152
-> Result<Option<Lrc<SyntaxExtension>>, Indeterminate> {
155-
let parent_scope = &self.invocation_parent_scopes[&invoc_id].clone();
153+
let parent_scope = self.invocation_parent_scopes[&invoc_id];
156154
let (path, kind, derives, after_derive) = match invoc.kind {
157155
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } =>
158-
(&attr.path, MacroKind::Attr, derives.clone(), after_derive),
156+
(&attr.path, MacroKind::Attr, self.arenas.alloc_ast_paths(derives), after_derive),
159157
InvocationKind::Bang { ref mac, .. } =>
160-
(&mac.path, MacroKind::Bang, Vec::new(), false),
158+
(&mac.path, MacroKind::Bang, &[][..], false),
161159
InvocationKind::Derive { ref path, .. } =>
162-
(path, MacroKind::Derive, Vec::new(), false),
160+
(path, MacroKind::Derive, &[][..], false),
163161
InvocationKind::DeriveContainer { ref derives, .. } => {
164162
// Block expansion of derives in the container until we know whether one of them
165163
// is a built-in `Copy`. Skip the resolution if there's only one derive - either
@@ -169,7 +167,7 @@ impl<'a> base::Resolver for Resolver<'a> {
169167
if derives.len() > 1 {
170168
for path in derives {
171169
match self.resolve_macro_path(path, Some(MacroKind::Derive),
172-
parent_scope, true, force) {
170+
&parent_scope, true, force) {
173171
Ok((Some(ref ext), _)) if ext.is_derive_copy => {
174172
self.add_derives(invoc.expansion_data.id, SpecialDerives::COPY);
175173
return Ok(None);
@@ -184,7 +182,7 @@ impl<'a> base::Resolver for Resolver<'a> {
184182
};
185183

186184
// Derives are not included when `invocations` are collected, so we have to add them here.
187-
let parent_scope = &ParentScope { derives, ..parent_scope.clone() };
185+
let parent_scope = &ParentScope { derives, ..parent_scope };
188186
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?;
189187

190188
let span = invoc.span();
@@ -324,7 +322,7 @@ impl<'a> Resolver<'a> {
324322
if trace {
325323
let kind = kind.expect("macro kind must be specified if tracing is enabled");
326324
self.multi_segment_macro_resolutions
327-
.push((path, path_span, kind, parent_scope.clone(), res.ok()));
325+
.push((path, path_span, kind, *parent_scope, res.ok()));
328326
}
329327

330328
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
@@ -341,7 +339,7 @@ impl<'a> Resolver<'a> {
341339
if trace {
342340
let kind = kind.expect("macro kind must be specified if tracing is enabled");
343341
self.single_segment_macro_resolutions
344-
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
342+
.push((path[0].ident, kind, *parent_scope, binding.ok()));
345343
}
346344

347345
let res = binding.map(|binding| binding.res());
@@ -410,8 +408,8 @@ impl<'a> Resolver<'a> {
410408
let result = match scope {
411409
Scope::DeriveHelpers => {
412410
let mut result = Err(Determinacy::Determined);
413-
for derive in &parent_scope.derives {
414-
let parent_scope = &ParentScope { derives: Vec::new(), ..*parent_scope };
411+
for derive in parent_scope.derives {
412+
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
415413
match this.resolve_macro_path(derive, Some(MacroKind::Derive),
416414
parent_scope, true, force) {
417415
Ok((Some(ext), _)) => if ext.helper_attrs.contains(&ident.name) {
@@ -457,7 +455,7 @@ impl<'a> Resolver<'a> {
457455
}
458456
}
459457
Scope::Module(module) => {
460-
let adjusted_parent_scope = &ParentScope { module, ..parent_scope.clone() };
458+
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
461459
let binding = this.resolve_ident_in_module_unadjusted_ext(
462460
ModuleOrUniformRoot::Module(module),
463461
ident,

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl<'a> Resolver<'a> {
394394
match ident.span.glob_adjust(module.expansion, glob_import.span) {
395395
Some(Some(def)) => {
396396
tmp_parent_scope =
397-
ParentScope { module: self.macro_def_scope(def), ..parent_scope.clone() };
397+
ParentScope { module: self.macro_def_scope(def), ..*parent_scope };
398398
adjusted_parent_scope = &tmp_parent_scope;
399399
}
400400
Some(None) => {}

0 commit comments

Comments
 (0)