Skip to content

Point at definition of item with type parameters that couldn't be inferred#153890

Closed
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-94969
Closed

Point at definition of item with type parameters that couldn't be inferred#153890
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-94969

Conversation

@estebank

@estebank estebank commented Mar 14, 2026

Copy link
Copy Markdown
Contributor

View all comments

When calling a function with type parameters that are unconstrained by the call expression, we mention the type parameter that couldn't be inferred, but the user previously didn't see where the name was coming from. By pointing at the function itself, including the type parameter, we give better context on what they should have done, specially when the inference machinery fails to provide any other context.

error[E0282]: type annotations needed
  --> $DIR/unresolved_type_param.rs:9:5
   |
LL |     bar().await;
   |     ^^^ cannot infer type of the type parameter `T` declared on the function `bar`
   |
note: type must be known for this
  --> $DIR/unresolved_type_param.rs:6:1
   |
LL | async fn bar<T>() -> () {}
   | ^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
   |
LL |     bar::<T>().await;
   |        +++++

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.

@rustbot

rustbot commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in need_type_info.rs

cc @lcnr

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 14, 2026
@rustbot

rustbot commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 15 candidates

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Initial review pass

View changes since this review

Comment thread tests/ui/array-slice-vec/vector-no-ann.stderr
Comment on lines +9 to +13
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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree that the output is not ideal. I'd tried an initial attempt at getting the same information from the existing label in the message, but I think I might have to do some mild refactoring to accomplish that. I agree that the wording should be closer to what you propose.

Comment thread tests/ui/associated-type-bounds/duplicate-bound-err.stderr
Comment on lines +7 to +11
note: type must be known for type parameter in this
--> $DIR/issue-99565.rs:3:1
|
LL | fn foo<T, U>(_: U) {}
| ^^^^^^^^^^^^^^^^^^

@jieyouxu jieyouxu Mar 15, 2026

Copy link
Copy Markdown
Member

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.

Comment on lines +59 to +63
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> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Copy Markdown
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, this is indeed incorrect

Comment on lines +7 to +11
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] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Copy Markdown
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?

Comment on lines +7 to +11
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] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Copy Markdown
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.

Comment on lines +55 to +58
note: type must be known for type parameter in this
--> $DIR/trait-impl-wrong-args-count.rs:9:9
|
LL | pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remark: likewise, hard to tell when there's not just only 1 type/const generic parameters.

Comment on lines +9 to +12
note: type must be known for type parameter in this
--> $DIR/issue-113264-incorrect-impl-trait-in-path-suggestion.rs:6:5
|
LL | fn owo(&self, _: Option<&impl T>) {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Discussion: hm, this is slightly more interesting, this is an APIT which we do not explicitly name a type parameter right? I wonder if in such cases we need to subspan highlight the impl T itself since you can have multiple impl Ts that users can only tell apart based on their source positions

fn foo(x: impl T, y: impl T) {}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The note should point at the bound itself, as well as the item name (that will require some changes to make that additional work with structured diagnostics).

Comment on lines +7 to +11
note: type must be known for type parameter in this
--> $DIR/issue-83606.rs:3:1
|
LL | fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remark: hm, and there can be a case where a user mixes explicit type params / const generic params and APITs

@jieyouxu

Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 15, 2026
@rustbot

rustbot commented Mar 15, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rust-bors

This comment has been minimized.

…erred

When calling a function with type parameters that are unconstrained by the call expression, we mention the type parameter that couldn't be inferred, but the user previously didn't see where the name was coming from. By pointing at the function itself, including the type parameter, we give better context on what they should have done, specially when the inference machinery fails to provide any other context.

```
error[E0282]: type annotations needed
  --> $DIR/unresolved_type_param.rs:9:5
   |
LL |     bar().await;
   |     ^^^ cannot infer type of the type parameter `T` declared on the function `bar`
   |
note: type must be known for this
  --> $DIR/unresolved_type_param.rs:6:1
   |
LL | async fn bar<T>() -> () {}
   | ^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
   |
LL |     bar::<T>().await;
   |        +++++
```

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.
@rustbot

rustbot commented Mar 17, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank estebank marked this pull request as draft March 18, 2026 01:59
@rust-bors

rust-bors Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #154308) made this pull request unmergeable. Please resolve the merge conflicts.

@estebank estebank closed this Jun 5, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants