Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Feb 2, 2026

The problem

Currently path resolution doesn't resolve paths of the form <SomeType as SomeTrait>.

The solution

The most precise resolution would be the impl block that makes SomeType implement SomeTrait. While not impossible, that requires an understanding of types that's currently left for type inference.

This PR takes a simple approach and resolves such a path to the trait after as. This makes a path like <Foo as Bar>::baz equivalent to Bar::baz and a path like <Self as Bar>::Assoc equivalent to Bar::Assoc. Hence such paths can be treated uniformly in type inference when that is convenient. The tests show an example where that removes spurious results.

DCA

The DCA results seem fine. We gain a few extra calls and types.

There is an increase in inconsistencies. I've investigated some, and while they look like real inconsistencies it doesn't seem like the changes in this PR are the root cause.

Here's one example. Inside a impl block we'd currently have an ill-formed TypeMention inconsistency at Self::Item below

type Item = T::Output; // `T` is a type parameter of the impl block
fn some_method(...) -> Self::Item

and after this PR we'd also have an inconsistency for Self::Item in the case below

type Item = <T as SomeTrait>::Output; // now using `as` here
fn some_method(...) -> Self::Item

This is because Self::Item is now (correctly) handled as an AliasPathTypeMention since the right-hand side of the alias is now resolved. This causes it to no longer be picked up by this inconsistency exception. This seems desirable as this does in fact reflect a real ill-formed type mention (both before and after this PR).

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Feb 2, 2026
@paldepind paldepind marked this pull request as ready for review February 4, 2026 09:27
@paldepind paldepind requested a review from a team as a code owner February 4, 2026 09:27
Copilot AI review requested due to automatic review settings February 4, 2026 09:27
@paldepind paldepind added the no-change-note-required This PR does not need a change note label Feb 4, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves Rust path resolution by enabling <SomeType as SomeTrait> qualified paths to resolve to the trait after the as keyword. This is a pragmatic approach that makes such paths equivalent to trait paths (e.g., <Foo as Bar>::baz is treated like Bar::baz), allowing type inference to handle them uniformly without requiring full type-level understanding of impl blocks.

Changes:

  • Modified path resolution logic to extract and resolve trait names from <Type as Trait> path segments
  • Added comprehensive test cases demonstrating trait disambiguation with the new syntax
  • Updated test expectations to reflect improved path resolution and reduced spurious inconsistencies

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
rust/ql/lib/codeql/rust/internal/PathResolution.qll Core change: extends isUnqualified predicate to include trait name from <Type as Trait> segments
rust/ql/test/library-tests/type-inference/main.rs Adds test cases for trait method disambiguation using <Type as Trait> syntax
rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected Updates line number for consistency check
rust/ql/test/library-tests/path-resolution/main.rs Updates test annotations to reflect newly resolved paths and adds associated type tests
rust/ql/test/library-tests/path-resolution/path-resolution.expected Updates expected path resolutions showing <Type as Trait> now resolves to trait items
rust/ql/test/library-tests/path-resolution/CONSISTENCY/PathResolutionConsistency.expected Removes spurious "multipleResolvedTargets" inconsistencies for <Type as Trait> calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant