Skip to content

unresolved reference for trait associated type #14393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jhgg opened this issue Mar 22, 2023 · 1 comment
Closed

unresolved reference for trait associated type #14393

jhgg opened this issue Mar 22, 2023 · 1 comment
Labels
A-hir hir and hir-def related A-nameres name, path and module resolution A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@jhgg
Copy link
Contributor

jhgg commented Mar 22, 2023

I'm not sure the best title for this issue, so feel free to rename:

Consider the following code:

trait Foo {
    type Bar;
    fn baz() -> Self::Bar;
}

enum BarEnum {
    Qux { x: u32 },
    Tup(),
    Reg,
}

impl BarEnum {
    fn method() -> Self {
        Self::Tup()
    }
}

impl Foo for String {
    type Bar = BarEnum;

    fn baz() -> Self::Bar {
        Self::Bar::Qux { x: 1 };
        //    ^^^  ^^^   ^
        //   unresolved

        Self::Bar::Tup();
        //    ^^^
        //    unresolved

        Self::Bar::Reg;
        //    ^^^
        //    unresolved

        Self::Bar::method()
        //    ^^^
        //    unresolved
    }
}

When referring to BarEnum as Self::Bar, rust-analyzer seems to be unable to resolve. Here is a screenshot with a red semantic token.

image

This breaks autocomplete, and also stuff like rename symbol, for example renaming BarEnum::Qux, does not cause Self::Bar::Qux to be renamed. Additionally renaming type Bar also does not rename where its unresilved.

Self::Bar only becomes unresolved after you type :: after it:

Code_-_Insiders_7kJhB66akj

rust-analyzer version: rust-analyzer version: 0.4.1445-standalone

rustc version: rustc 1.66.1 (90743e729 2023-01-10)

@jhgg jhgg added the C-bug Category: bug label Mar 22, 2023
@lowr lowr added A-nameres name, path and module resolution A-hir hir and hir-def related A-ty type system / type inference / traits / method resolution labels Mar 24, 2023
bors added a commit that referenced this issue Apr 5, 2023
…r=HKalbasi

Normalize associated types in paths in expressions

Part of #14393

When we resolve paths in expressions (either path expressions or paths in struct expressions), there's a need of projection normalization, which `TyLoweringContext` cannot do on its own. We've been properly applying normalization for paths in struct expressions without type anchor, but not for others:

```rust
enum E {
    S { v: i32 }
    Empty,
}

impl Foo for Bar {
    type Assoc = E;
    fn foo() {
        let _ = Self::Assoc::S { v: 42 };   // path in struct expr without type anchor; we already support this
        let _ = <Self>::Assoc::S { v: 42 }; // path in struct expr with type anchor; resolves with this PR
        let _ = Self::Assoc::Empty;         // path expr; resolves with this PR
    }
}
```

With this PR we correctly resolve the whole path, but we need some more tweaks in HIR and/or IDE layers to properly resolve a qualifier (prefix) of such paths and provide IDE features that are pointed out in #14393 to be currently broken.
@Veykril
Copy link
Member

Veykril commented Jul 21, 2024

All of these resolve now

@Veykril Veykril closed this as completed Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-hir hir and hir-def related A-nameres name, path and module resolution A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants