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

Setting associated type of a trait breaks supertrait bounds #77010

Closed
WaffleLapkin opened this issue Sep 21, 2020 · 1 comment
Closed

Setting associated type of a trait breaks supertrait bounds #77010

WaffleLapkin opened this issue Sep 21, 2020 · 1 comment
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug.

Comments

@WaffleLapkin
Copy link
Member

This code compiles fine until you'll uncomment BAssoc = ().

pub trait A {
    type AAssoc: B</*BAssoc = ()*/>;
}

pub trait B
where
    Self: Into<<Self as B>::BAssoc> 
{
    type BAssoc;
}

fn test<T: A>(x: T::AAssoc) -> <T::AAssoc as B>::BAssoc {
    x.into()
}

With BAssoc = () the code fails to compile with the following error:

error[E0277]: the trait bound `(): From<<T as A>::AAssoc>` is not satisfied
  --> src/lib.rs:13:5
   |
5  | pub trait B
   |           - required by a bound in this
6  | where
7  |     Self: Into<<Self as B>::BAssoc> 
   |           ------------------------- required by this bound in `B`
...
13 |     x.into()
   |     ^^^^^^^^ the trait `From<<T as A>::AAssoc>` is not implemented for `()`
   |
   = note: required because of the requirements on the impl of `Into<()>` for `<T as A>::AAssoc`

error[E0308]: mismatched types
  --> src/lib.rs:13:5
   |
12 | fn test<T: A>(x: T::AAssoc) -> <T::AAssoc as B>::BAssoc {
   |                                ------------------------ expected `()` because of return type
13 |     x.into()
   |     ^^^^^^^^- help: try adding a semicolon: `;`
   |     |
   |     expected `()`, found associated type
   |
   = note:    expected unit type `()`
           found associated type `<<T as A>::AAssoc as B>::BAssoc`
   = help: consider constraining the associated type `<<T as A>::AAssoc as B>::BAssoc` to `()`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

That seems really weird - how making type less generic makes code that should work for any type fail?

Meta

Tested on stable 1.46.0 and 1.48.0-nightly (2020-09-20 1fd5b9d)

@WaffleLapkin
Copy link
Member Author

This is fixed on nightly. Presumably by #73905

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants