Open
Conversation
Contributor
There was a problem hiding this comment.
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
SomeTypeimplementSomeTrait. 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>::bazequivalent toBar::bazand a path like<Self as Bar>::Assocequivalent toBar::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
TypeMentioninconsistency atSelf::Itembelowand after this PR we'd also have an inconsistency for
Self::Itemin the case belowThis is because
Self::Itemis now (correctly) handled as anAliasPathTypeMentionsince 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).