Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct InferenceDiagnosticsData {
pub struct InferenceDiagnosticsParentData {
prefix: &'static str,
name: String,
span: Span,
}

#[derive(Clone)]
Expand Down Expand Up @@ -124,10 +125,12 @@ impl InferenceDiagnosticsParentData {
) -> Option<InferenceDiagnosticsParentData> {
let parent_name =
tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
let span = tcx.def_span(parent_def_id);

Some(InferenceDiagnosticsParentData {
prefix: tcx.def_descr(parent_def_id),
name: parent_name,
span,
})
}

Expand Down Expand Up @@ -423,16 +426,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
error_code: TypeAnnotationNeeded,
) -> Diag<'a> {
let source_kind = "other";
let source_name = "";
let bad_error = arg_data.make_bad_error(span);
let source_name = &bad_error.name.clone();
let failure_span = None;
let infer_subdiags = Vec::new();
let multi_suggestions = Vec::new();
let bad_label = Some(arg_data.make_bad_error(span));
let bad_label = Some(bad_error);
match error_code {
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
span,
source_kind,
source_name,
source_span: arg_data.parent.as_ref().map(|p| p.span),
failure_span,
infer_subdiags,
multi_suggestions,
Expand All @@ -442,6 +447,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
source_kind,
source_name,
source_span: arg_data.parent.as_ref().map(|p| p.span),
failure_span,
infer_subdiags,
multi_suggestions,
Expand All @@ -451,6 +457,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
source_kind,
source_name,
source_span: arg_data.parent.as_ref().map(|p| p.span),
failure_span,
infer_subdiags,
multi_suggestions,
Expand Down Expand Up @@ -651,6 +658,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
source_kind,
source_name: &name,
source_span: arg_data.parent.as_ref().map(|p| p.span),
failure_span,
infer_subdiags,
multi_suggestions,
Expand All @@ -660,6 +668,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
source_kind,
source_name: &name,
source_span: arg_data.parent.as_ref().map(|p| p.span),
failure_span,
infer_subdiags,
multi_suggestions,
Expand All @@ -669,6 +678,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
source_kind,
source_name: &name,
source_span: arg_data.parent.as_ref().map(|p| p.span),
failure_span,
infer_subdiags,
multi_suggestions,
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_trait_selection/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub struct AnnotationRequired<'a> {
pub span: Span,
pub source_kind: &'static str,
pub source_name: &'a str,
#[note("type must be known for type parameter in this")]
pub source_span: Option<Span>,
#[label("type must be known at this point")]
pub failure_span: Option<Span>,
#[subdiagnostic]
Expand All @@ -214,6 +216,8 @@ pub struct AmbiguousImpl<'a> {
pub span: Span,
pub source_kind: &'static str,
pub source_name: &'a str,
#[note("type must be known for type parameter in this")]
pub source_span: Option<Span>,
#[label("type must be known at this point")]
pub failure_span: Option<Span>,
#[subdiagnostic]
Expand All @@ -236,6 +240,8 @@ pub struct AmbiguousReturn<'a> {
pub span: Span,
pub source_kind: &'static str,
pub source_name: &'a str,
#[note("type must be known for type parameter in this")]
pub source_span: Option<Span>,
#[label("type must be known at this point")]
pub failure_span: Option<Span>,
#[subdiagnostic]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/array-slice-vec/vector-no-ann.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ error[E0282]: type annotations needed for `Vec<_>`
LL | let _foo = Vec::new();
| ^^^^ ---------- type must be known at this point
|
note: type must be known for type parameter in this
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question (non-blocking) [NO-SRC 1/2]: hm, is this supposed to also show alloc's Vec definition? Though not really a big deal, was wondering if it is intentional the alloc snippet is not available

help: consider giving `_foo` an explicit type, where the type for type parameter `T` is specified
|
LL | let _foo: Vec<T> = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | impls(rigid);
| |
| cannot infer type of the type parameter `U` declared on the function `impls`
|
note: type must be known for type parameter in this
--> $DIR/dedup-normalized-2-higher-ranked.rs:25:1
|
LL | fn impls<T: for<'b> Bound<'b, U>, U>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Comment on lines +9 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion (non-blocking): hm, the phrasing on this stderr looks a little bit strange:

  • The current wording doesn't say the concrete types of which type parameter we don't know about
  • Saying "this" I suppose works, it just reads somewhat strange

I wonder if it's worth the trouble to say something more like

the concrete type of type parameter T must be known in impls

... or

the concrete type of type parameter T must be known in this function

s/function/$item_kind/

That being said, I don't think this PR needs to block on that.

= note: cannot satisfy `for<'b> <P as Trait>::Rigid: Bound<'b, _>`
note: required by a bound in `impls`
--> $DIR/dedup-normalized-2-higher-ranked.rs:25:13
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/associated-type-bounds/duplicate-bound-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ error[E0282]: type annotations needed
LL | iter::empty()
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
|
note: type must be known for type parameter in this
--> $SRC_DIR/core/src/iter/sources/empty.rs:LL:COL
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark [NO-SRC 2/2]: same here, the alloc source snippet seems to be missing

help: consider specifying the generic argument
|
LL | iter::empty::<T>()
Expand All @@ -15,6 +17,8 @@ error[E0282]: type annotations needed
LL | iter::empty()
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
|
note: type must be known for type parameter in this
--> $SRC_DIR/core/src/iter/sources/empty.rs:LL:COL
help: consider specifying the generic argument
|
LL | iter::empty::<T>()
Expand All @@ -26,6 +30,8 @@ error[E0282]: type annotations needed
LL | iter::empty()
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
|
note: type must be known for type parameter in this
--> $SRC_DIR/core/src/iter/sources/empty.rs:LL:COL
help: consider specifying the generic argument
|
LL | iter::empty::<T>()
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/async-await/unresolved_type_param.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0282]: type annotations needed
LL | bar().await;
| ^^^ cannot infer type of the type parameter `T` declared on the function `bar`
|
note: type must be known for type parameter in this
--> $DIR/unresolved_type_param.rs:6:1
|
LL | async fn bar<T>() -> () {}
| ^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | bar::<T>().await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ error[E0282]: type annotations needed
LL | with_closure(|x: u32, y| {});
| ^
|
note: type must be known for type parameter in this
--> $DIR/expect-two-infer-vars-supply-ty-with-bound-region.rs:1:1
|
LL | / fn with_closure<F, A, B>(_: F)
LL | | where F: FnOnce(A, B)
| |_________________________^
help: consider giving this closure parameter an explicit type
|
LL | with_closure(|x: u32, y: /* Type */| {});
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/closures/issue-99565.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0282]: type annotations needed
LL | foo(|| {});
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
|
note: type must be known for type parameter in this
--> $DIR/issue-99565.rs:3:1
|
LL | fn foo<T, U>(_: U) {}
| ^^^^^^^^^^^^^^^^^^
Comment on lines +7 to +11
Copy link
Member

