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

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

Closed
lcnr opened this issue May 2, 2020 · 2 comments
Closed

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

lcnr opened this issue May 2, 2020 · 2 comments
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented May 2, 2020

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.

@lcnr lcnr changed the title Add a help to implicitly required Sized bound in case it can be relaxed. Add a help if implicitly required Sized bounds can be relaxed. May 2, 2020
@lcnr
Copy link
Contributor Author

lcnr commented May 2, 2020

I would actually prefer if we add a lint for structs which implicitly require Sized when it is not needed.

i.e.

struct Struct<T> {
    field: *const T,
}

// Overly strict implicit `Sized` bound, consider making this trait bound explicit:
struct Struct<T: ?Sized> {
    field: *const T,
}
// or
struct Struct<T: Sized> {
    field: *const T,
}

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 2, 2020
@estebank estebank self-assigned this Jun 11, 2020
@estebank estebank added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Jun 17, 2020
@estebank
Copy link
Contributor

#73261 addresses the diagnostics part of this ticket, but won't close it so that we can track the following

add a lint for structs which implicitly require Sized when it is not needed.

@estebank estebank removed the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 17, 2020
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 19, 2020
…akis

Suggest `?Sized` when applicable for ADTs

Address rust-lang#71790, fix rust-lang#27964.
@bors bors closed this as completed in 95e5605 Jun 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants