Skip to content
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
13 changes: 13 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/imports/same-prefix-unresolved-import-148070.rs
Original file line number Diff line number Diff line change
@@ -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`
43 changes: 43 additions & 0 deletions tests/ui/imports/same-prefix-unresolved-import-148070.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion tests/ui/test-attrs/inaccessible-test-modules.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading