-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Do not dispatch to all implementations when trait target is accurate #20969
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
42fd1da to
5888ed3
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 Rust trait function call resolution by distinguishing between cases where trait dispatch is necessary and cases where the target can be precisely determined. The key improvement is adding a dispatch boolean parameter to track whether a resolved target is a trait item due to type ambiguity (requiring dispatch to all implementations) or a precise target (no dispatch needed).
- Modified
resolveCallTargetto return adispatchflag indicating whether the resolved target requires trait dispatch - Updated call target resolution logic to only dispatch to all trait implementations when type information is insufficient to determine a single target
- Added comprehensive test cases demonstrating correct resolution of trait default implementations vs. overridden implementations
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| rust/ql/lib/codeql/rust/internal/TypeInference.qll | Added dispatch parameter to resolveCallTarget to distinguish between precise targets and trait items requiring dispatch |
| rust/ql/lib/codeql/rust/elements/internal/CallImpl.qll | Updated getARuntimeTarget to only dispatch to implementations when dispatch=true, ensuring precise targets don't trigger unnecessary dispatch |
| rust/ql/lib/codeql/rust/elements/internal/InvocationExprImpl.qll | Updated getResolvedTarget to accommodate new resolveCallTarget signature |
| rust/ql/test/library-tests/dataflow/global/main.rs | Added test module demonstrating trait default implementations vs. overridden implementations with proper call resolution |
| rust/ql/test/library-tests/dataflow/global/viableCallable.expected | Updated expected test results showing correct resolution of trait calls without unnecessary dispatch |
| rust/ql/test/library-tests/dataflow/global/inline-flow.expected | Updated expected dataflow results for new test cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Currently when a call resolves to a trait function we enable dispatching to all implementations of that trait function. However, sometimes when a trait function has a default implementation, it is actually the one and only correct result. See the tests for an example.
This PR restrict the dispatching to the cases where the trait function is the resolved target because type information is not enough to find a single correct target.