Skip to content

before suggesting ?Sized, check where clause for explicit Sized #85945

Closed
@tlyu

Description

@tlyu

Given the following code: (playground)

fn foo<T>(_: &T) where T: Sized {}
fn bar() { foo(""); }

The current output is:

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:2:16
  |
1 | fn foo<T>(_: &T) where T: Sized {}
  |        - required by this bound in `foo`
2 | fn bar() { foo(""); }
  |                ^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
help: consider relaxing the implicit `Sized` restriction
  |
1 | fn foo<T: ?Sized>(_: &T) where T: Sized {}
  |         ^^^^^^^^

Ideally, the diagnostic shouldn't suggest relaxing an explicit Sized bound in the where clause. (There is already code that skips the ?Sized suggestion for type parameters that have an explicit Sized bound.)

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-papercut

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemA-type-systemArea: Type systemD-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