Skip to content

<Default>::default() makes the compiler very mad at me #51077

Open
@scottmcm

Description

@scottmcm

Suppose I learned

If I combine those two things together and try

let x: u32 = <Default>::default();

Then I get this spew of errors:

error[E0277]: the trait bound `std::default::Default: std::default::Default` is not satisfied
 --> src/main.rs:2:18
  |
2 |     let x: u32 = <Default>::default();
  |                  ^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `std::default::Default`
  |
  = note: required by `std::default::Default::default`

error[E0038]: the trait `std::default::Default` cannot be made into an object
 --> src/main.rs:2:19
  |
2 |     let x: u32 = <Default>::default();
  |                   ^^^^^^^ the trait `std::default::Default` cannot be made into an object
  |
  = note: the trait cannot require that `Self : Sized`

error[E0308]: mismatched types
 --> src/main.rs:2:18
  |
2 |     let x: u32 = <Default>::default();
  |                  ^^^^^^^^^^^^^^^^^^^^ expected u32, found trait std::default::Default
  |
  = note: expected type `u32`
             found type `std::default::Default`

error[E0038]: the trait `std::default::Default` cannot be made into an object
 --> src/main.rs:2:18
  |
2 |     let x: u32 = <Default>::default();
  |                  ^^^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` cannot be made into an object
  |
  = note: the trait cannot require that `Self : Sized`

error: aborting due to 4 previous errors

Some errors occurred: E0038, E0277, E0308.
For more information about an error, try `rustc --explain E0038`.

I don't know what I'm concretely asking for here (please retitle better), so here are some thoughts:

  • The second error is the valuable one here
  • "found trait std::default::Default" should perhaps be "found trait object std::default::Default"?
  • Maybe this all will just work in Rust 2021 when <Default> can mean Default, not dyn Default>...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-frontendArea: Compiler frontend (errors, parsing and HIR)A-trait-systemArea: Trait systemC-feature-requestCategory: A feature request, i.e: not implemented / a PR.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions