Rust: Type inference for macro expressions#19751
Merged
hvitved merged 2 commits intogithub:mainfrom Jun 13, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds support for inferring types from macro expressions in the Rust CodeQL type inference engine.
- Extended the
typeEqualitypredicate to treatMacroExprexpansions as equivalent nodes. - Added a new
macrostest module with aformat!invocation inmain.rs. - Updated the
type-inference.expectedfile to include expected entries for macro expressions and their expansions.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| rust/ql/lib/codeql/rust/internal/TypeInference.qll | Added an or clause in typeEquality to equate macro expressions with their expansions. |
| rust/ql/test/library-tests/type-inference/main.rs | Introduced a macros module and a format! call to exercise macro-based type inference. |
| rust/ql/test/library-tests/type-inference/type-inference.expected | Updated expected output to list MacroExpr nodes and their expanded types for the tests. |
Comments suppressed due to low confidence (2)
rust/ql/test/library-tests/type-inference/main.rs:1808
- [nitpick] The module name
macroscould be confused with Rust's built-in macros. Consider renaming it to something more descriptive likemacro_testsormacro_expr_tests.
mod macros {
rust/ql/test/library-tests/type-inference/main.rs:1810
- This inline TODO references an open PR. Remove or update this comment once that change is merged to avoid stale test markers.
let x = format!("Hello, {}", "World!"); // $ MISSING: type=x:String -- needs https://github.com/github/codeql/pull/19658
| n1 = be.getLhs() and | ||
| n2 = be.getRhs() | ||
| ) | ||
| or |
There was a problem hiding this comment.
[nitpick] Consider adding a brief comment above this clause explaining why macro expressions require special handling here, improving future maintainability.
Suggested change
| or | |
| or | |
| // Macro expressions require special handling because they can expand into multiple | |
| // expressions or statements, which affects type inference. |
60ea89b to
2f698d1
Compare
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.
DCA shows a nice 1 percentage point increase in resolvable calls.