Skip to content

Commit d33cd2d

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

File tree

4 files changed

+58
-29
lines changed

4 files changed

+58
-29
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 39 additions & 24 deletions
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,
@@ -474,11 +474,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
474474
}
475475

476476
let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
477-
478-
// if we have suggested using pattern matching, then don't add needless suggestions
479-
// for typos.
480477
fallback |= self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);
481-
478+
if self.suggest_shadowed(&mut err, source, path, following_seg, span) {
479+
// if there is already a shadowed name, don'suggest candidates for importing
480+
candidates.clear();
481+
fallback = true;
482+
}
482483
if fallback {
483484
// Fallback label.
484485
err.span_label(base_error.span, base_error.fallback_label);
@@ -872,25 +873,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
872873
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
873874
let typo_sugg =
874875
self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
875-
let is_in_same_file = &|sp1, sp2| {
876-
let source_map = self.r.tcx.sess.source_map();
877-
let file1 = source_map.span_to_filename(sp1);
878-
let file2 = source_map.span_to_filename(sp2);
879-
file1 == file2
880-
};
881-
// print 'you might have meant' if the candidate is (1) is a shadowed name with
882-
// accessible definition and (2) either defined in the same crate as the typo
883-
// (could be in a different file) or introduced in the same file as the typo
884-
// (could belong to a different crate)
885-
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
886-
&& res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
887-
{
888-
err.span_label(
889-
sugg_span,
890-
format!("you might have meant to refer to this {}", res.descr()),
891-
);
892-
return true;
893-
}
894876
let mut fallback = false;
895877
let typo_sugg = typo_sugg.to_opt_suggestion();
896878
if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
@@ -918,6 +900,39 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
918900
fallback
919901
}
920902

903+
fn suggest_shadowed(
904+
&mut self,
905+
err: &mut Diagnostic,
906+
source: PathSource<'_>,
907+
path: &[Segment],
908+
following_seg: Option<&Segment>,
909+
span: Span,
910+
) -> bool {
911+
let is_expected = &|res| source.is_expected(res);
912+
let typo_sugg =
913+
self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
914+
let is_in_same_file = &|sp1, sp2| {
915+
let source_map = self.r.tcx.sess.source_map();
916+
let file1 = source_map.span_to_filename(sp1);
917+
let file2 = source_map.span_to_filename(sp2);
918+
file1 == file2
919+
};
920+
// print 'you might have meant' if the candidate is (1) is a shadowed name with
921+
// accessible definition and (2) either defined in the same crate as the typo
922+
// (could be in a different file) or introduced in the same file as the typo
923+
// (could belong to a different crate)
924+
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
925+
&& res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
926+
{
927+
err.span_label(
928+
sugg_span,
929+
format!("you might have meant to refer to this {}", res.descr()),
930+
);
931+
return true;
932+
}
933+
false
934+
}
935+
921936
fn err_code_special_cases(
922937
&mut self,
923938
err: &mut Diagnostic,

tests/ui/resolve/issue-120559.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 0 additions & 5 deletions
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)