Skip to content

Confusing error when using Self in generic impl block missing trait bounds #52523

New issue

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

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

Already on GitHub? # to your account

Closed
cauebs opened this issue Jul 19, 2018 · 0 comments · Fixed by #69255
Closed

Confusing error when using Self in generic impl block missing trait bounds #52523

cauebs opened this issue Jul 19, 2018 · 0 comments · Fixed by #69255
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cauebs
Copy link
Contributor

cauebs commented Jul 19, 2018

#[derive(Default)]
struct Foo<T> {
    bar: Box<[T]>,
}

impl<T> Foo<T> {
    fn new() -> Self {
        Self::default()
    }
}

This gives me the following error:

error[E0599]: no function or associated item named `default` found for type `Foo<T>` in the current scope
 --> src/main.rs:8:9
  |
2 | struct Foo<T> {
  | ------------- function or associated item `default` not found for this
...
8 |         Self::default()
  |         ^^^^^^^^^^^^^ function or associated item not found in `Foo<T>`
  |
  = note: the method `default` exists but the following trait bounds were not satisfied:
          `Foo<T> : std::default::Default`
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `default`, perhaps you need to implement it:
          candidate #1: `std::default::Default`

If I change Self::default() to Foo::default(), I get the following error, which is much more straightforward:

error[E0277]: the trait bound `T: std::default::Default` is not satisfied
 --> src/main.rs:8:9
  |
8 |         Foo::default()
  |         ^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `T`
  |
  = help: consider adding a `where T: std::default::Default` bound
  = note: required because of the requirements on the impl of `std::default::Default` for `Foo<T>`
  = note: required by `std::default::Default::default`

Both can be fixed by adding a trait bound to the impl block (impl<T: Default> Foo<T> {...}).

@memoryruins memoryruins added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 15, 2018
@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 8, 2019
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jan 8, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 27, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 28, 2020
@bors bors closed this as completed in 55aee8d Feb 29, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

3 participants