Skip to content

Introduce Scope::NonGlobModule and Scope::GlobModule #144131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}
}
Scope::Module(module, _) => {
Scope::NonGlobModule(module, _) => {
this.add_module_candidates(module, suggestions, filter_fn, None);
}
Scope::GlobModule(..) => {
// already inserted in `Scope::NonGlobModule` arm
}
Scope::MacroUsePrelude => {
suggestions.extend(this.macro_use_prelude.iter().filter_map(
|(name, binding)| {
Expand Down Expand Up @@ -1476,9 +1479,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&parent_scope,
ident.span.ctxt(),
|this, scope, _use_prelude, _ctxt| {
let Scope::Module(m, _) = scope else {
let (Scope::NonGlobModule(m, _) | Scope::GlobModule(m, _)) = scope else {
return None;
};

for (_, resolution) in this.resolutions(m).borrow().iter() {
let Some(binding) = resolution.borrow().best_binding() else {
continue;
Expand Down
453 changes: 307 additions & 146 deletions compiler/rustc_resolve/src/ident.rs

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ enum Scope<'ra> {
DeriveHelpersCompat,
/// Textual `let`-like scopes introduced by `macro_rules!` items.
MacroRules(MacroRulesScopeRef<'ra>),
/// Names declared in the given module.
/// The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK`
/// lint if it should be reported.
Module(Module<'ra>, Option<NodeId>),
/// Names introduced by `#[macro_use]` attributes on `extern crate` items.
// The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK`
// lint if it should be reported.
NonGlobModule(Module<'ra>, Option<NodeId>),
// The node ID is for reporting the `PROC_MACRO_DERIVE_RESOLUTION_FALLBACK`
// lint if it should be reported.
GlobModule(Module<'ra>, Option<NodeId>),
MacroUsePrelude,
/// Built-in attributes.
BuiltinAttrs,
Expand Down Expand Up @@ -161,6 +162,8 @@ enum ScopeSet<'ra> {
/// All scopes with the given namespace, used for partially performing late resolution.
/// The node id enables lints and is used for reporting them.
Late(Namespace, Module<'ra>, Option<NodeId>),
/// Scope::NonGlobModule and Scope::GlobModule.
Module(Namespace, Module<'ra>),
}

/// Everything you need to know about a name's location to resolve it.
Expand Down Expand Up @@ -1883,9 +1886,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

self.cm().visit_scopes(ScopeSet::All(TypeNS), parent_scope, ctxt, |this, scope, _, _| {
match scope {
Scope::Module(module, _) => {
Scope::NonGlobModule(module, _) => {
this.get_mut().traits_in_module(module, assoc_item, &mut found_traits);
}
Scope::GlobModule(..) => {
// already inserted in `Scope::NonGlobModule` arm
}
Scope::StdLibPrelude => {
if let Some(module) = this.prelude {
this.get_mut().traits_in_module(module, assoc_item, &mut found_traits);
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/imports/import-after-macro-expand-10.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@ check-pass

mod b {
pub mod http {
pub struct HeaderMap;
Expand All @@ -14,4 +12,5 @@ use crate::b::*;

fn main() {
let h: crate::b::HeaderMap = HeaderMap;
//~^ ERROR ambiguous
}
23 changes: 23 additions & 0 deletions tests/ui/imports/import-after-macro-expand-10.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0659]: `HeaderMap` is ambiguous
--> $DIR/import-after-macro-expand-10.rs:14:22
|
LL | let h: crate::b::HeaderMap = HeaderMap;
| ^^^^^^^^^ ambiguous name
|
= 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
note: `HeaderMap` could refer to the struct defined here
--> $DIR/import-after-macro-expand-10.rs:8:5
|
LL | pub struct HeaderMap;
| ^^^^^^^^^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously
note: `HeaderMap` could also refer to the struct imported here
--> $DIR/import-after-macro-expand-10.rs:6:13
|
LL | pub use self::http::*;
| ^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
3 changes: 1 addition & 2 deletions tests/ui/imports/import-after-macro-expand-11.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@ check-pass

#[derive(Debug)]
struct H;

Expand All @@ -14,6 +12,7 @@ mod p {

fn f() {
let h: crate::p::H = H;
//~^ ERROR ambiguous
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/imports/import-after-macro-expand-11.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0659]: `H` is ambiguous
--> $DIR/import-after-macro-expand-11.rs:14:29
|
LL | let h: crate::p::H = H;
| ^ ambiguous name
|
= 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
note: `H` could refer to the struct defined here
--> $DIR/import-after-macro-expand-11.rs:8:5
|
LL | struct H;
| ^^^^^^^^^
= help: use `self::H` to refer to this struct unambiguously
note: `H` could also refer to the struct imported here
--> $DIR/import-after-macro-expand-11.rs:5:9
|
LL | use super::*;
| ^^^^^^^^
= help: use `self::H` to refer to this struct unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
2 changes: 1 addition & 1 deletion tests/ui/imports/import-after-macro-expand-13.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ check-pass
// similar as `import-after-macro-expand-6.rs`

use crate::a::HeaderMap;
Expand All @@ -19,4 +18,5 @@ mod a {

fn main() {
let h: crate::b::HeaderMap = HeaderMap;
//~^ ERROR ambiguous
}
23 changes: 23 additions & 0 deletions tests/ui/imports/import-after-macro-expand-13.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0659]: `HeaderMap` is ambiguous
--> $DIR/import-after-macro-expand-13.rs:20:22
|
LL | let h: crate::b::HeaderMap = HeaderMap;
| ^^^^^^^^^ ambiguous name
|
= 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
note: `HeaderMap` could refer to the struct defined here
--> $DIR/import-after-macro-expand-13.rs:12:5
|
LL | pub struct HeaderMap;
| ^^^^^^^^^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously
note: `HeaderMap` could also refer to the struct imported here
--> $DIR/import-after-macro-expand-13.rs:10:13
|
LL | pub use self::http::*;
| ^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
3 changes: 1 addition & 2 deletions tests/ui/imports/import-after-macro-expand-14.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@ check-pass

use crate::a::HeaderMap;

mod b {
Expand All @@ -19,4 +17,5 @@ mod a {

fn main() {
let h: crate::b::HeaderMap = HeaderMap;
//~^ ERROR ambiguous
}
23 changes: 23 additions & 0 deletions tests/ui/imports/import-after-macro-expand-14.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0659]: `HeaderMap` is ambiguous
--> $DIR/import-after-macro-expand-14.rs:19:22
|
LL | let h: crate::b::HeaderMap = HeaderMap;
| ^^^^^^^^^ ambiguous name
|
= 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
note: `HeaderMap` could refer to the struct defined here
--> $DIR/import-after-macro-expand-14.rs:11:5
|
LL | pub struct HeaderMap;
| ^^^^^^^^^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously
note: `HeaderMap` could also refer to the struct imported here
--> $DIR/import-after-macro-expand-14.rs:9:13
|
LL | pub use self::http::*;
| ^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
2 changes: 1 addition & 1 deletion tests/ui/imports/import-after-macro-expand-2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ check-pass
// https://github.com/rust-lang/rust/issues/56593#issuecomment-1133174514

use thing::*;
Expand All @@ -13,6 +12,7 @@ mod tests {

fn test_thing() {
let thing: crate::Thing = Thing::Foo;
//~^ ERROR ambiguous
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/ui/imports/import-after-macro-expand-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0659]: `Thing` is ambiguous
--> $DIR/import-after-macro-expand-2.rs:14:27
|
LL | let thing: crate::Thing = Thing::Foo;
| ^^^^^ ambiguous name
|
= 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
note: `Thing` could refer to the enum defined here
--> $DIR/import-after-macro-expand-2.rs:6:1
|
LL | / pub enum Thing {
LL | | Foo
LL | | }
| |_^
= help: use `crate::Thing` to refer to this enum unambiguously
note: `Thing` could also refer to the enum imported here
--> $DIR/import-after-macro-expand-2.rs:3:5
|
LL | use thing::*;
| ^^^^^^^^
= help: use `crate::Thing` to refer to this enum unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
2 changes: 1 addition & 1 deletion tests/ui/imports/import-after-macro-expand-6.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ check-pass
// https://github.com/rust-lang/rust/pull/113099#issuecomment-1633574396

pub mod a {
Expand All @@ -19,4 +18,5 @@ use crate::a::HeaderMap;

fn main() {
let h: crate::b::HeaderMap = HeaderMap;
//~^ ERROR ambiguous
}
23 changes: 23 additions & 0 deletions tests/ui/imports/import-after-macro-expand-6.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0659]: `HeaderMap` is ambiguous
--> $DIR/import-after-macro-expand-6.rs:20:22
|
LL | let h: crate::b::HeaderMap = HeaderMap;
| ^^^^^^^^^ ambiguous name
|
= 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
note: `HeaderMap` could refer to the struct defined here
--> $DIR/import-after-macro-expand-6.rs:14:5
|
LL | pub struct HeaderMap;
| ^^^^^^^^^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously
note: `HeaderMap` could also refer to the struct imported here
--> $DIR/import-after-macro-expand-6.rs:12:13
|
LL | pub use self::http::*;
| ^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
3 changes: 1 addition & 2 deletions tests/ui/imports/import-after-macro-expand-9.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@ check-pass

use crate::b::*;

mod b {
Expand All @@ -14,4 +12,5 @@ mod b {

fn main() {
let h: crate::b::HeaderMap = HeaderMap;
//~^ ERROR ambiguous
}
23 changes: 23 additions & 0 deletions tests/ui/imports/import-after-macro-expand-9.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0659]: `HeaderMap` is ambiguous
--> $DIR/import-after-macro-expand-9.rs:14:22
|
LL | let h: crate::b::HeaderMap = HeaderMap;
| ^^^^^^^^^ ambiguous name
|
= 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
note: `HeaderMap` could refer to the struct defined here
--> $DIR/import-after-macro-expand-9.rs:10:5
|
LL | pub struct HeaderMap;
| ^^^^^^^^^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously
note: `HeaderMap` could also refer to the struct imported here
--> $DIR/import-after-macro-expand-9.rs:8:13
|
LL | pub use self::http::*;
| ^^^^^^^^^^^^^
= help: use `self::HeaderMap` to refer to this struct unambiguously

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.
3 changes: 2 additions & 1 deletion tests/ui/imports/imports.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ run-pass
#![allow(unused)]

// Like other items, private imports can be imported and used non-lexically in paths.
Expand All @@ -22,13 +21,15 @@ fn g() {
use crate::bar::*;
fn f() -> bool { true }
let _: bool = f();
//~^ ERROR `f` is ambiguous
}

fn h() {
use crate::foo::*;
use crate::bar::*;
use crate::f;
let _: bool = f();
//~^ ERROR `f` is ambiguous
}

// Here, there appears to be shadowing but isn't because of namespaces.
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/imports/imports.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0659]: `f` is ambiguous
--> $DIR/imports.rs:23:19
|
LL | let _: bool = f();
| ^ ambiguous name
|
= note: ambiguous because of multiple glob imports of a name in the same module
note: `f` could refer to the function imported here
--> $DIR/imports.rs:20:9
|
LL | use crate::foo::*;
| ^^^^^^^^^^^^^
= help: consider adding an explicit import of `f` to disambiguate
note: `f` could also refer to the function imported here
--> $DIR/imports.rs:21:9
|
LL | use crate::bar::*;
| ^^^^^^^^^^^^^
= help: consider adding an explicit import of `f` to disambiguate

error[E0659]: `f` is ambiguous
--> $DIR/imports.rs:31:19
|
LL | let _: bool = f();
| ^ ambiguous name
|
= note: ambiguous because of multiple glob imports of a name in the same module
note: `f` could refer to the function imported here
--> $DIR/imports.rs:28:9
|
LL | use crate::foo::*;
| ^^^^^^^^^^^^^
= help: consider adding an explicit import of `f` to disambiguate
note: `f` could also refer to the function imported here
--> $DIR/imports.rs:29:9
|
LL | use crate::bar::*;
| ^^^^^^^^^^^^^
= help: consider adding an explicit import of `f` to disambiguate

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0659`.
1 change: 1 addition & 0 deletions tests/ui/imports/issue-114682-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ mac!();
fn main() {
A!();
//~^ ERROR `A` is ambiguous
//~^^ ERROR `A` is ambiguous
}
Loading
Loading