Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion rust/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rust/ql/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions rust/ql/lib/codeql/rust/elements/ConstAccess.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This module provides the public class `ConstAccess`.
*/

private import internal.ConstImpl

final class ConstAccess = Impl::ConstAccess;
27 changes: 26 additions & 1 deletion rust/ql/lib/codeql/rust/elements/internal/ConstImpl.qll
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// generated by codegen, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `Const`.
*
* INTERNAL: Do not use.
*/

private import codeql.rust.elements.internal.generated.Const
private import codeql.rust.elements.internal.PathExprImpl::Impl as PathExprImpl
private import codeql.rust.internal.PathResolution

/**
* INTERNAL: This module contains the customizable definition of `Const` and should not
* be referenced directly.
*/
module Impl {
// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* A constant item declaration.
*
Expand All @@ -21,4 +23,27 @@ module Impl {
* ```
*/
class Const extends Generated::Const { }

/**
* A constant access.
*
* For example:
* ```rust
* const X: i32 = 42;
*
* fn main() {
* println!("{}", X);
* }
* ```
*/
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.

Copy link
Contributor

Choose a reason for hiding this comment

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

We had a discussion about this, conclusion is that we can address this later if there turns out to be a need.

private Const c;

ConstAccess() { c = resolvePath(this.getPath()) }

/** Gets the constant being accessed. */
Const getConst() { result = c }

override string getAPrimaryQlClass() { result = "ConstAccess" }
}
}
1 change: 1 addition & 0 deletions rust/ql/lib/rust.qll
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import codeql.rust.elements.ArithmeticOperation
import codeql.rust.elements.AssignmentOperation
import codeql.rust.elements.BitwiseOperation
import codeql.rust.elements.ComparisonOperation
import codeql.rust.elements.ConstAccess
import codeql.rust.elements.DerefExpr
import codeql.rust.elements.LiteralExprExt
import codeql.rust.elements.LogicalOperation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ macro_expansion.rs:
# 98| getParamList(): [ParamList] ParamList
# 99| getFunctionBody(): [BlockExpr] { ... }
# 99| getStmtList(): [StmtList] StmtList
# 99| getTailExpr(): [PathExpr] CONST_MyDeriveUnion
# 99| getTailExpr(): [ConstAccess] CONST_MyDeriveUnion
# 99| getPath(): [Path] CONST_MyDeriveUnion
# 99| getSegment(): [PathSegment] CONST_MyDeriveUnion
# 99| getIdentifier(): [NameRef] CONST_MyDeriveUnion
Expand Down
7 changes: 7 additions & 0 deletions rust/ql/test/library-tests/const_access/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
testFailures
constAccess
| main.rs:17:13:17:24 | GLOBAL_CONST | main.rs:1:1:1:29 | Const |
| main.rs:19:13:19:24 | STRING_CONST | main.rs:2:1:2:35 | Const |
| main.rs:21:13:21:33 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const |
| main.rs:23:13:23:35 | ...::MODULE_CONST | main.rs:13:5:13:38 | Const |
| main.rs:25:8:25:19 | GLOBAL_CONST | main.rs:1:1:1:29 | Const |
| main.rs:29:16:29:36 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const |
21 changes: 21 additions & 0 deletions rust/ql/test/library-tests/const_access/const_access.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import rust
import utils.test.InlineExpectationsTest
import TestUtils

query predicate constAccess(ConstAccess ca, Const c) { toBeTested(ca) and c = ca.getConst() }

module ConstAccessTest implements TestSig {
string getARelevantTag() { result = "const_access" }

predicate hasActualResult(Location location, string element, string tag, string value) {
exists(ConstAccess ca |
toBeTested(ca) and
location = ca.getLocation() and
element = ca.toString() and
tag = "const_access" and
value = ca.getConst().getName().getText()
)
}
}

import MakeTest<ConstAccessTest>
34 changes: 34 additions & 0 deletions rust/ql/test/library-tests/const_access/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const GLOBAL_CONST: i32 = 42;
const STRING_CONST: &str = "hello";

struct MyStruct {
value: i32,
}

impl MyStruct {
const ASSOC_CONST: i32 = 100;
}

mod my_module {
pub const MODULE_CONST: i32 = 200;
}

fn use_consts() {
let x = GLOBAL_CONST; // $ const_access=GLOBAL_CONST

let s = STRING_CONST; // $ const_access=STRING_CONST

let y = MyStruct::ASSOC_CONST; // $ const_access=ASSOC_CONST

let z = my_module::MODULE_CONST; // $ const_access=MODULE_CONST

if GLOBAL_CONST > 0 { // $ const_access=GLOBAL_CONST
println!("positive");
}

let arr = [MyStruct::ASSOC_CONST; 5]; // $ const_access=ASSOC_CONST
}

fn main() {
use_consts();
}