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

Traits in type position should be a fatal error in ≥2018 editions #127548

Closed
kpreid opened this issue Jul 10, 2024 · 4 comments · Fixed by #131239
Closed

Traits in type position should be a fatal error in ≥2018 editions #127548

kpreid opened this issue Jul 10, 2024 · 4 comments · Fixed by #131239
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

@kpreid
Copy link
Contributor

kpreid commented Jul 10, 2024

Code

struct Foo {
    a: Fn(),
    b: Fn(),
}

Current output

error[E0277]: the size for values of type `(dyn Fn() + 'static)` cannot be known at compilation time
 --> src/lib.rs:2:8
  |
2 |     a: Fn(),
  |        ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Fn() + 'static)`
  = note: only the last field of a struct may have a dynamically sized type
  = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
  |
2 |     a: &Fn(),
  |        +
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
2 |     a: Box<Fn()>,
  |        ++++    +

error[E0782]: trait objects must include the `dyn` keyword
 --> src/lib.rs:2:8
  |
2 |     a: Fn(),
  |        ^^^^
  |
help: add `dyn` keyword before this trait
  |
2 |     a: dyn Fn(),
  |        +++

error[E0782]: trait objects must include the `dyn` keyword
 --> src/lib.rs:3:8
  |
3 |     b: Fn(),
  |        ^^^^
  |
help: add `dyn` keyword before this trait
  |
3 |     b: dyn Fn(),
  |        +++

Desired output

There are two fixes that I think would be wise here.

  1. 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.

  2. 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

rustc 1.81.0-nightly (ba1d7f4a0 2024-06-29)
binary: rustc
commit-hash: ba1d7f4a083e6402679105115ded645512a7aea8
commit-date: 2024-06-29
host: x86_64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
@kpreid kpreid 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 Jul 10, 2024
@kpreid kpreid changed the title Traits in type position should be a syntactic error in ≥2018 editions Traits in type position should be a fatal error in ≥2018 editions Jul 10, 2024
@GrigorenkoPV
Copy link
Contributor

cc #127689 #127690 #127691

@kpreid
Copy link
Contributor Author

kpreid commented Jul 16, 2024

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 dyn could still be mentioned in help, but in that case, impl Trait/generics should be mentioned at the same time as they are both ways of “I want to accept an implementor of a trait here”.)

@GrigorenkoPV
Copy link
Contributor

“stop implicitly assuming the user definitely wanted a trait object”.

I could not agree more. I myself have run into this issue about 3 times and 3 times out of 3 I actually meant impl Trait.

@VulnBandit
Copy link
Contributor

@rustbot claim
I think this vulnerability can be addressed by me.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 12, 2024
…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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 12, 2024
…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
@bors bors closed this as completed in 663da00 Oct 13, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 13, 2024
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
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.

3 participants