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

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

Open
lolbinarycat opened this issue May 20, 2024 · 1 comment · May be fixed by #125397
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

@lolbinarycat
Copy link

lolbinarycat commented May 20, 2024

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

@lolbinarycat lolbinarycat 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 May 20, 2024
@gurry
Copy link
Contributor

gurry commented May 20, 2024

@rustbot claim

@gurry gurry linked a pull request May 22, 2024 that will close this issue
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
Development

Successfully merging a pull request may close this issue.

2 participants