Compiler could give better suggestions on generics type.
Open
Nobody has claimed this yet.
A-diagnostics
A-suggestion-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
pub struct Point<T> {
x: T,
}
pub trait MyTrait1 {
fn fun(&self, p: Point<T>);
}
pub struct MyStruct;
impl MyTrait1 for MyStruct {
fn fun(&self, p: Point<T>) {
todo!();
}
}
fn main() {
todo!();
}
Current output
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:5:28
|
5 | fn fun(&self, p: Point<T>);
| ^ not found in this scope
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:9:28
|
9 | fn fun(&self, p: Point<T>) {
| ^ not found in this scope
|
help: you might be missing a type parameter
|
8 | impl<T> MyTrait1 for MyStruct {
| +++
For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
Desired output
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:5:28
|
5 | fn fun(&self, p: Point<T>);
| ^ not found in this scope
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:9:28
|
9 | fn fun(&self, p: Point<T>) {
| ^ not found in this scope
|
help: you might be missing a type parameter
|
9 | fn fun<T>(&self, p: Point<T>) {
| +++
For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
Rationale and extra context
The suggestions for adding type parameters should be specific to the function itself rather than the entire implementation.
Other cases
No response
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the provided Rust snippet and inspect the rustc E0412 diagnostic and its suggestion output. No repository file or test is named in the issue, so locate the diagnostic handling for this case before adding coverage. Done means the suggestion targets fn fun with the type parameter, rather than the impl block.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100