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

Suggest struct or union to add generic that impls trait #138042

Merged
merged 2 commits into from
Mar 7, 2025

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Mar 5, 2025

Fixes #135759

cc @tdittr

@rustbot
Copy link
Collaborator

rustbot commented Mar 5, 2025

Failed to set assignee to tdittr: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 5, 2025

HIR ty lowering was modified

cc @fmease

@xizheyin
Copy link
Contributor Author

xizheyin commented Mar 5, 2025

By the way, how to add the test? I tried but there were some problems. For example, here is a source file(lib.rs):

trait Trait {}

struct Foo {
    a: Trait,
    b: u32,

}

struct Foo1 {
    a: i32,
    b: Trait,
}

when I run it with cargo +stage1 build, it reports:

error[E0782]: expected a type, found a trait
 --> src/lib.rs:4:8
  |
4 |     a: Trait,
  |        ^^^^^
  |
help: you might be missing a type parameter
  |
3 ~ struct Foo<T: Trait> {
4 ~     a: T,
  |

error[E0782]: expected a type, found a trait
  --> src/lib.rs:11:8
   |
11 |     b: Trait,
   |        ^^^^^
   |
help: you can add the `dyn` keyword if you want a trait object
   |
11 |     b: dyn Trait,
   |        +++

For more information about this error, try `rustc --explain E0782`.

But with rustc +stage1 lib.rs, it reports:

error[E0601]: `main` function not found in crate `lib`
  --> src/lib.rs:12:2
   |
12 | }
   |  ^ consider adding a `main` function to `src/lib.rs`

warning: trait objects without an explicit `dyn` are deprecated
 --> src/lib.rs:4:8
  |
4 |     a: Trait,
  |        ^^^^^
  |
  = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
  = note: `#[warn(bare_trait_objects)]` on by default
help: if this is a dyn-compatible trait, use `dyn`
  |
4 |     a: dyn Trait,
  |        +++

warning: trait objects without an explicit `dyn` are deprecated
  --> src/lib.rs:11:8
   |
11 |     b: Trait,
   |        ^^^^^
   |
   = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: if this is a dyn-compatible trait, use `dyn`
   |
11 |     b: dyn Trait,
   |        +++

error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
 --> src/lib.rs:4:8
  |
4 |     a: Trait,
  |        ^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Trait + '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
  |
4 |     a: &Trait,
  |        +
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
4 |     a: Box<Trait>,
  |        ++++     +

error: aborting due to 2 previous errors; 2 warnings emitted

Some errors have detailed explanations: E0277, E0601.
For more information about an error, try `rustc --explain E0277`.

@xizheyin
Copy link
Contributor Author

xizheyin commented Mar 5, 2025

r? compiler

@nnethercote
Copy link
Contributor

If you run rustc directly it defaults to the 2015 edition. Using cargo you will use whatever edition is specified in Cargo.toml, which will default to a later edition. That explains the difference in output you are seeing for those two commands.

You should add the test as a new UI test under tests/ui/. See https://rustc-dev-guide.rust-lang.org/tests/adding.html for details.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 6, 2025
@xizheyin
Copy link
Contributor Author

xizheyin commented Mar 6, 2025

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 6, 2025
@xizheyin
Copy link
Contributor Author

xizheyin commented Mar 6, 2025

Thank you very much. I added ui test. @nnethercote

@nnethercote
Copy link
Contributor

Looks good, thanks.

A suggestion for next time: if you add the new test in the first commit, and then make the change in the second commit, the reviewer can see how the test output changes in the second commit. This makes it easier to review.

@bors r+

@bors
Copy link
Contributor

bors commented Mar 7, 2025

📌 Commit 7e199b1 has been approved by nnethercote

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 7, 2025
@xizheyin
Copy link
Contributor Author

xizheyin commented Mar 7, 2025

You mean that submitting the UI test in the first commit and then failing it in the PR will tell you the output of the old code, right? Makes sense, thanks!

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 7, 2025
Suggest struct or union to add generic that impls trait

Fixes rust-lang#135759

cc `@tdittr`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 7, 2025
Suggest struct or union to add generic that impls trait

Fixes rust-lang#135759

cc ``@tdittr``
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 7, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#134797 (Ergonomic ref counting)
 - rust-lang#137549 (Clean up various LLVM FFI things in codegen_llvm)
 - rust-lang#137977 (Reduce `kw::Empty` usage, part 1)
 - rust-lang#138042 (Suggest struct or union to add generic that impls trait)
 - rust-lang#138141 (tests: fix some typos in comment)
 - rust-lang#138150 (Streamline HIR intravisit `visit_id` calls for items)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e70adad into rust-lang:master Mar 7, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 7, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 7, 2025
Rollup merge of rust-lang#138042 - xizheyin:issue-135759, r=nnethercote

Suggest struct or union to add generic that impls trait

Fixes rust-lang#135759

cc ```@tdittr```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 9, 2025
…struct, r=nnethercote

Fix dyn -> param suggestion in struct ICEs

Makes the logic from rust-lang#138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes rust-lang#138229
Fixes rust-lang#138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 10, 2025
…struct, r=nnethercote

Fix dyn -> param suggestion in struct ICEs

Makes the logic from rust-lang#138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes rust-lang#138229
Fixes rust-lang#138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using a bare trait as a field type in a struct gives subpar suggestion
4 participants