Skip to content

Commit 2723569

Browse files
committed
resolve: Introduce "may appear after" abstraction for macro path resolutions
1 parent 9a539ad commit 2723569

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/librustc_resolve/lib.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,15 @@ impl<'a> NameBinding<'a> {
12691269
fn descr(&self) -> &'static str {
12701270
if self.is_extern_crate() { "extern crate" } else { self.def().kind_name() }
12711271
}
1272+
1273+
// Suppose that we resolved macro invocation with `invoc_id` to binding `binding` at some
1274+
// expansion round `max(invoc_id, binding)` when they both emerged from macros.
1275+
// Then this function returns `true` if `self` may emerge from a macro *after* that
1276+
// in some later round and screw up our previously found resolution.
1277+
fn may_appear_after(&self, _invoc_id: Mark, _binding: &NameBinding) -> bool {
1278+
// FIXME: This is a very conservative estimation.
1279+
self.expansion != Mark::root()
1280+
}
12721281
}
12731282

12741283
/// Interns the names of the primitive types.
@@ -3466,6 +3475,20 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
34663475
record_used: bool,
34673476
path_span: Span,
34683477
crate_lint: CrateLint,
3478+
) -> PathResult<'a> {
3479+
self.resolve_path_with_invoc_id(base_module, path, opt_ns, Mark::root(),
3480+
record_used, path_span, crate_lint)
3481+
}
3482+
3483+
fn resolve_path_with_invoc_id(
3484+
&mut self,
3485+
base_module: Option<ModuleOrUniformRoot<'a>>,
3486+
path: &[Ident],
3487+
opt_ns: Option<Namespace>, // `None` indicates a module path
3488+
invoc_id: Mark,
3489+
record_used: bool,
3490+
path_span: Span,
3491+
crate_lint: CrateLint,
34693492
) -> PathResult<'a> {
34703493
let mut module = base_module;
34713494
let mut allow_super = true;
@@ -3555,8 +3578,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
35553578
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
35563579
} else if opt_ns == Some(MacroNS) {
35573580
assert!(ns == TypeNS);
3558-
self.resolve_lexical_macro_path_segment(ident, ns, record_used, record_used,
3559-
false, path_span).map(|(b, _)| b)
3581+
self.resolve_lexical_macro_path_segment(ident, ns, invoc_id, record_used,
3582+
record_used, false, path_span)
3583+
.map(|(binding, _)| binding)
35603584
} else {
35613585
let record_used_id =
35623586
if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None };

src/librustc_resolve/macros.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
431431
Ok((def, self.get_macro(def)))
432432
}
433433

