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

Suggest Trait<Ty: Trait2> syntax when user typed Trait<Ty = Trait2> #105056

Closed
jdahlstrom opened this issue Nov 29, 2022 · 2 comments · Fixed by #122120
Closed

Suggest Trait<Ty: Trait2> syntax when user typed Trait<Ty = Trait2> #105056

jdahlstrom opened this issue Nov 29, 2022 · 2 comments · Fixed by #122120
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` F-associated_type_bounds `#![feature(associated_type_bounds)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jdahlstrom
Copy link

jdahlstrom commented Nov 29, 2022

Consider the following code:

trait Foo {}

fn bar(_: impl Iterator<Item = Foo>) {}

// Or another similar case:

trait Bar {
    type X: Iterator<Item = Foo>;
}

The current output is:

error[E0782]: trait objects must include the `dyn` keyword
 --> src/lib.rs:3:32
  |
3 | fn bar(_: impl Iterator<Item = Foo>) {}
  |                                ^^^
  |
help: add `dyn` keyword before this trait
  |
3 | fn bar(_: impl Iterator<Item = dyn Foo>) {}
  |                                +++

This is reasonable especially in light of the ongoing migration from the old dyn-less trait object syntax. However, an alternative interpretation of the syntax error is that the user accidentally used a type equality bound = when they actually meant a trait bound :. The error message could include a suggestion to replace = with :.

help: or if you meant to write a trait bound, replace `=` with `:`
  |
3 | fn bar(_: impl Iterator<Item = Foo>) {}
  |                             --
3 | fn bar(_: impl Iterator<Item: Foo>) {}
  |                             +

See also related #99304 which proposes that impl Foo should be suggested rather than dyn Foo.

@jdahlstrom jdahlstrom 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 Nov 29, 2022
@fmease
Copy link
Member

fmease commented Nov 29, 2022

This should only be suggested on nightly as associated type bounds are still unstable.

@jdahlstrom
Copy link
Author

jdahlstrom commented Nov 29, 2022

@fmease Oops, good point. Not sure how I thought that compiled on stable, mea culpa. I guess the compiler could suggest using the stable where X: Trait, X::Ty: Trait2 syntax if the user attempts to use ATBs, but that should probably be its own issue.

@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` requires-nightly This issue requires a nightly compiler in some way. F-associated_type_bounds `#![feature(associated_type_bounds)]` labels Sep 6, 2023
@fmease fmease self-assigned this Mar 6, 2024
@bors bors closed this as completed in ff8cdc9 Mar 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 27, 2024
Rollup merge of rust-lang#122120 - fmease:sugg-assoc-ty-bound-on-eq-bound, r=compiler-errors

Suggest associated type bounds on problematic associated equality bounds

Fixes rust-lang#105056. TL;DR: Suggest `Trait<Ty: Bound>` on `Trait<Ty = Bound>` in Rust >=2021.

~~Blocked on rust-lang#122055 (stabilization of `associated_type_bounds`), I'd say.~~ (merged)
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` F-associated_type_bounds `#![feature(associated_type_bounds)]` requires-nightly This issue requires a nightly compiler in some way. 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