Skip to content

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

Closed
@TonalidadeHidrica

Description

@TonalidadeHidrica

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.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions