Skip to content

when suggesting ?Sized, don't suggest indirection in where clauses #85943

Closed
@tlyu

Description

@tlyu

Given the following code: (playground)

struct A<T>(T) where T: Send;
struct B(A<[u8]>);

The current output is:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/lib.rs:2:10
  |
1 | struct A<T>(T) where T: Send;
  |          - required by this bound in `A`
2 | struct B(A<[u8]>);
  |          ^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
 --> src/lib.rs:1:10
  |
1 | struct A<T>(T) where T: Send;
  |          ^  -        - ...if indirection were used here: `Box<T>`
  |          |  |
  |          |  ...if indirection were used here: `Box<T>`
  |          this could be changed to `T: ?Sized`...

Ideally the output should look like:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/lib.rs:2:10
  |
1 | struct A<T>(T) where T: Send;
  |          - required by this bound in `A`
2 | struct B(A<[u8]>);
  |          ^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
 --> src/lib.rs:1:10
  |
1 | struct A<T>(T) where T: Send;
  |          ^  -
  |          |  |
  |          |  ...if indirection were used here: `Box<T>`
  |          this could be changed to `T: ?Sized`...

The visitor that collects bare type parameters should skip where clauses. I can work on this in an in-progress pull request where I'm making other improvements to unsized suggestions.

@rustbot claim
@rustbot label +A-traits +A-typesystem +D-invalid-suggestion +D-papercut

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemA-type-systemArea: Type systemD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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