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 }
    }
}

Activity

added
A-tytype system / type inference / traits / method resolution
C-bugCategory: bug
on Mar 18, 2022
flodiebold

flodiebold commented on Mar 18, 2022

@flodiebold
Member

Generic associated types aren't implemented yet.

setzer22

setzer22 commented on Mar 18, 2022

@setzer22
Author

Yes, I just had used GATs unknowingly (the feature is active in my project), then thought it was due to a bug in RA, that's why I originally reported this as a bug.

I understand RA limiting itself to the stable subset of features, but generic associated types are pretty close to stabilization, so I still felt it was worth reporting.

flodiebold

flodiebold commented on Mar 18, 2022

@flodiebold
Member

I understand RA limiting itself to the stable subset of features

Not necessarily, there's just a lot to do 🙂

lowr

lowr commented on Oct 29, 2022

@lowr
Contributor

GATs have been implmeneted in #13494. Manually tested and confirmed we can now correctly infer the variables in OP.

setzer22

setzer22 commented on Oct 30, 2022

@setzer22
Author

Exciting! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @lnicola@flodiebold@setzer22@lowr

        Issue actions

          Wrong type inference for associated type implementing Iterator · Issue #11751 · rust-lang/rust-analyzer