Skip to content

where clause shadows information about higher impls #38071

@Twey

Description

@Twey

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions