Skip to content

when SomeType::new() cannot be located due to trait bounds, rustc reccomends calling SomeType::new() #125303

Closed
@lolbinarycat

Description

@lolbinarycat

Code

struct Foo<T> {
	v: T,
}

impl<T: SomeTrait + Default> Foo<T> {
	fn new() -> Self {
		Foo{ v: T::default() }
	}
}

trait SomeTrait {}

fn main() {
    let _ = Foo::<u8>::new();
}

Current output

error[E0599]: the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied
  --> src/main.rs:17:24
   |
1  | struct Foo<T> {
   | ------------- function or associated item `new` not found for this struct
...
17 |     let _ = Foo::<u8>::new();
   |                        ^^^ function or associated item cannot be called on `Foo<u8>` due to unsatisfied trait bounds
   |
note: if you're trying to build a new `Foo<u8>`, consider using `Foo::<T>::new` which returns `Foo<_>`
  --> src/main.rs:6:2
   |
6  |     fn new() -> Self {
   |     ^^^^^^^^^^^^^^^^
note: trait bound `u8: SomeTrait` was not satisfied
  --> src/main.rs:5:9
   |
5  | impl<T: SomeTrait + Default> Foo<T> {
   |         ^^^^^^^^^            ------
   |         |
   |         unsatisfied trait bound introduced here

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

Desired output

error[E0599]: the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied
  --> src/main.rs:17:24
   |
1  | struct Foo<T> {
   | ------------- function or associated item `new` not found for this struct
...
17 |     let _ = Foo::<u8>::new();
   |                        ^^^ function or associated item cannot be called on `Foo<u8>` due to unsatisfied trait bounds
   |
note: trait bound `u8: SomeTrait` was not satisfied
  --> src/main.rs:5:9
   |
5  | impl<T: SomeTrait + Default> Foo<T> {
   |         ^^^^^^^^^            ------
   |         |
   |         unsatisfied trait bound introduced here

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

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.80.0-nightly (b1ec1bd65 2024-05-18)
binary: rustc
commit-hash: b1ec1bd65f89c1375d2cf2fb733a87ef390276d3
commit-date: 2024-05-18
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

Anything else?

No response

Metadata

Metadata

Assignees

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