Skip to content

Commit 939a224

Browse files
committed
Point at assoc fn definition on type param divergence
When the number of type parameters in the associated function of an impl and its trait differ, we now *always* point at the trait one, even if it comes from a foreign crate. When it is local, we point at the specific params, when it is foreign, we point at the whole associated item. Fix #69944.
1 parent cc705b8 commit 939a224

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -1557,38 +1557,24 @@ fn compare_number_of_generics<'tcx>(
15571557
DiagnosticId::Error("E0049".into()),
15581558
);
15591559

1560-
let mut suffix = None;
1561-
1560+
let msg =
1561+
format!("expected {trait_count} {kind} parameter{}", pluralize!(trait_count),);
15621562
if let Some(spans) = trait_spans {
15631563
let mut spans = spans.iter();
15641564
if let Some(span) = spans.next() {
1565-
err.span_label(
1566-
*span,
1567-
format!(
1568-
"expected {} {} parameter{}",
1569-
trait_count,
1570-
kind,
1571-
pluralize!(trait_count),
1572-
),
1573-
);
1565+
err.span_label(*span, msg);
15741566
}
15751567
for span in spans {
15761568
err.span_label(*span, "");
15771569
}
15781570
} else {
1579-
suffix = Some(format!(", expected {trait_count}"));
1571+
err.span_label(tcx.def_span(trait_.def_id), msg);
15801572
}
15811573

15821574
if let Some(span) = span {
15831575
err.span_label(
15841576
span,
1585-
format!(
1586-
"found {} {} parameter{}{}",
1587-
impl_count,
1588-
kind,
1589-
pluralize!(impl_count),
1590-
suffix.unwrap_or_default(),
1591-
),
1577+
format!("found {} {} parameter{}", impl_count, kind, pluralize!(impl_count),),
15921578
);
15931579
}
15941580

tests/ui/typeck/issue-36708.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ error[E0049]: method `foo` has 1 type parameter but its trait declaration has 0
22
--> $DIR/issue-36708.rs:8:12
33
|
44
LL | fn foo<T>() {}
5-
| ^ found 1 type parameter, expected 0
5+
| ^ found 1 type parameter
6+
|
7+
::: $DIR/auxiliary/issue-36708.rs:4:5
8+
|
9+
LL | fn foo();
10+
| --------- expected 0 type parameters
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)