Skip to content

Type inference breaks for types with generic lifetimes and generic type parameters with default values  #17515

Closed
@Veetaha

Description

@Veetaha

rust-analyzer version: 0.3.2011-standalone
rustc version: rustc 1.78.0 (9b00956e5 2024-04-29)
extension: VSCode extension v0.3.2011

Reproduction

Paste the following code into main.rs

struct Blackjack<'a, T = bool> {
    state: &'a T,
}

impl<'a, T> Blackjack<'a, T> {
    fn method(self) {
        let _ = self.state;
    }
}

fn make_blackjack<'a>() -> Blackjack<'a> {
    Blackjack { state: &true }
}

fn main() {
    let blackjack = make_blackjack();
    blackjack.method();
}

Expected

I expected rust-analyzer to resolve the .method() invocation correctly and highlight it as yellow according to my semantic tokens highlighting config.

Actual

You may see that method() method symbol is unresolved. Here is how it looks in my VSCode. I configured unresolved symbols semantic token type to be displayed in pale-red. You may see that the lifetime parameter of the struct is inferred to be {unknown}. I suppose this is the reason why .method() is unresolved.

Well-formed variation

If you remove the T = bool default value of the generic parameter on the struct definition and pass bool explicitly then all is fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions