Skip to content

Conversation

@ShoyuVanilla
Copy link
Member

Fixes #19339

In the following code:

trait Foo {
    type Assoc1;
    type Assoc2;
}

struct Bar {}

trait Baz {}

And we have some trait bounds like Self: Foo<Assoc1 = Bar, Assoc2: Baz>, rustc considers

  • the projection predicate <Self as Foo>::Assoc1 = Bar as a self bound
  • the assoc bound predicate <Self as Foo>::Assoc2: Baz as a non-self bound

https://github.com/rust-lang/rust/blob/00f49d22042dce59561b36eb075bbf5e343c23c4/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs#L547-L668

As I have previously stated in some FIXME comment 😅, we and rustc has a bit different predicates lowering implementation: rustc lowers them multiple times with granular filtering options, while we lower them as little as possible.
I think our current implementation makes sense for memory usages and we don't need much granular filtering options like rustc (yet) so I just reordered those assoc item bounds at the end of the predicates and discern them by range.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 4, 2026
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.

Few nits and one comment.

db: &'db dyn HirDatabase,
type_alias: TypeAliasId,
) -> (EarlyBinder<'db, Clauses<'db>>, Diagnostics) {
) -> (EarlyBinder<'db, Clauses<'db>>, u32, Diagnostics) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Return a struct? The role of the u32 isn't clear from the outside.

self.predicates.get().map_bound(|it| &it.as_slice()[self.own_predicates_start as usize..])
}

/// Returns the predicates, minus the implicit `Self: Trait` predicate for a trait.
Copy link
Contributor

Choose a reason for hiding this comment

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

Update this comment?


let diagnostics = create_diagnostics(ctx.diagnostics);
let assoc_ty_bounds_start = predicates.len() as u32;
predicates.extend(assoc_ty_bounds);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will mean query_own() will get the assoc type bounds from the parent as well, which isn't what we want. I think you'll need to have two assoc_ty_bounds_starts.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, right. I'm messing up bounds from parents here 😅

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Jan 7, 2026
Merged via the queue into rust-lang:master with commit 54f5eea Jan 8, 2026
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 8, 2026
@ShoyuVanilla ShoyuVanilla deleted the self-only-preds branch January 8, 2026 00:54
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.

rust-analyzer fails to resolve some of the bounds on traits with associated types

3 participants