Skip to content

Commit 730c5de

Browse files
committed
resolve: Support resolving identifier macros without their own ID
Invocation/expansion ID (aka `Mark`) is not really necessary for resolving a macro path. What is really necessary is its parent module, parent expansion and parent legacy scope. This is required for validation resolutions of built-in attributes, which don't get their own `Mark`s
1 parent 551244f commit 730c5de

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

src/librustc_resolve/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use std::mem::replace;
8181
use rustc_data_structures::sync::Lrc;
8282

8383
use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver};
84-
use macros::{InvocationData, LegacyBinding};
84+
use macros::{InvocationData, LegacyBinding, LegacyScope};
8585

8686
// NB: This module needs to be declared first so diagnostics are
8787
// registered before they are used.
@@ -1010,7 +1010,7 @@ pub struct ModuleData<'a> {
10101010
normal_ancestor_id: DefId,
10111011

10121012
resolutions: RefCell<FxHashMap<(Ident, Namespace), &'a RefCell<NameResolution<'a>>>>,
1013-
legacy_macro_resolutions: RefCell<Vec<(Mark, Ident, MacroKind, Option<Def>)>>,
1013+
legacy_macro_resolutions: RefCell<Vec<(Ident, MacroKind, Mark, LegacyScope<'a>, Option<Def>)>>,
10141014
macro_resolutions: RefCell<Vec<(Box<[Ident]>, Span)>>,
10151015

10161016
// Macro invocations that can expand into items in this module.
@@ -1276,19 +1276,18 @@ impl<'a> NameBinding<'a> {
12761276
if self.is_extern_crate() { "extern crate" } else { self.def().kind_name() }
12771277
}
12781278

1279-
// Suppose that we resolved macro invocation with `invoc_id` to binding `binding` at some
1280-
// expansion round `max(invoc_id, binding)` when they both emerged from macros.
1279+
// Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding`
1280+
// at some expansion round `max(invoc, binding)` when they both emerged from macros.
12811281
// Then this function returns `true` if `self` may emerge from a macro *after* that
12821282
// in some later round and screw up our previously found resolution.
12831283
// See more detailed explanation in
12841284
// https://github.com/rust-lang/rust/pull/53778#issuecomment-419224049
1285-
fn may_appear_after(&self, invoc_id: Mark, binding: &NameBinding) -> bool {
1286-
// self > max(invoc_id, binding) => !(self <= invoc_id || self <= binding)
1285+
fn may_appear_after(&self, invoc_parent_expansion: Mark, binding: &NameBinding) -> bool {
1286+
// self > max(invoc, binding) => !(self <= invoc || self <= binding)
12871287
// Expansions are partially ordered, so "may appear after" is an inversion of
12881288
// "certainly appears before or simultaneously" and includes unordered cases.
12891289
let self_parent_expansion = self.expansion;
12901290
let other_parent_expansion = binding.expansion;
1291-
let invoc_parent_expansion = invoc_id.parent();
12921291
let certainly_before_other_or_simultaneously =
12931292
other_parent_expansion.is_descendant_of(self_parent_expansion);
12941293
let certainly_before_invoc_or_simultaneously =
@@ -3493,16 +3492,16 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
34933492
path_span: Span,
34943493
crate_lint: CrateLint,
34953494
) -> PathResult<'a> {
3496-
self.resolve_path_with_invoc_id(base_module, path, opt_ns, Mark::root(),
3497-
record_used, path_span, crate_lint)
3495+
self.resolve_path_with_parent_expansion(base_module, path, opt_ns, Mark::root(),
3496+
record_used, path_span, crate_lint)
34983497
}
34993498

3500-
fn resolve_path_with_invoc_id(
3499+
fn resolve_path_with_parent_expansion(
35013500
&mut self,
35023501
base_module: Option<ModuleOrUniformRoot<'a>>,
35033502
path: &[Ident],
35043503
opt_ns: Option<Namespace>, // `None` indicates a module path
3505-
invoc_id: Mark,
3504+
parent_expansion: Mark,
35063505
record_used: bool,
35073506
path_span: Span,
35083507
crate_lint: CrateLint,
@@ -3595,7 +3594,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
35953594
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
35963595
} else if opt_ns == Some(MacroNS) {
35973596
assert!(ns == TypeNS);
3598-
self.resolve_lexical_macro_path_segment(ident, ns, invoc_id, record_used,
3597+
self.resolve_lexical_macro_path_segment(ident, ns, parent_expansion, record_used,
35993598
record_used, false, path_span)
36003599
.map(|(binding, _)| binding)
36013600
} else {

src/librustc_resolve/macros.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
313313
None
314314
}
315315

316-
fn resolve_macro_invocation(&mut self, invoc: &Invocation, scope: Mark, force: bool)
316+
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
317317
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
318318
let (path, kind, derives_in_scope) = match invoc.kind {
319319
InvocationKind::Attr { attr: None, .. } =>
@@ -326,7 +326,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
326326
(path, MacroKind::Derive, &[][..]),
327327
};
328328

329-
let (def, ext) = self.resolve_macro_to_def(path, kind, scope, derives_in_scope, force)?;
329+
let (def, ext) = self.resolve_macro_to_def(path, kind, invoc_id, derives_in_scope, force)?;
330330

331331
if let Def::Macro(def_id, _) = def {
332332
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
@@ -341,10 +341,10 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
341341
Ok(Some(ext))
342342
}
343343

344-
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
344+
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
345345
derives_in_scope: &[ast::Path], force: bool)
346346
-> Result<Lrc<SyntaxExtension>, Determinacy> {
347-
Ok(self.resolve_macro_to_def(path, kind, scope, derives_in_scope, force)?.1)
347+
Ok(self.resolve_macro_to_def(path, kind, invoc_id, derives_in_scope, force)?.1)
348348
}
349349

350350
fn check_unused_macros(&self) {
@@ -366,10 +366,10 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
366366
}
367367

368368
impl<'a, 'cl> Resolver<'a, 'cl> {
369-
fn resolve_macro_to_def(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
369+
fn resolve_macro_to_def(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
370370
derives_in_scope: &[ast::Path], force: bool)
371371
-> Result<(Def, Lrc<SyntaxExtension>), Determinacy> {
372-
let def = self.resolve_macro_to_def_inner(path, kind, scope, derives_in_scope, force);
372+
let def = self.resolve_macro_to_def_inner(path, kind, invoc_id, derives_in_scope, force);
373373

374374
// Report errors and enforce feature gates for the resolved macro.
375375
if def != Err(Determinacy::Undetermined) {
@@ -439,8 +439,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
439439
let ast::Path { ref segments, span } = *path;
440440
let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
441441
let invocation = self.invocations[&invoc_id];
442-
let module = invocation.module.get();
443-
self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
442+
let parent_expansion = invoc_id.parent();
443+
let parent_legacy_scope = invocation.parent_legacy_scope.get();
444+
self.current_module = invocation.module.get().nearest_item_scope();
444445

445446
// Possibly apply the macro helper hack
446447
if kind == MacroKind::Bang && path.len() == 1 &&
@@ -450,8 +451,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
450451
}
451452

452453
if path.len() > 1 {
453-
let def = match self.resolve_path_with_invoc_id(None, &path, Some(MacroNS), invoc_id,
454-
false, span, CrateLint::No) {
454+
let def = match self.resolve_path_with_parent_expansion(None, &path, Some(MacroNS),
455+
parent_expansion, false, span,
456+
CrateLint::No) {
455457
PathResult::NonModule(path_res) => match path_res.base_def() {
456458
Def::Err => Err(Determinacy::Determined),
457459
def @ _ => {
@@ -471,19 +473,19 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
471473
Err(Determinacy::Determined)
472474
},
473475
};
474-
self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
476+
self.current_module.macro_resolutions.borrow_mut()
475477
.push((path.into_boxed_slice(), span));
476478
return def;
477479
}
478480

479481
let legacy_resolution = self.resolve_legacy_scope(
480-
path[0], invoc_id, invocation.parent_legacy_scope.get(), false, kind == MacroKind::Attr
482+
path[0], parent_expansion, parent_legacy_scope, false, kind == MacroKind::Attr
481483
);
482484
let result = if let Some(legacy_binding) = legacy_resolution {
483485
Ok(legacy_binding.def())
484486
} else {
485-
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, invoc_id, false, force,
486-
kind == MacroKind::Attr, span) {
487+
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, parent_expansion, false,
488+
force, kind == MacroKind::Attr, span) {
487489
Ok((binding, _)) => Ok(binding.def_ignoring_ambiguity()),
488490
Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
489491
Err(Determinacy::Determined) => {
@@ -493,8 +495,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
493495
}
494496
};
495497

496-
self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
497-
.push((invoc_id, path[0], kind, result.ok()));
498+
self.current_module.legacy_macro_resolutions.borrow_mut()
499+
.push((path[0], kind, parent_expansion, parent_legacy_scope, result.ok()));
498500

499501
if let Ok(Def::NonMacroAttr(NonMacroAttrKind::Custom)) = result {} else {
500502
return result;
@@ -541,7 +543,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
541543
&mut self,
542544
mut ident: Ident,
543545
ns: Namespace,
544-
invoc_id: Mark,
546+
parent_expansion: Mark,
545547
record_used: bool,
546548
force: bool,
547549
is_attr: bool,
@@ -754,7 +756,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
754756
// Found another solution, if the first one was "weak", report an error.
755757
if result.0.def() != innermost_result.0.def() &&
756758
(innermost_result.0.is_glob_import() ||
757-
innermost_result.0.may_appear_after(invoc_id, result.0)) {
759+
innermost_result.0.may_appear_after(parent_expansion, result.0)) {
758760
self.ambiguity_errors.push(AmbiguityError {
759761
ident,
760762
b1: innermost_result.0,
@@ -798,8 +800,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
798800

799801
fn resolve_legacy_scope(&mut self,
800802
ident: Ident,
801-
invoc_id: Mark,
802-
invoc_parent_legacy_scope: LegacyScope<'a>,
803+
parent_expansion: Mark,
804+
parent_legacy_scope: LegacyScope<'a>,
803805
record_used: bool,
804806
is_attr: bool)
805807
-> Option<&'a NameBinding<'a>> {
@@ -826,7 +828,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
826828
let mut innermost_result: Option<&NameBinding> = None;
827829

828830
// Go through all the scopes and try to resolve the name.
829-
let mut where_to_resolve = invoc_parent_legacy_scope;
831+
let mut where_to_resolve = parent_legacy_scope;
830832
loop {
831833
let result = match where_to_resolve {
832834
LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
@@ -854,7 +856,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
854856
if let Some(innermost_result) = innermost_result {
855857
// Found another solution, if the first one was "weak", report an error.
856858
if result.def() != innermost_result.def() &&
857-
innermost_result.may_appear_after(invoc_id, result) {
859+
innermost_result.may_appear_after(parent_expansion, result) {
858860
self.ambiguity_errors.push(AmbiguityError {
859861
ident,
860862
b1: innermost_result,
@@ -891,14 +893,14 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
891893
}
892894
}
893895

894-
for &(invoc_id, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
896+
for &(ident, kind, parent_expansion, parent_legacy_scope, def)
897+
in module.legacy_macro_resolutions.borrow().iter() {
895898
let span = ident.span;
896-
let invocation = self.invocations[&invoc_id];
897899
let legacy_resolution = self.resolve_legacy_scope(
898-
ident, invoc_id, invocation.parent_legacy_scope.get(), true, kind == MacroKind::Attr
900+
ident, parent_expansion, parent_legacy_scope, true, kind == MacroKind::Attr
899901
);
900902
let resolution = self.resolve_lexical_macro_path_segment(
901-
ident, MacroNS, invoc_id, true, true, kind == MacroKind::Attr, span
903+
ident, MacroNS, parent_expansion, true, true, kind == MacroKind::Attr, span
902904
);
903905

904906
let check_consistency = |this: &Self, new_def: Def| {
@@ -932,12 +934,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
932934
err.emit();
933935
},
934936
(Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
935-
if !from_prelude || legacy_binding.may_appear_after(invoc_id, binding) => {
936-
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
937-
self.report_ambiguity_error(ident, legacy_binding, binding);
938-
}
937+
if legacy_binding.def() != binding.def_ignoring_ambiguity() &&
938+
(!from_prelude ||
939+
legacy_binding.may_appear_after(parent_expansion, binding)) => {
940+
self.report_ambiguity_error(ident, legacy_binding, binding);
939941
},
940942
// OK, non-macro-expanded legacy wins over prelude even if defs are different
943+
// Also, legacy and modern can co-exist if their defs are same
941944
(Some(legacy_binding), Ok(_)) |
942945
// OK, unambiguous resolution
943946
(Some(legacy_binding), Err(_)) => {

src/libsyntax/ext/base.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,9 @@ pub trait Resolver {
727727
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<Attribute>, allow_derive: bool)
728728
-> Option<Attribute>;
729729

730-
fn resolve_macro_invocation(&mut self, invoc: &Invocation, scope: Mark, force: bool)
730+
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
731731
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy>;
732-
733-
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
732+
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
734733
derives_in_scope: &[ast::Path], force: bool)
735734
-> Result<Lrc<SyntaxExtension>, Determinacy>;
736735

@@ -764,11 +763,11 @@ impl Resolver for DummyResolver {
764763
fn resolve_imports(&mut self) {}
765764
fn find_legacy_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>, _allow_derive: bool)
766765
-> Option<Attribute> { None }
767-
fn resolve_macro_invocation(&mut self, _invoc: &Invocation, _scope: Mark, _force: bool)
766+
fn resolve_macro_invocation(&mut self, _invoc: &Invocation, _invoc_id: Mark, _force: bool)
768767
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
769768
Err(Determinacy::Determined)
770769
}
771-
fn resolve_macro_path(&mut self, _path: &ast::Path, _kind: MacroKind, _scope: Mark,
770+
fn resolve_macro_path(&mut self, _path: &ast::Path, _kind: MacroKind, _invoc_id: Mark,
772771
_derives_in_scope: &[ast::Path], _force: bool)
773772
-> Result<Lrc<SyntaxExtension>, Determinacy> {
774773
Err(Determinacy::Determined)

0 commit comments

Comments
 (0)