diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 17cd466f96b87..dc31f8008114c 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -306,6 +306,19 @@ impl TrimmedSubstitutionPart { /// `BB` is. Return the length of the prefix, the "trimmed" suggestion, and the length /// of the suffix. fn as_substr<'a>(original: &'a str, suggestion: &'a str) -> Option<(usize, &'a str, usize)> { + // Case for import paths where the suggestion shares a prefix with the original. + // Without this, suggesting `std::sync` for `sync` would incorrectly highlight `td::s` + // instead of `std::` because of the common 's' prefix. See #148070. + if suggestion.contains("::") + && suggestion.ends_with(original) + && suggestion.len() > original.len() + && let prefix = &suggestion[..suggestion.len() - original.len()] + && prefix.ends_with("::") + && suggestion.chars().next() == original.chars().next() + { + return Some((0, prefix, original.len())); + } + let common_prefix = original .chars() .zip(suggestion.chars()) diff --git a/tests/ui/imports/same-prefix-unresolved-import-148070.rs b/tests/ui/imports/same-prefix-unresolved-import-148070.rs new file mode 100644 index 0000000000000..e9d991aa0376e --- /dev/null +++ b/tests/ui/imports/same-prefix-unresolved-import-148070.rs @@ -0,0 +1,5 @@ +// https://github.com/rust-lang/rust/issues/148070 +#![no_main] +use stat; //~ ERROR unresolved import `stat` +use str; //~ ERROR unresolved import `str` +use sync; //~ ERROR unresolved import `sync` diff --git a/tests/ui/imports/same-prefix-unresolved-import-148070.stderr b/tests/ui/imports/same-prefix-unresolved-import-148070.stderr new file mode 100644 index 0000000000000..e4a2b060fa435 --- /dev/null +++ b/tests/ui/imports/same-prefix-unresolved-import-148070.stderr @@ -0,0 +1,43 @@ +error[E0432]: unresolved import `stat` + --> $DIR/same-prefix-unresolved-import-148070.rs:3:5 + | +LL | use stat; + | ^^^^ no `stat` in the root + | +help: consider importing this struct instead + | +LL | use std::os::linux::raw::stat; + | +++++++++++++++++++++ + +error[E0432]: unresolved import `str` + --> $DIR/same-prefix-unresolved-import-148070.rs:4:5 + | +LL | use str; + | ^^^ no `str` in the root + | +help: a similar name exists in the module + | +LL - use str; +LL + use std; + | +help: consider importing one of these items instead + | +LL | use std::primitive::str; + | ++++++++++++++++ +LL | use std::str; + | +++++ + +error[E0432]: unresolved import `sync` + --> $DIR/same-prefix-unresolved-import-148070.rs:5:5 + | +LL | use sync; + | ^^^^ no `sync` in the root + | +help: consider importing this module instead + | +LL | use std::sync; + | +++++ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0432`. diff --git a/tests/ui/test-attrs/inaccessible-test-modules.stderr b/tests/ui/test-attrs/inaccessible-test-modules.stderr index c66dc0d0fc26c..dfb6985730af5 100644 --- a/tests/ui/test-attrs/inaccessible-test-modules.stderr +++ b/tests/ui/test-attrs/inaccessible-test-modules.stderr @@ -13,7 +13,7 @@ LL | use test as y; help: consider importing this module instead | LL | use test::test as y; - | ++++++ + | ++++++ error: aborting due to 2 previous errors