Skip to content

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

Closed
@alice-i-cecile

Description

@alice-i-cecile

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.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions