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

Order matters when resolving supertrait #72582

Closed
tavianator opened this issue May 25, 2020 · 2 comments
Closed

Order matters when resolving supertrait #72582

tavianator opened this issue May 25, 2020 · 2 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tavianator
Copy link
Contributor

This code:

trait Foo: PartialOrd + PartialOrd<i32> {}

trait Bar {
    type Baz: Foo;

    fn bar(&self) -> Self::Baz;
}

fn baz<T: Bar>(x: T, y: T) -> bool {
    x.bar() < y.bar()
}

gives this error:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/lib.rs:10:15
   |
10 |     x.bar() < y.bar()
   |               ^^^^^^^ expected `i32`, found associated type
   |
   = note:         expected type `i32`
           found associated type `<T as Bar>::Baz`
   = note: consider constraining the associated type `<T as Bar>::Baz` to `i32`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

which indicates that only the PartialOrd<i32> impl is being considered. It seems like the only way to fix it at the usage site is something like

<T::Baz as PartialOrd>::partial_cmp(&x.bar(), &y.bar())

On the other hand, just swapping the order of the supertraits fixes it:

trait Foo: PartialOrd<i32> + PartialOrd {}
@tavianator tavianator added the C-bug Category: This is a bug. label May 25, 2020
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 25, 2020
@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels May 31, 2020
@RustyYato
Copy link
Contributor

Note: This is a problem with associated types specifically, because the following code works (as expected)

trait Foo: PartialOrd + PartialOrd<i32> {}

fn baz<T: Foo>(x: T, y: T) -> bool {
    x < y
}

@tavianator
Copy link
Contributor Author

This is fixed by #73905

tavianator added a commit to tavianator/acap that referenced this issue Feb 24, 2021
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-trait-system Area: Trait system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants