Skip to content

Commit

Permalink
Auto merge of #109348 - cjgillot:issue-109146, r=petrochenkov
Browse files Browse the repository at this point in the history
Resolve visibility paths as modules not as types.

Asking for a resolution with `opt_ns = Some(TypeNS)` allows path resolution to look for type-relative paths, leaving unresolved segments behind. However, for visibility paths we really need to look for a module, so we need to pass `opt_ns = None`.

Fixes #109146

r? `@petrochenkov`
  • Loading branch information
bors committed Aug 5, 2023
2 parents 1cabb8e + 69ea85d commit 28b6607
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes/E0577.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Something other than a module was found in visibility scope.
Erroneous code example:

```compile_fail,E0577,edition2018
pub struct Sea;
pub enum Sea {}
pub (in crate::Sea) struct Shark; // error!
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
};
match self.r.resolve_path(
&segments,
Some(TypeNS),
None,
parent_scope,
finalize.then(|| Finalize::new(id, path.span)),
None,
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/resolve/unresolved-segments-visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Check that we do not ICE due to unresolved segments in visibility path.
#![crate_type = "lib"]

extern crate alloc as b;

mod foo {
mod bar {
pub(in b::string::String::newy) extern crate alloc as e;
//~^ ERROR failed to resolve: `String` is a struct, not a module [E0433]
}
}
9 changes: 9 additions & 0 deletions tests/ui/resolve/unresolved-segments-visibility.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0433]: failed to resolve: `String` is a struct, not a module
--> $DIR/unresolved-segments-visibility.rs:8:27
|
LL | pub(in b::string::String::newy) extern crate alloc as e;
| ^^^^^^ `String` is a struct, not a module

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
2 changes: 1 addition & 1 deletion tests/ui/span/visibility-ty-params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ macro_rules! m {

struct S<T>(T);
m!{ S<u8> } //~ ERROR unexpected generic arguments in path
//~| ERROR expected module, found struct `S`
//~| ERROR failed to resolve: `S` is a struct, not a module [E0433]

mod m {
m!{ m<> } //~ ERROR unexpected generic arguments in path
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/span/visibility-ty-params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ error: unexpected generic arguments in path
LL | m!{ S<u8> }
| ^^^^

error[E0577]: expected module, found struct `S`
error[E0433]: failed to resolve: `S` is a struct, not a module
--> $DIR/visibility-ty-params.rs:6:5
|
LL | m!{ S<u8> }
| ^^^^^ not a module
| ^ `S` is a struct, not a module

error: unexpected generic arguments in path
--> $DIR/visibility-ty-params.rs:10:10
Expand All @@ -18,4 +18,4 @@ LL | m!{ m<> }

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0577`.
For more information about this error, try `rustc --explain E0433`.
4 changes: 2 additions & 2 deletions tests/ui/use/use-self-type.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
error[E0433]: failed to resolve: `Self` cannot be used in imports
--> $DIR/use-self-type.rs:7:16
|
LL | pub(in Self::f) struct Z;
| ^^^^ `Self` is only available in impls, traits, and type definitions
| ^^^^ `Self` cannot be used in imports

error[E0432]: unresolved import `Self`
--> $DIR/use-self-type.rs:6:13
Expand Down

0 comments on commit 28b6607

Please sign in to comment.