Closed
Description
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)
| ^^^
T::Assoc
should implementOp<T::Assoc>
so the error shouldn't happen- Compiler suggests using
T: WithAssoc<'lt, Assoc = &'lt <T as WithAssoc<'lt>>::Assoc>
which is reccursion - The code works if
- you'll remove
+ Op<&'lt Self>
- you'll use op with explicit type
<T::Assoc as Op<T::Assoc>>::op(it)
- you'll remove
- There is a weird spacing between
= note:
andexpected 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)