Skip to content

Commit 97670b3

Browse files
committed
Rust: Unify handling of struct and tuple constructors
1 parent 8e19b05 commit 97670b3

File tree

5 files changed

+153
-296
lines changed

5 files changed

+153
-296
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@ module Impl {
5050
or
5151
result = this.getVariant().getStructField(name)
5252
}
53+
54+
/** Gets the `i`th struct field of the instantiated struct or variant. */
55+
StructField getNthStructField(int i) {
56+
result = [this.getStruct().getNthStructField(i), this.getVariant().getNthStructField(i)]
57+
}
5358
}
5459
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ module Impl {
3535
/** Gets a record field, if any. */
3636
StructField getAStructField() { result = this.getStructField(_) }
3737

38+
/** Gets the `i`th struct field, if any. */
39+
pragma[nomagic]
40+
StructField getNthStructField(int i) {
41+
result = this.getFieldList().(StructFieldList).getField(i)
42+
}
43+
3844
/** Gets the `i`th tuple field, if any. */
3945
pragma[nomagic]
4046
TupleField getTupleField(int i) { result = this.getFieldList().(TupleFieldList).getField(i) }

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ module Impl {
4242
)
4343
}
4444

45+
/** Gets the `i`th struct field of the instantiated struct or variant. */
46+
StructField getNthStructField(int i) {
47+
exists(PathResolution::ItemNode item | item = this.getResolvedPath(_) |
48+
result = [item.(Struct).getNthStructField(i), item.(Variant).getNthStructField(i)]
49+
)
50+
}
51+
4552
/** Gets the struct pattern for the field `name`. */
4653
pragma[nomagic]
4754
StructPatField getPatField(string name) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ module Impl {
3232
result.getName().getText() = name
3333
}
3434

35+
/** Gets the `i`th struct field, if any. */
36+
pragma[nomagic]
37+
StructField getNthStructField(int i) {
38+
result = this.getFieldList().(StructFieldList).getField(i)
39+
}
40+
3541
/** Gets the `i`th tuple field, if any. */
3642
pragma[nomagic]
3743
TupleField getTupleField(int i) { result = this.getFieldList().(TupleFieldList).getField(i) }

0 commit comments

Comments
 (0)