Skip to content
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

fix: auto-completion for associated type constants #19118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

boattime
Copy link
Contributor

@boattime boattime commented Feb 8, 2025

This commit fixes an issue where rust-analyzer failed to resolve and auto-complete constants defined inside associated types.

Changes:

  • Adjusts type_for_type_alias_with_diagnostics_query to resolve trait-associated types to their implementing type alias.
  • Introduces a lookup mechanism to find the correct implementation type alias if available.
  • Adds a test case to validate auto-completion of associated type constants.

Resolves #18935.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 8, 2025
};

let impl_data = db.type_alias_data(impl_type_alias_id);
if impl_data.name.as_str() == "Assoc" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure there is a better way to do this check but was having a hard time figuring it out. Open to any suggestions but wanted to get eyes on this change to make sure this was the ideal way to solve

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels very wrong to change analysis (which is working properly) in order to solve this. You're also duplicating a bunch of work here, slowing down analysis, and this has other disadvantages too (like creating a query dependency to trait_impls_in_crate() and a bunch of others).

In general, solving traits and normalizing associated types (that's how we call the process of determining what actual type an associated type has) is the job of the trait solver. In our case, it's Chalk. It can easily find the relevant impls, and normalize the associated type. I don't know why completion isn't using it, but this is what it should do. If you need any pointers/help don't hesitate to ask!

Thanks anyway for your efforts!

}
impl PersonPaths {
pub const NAME: &'static str = "name";
pub fn get_default() -> String { String::new() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String isn't available in our tests (that's why you have {unknown}), you can use primitive types like i32 or &str.

@boattime
Copy link
Contributor Author

boattime commented Feb 9, 2025

Not a problem at all! I can close the PR and solve using chalk. I figured there was a better place to solve. Thanks for the info!

@rustbot rustbot added has-merge-commits S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 18, 2025

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git pull --rebase https://github.com/rust-lang/rust-analyzer.git master
$ git push --force-with-lease

The following commits are merge commits:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-merge-commits S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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