-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
on unresolved import disambiguate suggested path if it would collide
- Loading branch information
Showing
8 changed files
with
160 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub struct SomeUsefulType; |
32 changes: 32 additions & 0 deletions
32
tests/ui/unresolved/unresolved-import-avoid-suggesting-global-path.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Test that we don't prepend `::` to paths referencing crates from the extern prelude | ||
// when it can be avoided[^1] since it's more idiomatic to do so. | ||
// | ||
// [^1]: Counterexample: `unresolved-import-suggest-disambiguated-crate-name.rs` | ||
#![feature(decl_macro)] // allows us to create items with hygienic names | ||
#![allow(unused_imports)] | ||
|
||
// aux-crate:library=library.rs | ||
// edition: 2021 | ||
|
||
mod hygiene { | ||
make!(); | ||
macro make() { | ||
// This won't conflict with the suggested *non-global* path as the syntax context differs. | ||
mod library {} | ||
} | ||
|
||
mod module {} | ||
use module::SomeUsefulType; //~ ERROR unresolved import `module::SomeUsefulType` | ||
} | ||
|
||
mod glob { | ||
use inner::*; | ||
mod inner { | ||
mod library {} | ||
} | ||
|
||
mod module {} | ||
use module::SomeUsefulType; //~ ERROR unresolved import `module::SomeUsefulType` | ||
} | ||
|
||
fn main() {} |
25 changes: 25 additions & 0 deletions
25
tests/ui/unresolved/unresolved-import-avoid-suggesting-global-path.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0432]: unresolved import `module::SomeUsefulType` | ||
--> $DIR/unresolved-import-avoid-suggesting-global-path.rs:19:9 | ||
| | ||
LL | use module::SomeUsefulType; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ no `SomeUsefulType` in `hygiene::module` | ||
| | ||
help: consider importing this struct instead | ||
| | ||
LL | use library::SomeUsefulType; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error[E0432]: unresolved import `module::SomeUsefulType` | ||
--> $DIR/unresolved-import-avoid-suggesting-global-path.rs:29:9 | ||
| | ||
LL | use module::SomeUsefulType; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ no `SomeUsefulType` in `glob::module` | ||
| | ||
help: consider importing this struct instead | ||
| | ||
LL | use library::SomeUsefulType; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0432`. |
19 changes: 19 additions & 0 deletions
19
tests/ui/unresolved/unresolved-import-suggest-disambiguated-crate-name.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Regression test for issue #116970. | ||
// | ||
// When we suggest importing an item from a crate found in the extern prelude and there | ||
// happens to exist a module or type in the current scope with the same name as the crate, | ||
// disambiguate the suggested path by making it global (i.e., by prefixing it with `::`). | ||
// | ||
// For context, when it can be avoided we don't prepend `::` to paths referencing crates | ||
// from the extern prelude. See also `unresolved-import-avoid-suggesting-global-path.rs`. | ||
|
||
// run-rustfix | ||
|
||
// compile-flags: --crate-type=lib | ||
// aux-crate:library=library.rs | ||
// edition: 2021 | ||
|
||
mod library {} // this module shares the same name as the external crate! | ||
|
||
mod module {} | ||
pub use ::library::SomeUsefulType; //~ ERROR unresolved import `module::SomeUsefulType` |
19 changes: 19 additions & 0 deletions
19
tests/ui/unresolved/unresolved-import-suggest-disambiguated-crate-name.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Regression test for issue #116970. | ||
// | ||
// When we suggest importing an item from a crate found in the extern prelude and there | ||
// happens to exist a module or type in the current scope with the same name as the crate, | ||
// disambiguate the suggested path by making it global (i.e., by prefixing it with `::`). | ||
// | ||
// For context, when it can be avoided we don't prepend `::` to paths referencing crates | ||
// from the extern prelude. See also `unresolved-import-avoid-suggesting-global-path.rs`. | ||
|
||
// run-rustfix | ||
|
||
// compile-flags: --crate-type=lib | ||
// aux-crate:library=library.rs | ||
// edition: 2021 | ||
|
||
mod library {} // this module shares the same name as the external crate! | ||
|
||
mod module {} | ||
pub use module::SomeUsefulType; //~ ERROR unresolved import `module::SomeUsefulType` |
14 changes: 14 additions & 0 deletions
14
tests/ui/unresolved/unresolved-import-suggest-disambiguated-crate-name.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0432]: unresolved import `module::SomeUsefulType` | ||
--> $DIR/unresolved-import-suggest-disambiguated-crate-name.rs:19:9 | ||
| | ||
LL | pub use module::SomeUsefulType; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ no `SomeUsefulType` in `module` | ||
| | ||
help: consider importing this struct instead | ||
| | ||
LL | pub use ::library::SomeUsefulType; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0432`. |