@jieyouxu jieyouxu Mar 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: hm, my first reaction when reading this diff is that maybe this is slightly redundant?

The inline note above says

cannot infer type of the type parameter T declared on the function foo

which feels a bit redundant with the new outline note

note: type must be known for type parameter in this

I guess I could see the explicit reference being useful since otherwise the original inline note does not actually point at the definition of foo

EDIT: now I recall you mentioned this specifically in the description lol

We should further refine when we present this info to make sure we don't provide redundant notes in some cases. Also, there are cases (like those in issue 94969) that don't provide any new information with this change.

help: consider specifying the generic arguments
|
LL | foo::<T, _>(|| {});
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/defaults/rp_impl_trait_fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ error[E0284]: type annotations needed
LL | uwu();
| ^^^ cannot infer the value of the const parameter `N` declared on the function `uwu`
|
note: type must be known for type parameter in this
--> $DIR/rp_impl_trait_fail.rs:16:1
|
LL | fn uwu<const N: u8>() -> impl Traitor<N> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Comment on lines +59 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: wait hold on, we call N here a const (generic) parameter, not a type parameter usually right?

note: required by a const generic parameter in `uwu`
--> $DIR/rp_impl_trait_fail.rs:16:8
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ error[E0282]: type annotations needed
LL | let _ = Checked::<generic>;
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
|
note: type must be known for type parameter in this
--> $DIR/fn-const-param-infer.rs:22:1
|
LL | fn generic<T>(val: usize) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | let _ = Checked::<generic::<T>>;
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/fn-const-param-infer.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ error[E0282]: type annotations needed
LL | let _ = Checked::<generic>;
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
|
note: type must be known for type parameter in this
--> $DIR/fn-const-param-infer.rs:22:1
|
LL | fn generic<T>(val: usize) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | let _ = Checked::<generic::<T>>;
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/fn-const-param-infer.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ error[E0282]: type annotations needed
LL | let _ = Checked::<generic>;
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
|
note: type must be known for type parameter in this
--> $DIR/fn-const-param-infer.rs:22:1
|
LL | fn generic<T>(val: usize) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | let _ = Checked::<generic::<T>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | use_dyn(&());
| ^^^^^^^ cannot infer the value of the const parameter `N` declared on the function `use_dyn`
|
note: type must be known for type parameter in this
--> $DIR/dyn-compatibility-ok-infer-err.rs:14:1
|
LL | fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `use_dyn`
--> $DIR/dyn-compatibility-ok-infer-err.rs:14:12
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/infer/cannot-infer-const-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | foo();
| ^^^ cannot infer the value of the const parameter `X` declared on the function `foo`
|
note: type must be known for type parameter in this
--> $DIR/cannot-infer-const-args.rs:1:1
|
LL | fn foo<const X: usize>() -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `foo`
--> $DIR/cannot-infer-const-args.rs:1:8
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/infer/issue-77092.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | println!("{:?}", take_array_from_mut(&mut arr, i));
| ^^^^^^^^^^^^^^^^^^^ cannot infer the value of the const parameter `N` declared on the function `take_array_from_mut`
|
note: type must be known for type parameter in this
--> $DIR/issue-77092.rs:3:1
|
LL | fn take_array_from_mut<T, const N: usize>(data: &mut [T], start: usize) -> &mut [T; N] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Comment on lines +7 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: this is the reason I suggested a wording nit previously, while technically we can say there's 1 type parameter T and one const generic parameter N on this item so it isn't ambiguous, it's not immediately straightforward that we mean N and not T here.

