-
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
Traits in type position should be a fatal error in ≥2018 editions #127548
Comments
Thanks for the cross-references. I'd like to emphasize that what I'm asking for, compared to those, is not just “make this error more concise and clear”, but “stop implicitly assuming the user definitely wanted a trait object”. (Perhaps |
I could not agree more. I myself have run into this issue about 3 times and 3 times out of 3 I actually meant |
@rustbot claim |
…lcnr Don't assume traits used as type are trait objs in 2021 edition Fixes rust-lang#127548 When you use a trait as a type, the compiler automatically assumes you meant to use a trait object, which is not always the case. This PR fixes the bug where you don't need a trait object, so the error message was changed to: ``` error[E0782]: expected a type, found a trait ``` Also fixes some ICEs: Fixes rust-lang#120241 Fixes rust-lang#120482 Fixes rust-lang#125512
…lcnr Don't assume traits used as type are trait objs in 2021 edition Fixes rust-lang#127548 When you use a trait as a type, the compiler automatically assumes you meant to use a trait object, which is not always the case. This PR fixes the bug where you don't need a trait object, so the error message was changed to: ``` error[E0782]: expected a type, found a trait ``` Also fixes some ICEs: Fixes rust-lang#120241 Fixes rust-lang#120482 Fixes rust-lang#125512
Rollup merge of rust-lang#131239 - VulnBandit:trait-vulnerability, r=lcnr Don't assume traits used as type are trait objs in 2021 edition Fixes rust-lang#127548 When you use a trait as a type, the compiler automatically assumes you meant to use a trait object, which is not always the case. This PR fixes the bug where you don't need a trait object, so the error message was changed to: ``` error[E0782]: expected a type, found a trait ``` Also fixes some ICEs: Fixes rust-lang#120241 Fixes rust-lang#120482 Fixes rust-lang#125512
Code
Current output
Desired output
There are two fixes that I think would be wise here.
Most importantly, the E0277 should not occur; the compiler should not
attempt to continue interpreting the program as if it were in edition 2015. Or, at a minimum, the E0782 should appear first.
E0782 could be better expressed, in the modern perspective, as “expected type, found trait”. Don't bring up trait objects at all until the author writes
dyn
.Rationale and extra context
Today, when a newcomer writes a trait in a type position, it most often means they're confused about what the difference is, not that they wanted a trait object and need to be taught the new syntax. The whole point of adding
dyn
to Rust's syntax was to remove that easy confusion. Therefore, the compiler should stop making the assumption whatsoever that the author of the code meant a trait object.This will have the further advantage that newcomers will not be confronted with errors about
Sized
when they probably didn't even mean to use any DST.Rust Version
The text was updated successfully, but these errors were encountered: