Skip to content

Commit 2e7da72

Browse files
authored
Merge pull request #21488 from paldepind/rust/tuple-constructor-self
Rust: Unify handling of struct and tuple constructors
2 parents 5b17d8c + f2a0724 commit 2e7da72

File tree

6 files changed

+159
-297
lines changed

6 files changed

+159
-297
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ module Impl {
99
// For the type `FunctionPosition` used by type inference, we work with function-call syntax
1010
// adjusted positions, so a call like `x.m(a, b, c)` needs positions `0` through `3`; for this
1111
// reason, there is no `- 1` after `max(...)` below.
12-
i in [0 .. max([any(ParamList l).getNumberOfParams(), any(ArgList l).getNumberOfArgs()])]
12+
i in [0 .. max([
13+
any(ParamList l).getNumberOfParams(),
14+
any(ArgList l).getNumberOfArgs(),
15+
any(StructFieldList l).getNumberOfFields() // Positions are used for struct expressions in type inference
16+
]
17+
)]
1318
} or
1419
TSelfArgumentPosition() or
1520
TTypeQualifierArgumentPosition()

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)