Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 31, 2025

Adds ConstAccess class for identifying constant accesses in Rust code, analogous to the existing VariableAccess class.

Changes

  • ConstAccess class in ConstImpl.qll extends PathExprBase, uses resolvePath() to resolve paths to Const items
  • getConst() predicate returns the constant being accessed
  • Test suite in rust/ql/test/library-tests/const_access/ covers global, associated, and module constants

Example Usage

import rust

from ConstAccess ca
where ca.getConst().getName().getText() = "MAX_SIZE"
select ca, "Access to MAX_SIZE constant"

Example code patterns matched:

const GLOBAL: i32 = 42;
impl MyStruct {
    const ASSOC: i32 = 100;
}

let x = GLOBAL;                    // ConstAccess
let y = MyStruct::ASSOC;           // ConstAccess
let z = module::CONST;             // ConstAccess

Implementation follows the same pattern as resolveSelfTy()/resolveTraitTy() in PathResolution.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
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 run //rust:install (dns block)
    • Triggering command: /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

This section details on the original issue you should resolve

<issue_title>Rust: Add new ConstAccess class</issue_title>
<issue_description>Just like we have the VariableAccess class, we should have a ConstAccess class with a Const getConst() predicate for retrieving that constant being accessed. getConst can be implemented by directly calling resolvePath from PathResolution.qll library. Test cases should be added as well.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Oct 31, 2025
Copilot AI and others added 2 commits October 31, 2025 13:01
Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
@github-actions github-actions bot added the Rust Pull requests that update Rust code label Oct 31, 2025
Copilot AI changed the title [WIP] Add new ConstAccess class for constant retrieval Rust: Add ConstAccess class with getConst() predicate Oct 31, 2025
Copilot AI requested a review from hvitved October 31, 2025 13:14
Copilot finished work on behalf of hvitved October 31, 2025 13:14
@hvitved hvitved force-pushed the copilot/add-constaccess-class branch 2 times, most recently from 0b9321f to 3477763 Compare November 3, 2025 12:03
@hvitved hvitved force-pushed the copilot/add-constaccess-class branch from 3477763 to 0ca62ca Compare November 3, 2025 13:15
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Nov 3, 2025
@hvitved hvitved marked this pull request as ready for review November 3, 2025 14:04
@hvitved hvitved requested a review from a team as a code owner November 3, 2025 14:04
Copilot AI review requested due to automatic review settings November 3, 2025 14:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ConstAccess class 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 ConstAccess rather than just PathExpr

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.

@hvitved hvitved requested a review from geoffw0 November 4, 2025 12:15
* }
* ```
*/
class ConstAccess extends PathExprImpl::PathExpr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected ConstAccess to extend VariableAccess (and re-use the getVariable() method).

Copy link
Contributor

@hvitved hvitved Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keeping them separate is easier, and also makes more sense IMO (a "constant variable" is a bit of an oxymoron).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in most other languages we model a constant as a variable with a const flag or specifier or some such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust: Add new ConstAccess class

3 participants