Skip to content

Type Inferencing of Type Parameters with Associated Types Is Incorrect #69502

@bobyangyf

Description

@bobyangyf

Rust fails to properly infer types when there is a type parameter that implements are trait that contains associated types that have relationships between each other.

Suppose there are two structs Wrapper1 and Wrapper2 that each hold a type I1 and I2 respectively.
We want to express the relationship that I2 : Deref<Target = I1>
So there's a 2 ways to do this:

  1. define Wrapper1 and Wrapper2 with 2 types, T1 and T2 where T2 : Deref<Target = T1>
    e.g.
    https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5c8869c7499b8cf628695b8b8a66ad40

  2. define a type WT where WT has types Inner1 and Inner2 such that Inner2 : Deref<Target = Self::Inner1>.
    e.g.
    https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=892489013b67f5f8ef5de1e60dcdb1ef

However, in the second case, the compiler cannot infer types correctly for code to use the properties of the associated types Inner2 : Deref<Target = Self::Inner1>.

In the above code examples, foo2 does not compile in 2.
It looks like the compiler is unable to infer the types correctly and apply the Deref, since for foo1, where we explicitly indicate the type, it compiles properly.

This ends up causing the Deref traits to not be always inferred properly by other users too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler 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