Skip to content

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

@lolbinarycat

Description

@lolbinarycat
Contributor

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on May 20, 2024
gurry

gurry commented on May 20, 2024

@gurry
Contributor

@rustbot claim

added a commit that references this issue on Jun 3, 2024
865eaf9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @gurry@lolbinarycat

    Issue actions

      when SomeType::new() cannot be located due to trait bounds, rustc reccomends calling SomeType::new() · Issue #125303 · rust-lang/rust