rust-lang / rust-lang/rust-analyzer

`rust-analyzer` fails to resolve/auto-complete constants of associated types.

Open
#18,935 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-completion A-ty C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer fails to resolve/auto-complete constants of associated types.

What was I doing?

I wanted to have multiple associated constants on a type (implemented by a proc macro), but I wanted them grouped/nested together under another associated type.

Basically I wanted something like MyType::CONSTS::MY_CONST (instead of adding them directly on the type like MyType::CONST_1, MyType::CONST_2, MyType::CONST_3, and so on).

What was my issue?

I can do <MyType as MyConstTrait>::CONSTS::MY_CONST and my code will compile and run properly, but none of the constants are suggested.

The only auto-complete suggestions are into, try_into, from, and try_from.

See below for full (minimal) implementation.

rust-analyzer version: 0.4.2262-standalone

rustc version: rustc 1.84.0-nightly (b91a3a056 2024-11-07)

editor or extension: I have the same issue using rust-analyzer on both VSCode and Neovim

relevant settings: N/A

repository link (if public, optional): (eg. rust-analyzer)

code snippet to reproduce:

pub struct Person {
    age: i32,
    name: String,
    alive: bool,
}

trait FieldPaths {
    type T;
}

impl FieldPaths for Person {
    type T = PersonPaths;
}

pub struct PersonPaths;

impl PersonPaths {
    pub const AGE: &'static str = "age";
    pub const NAME: &'static str = "name";
    pub const ALIVE: &'static str = "alive";
}

fn main() {
    // I get no intelli-sense to complete AGE, but it does compile
    let age = <Person as FieldPaths>::T::AGE;
    println!("{age}");

    // Still no intelli-sense to complete AGE, but still compiles
    type PATH = <Person as FieldPaths>::T;
    let age = PATH::AGE;
    println!("{age}");
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Rust reproduction in the issue and investigate rust-analyzer's completion handling for associated types and their constants. Reproduce the missing suggestions in VSCode or Neovim; done means AGE, NAME, and ALIVE are offered for ::T and the PATH alias.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.