Skip to content

Wrong type inference for associated type implementing Iterator #11751

@setzer22

Description

@setzer22

rust-analyzer version: 9700add 2022-01-17 stable

rustc version: rustc 1.60.0-nightly (9ad5d82f8 2022-01-18)

relevant settings: Enabled #![feature(generic_associated_types)]

I've spotted a bug where rust analyzer does not correctly infer some types when generic associated types are involved. I've managed to trim down my example to the following minimal snippet:

pub trait Context {
    type QueryResult<Q> : Iterator<Item=Q>;
    fn query<Q>(&self) -> Self::QueryResult<Q>;
}
pub fn test(ctx: impl Context) {
    for x in ctx.query::<&i32>() { // <--- Here
    }
}

The type for x in the loop is inferred as Context, when it should be i32. If I shadow x to itself, rustc compiles this code just fine, but the outer x is still inferred incorrectly.

pub fn test(ctx: impl Context) {
    for x in ctx.query::<&i32>() {
        let x : i32 = x; // <--- New
    }
}

If the type is something more complex, like a tuple, the type can no longer be inferred and is simply reported as {unknown}

pub fn test(ctx: impl Context) {
    for (x, y) in ctx.query::<(&i32, &i32)>() {
        // x and y reported as { unknown }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions