Skip to content

Commit 8436a8b

Browse files
authored
Merge pull request #22785 from github/unified/swift-extractions-improvements
Unified: Handle `additionalTrailingClosures` for more expression kinds
2 parents e8c7105 + a5d5a4f commit 8436a8b

9 files changed

Lines changed: 384 additions & 16 deletions

File tree

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
671671
calledExpression: (arrayExpr
672672
elements: (arrayElement expression: (genericSpecializationExpr) @element)) @@array
673673
arguments: _* @args
674-
trailingClosure: @tc)
674+
trailingClosure: @tc
675+
additionalTrailingClosures: _* @additional)
675676
=>
676677
call_expr {
677678
let callee = tree_at!(
@@ -683,7 +684,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
683684
tree!((call_expr
684685
callee: {callee}
685686
argument: {args}
686-
argument: (argument value: {tc})))
687+
argument: (argument value: {tc})
688+
argument: {additional}))
687689
}
688690
),
689691
rule!(
@@ -704,23 +706,28 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
704706
),
705707
// A function/method call (`foo(1, 2)`). `calledExpression` is the callee
706708
// and `arguments` is an (elided) list of `labeledExpr`, each translated
707-
// to an `argument` below. A trailing closure (`xs.map { … }`) becomes a
708-
// final unlabelled argument; that variant is matched first.
709-
rule!(
710-
(functionCallExpr calledExpression: @callee arguments: _* @args trailingClosure: @tc)
711-
=>
712-
(call_expr callee: {callee} argument: {args} argument: (argument value: {tc}))
713-
),
709+
// to an `argument` below. A trailing closure (`xs.map { … }`) becomes
710+
// a final unlabelled argument, followed by any additional labeled
711+
// trailing closures.
714712
rule!(
715-
(functionCallExpr calledExpression: @@rawCallee arguments: _* @args)
713+
(functionCallExpr
714+
calledExpression: @@rawCallee
715+
arguments: _* @args
716+
trailingClosure: _? @trailing
717+
additionalTrailingClosures: _* @additional)
716718
=>
717719
expr {
718720
// Always translate the callee in non-pattern context.
719721
let callee = ctx.scoped(|ctx| {
720722
ctx.in_pattern = false;
721723
ctx.translate(rawCallee)
722724
})?;
723-
tree!((call_expr callee: {callee} argument: {args}))
725+
tree!((call_expr
726+
callee: {callee}
727+
argument: {args}
728+
argument: {trailing.map(|trailing| tree!((argument value: {trailing})))}
729+
argument: {additional}
730+
))
724731
}
725732
),
726733
// A call or enum-case pattern argument.
@@ -1019,9 +1026,17 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
10191026
// subscripts their own shape needs only a `subscript_expr` node in
10201027
// ast_types.yml and a remap here.
10211028
rule!(
1022-
(subscriptCallExpr calledExpression: @callee arguments: _* @args)
1029+
(subscriptCallExpr
1030+
calledExpression: @callee
1031+
arguments: _* @args
1032+
trailingClosure: _? @trailing
1033+
additionalTrailingClosures: _* @additional)
10231034
=>
1024-
(call_expr callee: {callee} argument: {args})
1035+
(call_expr
1036+
callee: {callee}
1037+
argument: {args}
1038+
argument: {trailing.map(|trailing| tree!((argument value: {trailing})))}
1039+
argument: {additional})
10251040
),
10261041
// ---- Optionals and errors ----
10271042
// Optional chaining — unwrap the marker

‎unified/extractor/tests/corpus/swift/control-flow/nested-enum-case-pattern.output‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ case let .received(.some(value), timestamp):
33
print(value, timestamp)
44
case Type.some(let value):
55
print(value)
6+
case values?.map { $0 * 2 }:
7+
break
68
default:
79
break
810
}
@@ -138,6 +140,53 @@ sourceFile
138140
calledExpression:
139141
declReferenceExpr
140142
baseName: identifier "print"
143+
switchCase
144+
label:
145+
switchCaseLabel
146+
colon: :
147+
caseKeyword: case
148+
caseItems:
149+
switchCaseItem
150+
pattern:
151+
expressionPattern
152+
expression:
153+
functionCallExpr
154+
arguments:
155+
additionalTrailingClosures:
156+
calledExpression:
157+
memberAccessExpr
158+
period: .
159+
declName:
160+
declReferenceExpr
161+
baseName: identifier "map"
162+
base:
163+
optionalChainingExpr
164+
expression:
165+
declReferenceExpr
166+
baseName: identifier "values"
167+
questionMark: ?
168+
trailingClosure:
169+
closureExpr
170+
leftBrace: {
171+
rightBrace: }
172+
statements:
173+
codeBlockItem
174+
item:
175+
infixOperatorExpr
176+
operator:
177+
binaryOperatorExpr
178+
operator: binaryOperator "*"
179+
leftOperand:
180+
declReferenceExpr
181+
baseName: dollarIdentifier "$0"
182+
rightOperand:
183+
integerLiteralExpr
184+
literal: integerLiteral "2"
185+
statements:
186+
codeBlockItem
187+
item:
188+
breakStmt
189+
breakKeyword: break
141190
switchCase
142191
label:
143192
switchDefaultLabel
@@ -159,7 +208,7 @@ top_level source="⟨body⟩"
159208
body:
160209
block source="⟨stmt⟩"
161210
stmt:
162-
switch_expr source="switch ⟨value⟩ {\n⟨case⟩\n⟨case⟩\n⟨case⟩\n}"
211+
switch_expr source="switch ⟨value⟩ {\n⟨case⟩\n⟨case⟩\n⟨case⟩\n⟨case⟩\n}"
163212
value: identifier "event" source="event"
164213
case:
165214
switch_case source="case ⟨pattern⟩:\n ⟨body⟩"
@@ -216,6 +265,27 @@ top_level source="⟨body⟩"
216265
argument:
217266
argument source="⟨value⟩"
218267
value: identifier "value" source="value"
268+
switch_case source="case ⟨pattern⟩:\n ⟨body⟩"
269+
pattern:
270+
call_expr source="⟨callee⟩ ⟨argument⟩"
271+
callee:
272+
member_access_expr source="⟨base⟩?.⟨member_name_node⟩"
273+
base: identifier "values" source="values"
274+
member_name_node: identifier "map" source="map"
275+
argument:
276+
argument source="⟨value⟩"
277+
value:
278+
function_expr source="{ ⟨body⟩ }"
279+
body:
280+
block source="⟨stmt⟩"
281+
stmt:
282+
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
283+
left: identifier "$0" source="$0"
284+
operator: infix_operator "*" source="*"
285+
right: int_literal "2" source="2"
286+
body:
287+
block source="⟨stmt⟩"
288+
stmt: break_expr "break" source="break"
219289
switch_case source="default:\n ⟨body⟩"
220290
body:
221291
block source="⟨stmt⟩"

‎unified/extractor/tests/corpus/swift/control-flow/nested-enum-case-pattern.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ case let .received(.some(value), timestamp):
33
print(value, timestamp)
44
case Type.some(let value):
55
print(value)
6+
case values?.map { $0 * 2 }:
7+
break
68
default:
79
break
810
}

‎unified/extractor/tests/corpus/swift/expressions/array-type-constructor.output‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
let values = [Result<Void>]()
22
let initialized = [Result<Void>](unsafeUninitializedCapacity: 1) { _, count in
33
count = 0
4+
} completion: {
5+
done()
46
}
57