Wait, actually, this note would be slightly misleading then, the one we can't infer is actually the const generic parameter N not the type parameter T looking at the inline note above, right?

note: required by a const generic parameter in `take_array_from_mut`
--> $DIR/issue-77092.rs:3:27
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/infer/method-chain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | Foo.bar().bar().bar().bar().baz();
| ^^^ cannot infer the value of the const parameter `N` declared on the method `baz`
|
note: type must be known for type parameter in this
--> $DIR/method-chain.rs:8:5
|
LL | fn baz<const N: usize>(self) -> Foo {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `Foo::baz`
--> $DIR/method-chain.rs:8:12
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/infer/one-param-uninferred.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | let _: [u8; 17] = foo();
| ^^^ cannot infer the value of the const parameter `M` declared on the function `foo`
|
note: type must be known for type parameter in this
--> $DIR/one-param-uninferred.rs:2:1
|
LL | fn foo<const N: usize, const M: usize>() -> [u8; N] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Comment on lines +7 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: likewise, there are actually two const generic parameters on foo and the one we cannot infer is M, so the note as-is doesn't really tell that nuance.

note: required by a const generic parameter in `foo`
--> $DIR/one-param-uninferred.rs:2:24
|
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/infer/uninferred-consts.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | Foo.foo();
| ^^^ cannot infer the value of the const parameter `A` declared on the method `foo`
|
note: type must be known for type parameter in this
--> $DIR/uninferred-consts.rs:6:5
|
LL | fn foo<const A: usize, const B: usize>(self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `Foo::foo`
--> $DIR/uninferred-consts.rs:6:12
|
Expand All @@ -20,6 +25,11 @@ error[E0284]: type annotations needed
LL | Foo.foo();
| ^^^ cannot infer the value of the const parameter `B` declared on the method `foo`
|
note: type must be known for type parameter in this
--> $DIR/uninferred-consts.rs:6:5
|
LL | fn foo<const A: usize, const B: usize>(self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `Foo::foo`
--> $DIR/uninferred-consts.rs:6:28
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/issues/issue-83249.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | let _ = foo([0; 1]);
| |
| required by a bound introduced by this call
|
note: type must be known for type parameter in this
--> $DIR/issue-83249.rs:12:1
|
LL | fn foo<T: Foo>(_: [u8; T::N]) -> T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: cannot satisfy `_: Foo`
help: the trait `Foo` is implemented for `u8`
--> $DIR/issue-83249.rs:8:1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0284]: type annotations needed
LL | generics_of_parent_impl_trait::foo([()]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `foo`
|
note: type must be known for type parameter in this
--> $DIR/auxiliary/generics_of_parent_impl_trait.rs:5:1
|
LL | pub fn foo<const N: usize>(foo: impl Into<[(); N + 1]>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `foo`
--> $DIR/auxiliary/generics_of_parent_impl_trait.rs:5:12
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | SmallCString::try_from(p).map(|cstr| cstr);
| |
| type must be known at this point
|
note: type must be known for type parameter in this
--> $DIR/try-from-with-const-genericsrs-98299.rs:9:1
|
LL | pub struct SmallCString<const N: usize> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a const generic parameter in `SmallCString`
--> $DIR/try-from-with-const-genericsrs-98299.rs:9:25
|
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/const-generics/unify_with_nested_expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ error[E0284]: type annotations needed
LL | bar();
| ^^^ cannot infer the value of the const parameter `N` declared on the function `bar`
|
note: type must be known for type parameter in this
--> $DIR/unify_with_nested_expr.rs:12:1
|
LL | / fn bar<const N: usize>()
LL | | where
LL | | [(); N + 1]:,
| |_________________^
note: required by a const generic parameter in `bar`
--> $DIR/unify_with_nested_expr.rs:12:8
|
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/consts/issue-64662.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0282]: type annotations needed
LL | A = foo(),
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
|
note: type must be known for type parameter in this
--> $DIR/issue-64662.rs:6:1
|
LL | const fn foo<T>() -> isize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | A = foo::<T>(),
Expand All @@ -15,6 +20,11 @@ error[E0282]: type annotations needed
LL | B = foo(),
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
|
note: type must be known for type parameter in this
--> $DIR/issue-64662.rs:6:1
|
LL | const fn foo<T>() -> isize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | B = foo::<T>(),
Expand Down
Loading
Loading