-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Refine Self resolution inside impl blocks
#20853
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
base: main
Are you sure you want to change the base?
Conversation
104b715 to
6f83525
Compare
6f83525 to
63926e6
Compare
There was a problem hiding this 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 refines the resolution of Self inside impl blocks to consistently resolve to the type being implemented rather than the impl block itself. This enables more accurate path resolution, trait method calls, and type inference. The changes include restricting the scope of default trait implementations, preventing type parameters from escaping their scope, and improving consistency in path resolution.
- Resolves
Selfto the implementing type in all contexts, not to the impl block - Prevents type parameters from escaping their declared scope during type resolution
- Restricts default trait implementations to only be inherited from the directly implemented trait, not super traits
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
rust/ql/lib/codeql/rust/internal/PathResolution.qll |
Updates Self resolution logic to always resolve to the implementing type; restricts default trait implementation inheritance to direct traits only; simplifies path resolution logic |
rust/ql/lib/codeql/rust/internal/TypeMention.qll |
Adds type parameter scoping logic to prevent parameters from escaping their declaring item's scope; refactors type resolution into resolvePathTypeAt |
rust/ql/lib/codeql/rust/internal/Type.qll |
Adds getDeclaringItem() method to TypeParameter to track the scope of type parameters |
rust/ql/lib/codeql/rust/internal/TypeInferenceConsistency.qll |
Filters out known limitations for escaping type parameters in consistency checks |
rust/ql/test/library-tests/path-resolution/main.rs |
Adds test cases for improved Self resolution in impl blocks; demonstrates resolution of methods from different traits |
rust/ql/test/library-tests/type-inference/main.rs |
Removes spurious target annotation that is now correctly resolved |
rust/ql/test/library-tests/type-inference/type-inference.expected |
Updates expected results reflecting improved type inference with fewer incorrect inferences |
rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected |
Removes spurious call target and adds multiple path resolution entries for associated types |
rust/ql/test/library-tests/sensitivedata/CONSISTENCY/PathResolutionConsistency.expected |
Removes false positive for multiple call targets |
rust/ql/test/library-tests/path-resolution/path-resolution.expected |
Updates expected path resolutions reflecting improved Self resolution to implementing types |
rust/ql/test/library-tests/path-resolution/CONSISTENCY/PathResolutionConsistency.expected |
Updates line numbers for multiple call targets after code changes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Before this PR, we would resolve
Selfinsideimplblocks to either (1) the type being implemented, ifSelfis not a qualifier of another path, or (2) theimplblock itself, otherwise.This PR changes the logic so we always resolve
Selfto the type being implemented, which enables us to correctly resolve more paths.We also restrict the scope of default trait implementations in path resolution, similar to @paldepind 's #20723, and finally prevent type parameters from escaping their scope when resolving type mentions.
DCA looks great: A significant reduction in
Nodes With Type At Length Limit, and a modest increase in resolved calls.