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

rustc suggests removing all trait bounds leaving stray impl #127441

Closed
matthiaskrgr opened this issue Jul 6, 2024 · 1 comment · Fixed by #127717
Closed

rustc suggests removing all trait bounds leaving stray impl #127441

matthiaskrgr opened this issue Jul 6, 2024 · 1 comment · Fixed by #127717
Assignees
Labels
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.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jul 6, 2024

Code

fn foo(_: impl ?Sized + ?Sized) {}
fn main() {}

Current output

error[E0203]: type parameter has more than one relaxed default bound, only one is supported
 --> a.rs:1:16
  |
1 | fn foo(_: impl ?Sized + ?Sized) {}
  |                ^^^^^^   ^^^^^^

error[E0277]: the size for values of type `impl ?Sized + ?Sized` cannot be known at compilation time
 --> a.rs:1:8
  |
1 | fn foo(_: impl ?Sized + ?Sized) {}
  |        ^  -------------------- this type parameter needs to be `Sized`
  |        |
  |        doesn't have a size known at compile-time
  |
  = help: unsized fn params are gated as an unstable feature
help: consider restricting type parameters
  |
1 - fn foo(_: impl ?Sized + ?Sized) {}
1 + fn foo(_: impl ) {}
  |
help: function arguments must have a statically known size, borrowed types always have a known size
  |
1 | fn foo(_: &impl ?Sized + ?Sized) {}
  |           +

error: aborting due to 2 previous

Desired output

No response

Rationale and extra context

No response

Other cases

No response

Rust Version

51917e2e69702e5752bce6a4f3bfd285d0f4ae39

Anything else?

I found the fn foo(_: impl ) {} suggestion slightly amusing :)

@matthiaskrgr matthiaskrgr 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 Jul 6, 2024
@matthiaskrgr matthiaskrgr changed the title rustc suggests removing all trait bounds leaving stray `impl rustc suggests removing all trait bounds leaving stray impl Jul 6, 2024
@gurry
Copy link
Contributor

gurry commented Jul 7, 2024

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 24, 2024
…mpiler-errors

Fix malformed suggestion for repeated maybe unsized bounds

Fixes rust-lang#127441

Now when we encounter something like `foo(a : impl ?Sized + ?Sized)`, instead of suggesting removal of both bounds and leaving `foo(a: impl )` behind, we suggest changing the first bound to `Sized` and removing the second bound, resulting in `foo(a: impl Sized)`.

Although the issue was reported for impl trait types, it also occurred with regular param bounds. So if we encounter `foo<T: ?Sized + ?Sized>(a: T)` we now detect that all the bounds are `?Sized` and therefore emit the suggestion to remove the entire predicate `: ?Sized + ?Sized` resulting in `foo<T>(a: T)`.

Lastly, if we encounter a situation where some of the bounds are something other than `?Sized`, then we emit separate removal suggestions for each `?Sized` bound. E.g. if we see `foo(a: impl ?Sized + Bar + ?Sized)` or `foo<T: ?Sized + Bar + ?Sized>(a: T)` we emit suggestions such that the user will be left with `foo(a : impl Bar)` or `foo<T: Bar>(a: T)` respectively.
@bors bors closed this as completed in 2ff33bb Jul 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2024
Rollup merge of rust-lang#127717 - gurry:127441-stray-impl-sugg, r=compiler-errors

Fix malformed suggestion for repeated maybe unsized bounds

Fixes rust-lang#127441

Now when we encounter something like `foo(a : impl ?Sized + ?Sized)`, instead of suggesting removal of both bounds and leaving `foo(a: impl )` behind, we suggest changing the first bound to `Sized` and removing the second bound, resulting in `foo(a: impl Sized)`.

Although the issue was reported for impl trait types, it also occurred with regular param bounds. So if we encounter `foo<T: ?Sized + ?Sized>(a: T)` we now detect that all the bounds are `?Sized` and therefore emit the suggestion to remove the entire predicate `: ?Sized + ?Sized` resulting in `foo<T>(a: T)`.

Lastly, if we encounter a situation where some of the bounds are something other than `?Sized`, then we emit separate removal suggestions for each `?Sized` bound. E.g. if we see `foo(a: impl ?Sized + Bar + ?Sized)` or `foo<T: ?Sized + Bar + ?Sized>(a: T)` we emit suggestions such that the user will be left with `foo(a : impl Bar)` or `foo<T: Bar>(a: T)` respectively.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants