Skip to content

Merge Trait and TraitAlias handling #20376

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

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

Conversation

fee1-dead
Copy link
Member

Much of the current infrastructure around traits are good and cool but trait aliases don't get to enjoy type inference because they are treated as distinct entities from traits.

Doesn't seem easy to change all the existing facilities to accept something like Either<Trait, TraitAlias>.

We can actually just merge them and most things will work. Second commit adds a simple test.

The core of this change is in crates/syntax/rust.ungram.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 4, 2025
@fee1-dead fee1-dead changed the title Merge Trait and TraitAlias handing Merge Trait and TraitAlias handling Aug 4, 2025
let ast_id_map = db.ast_id_map(ast_id.file_id);
let source = ast_id.with_value(ast_id_map.get(ast_id.value)).to_node(db);
if source.eq_token().is_some() {
// FIXME(trait-alias) probably needs special handling here
Copy link
Member

Choose a reason for hiding this comment

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

I guess this would be okay as this PR treats TraitAliass somewhat like new trait with bounds and no assoc items and rust-analyzer won't demand assoc items for such traits or (currently for) trait aliases?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I reconsidered and thought it might not need special handling halfway into implementing this PR. I originally was thinking maybe we should return the associated items of the trait that it aliases but we already lower them with bound handling - so the aliased trait will be treated as a super trait

Copy link
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

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

I think this is a solid improvement overall. It might end up marking trait aliases as actual traits, but it correctly resolves associated types and, more importantly, trait methods. That alone could potentially address our long-standing issues with trait aliases, such as #15566 .

My only hesitation is whether this could complicate the merging of #20329. I skimmed that PR and only noticed a few minor references to TraitAlias, so the impact may be limited.

That said, I'm unsure whether we should merge this before or after #20329. cc @rust-lang/rust-analyzer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants