Rust: Add ConstAccess class with getConst() predicate#20736
Conversation
Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
0b9321f to
3477763
Compare
3477763 to
0ca62ca
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR introduces support for identifying and querying constant accesses in Rust code. It adds a new ConstAccess class that extends PathExpr to represent expressions that access constant declarations.
Key changes:
- Created a new
ConstAccessclass that uses path resolution to identify when a path expression refers to a constant - Added comprehensive test coverage including global constants, associated constants, and module constants
- Updated the extractor test expectations to reflect that constant accesses are now identified as
ConstAccessrather than justPathExpr
Reviewed Changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| rust/ql/lib/codeql/rust/elements/internal/ConstImpl.qll | Added ConstAccess class definition that extends PathExpr and resolves paths to constants |
| rust/ql/lib/codeql/rust/elements/ConstAccess.qll | Created public interface for the ConstAccess class |
| rust/ql/lib/rust.qll | Added import for the new ConstAccess element |
| rust/ql/.gitattributes | Removed linguist-generated marker from ConstImpl.qll to allow manual edits |
| rust/ql/.generated.list | Removed ConstImpl.qll entry as it's now manually maintained |
| rust/ql/test/library-tests/const_access/* | Added comprehensive test suite for constant access functionality |
| rust/ql/test/extractor-tests/macro-expansion/PrintAst.expected | Updated expected output to reflect ConstAccess classification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * } | ||
| * ``` | ||
| */ | ||
| class ConstAccess extends PathExprImpl::PathExpr { |
There was a problem hiding this comment.
I would have expected ConstAccess to extend VariableAccess (and re-use the getVariable() method).
There was a problem hiding this comment.
I think keeping them separate is easier, and also makes more sense IMO (a "constant variable" is a bit of an oxymoron).
There was a problem hiding this comment.
a "constant variable" is a bit of an oxymoron
That's true.
I was thinking more about how typical QL involving variables (e.g. looking for initializers) is likely to want to look at constants just the same.
There was a problem hiding this comment.
I think in most other languages we model a constant as a variable with a const flag or specifier or some such.
There was a problem hiding this comment.
We had a discussion about this, conclusion is that we can address this later if there turns out to be a need.
Adds
ConstAccessclass for identifying constant accesses in Rust code, analogous to the existingVariableAccessclass.Changes
ConstAccessclass inConstImpl.qllextendsPathExprBase, usesresolvePath()to resolve paths toConstitemsgetConst()predicate returns the constant being accessedrust/ql/test/library-tests/const_access/covers global, associated, and module constantsExample Usage
Example code patterns matched:
Implementation follows the same pattern as
resolveSelfTy()/resolveTraitTy()inPathResolution.qll.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
releases.bazel.build/usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 run //rust:install(dns block)/usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 run ast-generator:inject-sources(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
ConstAccessclass #20735✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.