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

Adding Op<&T> bound breaks code depending on Op<T> bound #77159

Closed
WaffleLapkin opened this issue Sep 24, 2020 · 2 comments
Closed

Adding Op<&T> bound breaks code depending on Op<T> bound #77159

WaffleLapkin opened this issue Sep 24, 2020 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@WaffleLapkin
Copy link
Member

I tried this code:

trait Op<Arg> { 
    fn op (_: Arg); 
}

trait Trait<'lt>: 'lt + Sized + Op<Self> + Op<&'lt Self> {}

trait WithAssoc<'lt> { type Assoc: Trait<'lt>; }

fn test<'lt, T: WithAssoc<'lt>> (it: T::Assoc) {
    <T::Assoc as Op<_>>::op(it)
}

I expected to see this happen: code compiles

Instead, this happened: code doesn't compile with the following error:

error[E0308]: mismatched types
  --> src/lib.rs:10:29
   |
10 |     <T::Assoc as Op<_>>::op(it)
   |                             ^^ expected reference, found associated type
   |
   = note:    expected reference `&'lt <T as WithAssoc<'lt>>::Assoc`
           found associated type `<T as WithAssoc<'lt>>::Assoc`
help: consider constraining the associated type `<T as WithAssoc<'lt>>::Assoc` to `&'lt <T as WithAssoc<'lt>>::Assoc`
   |
9  | fn test<'lt, T: WithAssoc<'lt, Assoc = &'lt <T as WithAssoc<'lt>>::Assoc>> (it: T::Assoc) {
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider borrowing here
   |
10 |     <T::Assoc as Op<_>>::op(&it)
   |                             ^^^
  1. T::Assoc should implement Op<T::Assoc> so the error shouldn't happen
  2. Compiler suggests using T: WithAssoc<'lt, Assoc = &'lt <T as WithAssoc<'lt>>::Assoc> which is reccursion
  3. The code works if
    1. you'll remove + Op<&'lt Self>
    2. you'll use op with explicit type <T::Assoc as Op<T::Assoc>>::op(it)
  4. There is a weird spacing between = note: and expected reference ... though this is unrelated to the problem

Playground: [link]

Meta

tested on 1.46.0 and 1.48.0-nightly (2020-09-23 8b40853)

@WaffleLapkin WaffleLapkin added the C-bug Category: This is a bug. label Sep 24, 2020
@jyn514 jyn514 changed the title Adding Op<&T> bound brakes code depending on Op<T> bound Adding Op<&T> bound breaks code depending on Op<T> bound Sep 24, 2020
@tavianator
Copy link
Contributor

This "fixes" it:

trait Trait<'lt>: 'lt + Sized + Op<&'lt Self> + Op<Self> {}

so I think this is the same issue as #72582

@tavianator
Copy link
Contributor

This is fixed by #73905

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

No branches or pull requests

2 participants