Rust: Type inference for non-overloadable operators#19549
Merged
paldepind merged 3 commits intogithub:mainfrom May 21, 2025
Merged
Rust: Type inference for non-overloadable operators#19549paldepind merged 3 commits intogithub:mainfrom
paldepind merged 3 commits intogithub:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds type inference support for Rust’s built-in logical (&&, ||) and assignment (=) operators by introducing a unit type.
- Implement inference rules for
&&and||returningbool - Infer assignment expressions as
()and track LHS types - Define
TUnit()in the type system and aUnitTypeclass
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| rust/ql/test/library-tests/type-inference/type-inference.expected | Updated expected results to cover &&, ` |
| rust/ql/test/library-tests/type-inference/main.rs | Added operators module with tests for logical and assignment operators |
| rust/ql/lib/codeql/rust/internal/TypeInference.qll | Imported builtins and added inferLogicalOperationType and inferAssignmentOperationType rules |
| rust/ql/lib/codeql/rust/internal/Type.qll | Extended TType with TUnit() and implemented a UnitType class |
Comments suppressed due to low confidence (3)
rust/ql/lib/codeql/rust/internal/TypeInference.qll:10
- Typo in module path:
Bultinsshould beBuiltinsto correctly import the stdlib Builtins module.
private import codeql.rust.frameworks.stdlib.Bultins as Builtins
rust/ql/lib/codeql/rust/internal/TypeInference.qll:257
- In
typeEquality, the QL classAssignmentExpris used but elsewhereAssignmentOperationis referenced; use a consistent AST class (likelyAssignmentOperation) to ensure coverage.
exists(AssignmentExpr be |
rust/ql/lib/codeql/rust/internal/Type.qll:12
- [nitpick] Consider adding a brief comment next to
TUnit()in thenewtype TTypedeclaration to explain its role representing the unit()type.
TUnit() or
hvitved
approved these changes
May 21, 2025
Contributor
hvitved
left a comment
There was a problem hiding this comment.
LGTM. I have started a DCA run.
Contributor
Author
|
DCA is looking good. Thanks for starting it. |
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.
This PR adds type inference for all non-overloadable operators in Rust. It turns out that there are only three such operators.
&&and||(which are not overloadable for short-circuiting reasons) and=.The assignment operator evaluates to unit, and as we didn't have a unit type I added one.