Rust: Speedup type inference for Trait::function() calls#21301
Rust: Speedup type inference for Trait::function() calls#21301hvitved wants to merge 1 commit intogithub:mainfrom
Trait::function() calls#21301Conversation
There was a problem hiding this comment.
Pull request overview
Speeds up Rust type inference for UFCS-style calls like Trait::function(...) by reducing the number of candidate implementations considered upfront using inferred Self-type information, addressing observed analysis timeouts (e.g., ArmchairDevelopers/Glacier).
Changes:
- Introduces helper predicates to compute/require relevant type mentions for trait function resolution.
- Adds
Self-type–based filtering for non-method trait function call target candidate selection. - Adjusts non-blanket target compatibility checks to leverage the refined candidate set.
Comments suppressed due to low confidence (1)
rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll:3061
- This doc comment could be more precise about what
pos/pathrefer to (argument vs return position) and what “matches” means here (e.g., aftersubstituteLookupTraits). Tightening the wording would make it easier to maintain.
/**
* Holds if this call is of the form `Trait::function(args)`, and the type at `pos` and
* `path` matches the `Self` type parameter of `Trait`.
*/
| traitFunctionResolutionDependsOnArgument0(trait, traitFunction, pos, impl, implFunction, path, | ||
| traitTp) and | ||
| // Exclude functions where we cannot resolve all relevant type mentions; this allows | ||
| // for blanket implementations to be applied in those cases |
There was a problem hiding this comment.
The inline comment ends without punctuation and could be clearer as a complete sentence. Consider adding a period (and optionally rephrasing) to improve readability (e.g., make explicit that this is to allow blanket impls when type mentions can’t be resolved).
This issue also appears on line 3058 of the same file.
| // for blanket implementations to be applied in those cases | |
| // blanket implementations to be applied in those cases when type mentions cannot be resolved. |
|
I'm happy with the effect of this change. I'd prefer if @paldepind reviewed the code changes here, but I can do that if he isn't available. |
When resolving calls like
Trait::function(...)we need to take all possible implementations offunctioninto account, which can be a very large set.However, with #21217:
which means that we can strictly reduce this set up front, by only looking at implementations for types that might match the
Selftype inferred at the call site, similar to what we already do for method calls.DCA looks good; a significant speedup.
Fixes timeout on
ArmchairDevelopers/Glacier.