rust-lang / rust-lang/rust-analyzer

Feature request: Add assist for switching between `<Type as Trait>::Assoc` and `Type::Assoc`

Open
#12,132 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When referring to associated items from a trait on generic or concrete types, one can use either fully-qualified syntax (<Type as Trait>::Assoc) or shorter syntax without as qualifier (Type::Assoc). In cases where the use of the shorter syntax would be ambiguous, the fully-qualified is required, but in non-ambiguous cases it is up to the developer's preference (not sure if there are any recommendations/good practices).

It would be nice to have an assist that switches between these two options.

The main motivation is for code auto-generated by rust analyzer which conservatively uses as Trait option to be safe from ambiguities and the user needs to manually remove the full qualification if they prefer not to have it.

It needs to work with all associated items: functions, types and constants.

I made a fix to rust analyzer in the past, which was even related to associated items, so I believe I should be able to implement this assist and send a PR. I mostly need help with wording for the assist (how should I refer to those two syntax options) and any thoughts that other people have on this functionality.

Examples
  1. full -> short (Remove fully-qualified syntax (?))

from

    <Type as Trait>::Assoc
//  ~~~~~~~~~~~~~~~ (assist requested anywhere above the tildes segment)

to

    Type::Assoc
  1. short -> full (Use fully-qualified syntax (?))

from

    Type::Assoc
//  ~~~~ (assist requested anywhere above the tildes segment)

to

    <Type as Trait>::Assoc

When multiple possibilities for full qualification are available (i.e., use of the associated item is ambiguous), the list of the possibilities would be presented to the user to select one of them (similarly as in auto import assist).

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

Use the issue's Rust examples as the starting specification. Compare existing rust-analyzer assists and the auto-import assist, then identify where associated functions, types, and constants are handled and tested. Done means both conversions work, ambiguous short forms offer selectable traits, and the behavior is covered for all three associated-item kinds.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.