434-
pub fn resolve_macro_to_def_inner(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
434+
pub fn resolve_macro_to_def_inner(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
435435
derives_in_scope: &[ast::Path], force: bool)
436436
-> Result<Def, Determinacy> {
437437
let ast::Path { ref segments, span } = *path;
438438
let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
439-
let invocation = self.invocations[&scope];
439+
let invocation = self.invocations[&invoc_id];
440440
let module = invocation.module.get();
441441
self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
442442

@@ -448,8 +448,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
448448
}
449449

450450
if path.len() > 1 {
451-
let res = self.resolve_path(None, &path, Some(MacroNS), false, span, CrateLint::No);
452-
let def = match res {
451+
let def = match self.resolve_path_with_invoc_id(None, &path, Some(MacroNS), invoc_id,
452+
false, span, CrateLint::No) {
453453
PathResult::NonModule(path_res) => match path_res.base_def() {
454454
Def::Err => Err(Determinacy::Determined),
455455
def @ _ => {
@@ -480,11 +480,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
480480
}
481481
}
482482

483-
let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
483+
let legacy_resolution =
484+
self.resolve_legacy_scope(path[0], invoc_id, &invocation.legacy_scope, false);
484485
let result = if let Some(legacy_binding) = legacy_resolution {
485486
Ok(legacy_binding.def())
486487
} else {
487-
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
488+
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, invoc_id, false, force,
488489
kind == MacroKind::Attr, span) {
489490
Ok((binding, _)) => Ok(binding.def_ignoring_ambiguity()),
490491
Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
@@ -496,7 +497,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
496497
};
497498

498499
self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
499-
.push((scope, path[0], kind, result.ok()));
500+
.push((invoc_id, path[0], kind, result.ok()));
500501

501502
if let Ok(Def::NonMacroAttr(NonMacroAttrKind::Custom)) = result {} else {
502503
return result;
@@ -515,7 +516,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
515516
enum ConvertToDeriveHelper { Yes, No, DontKnow }
516517
let mut convert_to_derive_helper = ConvertToDeriveHelper::No;
517518
for derive in derives_in_scope {
518-
match self.resolve_macro_path(derive, MacroKind::Derive, scope, &[], force) {
519+
match self.resolve_macro_path(derive, MacroKind::Derive, invoc_id, &[], force) {
519520
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
520521
if inert_attrs.contains(&path[0].name) {
521522
convert_to_derive_helper = ConvertToDeriveHelper::Yes;
@@ -543,10 +544,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
543544
&mut self,
544545
mut ident: Ident,
545546
ns: Namespace,
547+
invoc_id: Mark,
546548
record_used: bool,
547549
force: bool,
548550
is_attr: bool,
549-
path_span: Span
551+
path_span: Span,
550552
) -> Result<(&'a NameBinding<'a>, FromPrelude), Determinacy> {
551553
// General principles:
552554
// 1. Not controlled (user-defined) names should have higher priority than controlled names
@@ -737,7 +739,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
737739
// Found another solution, if the first one was "weak", report an error.
738740
if result.0.def() != innermost_result.0.def() &&
739741
(innermost_result.0.is_glob_import() ||
740-
innermost_result.0.expansion != Mark::root()) {
742+
innermost_result.0.may_appear_after(invoc_id, result.0)) {
741743
self.ambiguity_errors.push(AmbiguityError {
742744
span: path_span,
743745
name: ident.name,
@@ -781,8 +783,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
781783
}
782784

783785
fn resolve_legacy_scope(&mut self,
784-
invocation_legacy_scope: &'a Cell<LegacyScope<'a>>,
785786
ident: Ident,
787+
invoc_id: Mark,
788+
invoc_parent_legacy_scope: &'a Cell<LegacyScope<'a>>,
786789
record_used: bool)
787790
-> Option<&'a NameBinding<'a>> {
788791
let ident = ident.modern();
@@ -801,7 +804,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
801804
let mut innermost_result: Option<&NameBinding> = None;
802805

803806
// Go through all the scopes and try to resolve the name.
804-
let mut where_to_resolve = invocation_legacy_scope;
807+
let mut where_to_resolve = invoc_parent_legacy_scope;
805808
loop {
806809
let result = match where_to_resolve.get() {
807810
LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
@@ -837,7 +840,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
837840
if let Some(innermost_result) = innermost_result {
838841
// Found another solution, if the first one was "weak", report an error.
839842
if result.def() != innermost_result.def() &&
840-
innermost_result.expansion != Mark::root() {
843+
innermost_result.may_appear_after(invoc_id, result) {
841844
self.ambiguity_errors.push(AmbiguityError {
842845
span: ident.span,
843846
name: ident.name,
@@ -875,12 +878,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
875878
}
876879
}
877880

878-
for &(mark, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
881+
for &(invoc_id, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
879882
let span = ident.span;
880-
let legacy_scope = &self.invocations[&mark].legacy_scope;
881-
let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident, true);
882-
let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, true,
883-
kind == MacroKind::Attr, span);
883+
let legacy_scope = &self.invocations[&invoc_id].legacy_scope;
884+
let legacy_resolution = self.resolve_legacy_scope(ident, invoc_id, legacy_scope, true);
885+
let resolution = self.resolve_lexical_macro_path_segment(
886+
ident, MacroNS, invoc_id, true, true, kind == MacroKind::Attr, span
887+
);
884888

885889
let check_consistency = |this: &Self, new_def: Def| {
886890
if let Some(def) = def {
@@ -913,7 +917,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
913917
err.emit();
914918
},
915919
(Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
916-
if !from_prelude || legacy_binding.expansion != Mark::root() => {
920+
if !from_prelude || legacy_binding.may_appear_after(invoc_id, binding) => {
917921
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
918922
self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
919923
}

src/libsyntax_pos/hygiene.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ impl Mark {
100100
Mark(raw)
101101
}
102102

103+
#[inline]
104+
pub fn parent(self) -> Mark {
105+
HygieneData::with(|data| data.marks[self.0 as usize].parent)
106+
}
107+
103108
#[inline]
104109
pub fn expn_info(self) -> Option<ExpnInfo> {
105110
HygieneData::with(|data| data.marks[self.0 as usize].expn_info.clone())

0 commit comments

Comments
 (0)