Skip to content

Commit dd031f5

Browse files
authored
Merge pull request #23104 from github/unified/modified-expr
Unified: Support Swift `borrow`, `consume`, `copy`, and `unsafe` expressions
2 parents bb75004 + 16f6306 commit dd031f5

14 files changed

Lines changed: 320 additions & 8 deletions

File tree

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,18 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
12131213
rule!((awaitExpr awaitKeyword: @@keyword expression: @val) => (unary_expr
12141214
operator: (prefix_operator "await" location: {keyword})
12151215
operand: {val})),
1216+
rule!((borrowExpr borrowKeyword: @@keyword expression: @val) => (unary_expr
1217+
operator: (prefix_operator #{keyword})
1218+
operand: {val})),
1219+
rule!((consumeExpr consumeKeyword: @@keyword expression: @val) => (unary_expr
1220+
operator: (prefix_operator #{keyword})
1221+
operand: {val})),
1222+
rule!((copyExpr copyKeyword: @@keyword expression: @val) => (unary_expr
1223+
operator: (prefix_operator #{keyword})
1224+
operand: {val})),
1225+
rule!((unsafeExpr unsafeKeyword: @@keyword expression: @val) => (unary_expr
1226+
operator: (prefix_operator #{keyword})
1227+
operand: {val})),
12161228
// Force-unwrap (`x!`) → postfix unary_expr, via swift-syntax's dedicated
12171229
// `forceUnwrapExpr` node.
12181230
rule!((forceUnwrapExpr expression: @e exclamationMark: @@mark) => (unary_expr
@@ -1613,11 +1625,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
16131625
// these roots is discarded with its parent and needs no separate rule.
16141626
// Explicit empty content preserves their existing representation
16151627
// instead of inheriting the matched source text.
1616-
rule!((borrowExpr) => (unsupported_node "")),
16171628
rule!((classRestrictionType) => (unsupported_node "")),
16181629
rule!((compositionType) => (unsupported_node "")),
1619-
rule!((consumeExpr) => (unsupported_node "")),
1620-
rule!((copyExpr) => (unsupported_node "")),
16211630
rule!((discardStmt) => (unsupported_node "")),
16221631
rule!((fallThroughStmt) => (unsupported_node "")),
16231632
rule!((implicitlyUnwrappedOptionalType) => (unsupported_node "")),
@@ -1637,7 +1646,6 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
16371646
rule!((subscriptDecl) => (unsupported_node "")),
16381647
rule!((suppressedType) => (unsupported_node "")),
16391648
rule!((typeExpr) => (unsupported_node "")),
1640-
rule!((unsafeExpr) => (unsupported_node "")),
16411649
rule!((yieldStmt) => (unsupported_node "")),
16421650
// Anything reaching this final generic handler has neither a
16431651
// dedicated rule nor an explicit unsupported entry.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
let original = [1, 2, 3]
2+
let borrowed = borrow original
3+
4+
---
5+
6+
sourceFile
7+
endOfFileToken: endOfFile
8+
statements:
9+
codeBlockItem
10+
item:
11+
variableDecl
12+
attributes:
13+
modifiers:
14+
bindingSpecifier: let
15+
bindings:
16+
patternBinding
17+
initializer:
18+
initializerClause
19+
equal: =
20+
value:
21+
arrayExpr
22+
elements:
23+
arrayElement
24+
expression:
25+
integerLiteralExpr
26+
literal: integerLiteral "1"
27+
trailingComma: ,
28+
arrayElement
29+
expression:
30+
integerLiteralExpr
31+
literal: integerLiteral "2"
32+
trailingComma: ,
33+
arrayElement
34+
expression:
35+
integerLiteralExpr
36+
literal: integerLiteral "3"
37+
leftSquare: [
38+
rightSquare: ]
39+
pattern:
40+
identifierPattern
41+
identifier: identifier "original"
42+
codeBlockItem
43+
item:
44+
variableDecl
45+
attributes:
46+
modifiers:
47+
bindingSpecifier: let
48+
bindings:
49+
patternBinding
50+
initializer:
51+
initializerClause
52+
equal: =
53+
value:
54+
borrowExpr
55+
expression:
56+
declReferenceExpr
57+
baseName: identifier "original"
58+
borrowKeyword: borrow
59+
pattern:
60+
identifierPattern
61+
identifier: identifier "borrowed"
62+
63+
---
64+
65+
top_level source="⟨body⟩"
66+
body:
67+
block source="⟨stmt⟩\n⟨stmt⟩"
68+
stmt:
69+
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
70+
modifier: modifier "let" source="let"
71+
pattern: identifier "original" source="original"
72+
value:
73+
array_literal source="[⟨element⟩, ⟨element⟩, ⟨element⟩]"
74+
element:
75+
int_literal "1" source="1"
76+
int_literal "2" source="2"
77+
int_literal "3" source="3"
78+
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
79+
modifier: modifier "let" source="let"
80+
pattern: identifier "borrowed" source="borrowed"
81+
value:
82+
unary_expr source="⟨operator⟩ ⟨operand⟩"
83+
operand: identifier "original" source="original"
84+
operator: prefix_operator "borrow" source="borrow"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let original = [1, 2, 3]
2+
let borrowed = borrow original

‎unified/extractor/tests/corpus/swift/expressions/consume-expression.output‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@ top_level source="⟨body⟩"
7878
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
7979
modifier: modifier "let" source="let"
8080
pattern: identifier "consumed" source="consumed"
81-
value: unsupported_node "consume original" source="consume original"
81+
value:
82+
unary_expr source="⟨operator⟩ ⟨operand⟩"
83+
operand: identifier "original" source="original"
84+
operator: prefix_operator "consume" source="consume"

‎unified/extractor/tests/corpus/swift/expressions/copy-expression.output‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@ top_level source="⟨body⟩"
7878
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
7979
modifier: modifier "let" source="let"
8080
pattern: identifier "copied" source="copied"
81-
value: unsupported_node "copy original" source="copy original"
81+
value:
82+
unary_expr source="⟨operator⟩ ⟨operand⟩"
83+
operand: identifier "original" source="original"
84+
operator: prefix_operator "copy" source="copy"

‎unified/extractor/tests/corpus/swift/expressions/unsafe-expression.output‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ top_level source="⟨body⟩"
4848
function_declaration source="func ⟨name_node⟩() ⟨body⟩"
4949
name_node: identifier "doWork" source="doWork"
5050
body: block "{}" source="{}"
51-
unsupported_node "unsafe doWork()" source="unsafe doWork()"
51+
unary_expr source="⟨operator⟩ ⟨operand⟩"
52+
operand:
53+
call_expr source="⟨callee⟩()"
54+
callee: identifier "doWork" source="doWork"
55+
operator: prefix_operator "unsafe" source="unsafe"

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowPluginSwift.qll‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ private class SwiftDataFlowPlugin extends DataFlowPlugin {
7171
)
7272
or
7373
exists(UnaryExpr expr |
74-
expr.getOperator().(PrefixOperator).getValue() = ["try", "try!", "try?", "await"] and
74+
expr.getOperator().(PrefixOperator).getValue() =
75+
["try", "try!", "try?", "await", "borrow", "consume", "copy", "unsafe"] and
7576
node1.isResultValue(expr.getOperand()) and
7677
step.value() and
7778
node2.isResultValue(expr)

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ module Input3 implements InputSig3 {
640640
}
641641

642642
predicate stepLanguageSpecific(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
643+
Plugin::stepLanguageSpecific(n1, prefix1, n2, prefix2)
644+
or
643645
n1 = n2.(Block).getLastStmt() and
644646
prefix1.isEmpty() and
645647
prefix2.isEmpty()

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInferencePlugin.qll‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ class TypeInferencePlugin extends Unit {
8181
/** Holds if `field` is a field of the tuple type `tuple` with the given `name`. */
8282
abstract predicate tupleField(Type tuple, string name, VariableDeclaration field);
8383

84+
/**
85+
* Holds if a type at `prefix1.suffix` on `n1` implies the same type at
86+
* `prefix2.suffix` on `n2`. The reverse step is used for contextual inference
87+
* when `n1` already has an inferred type matching `prefix1`.
88+
*/
89+
predicate stepLanguageSpecific(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
90+
none()
91+
}
92+
8493
/** Gets the inferred type for the given AST node and type path. */
8594
Type inferType(AstNode n, TypePath path) { none() }
8695
}
@@ -131,6 +140,10 @@ class TupleType extends Type {
131140
}
132141
}
133142

143+
predicate stepLanguageSpecific(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
144+
any(TypeInferencePlugin p).stepLanguageSpecific(n1, prefix1, n2, prefix2)
145+
}
146+
134147
Type inferType(AstNode n, TypePath path) { result = any(TypeInferencePlugin p).inferType(n, path) }
135148

136149
Callable getFunctionInvoke(Type t) { result = any(TypeInferencePlugin p).getFunctionInvoke(t) }

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInferencePluginSwift.qll‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ private class SwiftTypeInferencePlugin extends Plugin::TypeInferencePlugin {
144144
field = tuple.(TupleType).getField(name)
145145
}
146146

147+
override predicate stepLanguageSpecific(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
148+
n2 =
149+
any(UnaryExpr expr |
150+
n1 = expr.getOperand() and
151+
expr.getOperator().(PrefixOperator).getValue() = ["borrow", "consume", "copy", "unsafe"] and
152+
prefix1.isEmpty() and
153+
prefix2.isEmpty()
154+
)
155+
}
156+
147157
override Type inferType(AstNode n, TypePath path) {
148158
// todo: approximation for now
149159
n instanceof BinaryExpr and

0 commit comments

Comments
 (0)