Skip to content

Commit 38812a3

Browse files
committed
resolve: Split Scope::Module into two scopes for non-glob and glob bindings
1 parent 1e002a5 commit 38812a3

File tree

9 files changed

+157
-22
lines changed

9 files changed

+157
-22
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12051205
}
12061206
}
12071207
}
1208-
Scope::Module(module, _) => {
1208+
Scope::ModuleNonGlobs(module, _) => {
12091209
this.add_module_candidates(module, suggestions, filter_fn, None);
12101210
}
1211+
Scope::ModuleGlobs(..) => {
1212+
// Already handled in `ModuleNonGlobs`.
1213+
}
12111214
Scope::MacroUsePrelude => {
12121215
suggestions.extend(this.macro_use_prelude.iter().filter_map(
12131216
|(name, binding)| {

compiler/rustc_resolve/src/ident.rs

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
127127
let module_and_extern_prelude = matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..));
128128
let extern_prelude = matches!(scope_set, ScopeSet::ExternPrelude);
129129
let mut scope = match ns {
130-
_ if module_and_extern_prelude => Scope::Module(module, None),
130+
_ if module_and_extern_prelude => Scope::ModuleNonGlobs(module, None),
131131
_ if extern_prelude => Scope::ExternPreludeItems,
132-
TypeNS | ValueNS => Scope::Module(module, None),
132+
TypeNS | ValueNS => Scope::ModuleNonGlobs(module, None),
133133
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
134134
};
135135
let mut ctxt = ctxt.normalize_to_macros_2_0();
@@ -156,7 +156,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
156156
}
157157
true
158158
}
159-
Scope::Module(..) => true,
159+
Scope::ModuleNonGlobs(..) | Scope::ModuleGlobs(..) => true,
160160
Scope::MacroUsePrelude => use_prelude || rust_2015,
161161
Scope::BuiltinAttrs => true,
162162
Scope::ExternPreludeItems | Scope::ExternPreludeFlags => {
@@ -197,20 +197,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
197197
MacroRulesScope::Invocation(invoc_id) => {
198198
Scope::MacroRules(self.invocation_parent_scopes[&invoc_id].macro_rules)
199199
}
200-
MacroRulesScope::Empty => Scope::Module(module, None),
200+
MacroRulesScope::Empty => Scope::ModuleNonGlobs(module, None),
201201
},
202-
Scope::Module(..) if module_and_extern_prelude => match ns {
202+
Scope::ModuleNonGlobs(module, lint_id) => Scope::ModuleGlobs(module, lint_id),
203+
Scope::ModuleGlobs(..) if module_and_extern_prelude => match ns {
203204
TypeNS => {
204205
ctxt.adjust(ExpnId::root());
205206
Scope::ExternPreludeItems
206207
}
207208
ValueNS | MacroNS => break,
208209
},
209-
Scope::Module(module, prev_lint_id) => {
210+
Scope::ModuleGlobs(module, prev_lint_id) => {
210211
use_prelude = !module.no_implicit_prelude;
211212
match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
212213
Some((parent_module, lint_id)) => {
213-
Scope::Module(parent_module, lint_id.or(prev_lint_id))
214+
Scope::ModuleNonGlobs(parent_module, lint_id.or(prev_lint_id))
214215
}
215216
None => {
216217
ctxt.adjust(ExpnId::root());
@@ -584,7 +585,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
584585
MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
585586
_ => Err(Determinacy::Determined),
586587
},
587-
Scope::Module(module, derive_fallback_lint_id) => {
588+
Scope::ModuleNonGlobs(module, derive_fallback_lint_id) => {
588589
let (adjusted_parent_scope, adjusted_finalize) =
589590
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
590591
(parent_scope, finalize)
@@ -594,7 +595,58 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
594595
finalize.map(|f| Finalize { used: Used::Scope, ..f }),
595596
)
596597
};
597-
let binding = self.reborrow().resolve_ident_in_module_unadjusted(
598+
let binding = self.reborrow().resolve_ident_in_module_non_globs_unadjusted(
599+
module,
600+
ident,
601+
ns,
602+
adjusted_parent_scope,
603+
Shadowing::Restricted,
604+
adjusted_finalize,
605+
ignore_binding,
606+
ignore_import,
607+
);
608+
match binding {
609+
Ok(binding) => {
610+
if let Some(lint_id) = derive_fallback_lint_id {
611+
self.get_mut().lint_buffer.buffer_lint(
612+
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
613+
lint_id,
614+
orig_ident.span,
615+
errors::ProcMacroDeriveResolutionFallback {
616+
span: orig_ident.span,
617+
ns_descr: ns.descr(),
618+
ident,
619+
},
620+
);
621+
}
622+
let misc_flags = if module == self.graph_root {
623+
Flags::MISC_SUGGEST_CRATE
624+
} else if module.is_normal() {
625+
Flags::MISC_SUGGEST_SELF
626+
} else {
627+
Flags::empty()
628+
};
629+
Ok((binding, Flags::MODULE | misc_flags))
630+
}
631+
Err(ControlFlow::Continue(determinacy)) => Err(determinacy),
632+
Err(ControlFlow::Break(Determinacy::Undetermined)) => {
633+
return Err(ControlFlow::Break(Determinacy::determined(force)));
634+
}
635+
// Privacy errors, do not happen during in scope resolution.
636+
Err(ControlFlow::Break(Determinacy::Determined)) => unreachable!(),
637+
}
638+
}
639+
Scope::ModuleGlobs(module, derive_fallback_lint_id) => {
640+
let (adjusted_parent_scope, adjusted_finalize) =
641+
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
642+
(parent_scope, finalize)
643+
} else {
644+
(
645+
&ParentScope { module, ..*parent_scope },
646+
finalize.map(|f| Finalize { used: Used::Scope, ..f }),
647+
)
648+
};
649+
let binding = self.reborrow().resolve_ident_in_module_globs_unadjusted(
598650
module,
599651
ident,
600652
ns,

compiler/rustc_resolve/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ enum Scope<'ra> {
122122
DeriveHelpersCompat,
123123
/// Textual `let`-like scopes introduced by `macro_rules!` items.
124124
MacroRules(MacroRulesScopeRef<'ra>),
125-
/// Names declared in the given module.
125+
/// Non-glob names declared in the given module.
126126
/// The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK`
127127
/// lint if it should be reported.
128-
Module(Module<'ra>, Option<NodeId>),
128+
ModuleNonGlobs(Module<'ra>, Option<NodeId>),
129+
/// Glob names declared in the given module.
130+
/// The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK`
131+
/// lint if it should be reported.
132+
ModuleGlobs(Module<'ra>, Option<NodeId>),
129133
/// Names introduced by `#[macro_use]` attributes on `extern crate` items.
130134
MacroUsePrelude,
131135
/// Built-in attributes.
@@ -1898,9 +1902,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18981902
let scope_set = ScopeSet::All(TypeNS);
18991903
self.cm().visit_scopes(scope_set, parent_scope, ctxt, None, |this, scope, _, _| {
19001904
match scope {
1901-
Scope::Module(module, _) => {
1905+
Scope::ModuleNonGlobs(module, _) => {
19021906
this.get_mut().traits_in_module(module, assoc_item, &mut found_traits);
19031907
}
1908+
Scope::ModuleGlobs(..) => {
1909+
// Already handled in `ModuleNonGlobs` (but see #144993).
1910+
}
19041911
Scope::StdLibPrelude => {
19051912
if let Some(module) = this.prelude {
19061913
this.get_mut().traits_in_module(module, assoc_item, &mut found_traits);

tests/ui/imports/issue-114682-1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ mac!();
2222
fn main() {
2323
A!();
2424
//~^ ERROR `A` is ambiguous
25+
//~| ERROR `A` is ambiguous
2526
}

tests/ui/imports/issue-114682-1.stderr

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ LL | pub use m::*;
2323
= help: consider adding an explicit import of `A` to disambiguate
2424
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
2525

26-
error: aborting due to 1 previous error
26+
error[E0659]: `A` is ambiguous
27+
--> $DIR/issue-114682-1.rs:23:5
28+
|
29+
LL | A!();
30+
| ^ ambiguous name
31+
|
32+
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
33+
note: `A` could refer to the macro defined here
34+
--> $DIR/issue-114682-1.rs:7:9
35+
|
36+
LL | / pub macro A() {
37+
LL | | println!("non import")
38+
LL | | }
39+
| |_________^
40+
...
41+
LL | mac!();
42+
| ------ in this macro invocation
43+
= help: use `crate::A` to refer to this macro unambiguously
44+
note: `A` could also refer to the macro imported here
45+
--> $DIR/issue-114682-1.rs:19:9
46+
|
47+
LL | pub use m::*;
48+
| ^^^^
49+
= help: use `crate::A` to refer to this macro unambiguously
50+
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
51+
52+
error: aborting due to 2 previous errors
2753

2854
For more information about this error, try `rustc --explain E0659`.

tests/ui/imports/local-modularized-tricky-fail-1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod inner1 {
2727
}
2828

2929
exported!(); //~ ERROR `exported` is ambiguous
30+
//~| ERROR `exported` is ambiguous
3031

3132
mod inner2 {
3233
define_exported!();

tests/ui/imports/local-modularized-tricky-fail-1.stderr

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,34 @@ LL | use inner1::*;
2323
= help: consider adding an explicit import of `exported` to disambiguate
2424
= note: this error originates in the macro `define_exported` (in Nightly builds, run with -Z macro-backtrace for more info)
2525

26+
error[E0659]: `exported` is ambiguous
27+
--> $DIR/local-modularized-tricky-fail-1.rs:29:1
28+
|
29+
LL | exported!();
30+
| ^^^^^^^^ ambiguous name
31+
|
32+
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
33+
note: `exported` could refer to the macro defined here
34+
--> $DIR/local-modularized-tricky-fail-1.rs:6:5
35+
|
36+
LL | / macro_rules! exported {
37+
LL | | () => ()
38+
LL | | }
39+
| |_____^
40+
...
41+
LL | define_exported!();
42+
| ------------------ in this macro invocation
43+
= help: use `crate::exported` to refer to this macro unambiguously
44+
note: `exported` could also refer to the macro imported here
45+
--> $DIR/local-modularized-tricky-fail-1.rs:23:5
46+
|
47+
LL | use inner1::*;
48+
| ^^^^^^^^^
49+
= help: use `crate::exported` to refer to this macro unambiguously
50+
= note: this error originates in the macro `define_exported` (in Nightly builds, run with -Z macro-backtrace for more info)
51+
2652
error[E0659]: `panic` is ambiguous
27-
--> $DIR/local-modularized-tricky-fail-1.rs:36:5
53+
--> $DIR/local-modularized-tricky-fail-1.rs:37:5
2854
|
2955
LL | panic!();
3056
| ^^^^^ ambiguous name
@@ -45,7 +71,7 @@ LL | define_panic!();
4571
= note: this error originates in the macro `define_panic` (in Nightly builds, run with -Z macro-backtrace for more info)
4672

4773
error[E0659]: `include` is ambiguous
48-
--> $DIR/local-modularized-tricky-fail-1.rs:47:1
74+
--> $DIR/local-modularized-tricky-fail-1.rs:48:1
4975
|
5076
LL | include!();
5177
| ^^^^^^^ ambiguous name
@@ -65,6 +91,6 @@ LL | define_include!();
6591
= help: use `crate::include` to refer to this macro unambiguously
6692
= note: this error originates in the macro `define_include` (in Nightly builds, run with -Z macro-backtrace for more info)
6793

68-
error: aborting due to 3 previous errors
94+
error: aborting due to 4 previous errors
6995

7096
For more information about this error, try `rustc --explain E0659`.

tests/ui/imports/macro-paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod foo {
1111
fn f() {
1212
use foo::*;
1313
bar::m! { //~ ERROR ambiguous
14+
//~| ERROR `bar` is ambiguous
1415
mod bar { pub use two_macros::m; }
1516
}
1617
}

tests/ui/imports/macro-paths.stderr

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | bar::m! {
66
|
77
= note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution
88
note: `bar` could refer to the module defined here
9-
--> $DIR/macro-paths.rs:14:9
9+
--> $DIR/macro-paths.rs:15:9
1010
|
1111
LL | mod bar { pub use two_macros::m; }
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -17,27 +17,45 @@ LL | use foo::*;
1717
| ^^^^^^
1818
= help: consider adding an explicit import of `bar` to disambiguate
1919

20+
error[E0659]: `bar` is ambiguous
21+
--> $DIR/macro-paths.rs:13:5
22+
|
23+
LL | bar::m! {
24+
| ^^^ ambiguous name
25+
|
26+
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
27+
note: `bar` could refer to the module defined here
28+
--> $DIR/macro-paths.rs:15:9
29+
|
30+
LL | mod bar { pub use two_macros::m; }
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
note: `bar` could also refer to the module imported here
33+
--> $DIR/macro-paths.rs:12:9
34+
|
35+
LL | use foo::*;
36+
| ^^^^^^
37+
2038
error[E0659]: `baz` is ambiguous
21-
--> $DIR/macro-paths.rs:23:5
39+
--> $DIR/macro-paths.rs:24:5
2240
|
2341
LL | baz::m! {
2442
| ^^^ ambiguous name
2543
|
2644
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
2745
note: `baz` could refer to the module defined here
28-
--> $DIR/macro-paths.rs:24:9
46+
--> $DIR/macro-paths.rs:25:9
2947
|
3048
LL | mod baz { pub use two_macros::m; }
3149
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3250
note: `baz` could also refer to the module defined here
33-
--> $DIR/macro-paths.rs:18:1
51+
--> $DIR/macro-paths.rs:19:1
3452
|
3553
LL | / pub mod baz {
3654
LL | | pub use two_macros::m;
3755
LL | | }
3856
| |_^
3957
= help: use `crate::baz` to refer to this module unambiguously
4058

41-
error: aborting due to 2 previous errors
59+
error: aborting due to 3 previous errors
4260

4361
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)