Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor diagnostic message when passing in type constructor to generic function #102852

Closed
alice-i-cecile opened this issue Oct 9, 2022 · 0 comments · Fixed by #102863
Closed

Poor diagnostic message when passing in type constructor to generic function #102852

alice-i-cecile opened this issue Oct 9, 2022 · 0 comments · Fixed by #102863
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alice-i-cecile
Copy link

alice-i-cecile commented Oct 9, 2022

When passing arguments to a generic function, users can accidentally provide the type, rather than an instance to a type.
The error message provided is not very helpful (especially for beginners), as it uses the anonymous type, rather than pointing out the obvious error.

Given the following code:

fn main() {
    insert_resource(Marker);
    insert_resource(Time);
}

trait Resource {}

fn insert_resource<R: Resource>(resource: R) {}

struct Marker;
impl Resource for Marker {}

struct Time(u32);

impl Resource for Time {}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `fn(u32) -> Time {Time}: Resource` is not satisfied
 --> src/main.rs:3:21
  |
3 |     insert_resource(Time);
  |     --------------- ^^^^ the trait `Resource` is not implemented for `fn(u32) -> Time {Time}`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the following other types implement trait `Resource`:
            Marker
            Time
note: required by a bound in `insert_resource`
 --> src/main.rs:8:23
  |
8 | fn insert_resource<R: Resource>(resource: R) {}
  |                       ^^^^^^^^ required by this bound in `insert_resource`

For more information about this error, try `rustc --explain E0277`.```

Ideally the output should look like:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `fn(u32) -> Time {Time}: Resource` is not satisfied
 --> src/main.rs:3:21
  |
3 |     insert_resource(Time);
  |     --------------- ^^^^ the trait `Resource` is not implemented for `fn(u32) -> Time {Time}`
  |     |
  |     required by a bound introduced by this call
  |
  = help: you've passed in a constructor, but the type itself implements the required trait. Did you mean to supply a concrete value for `Time`?
note: required by a bound in `insert_resource`
 --> src/main.rs:8:23
  |
8 | fn insert_resource<R: Resource>(resource: R) {}
  |                       ^^^^^^^^ required by this bound in `insert_resource`

For more information about this error, try `rustc --explain E0277`.```

Note that this particular diagnostic error is frustrating, since as shown with the `Marker` struct above, equivalent code compiles just fine when using unit structs.

This error is commonly encountered when writing or refactoring Bevy code, as both components and resources have trait bounds and unit structs are widely used.

@alice-i-cecile alice-i-cecile added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 9, 2022
@compiler-errors compiler-errors self-assigned this Oct 9, 2022
@bors bors closed this as completed in 5c2c476 Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants