where clause shadows information about higher impls
It appears that the typechecker, on encountering a where clause, will assume the impl information given in the where bound and stop looking for impls, thus missing additional information that may be available about the impl outside the item.
For example, this code doesn't typecheck:
pub trait Id { type Id; }
impl<T> Id for T { type Id = T; }
pub fn foo<A>(a: A) -> <A as Id>::Id
where A: Id
{ a }
giving the error:
hkt-mwe.rs:6:3: 6:4 error: mismatched types [E0308]
hkt-mwe.rs:6 { a }
^
hkt-mwe.rs:6:3: 6:4 help: run `rustc --explain E0308` to see a detailed explanation
hkt-mwe.rs:6:3: 6:4 note: expected type `<A as Id>::Id`
hkt-mwe.rs:6:3: 6:4 note: found type `A`
but the same code without the where-bound, which should be entirely redundant since it provides only information already known to the type-checker, compiles.
This makes putting emulated HKTs into traits difficult, as a type that depends on such a thing must have a where clause in the trait that, though redundant, must appear in the impl to avoid E0195.
Edit: Current error:
error[[E0308]](https://doc.rust-lang.org/stable/error_codes/E0308.html): mismatched types
--> src/lib.rs:6:3
|
4 | pub fn foo<A>(a: A) -> <A as Id>::Id
| - ------------- expected `<A as Id>::Id` because of return type
| |
| this type parameter
5 | where A: Id
6 | { a }
| ^ expected associated type, found type parameter `A`
|
= note: expected associated type `<A as Id>::Id`
found type parameter `A`
help: consider further restricting this bound
|
5 | where A: Id + Id<Id = A>
| ++++++++++++
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
At a glance it looks related to #24066 but it may also be simply missing normalization.
Hanna Kruppe at 2016-12-11 21:59:06
Can someone confirm that this is #24066? I think it is, since it's masking other impls, but I'm uncertain.
Mark Rousskov at 2017-05-04 18:07:35