Skip to content

Commit f8cde74

Browse files
authored
Merge pull request #20736 from github/copilot/add-constaccess-class
Rust: Add ConstAccess class with getConst() predicate
2 parents 5eac0f7 + 0ca62ca commit f8cde74

File tree

10 files changed

+105
-4
lines changed

10 files changed

+105
-4
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* This module provides the public class `ConstAccess`.
3+
*/
4+
5+
private import internal.ConstImpl
6+
7+
final class ConstAccess = Impl::ConstAccess;

rust/ql/lib/codeql/rust/elements/internal/ConstImpl.qll

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `Const`.
43
*
54
* INTERNAL: Do not use.
65
*/
76

87
private import codeql.rust.elements.internal.generated.Const
8+
private import codeql.rust.elements.internal.PathExprImpl::Impl as PathExprImpl
9+
private import codeql.rust.internal.PathResolution
910

1011
/**
1112
* INTERNAL: This module contains the customizable definition of `Const` and should not
1213
* be referenced directly.
1314
*/
1415
module Impl {
16+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1517
/**
1618
* A constant item declaration.
1719
*
@@ -21,4 +23,27 @@ module Impl {
2123
* ```
2224
*/
2325
class Const extends Generated::Const { }
26+
27+
/**
28+
* A constant access.
29+
*
30+
* For example:
31+
* ```rust
32+
* const X: i32 = 42;
33+
*
34+
* fn main() {
35+
* println!("{}", X);
36+
* }
37+
* ```
38+
*/
39+
class ConstAccess extends PathExprImpl::PathExpr {
40+
private Const c;
41+
42+
ConstAccess() { c = resolvePath(this.getPath()) }
43+
44+
/** Gets the constant being accessed. */
45+
Const getConst() { result = c }
46+
47+
override string getAPrimaryQlClass() { result = "ConstAccess" }
48+
}
2449
}

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import codeql.rust.elements.ArithmeticOperation
99
import codeql.rust.elements.AssignmentOperation
1010
import codeql.rust.elements.BitwiseOperation
1111
import codeql.rust.elements.ComparisonOperation
12+
import codeql.rust.elements.ConstAccess
1213
import codeql.rust.elements.DerefExpr
1314
import codeql.rust.elements.LiteralExprExt
1415
import codeql.rust.elements.LogicalOperation

rust/ql/test/extractor-tests/macro-expansion/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ macro_expansion.rs:
10431043
# 98| getParamList(): [ParamList] ParamList
10441044
# 99| getFunctionBody(): [BlockExpr] { ... }
10451045
# 99| getStmtList(): [StmtList] StmtList
1046-
# 99| getTailExpr(): [PathExpr] CONST_MyDeriveUnion
1046+
# 99| getTailExpr(): [ConstAccess] CONST_MyDeriveUnion
10471047
# 99| getPath(): [Path] CONST_MyDeriveUnion
10481048
# 99| getSegment(): [PathSegment] CONST_MyDeriveUnion
10491049
# 99| getIdentifier(): [NameRef] CONST_MyDeriveUnion

rust/ql/test/library-tests/const_access/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
testFailures
2+
constAccess
3+
| main.rs:17:13:17:24 | GLOBAL_CONST | main.rs:1:1:1:29 | Const |
4+
| main.rs:19:13:19:24 | STRING_CONST | main.rs:2:1:2:35 | Const |
5+
| main.rs:21:13:21:33 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const |
6+
| main.rs:23:13:23:35 | ...::MODULE_CONST | main.rs:13:5:13:38 | Const |
7+
| main.rs:25:8:25:19 | GLOBAL_CONST | main.rs:1:1:1:29 | Const |
8+
| main.rs:29:16:29:36 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import rust
2+
import utils.test.InlineExpectationsTest
3+
import TestUtils
4+
5+
query predicate constAccess(ConstAccess ca, Const c) { toBeTested(ca) and c = ca.getConst() }
6+
7+
module ConstAccessTest implements TestSig {
8+
string getARelevantTag() { result = "const_access" }
9+
10+
predicate hasActualResult(Location location, string element, string tag, string value) {
11+
exists(ConstAccess ca |
12+
toBeTested(ca) and
13+
location = ca.getLocation() and
14+
element = ca.toString() and
15+
tag = "const_access" and
16+
value = ca.getConst().getName().getText()
17+
)
18+
}
19+
}
20+
21+
import MakeTest<ConstAccessTest>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const GLOBAL_CONST: i32 = 42;
2+
const STRING_CONST: &str = "hello";
3+
4+
struct MyStruct {
5+
value: i32,
6+
}
7+
8+
impl MyStruct {
9+
const ASSOC_CONST: i32 = 100;
10+
}
11+
12+
mod my_module {
13+
pub const MODULE_CONST: i32 = 200;
14+
}
15+
16+
fn use_consts() {
17+
let x = GLOBAL_CONST; // $ const_access=GLOBAL_CONST
18+
19+
let s = STRING_CONST; // $ const_access=STRING_CONST
20+
21+
let y = MyStruct::ASSOC_CONST; // $ const_access=ASSOC_CONST
22+
23+
let z = my_module::MODULE_CONST; // $ const_access=MODULE_CONST
24+
25+
if GLOBAL_CONST > 0 { // $ const_access=GLOBAL_CONST
26+
println!("positive");
27+
}
28+
29+
let arr = [MyStruct::ASSOC_CONST; 5]; // $ const_access=ASSOC_CONST
30+
}
31+
32+
fn main() {
33+
use_consts();
34+
}

0 commit comments

Comments
 (0)