Skip to content

Add a help if implicitly required Sized bounds can be relaxed. #71790

Closed
@lcnr

Description

@lcnr
trait Trait{
    fn func() -> Struct<Self>;    
}

struct Struct<T>{
    _t: std::marker::PhantomData<*const T>,
}

Currently errors with

error[E0277]: the size for values of type `Self` cannot be known at compilation time
 --> src/lib.rs:2:18
  |
2 |     fn func() -> Struct<Self>;    
  |                  ^^^^^^^^^^^^- help: consider further restricting `Self`: `where Self: std::marker::Sized`
  |                  |
  |                  doesn't have a size known at compile-time
...
5 | struct Struct<T>{
  |               - required by this bound in `Struct`
  |
  = help: the trait `std::marker::Sized` is not implemented for `Self`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

This seems to cause at least some confusion (see #71788)

When adding the "help: the trait std::marker::Sized is not implemented for Self",
we can check if the implicit Sized bound is required for Self and if not, add
"help: consider relaxing this trait bound" and recommend adding ?Sized to T.

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.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