-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
<Default>::default()
makes the compiler very mad at me
#51077
Comments
This can't work, as That is,
|
@eddyb I think what Scott is saying is that you could also have a new rule in 2021 that given That said, the first error message above is pretty darn confusing and we should do something about it now. |
@Centril I disagree on the grounds of |
@eddyb sure; whether that's desirable is entirely separate from the technical feasibility :) I'm not sure where I stand on desirability at this point. |
Yes, I agree. I did figure out that was what was happening, but it's not incredibly obvious, so I wanted to file something about it. (For example, it's not obvious to me why it must be a type specifically, rather than something in the type namespace. My mental model had been that it just flipped the parser into type-namespace-mode, so no turbofish needed, etc, but apparently that's insufficient.) |
@scottmcm It doesn't do anything to paths, instead it fully contains some arbitrary type. |
Trying this today gives a slightly better, but still not great error:
|
At the very least, this part is still misleading:
|
Current output:
The E0038 and E0277 errors both say the same thing ( |
Note that I wasn't actually trying to use I think it's possible that the root issue might be things turning |
I'd added it to the playground to see the other errors without the "you must use |
Address rust-lang#51077 in editions>=2021 by treating `<Trait>` as a `<{type error}>` instead of `<dyn Trait>`, silencing subsequent errors. We new emit a single error ("missing `dyn`"). ``` error[E0782]: trait objects must include the `dyn` keyword --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ | help: add `dyn` keyword before this trait | 2 | let x: u32 = <dyn Default>::default(); | +++ ``` instead of 6 ``` error[E0782]: trait objects must include the `dyn` keyword --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ | help: add `dyn` keyword before this trait | 2 | let x: u32 = <dyn Default>::default(); | +++ error[E0038]: the trait `Default` cannot be made into an object --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ `Default` cannot be made into an object | = note: the trait cannot be made into an object because it requires `Self: Sized` = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `dyn Default` note: required by a bound in `default` --> /home/gh-estebank/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/default.rs:104:20 | 104 | pub trait Default: Sized { | ^^^^^ required by this bound in `Default::default` ... 136 | fn default() -> Self; | ------- required by a bound in this associated function error[E0308]: mismatched types --> f800.rs:2:14 | 2 | let x: u32 = <Default>::default(); | --- ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `dyn Default` | | | expected due to this | = note: expected type `u32` found trait object `dyn Default` = help: `u32` implements `Default` so you could change the expected type to `Box<dyn Default>` error[E0038]: the trait `Default` cannot be made into an object --> f800.rs:2:14 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^^^^^^^^^^^^^^ `Default` cannot be made into an object | = note: the trait cannot be made into an object because it requires `Self: Sized` = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time --> f800.rs:2:14 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `dyn Default` = note: the return type of a function must have a statically known size ```
Address rust-lang#51077 in editions>=2021 by treating `<Trait>` as a `<{type error}>` instead of `<dyn Trait>`, silencing subsequent errors. We new emit a single error ("missing `dyn`"). ``` error[E0782]: trait objects must include the `dyn` keyword --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ | help: add `dyn` keyword before this trait | 2 | let x: u32 = <dyn Default>::default(); | +++ ``` instead of 6 ``` error[E0782]: trait objects must include the `dyn` keyword --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ | help: add `dyn` keyword before this trait | 2 | let x: u32 = <dyn Default>::default(); | +++ error[E0038]: the trait `Default` cannot be made into an object --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ `Default` cannot be made into an object | = note: the trait cannot be made into an object because it requires `Self: Sized` = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time --> f800.rs:2:15 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `dyn Default` note: required by a bound in `default` --> /home/gh-estebank/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/default.rs:104:20 | 104 | pub trait Default: Sized { | ^^^^^ required by this bound in `Default::default` ... 136 | fn default() -> Self; | ------- required by a bound in this associated function error[E0308]: mismatched types --> f800.rs:2:14 | 2 | let x: u32 = <Default>::default(); | --- ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `dyn Default` | | | expected due to this | = note: expected type `u32` found trait object `dyn Default` = help: `u32` implements `Default` so you could change the expected type to `Box<dyn Default>` error[E0038]: the trait `Default` cannot be made into an object --> f800.rs:2:14 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^^^^^^^^^^^^^^ `Default` cannot be made into an object | = note: the trait cannot be made into an object because it requires `Self: Sized` = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time --> f800.rs:2:14 | 2 | let x: u32 = <Default>::default(); | ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `dyn Default` = note: the return type of a function must have a statically known size ```
Suppose I learned
[f32; 2]::deserialize(deserializer)
doesn't work, and I need to do<[f32; 2]>::deserialize(deserializer)
instead (thank you syntax: recovery for incorrect associated item paths like[T; N]::clone
#46788), andDefault::default()
, instead ofConcreteType::default()
If I combine those two things together and try
Then I get this spew of errors:
I don't know what I'm concretely asking for here (please retitle better), so here are some thoughts:
<Default>
can meanDefault
, notdyn Default>
...The text was updated successfully, but these errors were encountered: