Skip to content

Commit 3fbb254

Browse files
committed
resolve: Legacy(Scope,Binding) -> MacroRules(Scope,Binding)
1 parent 65bf483 commit 3fbb254

File tree

4 files changed

+67
-53
lines changed

4 files changed

+67
-53
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use crate::def_collector::collect_definitions;
99
use crate::imports::{Import, ImportKind};
10-
use crate::macros::{LegacyBinding, LegacyScope};
10+
use crate::macros::{MacroRulesBinding, MacroRulesScope};
1111
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
1212
use crate::{CrateLint, Determinacy, PathResult, ResolutionError, VisResolutionError};
1313
use crate::{
@@ -165,11 +165,11 @@ impl<'a> Resolver<'a> {
165165
&mut self,
166166
fragment: &AstFragment,
167167
parent_scope: ParentScope<'a>,
168-
) -> LegacyScope<'a> {
168+
) -> MacroRulesScope<'a> {
169169
collect_definitions(&mut self.definitions, fragment, parent_scope.expansion);
170170
let mut visitor = BuildReducedGraphVisitor { r: self, parent_scope };
171171
fragment.visit_with(&mut visitor);
172-
visitor.parent_scope.legacy
172+
visitor.parent_scope.macro_rules
173173
}
174174

175175
crate fn build_reduced_graph_external(&mut self, module: Module<'a>) {
@@ -1060,15 +1060,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10601060
false
10611061
}
10621062

1063-
fn visit_invoc(&mut self, id: NodeId) -> LegacyScope<'a> {
1063+
fn visit_invoc(&mut self, id: NodeId) -> MacroRulesScope<'a> {
10641064
let invoc_id = id.placeholder_to_expn_id();
10651065

10661066
self.parent_scope.module.unexpanded_invocations.borrow_mut().insert(invoc_id);
10671067

10681068
let old_parent_scope = self.r.invocation_parent_scopes.insert(invoc_id, self.parent_scope);
10691069
assert!(old_parent_scope.is_none(), "invocation data is reset for an invocation");
10701070

1071-
LegacyScope::Invocation(invoc_id)
1071+
MacroRulesScope::Invocation(invoc_id)
10721072
}
10731073

10741074
fn proc_macro_stub(item: &ast::Item) -> Option<(MacroKind, Ident, Span)> {
@@ -1095,7 +1095,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10951095
}
10961096
}
10971097

1098-
fn define_macro(&mut self, item: &ast::Item) -> LegacyScope<'a> {
1098+
fn define_macro(&mut self, item: &ast::Item) -> MacroRulesScope<'a> {
10991099
let parent_scope = self.parent_scope;
11001100
let expansion = parent_scope.expansion;
11011101
let (ext, ident, span, macro_rules) = match &item.kind {
@@ -1108,7 +1108,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11081108
self.r.proc_macro_stubs.insert(item.id);
11091109
(self.r.dummy_ext(macro_kind), ident, span, false)
11101110
}
1111-
None => return parent_scope.legacy,
1111+
None => return parent_scope.macro_rules,
11121112
},
11131113
_ => unreachable!(),
11141114
};
@@ -1137,8 +1137,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11371137
self.r.check_reserved_macro_name(ident, res);
11381138
self.insert_unused_macro(ident, item.id, span);
11391139
}
1140-
LegacyScope::Binding(self.r.arenas.alloc_legacy_binding(LegacyBinding {
1141-
parent_legacy_scope: parent_scope.legacy,
1140+
MacroRulesScope::Binding(self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
1141+
parent_macro_rules_scope: parent_scope.macro_rules,
11421142
binding,
11431143
ident,
11441144
}))
@@ -1149,7 +1149,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11491149
self.insert_unused_macro(ident, item.id, span);
11501150
}
11511151
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
1152-
self.parent_scope.legacy
1152+
self.parent_scope.macro_rules
11531153
}
11541154
}
11551155
}
@@ -1174,29 +1174,29 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
11741174
fn visit_item(&mut self, item: &'b Item) {
11751175
let macro_use = match item.kind {
11761176
ItemKind::MacroDef(..) => {
1177-
self.parent_scope.legacy = self.define_macro(item);
1177+
self.parent_scope.macro_rules = self.define_macro(item);
11781178
return;
11791179
}
11801180
ItemKind::MacCall(..) => {
1181-
self.parent_scope.legacy = self.visit_invoc(item.id);
1181+
self.parent_scope.macro_rules = self.visit_invoc(item.id);
11821182
return;
11831183
}
11841184
ItemKind::Mod(..) => self.contains_macro_use(&item.attrs),
11851185
_ => false,
11861186
};
11871187
let orig_current_module = self.parent_scope.module;
1188-
let orig_current_legacy_scope = self.parent_scope.legacy;
1188+
let orig_current_macro_rules_scope = self.parent_scope.macro_rules;
11891189
self.build_reduced_graph_for_item(item);
11901190
visit::walk_item(self, item);
11911191
self.parent_scope.module = orig_current_module;
11921192
if !macro_use {
1193-
self.parent_scope.legacy = orig_current_legacy_scope;
1193+
self.parent_scope.macro_rules = orig_current_macro_rules_scope;
11941194
}
11951195
}
11961196

