Skip to content

Invalid trait bounds and equality constraints should suggest appropriate code #85307

Open
@estebank

Description

@estebank

Given

trait Z {}
trait Bar {
    type A;
}

trait Foo {
    fn foo<T, X: Z>()
    where
        T: Bar<A: Z>,
        T::A = X,
        T: Bar<A = X>, // valid
        T::A: Z, // valid
    {       
    }
}

we currently emit:

error: equality constraints are not yet supported in `where` clauses
  --> src/lib.rs:10:9
   |
10 |         T::A = X,
   |         ^^^^^^^^ not supported
   |
   = note: see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information

error[E0658]: associated type bounds are unstable
 --> src/lib.rs:9:16
  |
9 |         T: Bar<A: Z>,
  |                ^^^^
  |
  = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information

We should be suggesting the alternative, correct form:

error: equality constraints are not yet supported in `where` clauses
  --> src/lib.rs:10:9
   |
10 |         T::A = X,
   |         ^^^^^^^^ not supported
   |
   = note: see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information
help: constrain the associated type directly
  |
9 |         T: Bar<A = X>,
  |                ^^^^^

error[E0658]: associated type bounds are unstable
 --> src/lib.rs:9:16
  |
9 |         T: Bar<A: Z>,
  |                ^^^^
  |
  = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
help: introduce a new trait bound to constrain the assocated type
   |
 9 |         T: Bar,
10 |         T::A: Z,
   |

Also, when encountering

trait Foo {
    fn foo<T, X: Z>()
    where
        T::A: X,
    {}
}

we emit

error[E0404]: expected trait, found type parameter `X`
 --> src/lib.rs:9:15
  |
1 | trait Z {}
  | ------- similarly named trait `Z` defined here
...
9 |         T::A: X,
  |               ^ help: a trait with a similar name exists: `Z`

error[E0220]: associated type `A` not found for `T`
 --> src/lib.rs:9:12
  |
9 |         T::A: X,
  |            ^ associated type `A` not found

Where it should instead be

error[E0404]: expected trait, found type parameter `X`
 --> src/lib.rs:9:15
  |
7 |     fn foo<T, X: Z>()
  |               ---- `X` constrained to trait `Z` here
8 |     where
9 |         T::A: X,
  |               ^ help: you might have meant to use the trait this type parameter was constrained by: `Z`

error[E0220]: associated type `A` not found for `T`
 --> src/lib.rs:9:12
  |
9 |         T::A: X,
  |            ^ associated type `A` not found
help: trait `Bar` has an associated type `A`, you might have meant to constrain type parameter `T` with `Bar`
   |
9  |        T: Bar,
10 |        T::A: X,
   |

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-systemArea: Trait systemD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.F-associated_type_bounds`#![feature(associated_type_bounds)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions