Skip to content

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

Closed

Description

@jdahlstrom

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.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 29, 2022
  2. fmease commented on Nov 29, 2022

    @fmease
    Member

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

  3. jdahlstrom commented on Nov 29, 2022

    @jdahlstrom
    Author

    @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.

  4. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    on Sep 6, 2023
  5. self-assigned this
    on Mar 6, 2024
  6. added a commit that references this issue on Mar 26, 2024
    ff8cdc9
  7. added a commit that references this issue on Mar 27, 2024
    455e977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`F-associated_type_bounds`#![feature(associated_type_bounds)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions