Rust: Implement type inference for ref expression as type equality#19724
Merged
Rust: Implement type inference for ref expression as type equality#19724
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enhances Rust expression type inference by treating &x as a true type equality and removes legacy reference-handling logic. Key changes include:
- Added new test cases in
main.rsto verify boolean and custom-flag type inference through multiple borrows. - Extended the
typeEqualitypredicate to includeRefExprand simplifiedinferRefExprTypein QL. - Removed outdated logic for reference-expression inference in
TypeInference.qll.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| rust/ql/test/library-tests/type-inference/main.rs | Added MyFlag struct tests and **&&true inference |
| rust/ql/lib/codeql/rust/internal/TypeInference.qll | Updated typeEquality, simplified inferRefExprType and inference pipeline |
Comments suppressed due to low confidence (2)
rust/ql/test/library-tests/type-inference/main.rs:1158
- [nitpick] The field name
boolshadows the built-in type name and may be confusing; consider renaming it to something more descriptive likeflagorstate.
struct MyFlag {
rust/ql/lib/codeql/rust/internal/TypeInference.qll:980
- [nitpick] Update this doc comment to mention that
inferRefExprTypeno longer takes apathparameter and now only returnsTRefTypefor the givenRefExpr.
/** Gets the root type of the reference expression `re`. */
hvitved
approved these changes
Jun 11, 2025
Contributor
hvitved
left a comment
There was a problem hiding this comment.
LGTM. Let's see what DCA says before merging.
Contributor
Author
|
DCA seems fine. Overall a reduction in missing call targets. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I was debugging an issue where types where not inferred correctly, and realized that for a call like
f(&a)the declared type offs first parameters would not flow toathrough the borrow.This PR fixes that and also removes some logic for ref expressions that doesn't match Rust's type system. Due to implicit dereferencing
&&&&aand&acan seem interchangeable, but the types are distinct. Removing this collapsing doesn't seem to affect the tests.