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

E0277: suggest ?Sized to relax implicit Sized bound on associated types #85378

Closed
tlyu opened this issue May 16, 2021 · 4 comments · Fixed by #116911
Closed

E0277: suggest ?Sized to relax implicit Sized bound on associated types #85378

tlyu opened this issue May 16, 2021 · 4 comments · Fixed by #116911
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system A-type-system Area: Type system D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tlyu
Copy link
Contributor

tlyu commented May 16, 2021

Given the following code: (playground)

trait TraitWithAType {
    type Item;
}
trait Trait {}
struct A {}
impl TraitWithAType for A {
    type Item = dyn Trait;
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
 --> src/lib.rs:7:5
  |
2 |     type Item;
  |     ---------- required by this bound in `TraitWithAType::Item`
...
7 |     type Item = dyn Trait;
  |     ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Ideally the output should look like:

  |
2 |     type Item;
  |     ---------- required by this bound in `TraitWithAType::Item`
...
7 |     type Item = dyn Trait;
  |     ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`
help: consider relaxing the implicit `Sized` restriction
  |
2 |     type Item: ?Sized
  |              ^^^^^^^^

A similar error message with an unsized generic type argument does mention the implicit Sized bound. (playground) Unfortunately, it also doesn't explain the implicit bound up front in the "required by this bound" text. (It mentions it when suggesting to relax the implicit bound, but I think it would be better to mention it up front as well.)

Both error messages could benefit from changing the "required by this bound" text to "required by an implicit Sized bound".

@tlyu tlyu added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 16, 2021
@tlyu
Copy link
Contributor Author

tlyu commented May 16, 2021

@rustbot label +A-associated-items +A-traits +A-typesystem +D-papercut +D-terse

@rustbot rustbot added A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system A-type-system Area: Type system D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels May 16, 2021
@tlyu
Copy link
Contributor Author

tlyu commented Jun 2, 2021

I'm working on a pull request for this issue.

@rustbot claim

@estebank
Copy link
Contributor

estebank commented Jun 3, 2021

@tlyu Oh! Very sorry, if you spent too much time on this already, but I have done some work around this already in #85947. If you want, you can take over it and bring it to completion, there are some cases, like #85799 (comment) that still need to be addressed and I haven't yet figured out. Alternatively, if you can find a way to accomplish the same result without touching the TraitPredicate and instead deal only with a new ObligationCauseCode, that would be preferable to my approach.

@tlyu
Copy link
Contributor Author

tlyu commented Jun 3, 2021

I've pushed what I have so far to https://github.com/tlyu/rust/tree/suggest-unsized , sorry if it conflicts with stuff you've already done. If you'd like to take a look at it, that would be great. I'll take a look at what you have as well.

There are some issues to be worked out with conflicting suggestions for associated types for ?Sized vs the existing suggestion for adding an explicit Sized to a trait's bounds, in test cases such as type Foo = Self; within a trait declaration.

estebank added a commit to estebank/rust that referenced this issue Oct 18, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 20, 2023
Suggest relaxing implicit `type Assoc: Sized;` bound

Fix rust-lang#85378.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 20, 2023
Suggest relaxing implicit `type Assoc: Sized;` bound

Fix rust-lang#85378.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 21, 2023
Suggest relaxing implicit `type Assoc: Sized;` bound

Fix rust-lang#85378.
@bors bors closed this as completed in e8d4fb8 Oct 21, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 21, 2023
Rollup merge of rust-lang#116911 - estebank:issue-85378, r=oli-obk

Suggest relaxing implicit `type Assoc: Sized;` bound

Fix rust-lang#85378.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system A-type-system Area: Type system D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants