Skip to content

Commit d72ceb4

Browse files
committed
Fix suggestion span for typo in associated type name
1 parent 87bf6bd commit d72ceb4

File tree

8 files changed

+41
-35
lines changed

8 files changed

+41
-35
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
18141814
assoc_name: ast::Ident,
18151815
span: Span,
18161816
is_equality: Option<String>,
1817-
) -> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
1817+
) -> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
18181818
where I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
18191819
{
18201820
let mut matching_candidates = all_candidates().filter(|r| {
@@ -1828,7 +1828,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
18281828
all_candidates,
18291829
ty_param_name,
18301830
assoc_name,
1831-
span
1831+
span,
18321832
);
18331833
return Err(ErrorReported);
18341834
}
@@ -1916,16 +1916,26 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19161916
return Ok(bound);
19171917
}
19181918

1919-
fn complain_about_assoc_type_not_found<I>(&self,
1920-
all_candidates: impl Fn() -> I,
1921-
ty_param_name: &str,
1922-
assoc_name: ast::Ident,
1923-
span: Span)
1924-
where I: Iterator<Item = ty::PolyTraitRef<'tcx>> {
1925-
let mut err = struct_span_err!(self.tcx().sess, span, E0220,
1926-
"associated type `{}` not found for `{}`",
1927-
assoc_name,
1928-
ty_param_name);
1919+
fn complain_about_assoc_type_not_found<I>(
1920+
&self,
1921+
all_candidates: impl Fn() -> I,
1922+
ty_param_name: &str,
1923+
assoc_name: ast::Ident,
1924+
span: Span,
1925+
) where I: Iterator<Item = ty::PolyTraitRef<'tcx>> {
1926+
// The fallback span is needed because `assoc_name` might be an `Fn()`'s `Output` without a
1927+
// valid span, so we point at the whole path segment instead.
1928+
let span = if assoc_name.span != DUMMY_SP {
1929+
assoc_name.span
1930+
} else {
1931+
span
1932+
};
1933+
let mut err = struct_span_err!(
1934+
self.tcx().sess, span, E0220,
1935+
"associated type `{}` not found for `{}`",
1936+
assoc_name,
1937+
ty_param_name
1938+
);
19291939

19301940
let all_candidate_names: Vec<_> = all_candidates()
19311941
.map(|r| self.tcx().associated_items(r.def_id()))
@@ -1939,22 +1949,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19391949
)
19401950
.collect();
19411951

1942-
if let Some(suggested_name) = find_best_match_for_name(
1943-
all_candidate_names.iter(),
1944-
&assoc_name.as_str(),
1945-
None,
1952+
if let (Some(suggested_name), true) = (
1953+
find_best_match_for_name(all_candidate_names.iter(), &assoc_name.as_str(), None),
1954+
assoc_name.span != DUMMY_SP,
19461955
) {
19471956
err.span_suggestion(
1948-
span,
1957+
assoc_name.span,
19491958
"there is an associated type with a similar name",
19501959
suggested_name.to_string(),
19511960
Applicability::MaybeIncorrect,
19521961
);
19531962
} else {
1954-
err.span_label(
1955-
span,
1956-
format!("associated type `{}` not found", assoc_name)
1957-
);
1963+
err.span_label(span, format!("associated type `{}` not found", assoc_name));
19581964
}
19591965

19601966
err.emit();

src/test/ui/associated-types/associated-types-path-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `A` not found for `T`
2-
--> $DIR/associated-types-path-1.rs:10:23
2+
--> $DIR/associated-types-path-1.rs:10:26
33
|
44
LL | pub fn f1<T>(a: T, x: T::A) {}
5-
| ^^^^ associated type `A` not found
5+
| ^ associated type `A` not found
66

77
error[E0221]: ambiguous associated type `A` in bounds of `T`
88
--> $DIR/associated-types-path-1.rs:11:34

src/test/ui/error-codes/E0220.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `F` not found for `Trait`
2-
--> $DIR/E0220.rs:5:16
2+
--> $DIR/E0220.rs:5:22
33
|
44
LL | type Foo = dyn Trait<F=i32>;
5-
| ^^^^^^^^^^^^ associated type `F` not found
5+
| ^ associated type `F` not found
66

77
error[E0191]: the value of the associated type `Bar` (from trait `Trait`) must be specified
88
--> $DIR/E0220.rs:5:16

src/test/ui/issues/issue-23595-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `anything_here_kills_it` not found for `Self`
2-
--> $DIR/issue-23595-2.rs:6:16
2+
--> $DIR/issue-23595-2.rs:6:22
33
|
44
LL | type B = C<Self::anything_here_kills_it>;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `anything_here_kills_it` not found
5+
| ^^^^^^^^^^^^^^^^^^^^^^ associated type `anything_here_kills_it` not found
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-39211.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `Row` not found for `M`
2-
--> $DIR/issue-39211.rs:11:17
2+
--> $DIR/issue-39211.rs:11:20
33
|
44
LL | let a = [3; M::Row::DIM];
5-
| ^^^^^^^^^^^ associated type `Row` not found
5+
| ^^^ associated type `Row` not found
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-59029-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `Res` not found for `Self`
2-
--> $DIR/issue-59029-1.rs:5:46
2+
--> $DIR/issue-59029-1.rs:5:52
33
|
44
LL | trait MkSvc<Target, Req> = Svc<Target> where Self::Res: Svc<Req>;
5-
| ^^^^^^^^^ associated type `Res` not found
5+
| ^^^ associated type `Res` not found
66

77
error: aborting due to previous error
88

src/test/ui/span/type-binding.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `Trget` not found for `std::ops::Deref`
2-
--> $DIR/type-binding.rs:6:14
2+
--> $DIR/type-binding.rs:6:20
33
|
44
LL | fn homura<T: Deref<Trget = i32>>(_: T) {}
5-
| ^^^^^^^^^^^ help: there is an associated type with a similar name: `Target`
5+
| ^^^^^ help: there is an associated type with a similar name: `Target`
66

77
error: aborting due to previous error
88

src/test/ui/type-alias-impl-trait/not_well_formed.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0220]: associated type `Assoc` not found for `V`
2-
--> $DIR/not_well_formed.rs:10:26
2+
--> $DIR/not_well_formed.rs:10:29
33
|
44
LL | type Foo<V> = impl Trait<V::Assoc>;
5-
| ^^^^^^^^ associated type `Assoc` not found
5+
| ^^^^^ associated type `Assoc` not found
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)