11971197
fn visit_stmt(&mut self, stmt: &'b ast::Stmt) {
11981198
if let ast::StmtKind::MacCall(..) = stmt.kind {
1199-
self.parent_scope.legacy = self.visit_invoc(stmt.id);
1199+
self.parent_scope.macro_rules = self.visit_invoc(stmt.id);
12001200
} else {
12011201
visit::walk_stmt(self, stmt);
12021202
}
@@ -1214,11 +1214,11 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12141214

12151215
fn visit_block(&mut self, block: &'b Block) {
12161216
let orig_current_module = self.parent_scope.module;
1217-
let orig_current_legacy_scope = self.parent_scope.legacy;
1217+
let orig_current_macro_rules_scope = self.parent_scope.macro_rules;
12181218
self.build_reduced_graph_for_block(block);
12191219
visit::walk_block(self, block);
12201220
self.parent_scope.module = orig_current_module;
1221-
self.parent_scope.legacy = orig_current_legacy_scope;
1221+
self.parent_scope.macro_rules = orig_current_macro_rules_scope;
12221222
}
12231223

12241224
fn visit_assoc_item(&mut self, item: &'b AssocItem, ctxt: AssocCtxt) {

src/librustc_resolve/diagnostics.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use rustc_span::{BytePos, MultiSpan, Span};
2121
use crate::imports::{Import, ImportKind, ImportResolver};
2222
use crate::path_names_to_string;
2323
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
24-
use crate::{BindingError, CrateLint, HasGenericParams, LegacyScope, Module, ModuleOrUniformRoot};
24+
use crate::{
25+
BindingError, CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot,
26+
};
2527
use crate::{NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
2628
use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment};
2729

@@ -498,12 +500,12 @@ impl<'a> Resolver<'a> {
498500
}
499501
}
500502
}
501-
Scope::MacroRules(legacy_scope) => {
502-
if let LegacyScope::Binding(legacy_binding) = legacy_scope {
503-
let res = legacy_binding.binding.res();
503+
Scope::MacroRules(macro_rules_scope) => {
504+
if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope {
505+
let res = macro_rules_binding.binding.res();
504506
if filter_fn(res) {
505507
suggestions
506-
.push(TypoSuggestion::from_res(legacy_binding.ident.name, res))
508+
.push(TypoSuggestion::from_res(macro_rules_binding.ident.name, res))
507509
}
508510
}
509511
}

src/librustc_resolve/lib.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_ne
5858
use diagnostics::{ImportSuggestion, Suggestion};
5959
use imports::{Import, ImportKind, ImportResolver, NameResolution};
6060
use late::{HasGenericParams, PathSource, Rib, RibKind::*};
61-
use macros::{LegacyBinding, LegacyScope};
61+
use macros::{MacroRulesBinding, MacroRulesScope};
6262

6363
type Res = def::Res<NodeId>;
6464

@@ -94,7 +94,7 @@ impl Determinacy {
9494
enum Scope<'a> {
9595
DeriveHelpers(ExpnId),
9696
DeriveHelpersCompat,
97-
MacroRules(LegacyScope<'a>),
97+
MacroRules(MacroRulesScope<'a>),
9898
CrateRoot,
9999
Module(Module<'a>),
100100
RegisteredAttrs,
@@ -127,15 +127,20 @@ enum ScopeSet {
127127
pub struct ParentScope<'a> {
128128
module: Module<'a>,
129129
expansion: ExpnId,
130-
legacy: LegacyScope<'a>,
130+
macro_rules: MacroRulesScope<'a>,
131131
derives: &'a [ast::Path],
132132
}
133133

134134
impl<'a> ParentScope<'a> {
135135
/// Creates a parent scope with the passed argument used as the module scope component,
136136
/// and other scope components set to default empty values.
137137
pub fn module(module: Module<'a>) -> ParentScope<'a> {
138-
ParentScope { module, expansion: ExpnId::root(), legacy: LegacyScope::Empty, derives: &[] }
138+
ParentScope {
139+
module,
140+
expansion: ExpnId::root(),
141+
macro_rules: MacroRulesScope::Empty,
142+
derives: &[],
143+
}
139144
}
140145
}
141146

@@ -930,9 +935,9 @@ pub struct Resolver<'a> {
930935
/// Parent scopes in which the macros were invoked.
931936
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
932937
invocation_parent_scopes: FxHashMap<ExpnId, ParentScope<'a>>,
933-
/// Legacy scopes *produced* by expanding the macro invocations,
938+
/// `macro_rules` scopes *produced* by expanding the macro invocations,
934939
/// include all the `macro_rules` items and other invocations generated by them.
935-
output_legacy_scopes: FxHashMap<ExpnId, LegacyScope<'a>>,
940+
output_macro_rules_scopes: FxHashMap<ExpnId, MacroRulesScope<'a>>,
936941
/// Helper attributes that are in scope for the given expansion.
937942
helper_attrs: FxHashMap<ExpnId, Vec<Ident>>,
938943

@@ -965,7 +970,7 @@ pub struct ResolverArenas<'a> {
965970
name_bindings: arena::TypedArena<NameBinding<'a>>,
966971
imports: arena::TypedArena<Import<'a>>,
967972
name_resolutions: arena::TypedArena<RefCell<NameResolution<'a>>>,
968-
legacy_bindings: arena::TypedArena<LegacyBinding<'a>>,
973+
macro_rules_bindings: arena::TypedArena<MacroRulesBinding<'a>>,
969974
ast_paths: arena::TypedArena<ast::Path>,
970975
}
971976

@@ -989,8 +994,11 @@ impl<'a> ResolverArenas<'a> {
989994
fn alloc_name_resolution(&'a self) -> &'a RefCell<NameResolution<'a>> {
990995
self.name_resolutions.alloc(Default::default())
991996
}
992-
fn alloc_legacy_binding(&'a self, binding: LegacyBinding<'a>) -> &'a LegacyBinding<'a> {
993-
self.legacy_bindings.alloc(binding)
997+
fn alloc_macro_rules_binding(
998+
&'a self,
999+
binding: MacroRulesBinding<'a>,
1000+
) -> &'a MacroRulesBinding<'a> {
1001+
self.macro_rules_bindings.alloc(binding)
9941002
}
9951003
fn alloc_ast_paths(&'a self, paths: &[ast::Path]) -> &'a [ast::Path] {
9961004
self.ast_paths.alloc_from_iter(paths.iter().cloned())
@@ -1210,7 +1218,7 @@ impl<'a> Resolver<'a> {
12101218
dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(session.edition())),
12111219
non_macro_attrs: [non_macro_attr(false), non_macro_attr(true)],
12121220
invocation_parent_scopes,
1213-
output_legacy_scopes: Default::default(),
1221+
output_macro_rules_scopes: Default::default(),
12141222
helper_attrs: Default::default(),
12151223
macro_defs,
12161224
local_macro_def_scopes: FxHashMap::default(),
@@ -1530,16 +1538,18 @@ impl<'a> Resolver<'a> {
15301538
}
15311539
}
15321540
Scope::DeriveHelpers(..) => Scope::DeriveHelpersCompat,
1533-
Scope::DeriveHelpersCompat => Scope::MacroRules(parent_scope.legacy),
1534-
Scope::MacroRules(legacy_scope) => match legacy_scope {
1535-
LegacyScope::Binding(binding) => Scope::MacroRules(binding.parent_legacy_scope),
1536-
LegacyScope::Invocation(invoc_id) => Scope::MacroRules(
1537-
self.output_legacy_scopes
1541+
Scope::DeriveHelpersCompat => Scope::MacroRules(parent_scope.macro_rules),
1542+
Scope::MacroRules(macro_rules_scope) => match macro_rules_scope {
1543+
MacroRulesScope::Binding(binding) => {
1544+
Scope::MacroRules(binding.parent_macro_rules_scope)
1545+
}
1546+
MacroRulesScope::Invocation(invoc_id) => Scope::MacroRules(
1547+
self.output_macro_rules_scopes
15381548
.get(&invoc_id)
15391549
.cloned()
1540-
.unwrap_or(self.invocation_parent_scopes[&invoc_id].legacy),
1550+
.unwrap_or(self.invocation_parent_scopes[&invoc_id].macro_rules),
15411551
),
1542-
LegacyScope::Empty => Scope::Module(module),
1552+
MacroRulesScope::Empty => Scope::Module(module),
15431553
},
15441554
Scope::CrateRoot => match ns {
15451555
TypeNS => {

src/librustc_resolve/macros.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ use std::{mem, ptr};
3333
type Res = def::Res<NodeId>;
3434

3535
/// Binding produced by a `macro_rules` item.
36-
/// Not modularized, can shadow previous legacy bindings, etc.
36+
/// Not modularized, can shadow previous `macro_rules` bindings, etc.
3737
#[derive(Debug)]
38-
pub struct LegacyBinding<'a> {
38+
pub struct MacroRulesBinding<'a> {
3939
crate binding: &'a NameBinding<'a>,
40-
/// Legacy scope into which the `macro_rules` item was planted.
41-
crate parent_legacy_scope: LegacyScope<'a>,
40+
/// `macro_rules` scope into which the `macro_rules` item was planted.
41+
crate parent_macro_rules_scope: MacroRulesScope<'a>,
4242
crate ident: Ident,
4343
}
4444

4545
/// The scope introduced by a `macro_rules!` macro.
4646
/// This starts at the macro's definition and ends at the end of the macro's parent
4747
/// module (named or unnamed), or even further if it escapes with `#[macro_use]`.
48-
/// Some macro invocations need to introduce legacy scopes too because they
48+
/// Some macro invocations need to introduce `macro_rules` scopes too because they
4949
/// can potentially expand into macro definitions.
5050
#[derive(Copy, Clone, Debug)]
51-
pub enum LegacyScope<'a> {
51+
pub enum MacroRulesScope<'a> {
5252
/// Empty "root" scope at the crate start containing no names.
5353
Empty,
5454
/// The scope introduced by a `macro_rules!` macro definition.
55-
Binding(&'a LegacyBinding<'a>),
55+
Binding(&'a MacroRulesBinding<'a>),
5656
/// The scope introduced by a macro invocation that can potentially
5757
/// create a `macro_rules!` macro definition.
5858
Invocation(ExpnId),
@@ -159,8 +159,8 @@ impl<'a> base::Resolver for Resolver<'a> {
159159
// Integrate the new AST fragment into all the definition and module structures.
160160
// We are inside the `expansion` now, but other parent scope components are still the same.
161161
let parent_scope = ParentScope { expansion, ..self.invocation_parent_scopes[&expansion] };
162-
let output_legacy_scope = self.build_reduced_graph(fragment, parent_scope);
163-
self.output_legacy_scopes.insert(expansion, output_legacy_scope);
162+
let output_macro_rules_scope = self.build_reduced_graph(fragment, parent_scope);
163+
self.output_macro_rules_scopes.insert(expansion, output_macro_rules_scope);
164164

165165
parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion);
166166
}
@@ -608,12 +608,14 @@ impl<'a> Resolver<'a> {
608608
}
609609
result
610610
}
611-
Scope::MacroRules(legacy_scope) => match legacy_scope {
612-
LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident => {
613-
Ok((legacy_binding.binding, Flags::MACRO_RULES))
611+
Scope::MacroRules(macro_rules_scope) => match macro_rules_scope {
612+
MacroRulesScope::Binding(macro_rules_binding)
613+
if ident == macro_rules_binding.ident =>
614+
{
615+
Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
614616
}
615-
LegacyScope::Invocation(invoc_id)
616-
if !this.output_legacy_scopes.contains_key(&invoc_id) =>
617+
MacroRulesScope::Invocation(invoc_id)
618+
if !this.output_macro_rules_scopes.contains_key(&invoc_id) =>
617619
{
618620
Err(Determinacy::Undetermined)
619621
}

0 commit comments

Comments
 (0)