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

Non-last field of generic struct without appropriate trait bound causes confusing hint about dynamically sized field #96810

Closed
TonalidadeHidrica opened this issue May 7, 2022 · 1 comment · Fixed by #97780
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

@TonalidadeHidrica
Copy link
Contributor

Given the following code:

struct S<T: Tr>(T::Assoc);

trait Tr {
    type Assoc;
}

struct Hoge<K> {
    s: S<K>,
    a: u32,
}

The current output is:

error[E0277]: the trait bound `K: Tr` is not satisfied in `S<K>`
 --> src/lib.rs:8:8
  |
8 |     s: S<K>,
  |        ^^^^ within `S<K>`, the trait `Tr` is not implemented for `K`
  |
note: required because it appears within the type `S<K>`
 --> src/lib.rs:1:8
  |
1 | struct S<T: Tr>(T::Assoc);
  |        ^
  = 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: consider restricting type parameter `K`
  |
7 | struct Hoge<K: Tr> {
  |              ++++
help: borrowed types always have a statically known size
  |
8 |     s: &S<K>,
  |        +
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
8 |     s: Box<S<K>>,
  |        ++++    +

Ideally the output should look like:

error[E0277]: the trait bound `K: Tr` is not satisfied
 --> src/lib.rs:8:8
  |
8 |     s: S<K>,
  |        ^^^^ the trait `Tr` is not implemented for `K`
  |
note: required by a bound in `S`
 --> src/lib.rs:1:13
  |
1 | struct S<T: Tr>(T::Assoc);
  |             ^^ required by this bound in `S`
help: consider restricting type parameter `K`
  |
7 | struct Hoge<K: Tr> {
  |              ++++

The wrong hint was reproduced in both stable and nightly.
Removing a: u32 ceases the wrong hint, so this seems to be an issue for non-last field.
Replacing T::Assoc with T also gets rid of the misleading suggestion, which implies that this issue is related to associated types.

@TonalidadeHidrica TonalidadeHidrica 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 May 7, 2022
@compiler-errors compiler-errors self-assigned this May 9, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 26, 2022
…e-sized, r=jackh726

Check ADT field is well-formed before checking it is sized

Fixes rust-lang#96810.

There is one diagnostics regression, in [`src/test/ui/generic-associated-types/bugs/issue-80626.stderr`](https://github.com/rust-lang/rust/pull/97780/files#diff-53795946378e78a0af23a10277c628ff79091c18090fdc385801ee70c1ba6963). I am not super concerned about it, since it's GAT related.
We _could_ fix it, possibly by using the `FieldSized` obligation cause code instead of `BuiltinDerivedObligation`. But that would require changing `Sized` trait confirmation and the `adt_sized_constraint` query.
@bors bors closed this as completed in 43dd0e2 Jun 27, 2022
@TonalidadeHidrica
Copy link
Contributor Author

@compiler-errors Thanks for the fix!

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.

2 participants