diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 5d712461993d8..0f36011e4a17e 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -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, @@ -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); } @@ -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 = @@ -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(); @@ -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( diff --git a/tests/ui/resolve/issue-120559.rs b/tests/ui/resolve/issue-120559.rs new file mode 100644 index 0000000000000..e874e840de785 --- /dev/null +++ b/tests/ui/resolve/issue-120559.rs @@ -0,0 +1,5 @@ +use std::io::Read; + +fn f() {} //~ ERROR expected trait, found type parameter `Read` + +fn main() {} diff --git a/tests/ui/resolve/issue-120559.stderr b/tests/ui/resolve/issue-120559.stderr new file mode 100644 index 0000000000000..9150fb38ad5ce --- /dev/null +++ b/tests/ui/resolve/issue-120559.stderr @@ -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() {} + | ^^^^ ---- found this type parameter + | | + | not a trait + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0404`. diff --git a/tests/ui/span/issue-35987.stderr b/tests/ui/span/issue-35987.stderr index 4c4b100da3dd8..d3014f276fd84 100644 --- a/tests/ui/span/issue-35987.stderr +++ b/tests/ui/span/issue-35987.stderr @@ -8,11 +8,6 @@ LL | impl Add for Foo { | --- ^^^ 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