Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Dec 5, 2025

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.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Dec 5, 2025
@paldepind paldepind force-pushed the rust/dispath-default-trait branch from 42fd1da to 5888ed3 Compare December 5, 2025 12:01
@paldepind paldepind marked this pull request as ready for review December 5, 2025 12:56
@paldepind paldepind requested a review from a team as a code owner December 5, 2025 12:56
Copilot AI review requested due to automatic review settings December 5, 2025 12:56
Copilot finished reviewing on behalf of paldepind December 5, 2025 12:58
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 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 resolveCallTarget to return a dispatch flag 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.

paldepind and others added 2 commits December 5, 2025 14:03
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant