Skip to content

Commit

Permalink
remove importing suggestions when there is a shadowed typo canddiate
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Feb 14, 2024
1 parent b17491c commit 9461800
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
21 changes: 13 additions & 8 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
return (err, Vec::new());
}

let (found, candidates) = self.try_lookup_name_relaxed(
let (found, mut candidates) = self.try_lookup_name_relaxed(
&mut err,
source,
path,
Expand All @@ -473,13 +473,18 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
return (err, candidates);
}

let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
let trait_fallback =
self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);

// if we have suggested using pattern matching, then don't add needless suggestions
// for typos.
fallback |= self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);

if fallback {
let (typo_fallback, suggested_shadowed) =
self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);
if suggested_shadowed {
// if there is already a shadowed name, don'suggest candidates for importing
candidates.clear();
}
if trait_fallback || typo_fallback {
// Fallback label.
err.span_label(base_error.span, base_error.fallback_label);
}
Expand Down Expand Up @@ -867,7 +872,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
following_seg: Option<&Segment>,
span: Span,
base_error: &BaseError,
) -> bool {
) -> (bool, bool) {
let is_expected = &|res| source.is_expected(res);
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
let typo_sugg =
Expand All @@ -889,7 +894,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
sugg_span,
format!("you might have meant to refer to this {}", res.descr()),
);
return true;
return (true, true);
}
let mut fallback = false;
let typo_sugg = typo_sugg.to_opt_suggestion();
Expand All @@ -915,7 +920,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
fallback = !self.let_binding_suggestion(err, ident_span);
}
}
fallback
(fallback, false)
}

fn err_code_special_cases(
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/resolve/issue-120559.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::io::Read;

fn f<T: Read, U, Read>() {} //~ ERROR expected trait, found type parameter `Read`

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/resolve/issue-120559.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0404]: expected trait, found type parameter `Read`
--> $DIR/issue-120559.rs:3:9
|
LL | use std::io::Read;
| ---- you might have meant to refer to this trait
LL |
LL | fn f<T: Read, U, Read>() {}
| ^^^^ ---- found this type parameter
| |
| not a trait

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0404`.
5 changes: 0 additions & 5 deletions tests/ui/span/issue-35987.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ LL | impl<T: Clone, Add> Add for Foo<T> {
| --- ^^^ not a trait
| |
| found this type parameter
|
help: consider importing this trait instead
|
LL + use std::ops::Add;
|

error: aborting due to 1 previous error

Expand Down

0 comments on commit 9461800

Please sign in to comment.