Skip to content

Commit 9461800

Browse files
committed
remove importing suggestions when there is a shadowed typo canddiate
1 parent b17491c commit 9461800

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
460460
return (err, Vec::new());
461461
}
462462

463-
let (found, candidates) = self.try_lookup_name_relaxed(
463+
let (found, mut candidates) = self.try_lookup_name_relaxed(
464464
&mut err,
465465
source,
466466
path,
@@ -473,13 +473,18 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
473473
return (err, candidates);
474474
}
475475

476-
let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
476+
let trait_fallback =
477+
self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
477478

478479
// if we have suggested using pattern matching, then don't add needless suggestions
479480
// for typos.
480-
fallback |= self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);
481-
482-
if fallback {
481+
let (typo_fallback, suggested_shadowed) =
482+
self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);
483+
if suggested_shadowed {
484+
// if there is already a shadowed name, don'suggest candidates for importing
485+
candidates.clear();
486+
}
487+
if trait_fallback || typo_fallback {
483488
// Fallback label.
484489
err.span_label(base_error.span, base_error.fallback_label);
485490
}
@@ -867,7 +872,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
867872
following_seg: Option<&Segment>,
868873
span: Span,
869874
base_error: &BaseError,
870-
) -> bool {
875+
) -> (bool, bool) {
871876
let is_expected = &|res| source.is_expected(res);
872877
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
873878
let typo_sugg =
@@ -889,7 +894,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
889894
sugg_span,
890895
format!("you might have meant to refer to this {}", res.descr()),
891896
);
892-
return true;
897+
return (true, true);
893898
}
894899
let mut fallback = false;
895900
let typo_sugg = typo_sugg.to_opt_suggestion();
@@ -915,7 +920,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
915920
fallback = !self.let_binding_suggestion(err, ident_span);
916921
}
917922
}
918-
fallback
923+
(fallback, false)
919924
}
920925

921926
fn err_code_special_cases(

tests/ui/resolve/issue-120559.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::io::Read;
2+
3+
fn f<T: Read, U, Read>() {} //~ ERROR expected trait, found type parameter `Read`
4+
5+
fn main() {}

tests/ui/resolve/issue-120559.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0404]: expected trait, found type parameter `Read`
2+
--> $DIR/issue-120559.rs:3:9
3+
|
4+
LL | use std::io::Read;
5+
| ---- you might have meant to refer to this trait
6+
LL |
7+
LL | fn f<T: Read, U, Read>() {}
8+
| ^^^^ ---- found this type parameter
9+
| |
10+
| not a trait
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0404`.

tests/ui/span/issue-35987.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ LL | impl<T: Clone, Add> Add for Foo<T> {
88
| --- ^^^ not a trait
99
| |
1010
| found this type parameter
11-
|
12-
help: consider importing this trait instead
13-
|
14-
LL + use std::ops::Add;
15-
|
1611

1712
error: aborting due to 1 previous error
1813

0 commit comments

Comments
 (0)