Skip to content

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Jul 28, 2025

This PR implements method resolution for blanket implementations that satisfies the criteria: the type parameter that the implementation targets has a trait bound.

For instance, this impl block

impl<R: AsyncRead> AsyncReadExt for R {}

is handled as there is a trait bound AsyncRead on the type parameter R.

On the other hand, this impl block

impl<T, U> Into<U> for T where U: From<T> {

is not handled since there is not trait bound for T.

At a high level, the implementation works as follows:

  1. For a method call foo.bar check if there's a blanket implementation that has the method bar.
  2. For a trait bound on the blanket implementation's type parameter, check if foo satisfies the trait bound.
  3. Given the above, resolve foo.bar to the method from the blanket implementation.

If there are multiple trait bounds on the type parameter, then only one is checked to be satisfied. This could give false results, but from looking at some results in doesn't look too bad after filtering away some trivial traits (Clone, Send, etc.).

The DCA report seems great. A 0.819% point increase in resolved calls with little to no change to performance. Some inconsistencies are up, but a good chunk of that is probably explained by more type information in general.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jul 28, 2025
@paldepind paldepind force-pushed the rust/type-inference-blanket-impl branch from 983257d to 2e98c24 Compare August 2, 2025 13:10
@paldepind paldepind force-pushed the rust/type-inference-blanket-impl branch from 2e98c24 to f130198 Compare August 3, 2025 08:37
@paldepind paldepind force-pushed the rust/type-inference-blanket-impl branch from 0314813 to 8c91ef0 Compare September 10, 2025 08:23
@paldepind paldepind marked this pull request as ready for review September 10, 2025 09:41
@paldepind paldepind requested a review from a team as a code owner September 10, 2025 09:41
@Copilot Copilot AI review requested due to automatic review settings September 10, 2025 09:41
Copy link
Contributor

@Copilot 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 implements method resolution for blanket implementations in Rust type inference, specifically targeting blanket implementations where the type parameter has trait bounds. The implementation enables CodeQL to better resolve method calls that depend on blanket trait implementations.

Key changes:

  • Added support for blanket implementation method resolution with trait bounds
  • Enhanced type inference to handle universally quantified type parameters in constraints
  • Updated test cases to reflect improved method resolution capabilities

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
shared/typeinference/codeql/typeinference/internal/TypeInference.qll Added useUniversalConditions predicate to control constraint satisfaction for universally quantified type parameters
rust/ql/lib/codeql/rust/internal/TypeInference.qll Core implementation of blanket implementation support including detection, canonicalization, and method resolution
rust/ql/lib/codeql/rust/internal/PathResolution.qll Added resolveBound method to resolve type parameter bounds by index
rust/ql/test/library-tests/type-inference/blanket_impl.rs New test file demonstrating blanket implementation scenarios
Multiple test expected files Updated to reflect improved method resolution results from blanket implementation support

Comment on lines +2217 to +2222
not trait.getName().getText() =
[
"Sized", "Clone",
// The auto traits
"Send", "Sync", "Unpin", "UnwindSafe", "RefUnwindSafe"
]
Copy link
Preview

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

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

The hardcoded list of excluded trait names should be extracted to a constant or configuration to improve maintainability and make it easier to extend.

Copilot uses AI. Check for mistakes.

* We detect these duplicates based on some simple heuristics (same trait
* name, file name, etc.). For these duplicates we select the one with the
* greatest file name (which usually is also the one with the greatest library
* version in the path)
Copy link
Preview

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

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

The heuristic for selecting the 'canonical' implementation based on greatest file name seems fragile and may not always correspond to the greatest library version. Consider documenting the limitations of this approach or exploring more robust versioning detection.

Suggested change
* version in the path)
* version in the path). **Note:** This heuristic is fragile and may not always
* correspond to the greatest library version, as file names may not reliably
* encode version information and lexicographical order does not match semantic
* versioning. Users should be aware of this limitation and consider more robust
* version detection if precise canonicalization is required.

Copilot uses AI. Check for mistakes.

@paldepind paldepind added the no-change-note-required This PR does not need a change note label Sep 10, 2025
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