68
---
@@ -71,6 +73,24 @@ sourceFile
7173
integerLiteralExpr
7274
literal: integerLiteral "1"
7375
additionalTrailingClosures:
76+
multipleTrailingClosureElement
77+
colon: :
78+
label: identifier "completion"
79+
closure:
80+
closureExpr
81+
leftBrace: {
82+
rightBrace: }
83+
statements:
84+
codeBlockItem
85+
item:
86+
functionCallExpr
87+
leftParen: (
88+
rightParen: )
89+
arguments:
90+
additionalTrailingClosures:
91+
calledExpression:
92+
declReferenceExpr
93+
baseName: identifier "done"
7494
calledExpression:
7595
arrayExpr
7696
elements:
@@ -144,7 +164,7 @@ top_level source="⟨body⟩"
144164
modifier: modifier "let" source="let"
145165
pattern: identifier "initialized" source="initialized"
146166
value:
147-
call_expr source="⟨callee⟩(⟨argument⟩) ⟨argument⟩"
167+
call_expr source="⟨callee⟩(⟨argument⟩) ⟨argument⟩ ⟨argument⟩"
148168
callee:
149169
generic_type_expr source="[⟨type_argument⟩]"
150170
base: identifier "Array" source=""
@@ -171,3 +191,12 @@ top_level source="⟨body⟩"
171191
left: identifier "count" source="count"
172192
operator: infix_operator "=" source="="
173193
right: int_literal "0" source="0"
194+
argument source="⟨name_node⟩: ⟨value⟩"
195+
name_node: identifier "completion" source="completion"
196+
value:
197+
function_expr source="{\n\t⟨body⟩\n}"
198+
body:
199+
block source="⟨stmt⟩"
200+
stmt:
201+
call_expr source="⟨callee⟩()"
202+
callee: identifier "done" source="done"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
let values = [Result<Void>]()
22
let initialized = [Result<Void>](unsafeUninitializedCapacity: 1) { _, count in
33
count = 0
4+
} completion: {
5+
done()
46
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
handlers[0]
2+
3+
handlers[0] {
4+
first()
5+
} completion: {
6+
second()
7+
} failure: {
8+
third()
9+
}
10+
11+
---
12+
13+
sourceFile
14+
endOfFileToken: endOfFile
15+
statements:
16+
codeBlockItem
17+
item:
18+
subscriptCallExpr
19+
leftSquare: [
20+
rightSquare: ]
21+
arguments:
22+
labeledExpr
23+
expression:
24+
integerLiteralExpr
25+
literal: integerLiteral "0"
26+
additionalTrailingClosures:
27+
calledExpression:
28+
declReferenceExpr
29+
baseName: identifier "handlers"
30+
codeBlockItem
31+
item:
32+
subscriptCallExpr
33+
leftSquare: [
34+
rightSquare: ]
35+
arguments:
36+
labeledExpr
37+
expression:
38+
integerLiteralExpr
39+
literal: integerLiteral "0"
40+
additionalTrailingClosures:
41+
multipleTrailingClosureElement
42+
colon: :
43+
label: identifier "completion"
44+
closure:
45+
closureExpr
46+
leftBrace: {
47+
rightBrace: }
48+
statements:
49+
codeBlockItem
50+
item:
51+
functionCallExpr
52+
leftParen: (
53+
rightParen: )
54+
arguments:
55+
additionalTrailingClosures:
56+
calledExpression:
57+
declReferenceExpr
58+
baseName: identifier "second"
59+
multipleTrailingClosureElement
60+
colon: :
61+
label: identifier "failure"
62+
closure:
63+
closureExpr
64+
leftBrace: {
65+
rightBrace: }
66+
statements:
67+
codeBlockItem
68+
item:
69+
functionCallExpr
70+
leftParen: (
71+
rightParen: )
72+
arguments:
73+
additionalTrailingClosures:
74+
calledExpression:
75+
declReferenceExpr
76+
baseName: identifier "third"
77+
calledExpression:
78+
declReferenceExpr
79+
baseName: identifier "handlers"
80+
trailingClosure:
81+
closureExpr
82+
leftBrace: {
83+
rightBrace: }
84+
statements:
85+
codeBlockItem
86+
item:
87+
functionCallExpr
88+
leftParen: (
89+
rightParen: )
90+
arguments:
91+
additionalTrailingClosures:
92+
calledExpression:
93+
declReferenceExpr
94+
baseName: identifier "first"
95+
96+
---
97+
98+
top_level source="⟨body⟩"
99+
body:
100+
block source="⟨stmt⟩\n\n⟨stmt⟩"
101+
stmt:
102+
call_expr source="⟨callee⟩[⟨argument⟩]"
103+
callee: identifier "handlers" source="handlers"
104+
argument:
105+
argument source="⟨value⟩"
106+
value: int_literal "0" source="0"
107+
call_expr source="⟨callee⟩[⟨argument⟩] ⟨argument⟩ ⟨argument⟩ ⟨argument⟩"
108+
callee: identifier "handlers" source="handlers"
109+
argument:
110+
argument source="⟨value⟩"
111+
value: int_literal "0" source="0"
112+
argument source="⟨value⟩"
113+
value:
114+
function_expr source="{\n ⟨body⟩\n}"
115+
body:
116+
block source="⟨stmt⟩"
117+
stmt:
118+
call_expr source="⟨callee⟩()"
119+
callee: identifier "first" source="first"
120+
argument source="⟨name_node⟩: ⟨value⟩"
121+
name_node: identifier "completion" source="completion"
122+
value:
123+
function_expr source="{\n ⟨body⟩\n}"
124+
body:
125+
block source="⟨stmt⟩"
126+
stmt:
127+
call_expr source="⟨callee⟩()"
128+
callee: identifier "second" source="second"
129+
argument source="⟨name_node⟩: ⟨value⟩"
130+
name_node: identifier "failure" source="failure"
131+
value:
132+
function_expr source="{\n ⟨body⟩\n}"
133+
body:
134+
block source="⟨stmt⟩"
135+
stmt:
136+
call_expr source="⟨callee⟩()"
137+
callee: identifier "third" source="third"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
handlers[0]
2+
3+
handlers[0] {
4+
first()
5+
} completion: {
6+
second()
7+
} failure: {
8+
third()
9+
}

0 commit comments

Comments
 (0)