Skip to content

internal: merge Trait and TraitAlias handling#20376

Merged
Veykril merged 3 commits into
rust-lang:masterfrom
fee1-dead:traitalias
Aug 13, 2025
Merged

internal: merge Trait and TraitAlias handling#20376
Veykril merged 3 commits into
rust-lang:masterfrom
fee1-dead:traitalias

Conversation

@fee1-dead

Copy link
Copy Markdown
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
@fee1-dead fee1-dead force-pushed the traitalias branch 2 times, most recently from 900a128 to e3cf7eb Compare August 4, 2025 08:29
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
Copy Markdown
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
Copy Markdown
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

@ShoyuVanilla ShoyuVanilla left a comment

Copy link
Copy Markdown
Member

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

@fee1-dead

Copy link
Copy Markdown
Member Author

#20329 was merged already and GitHub does not report any merge conflicts with this PR - so I think this is good to go?

@Veykril

Veykril commented Aug 13, 2025

Copy link
Copy Markdown
Member

Surprising that there are no conflicts, but in that case yes let's merge this 👍

@Veykril Veykril added this pull request to the merge queue Aug 13, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 13, 2025
@fee1-dead

Copy link
Copy Markdown
Member Author

Rebased, only the newest commit has changed, please review :)

@Veykril Veykril added this pull request to the merge queue Aug 13, 2025
Merged via the queue into rust-lang:master with commit ac9d0f8 Aug 13, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 13, 2025
@fee1-dead fee1-dead deleted the traitalias branch August 13, 2025 15:25
@lnicola lnicola changed the title Merge Trait and TraitAlias handling internal: merge Trait and TraitAlias handling Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants