Skip to content

Commit b0087fc

Browse files
committed
unified: Fix translation of tuple expressions
1 parent e452286 commit b0087fc

5 files changed

Lines changed: 29 additions & 11 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
297297
rule!((sequenceExpr elements: _* @els) => (unresolved_operator_sequence element: {els})),
298298
// Prefix unary operators (`!a`, `-x`).
299299
rule!((prefixOperatorExpr operator: @op expression: @operand) => (unary_expr operator: (prefix_operator #{op}) operand: {operand})),
300-
// A `tupleExpr` is a tuple literal (`(a, b)`) or a parenthesised
301-
// expression (`(x)`). For now it is kept as an opaque `tuple_expr` leaf
302-
// (its source text); its elements are not descended into.
303-
//
304-
// TODO: a parenthesised single-element `tupleExpr` is really a grouping
305-
// expression and should be elided (unwrapped to its inner expression)
306-
// rather than modelled as a tuple.
307-
rule!((tupleExpr) => (tuple_expr)),
300+
// A parenthesised expression has a single tuple element; elide the
301+
// grouping and preserve the expression itself. Actual tuple literals
302+
// retain their translated labeled elements as `argument` children.
303+
rule!((tupleExpr
304+
elements: (labeledExpr label: _? @@lbl expression: @element)
305+
elements: _* @@rest)
306+
where rest.is_empty() && lbl.is_none() => expr { element }),
307+
rule!((tupleExpr elements: _* @elements) => (tuple_expr element: {elements})),
308308
// A code block contains its statements directly.
309309
rule!((codeBlock statements: _* @stmts) => (block stmt: {stmts})),
310310
// ---- Properties with accessors ----

unified/extractor/tests/corpus/swift/collections/tuple-literal.output

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ top_level
5252
variable_declaration
5353
modifier: modifier "let"
5454
pattern: identifier "t"
55-
value: tuple_expr "(1, \"two\", 3.0)"
55+
value:
56+
tuple_expr
57+
element:
58+
argument
59+
value: int_literal "1"
60+
argument
61+
value: string_literal "\"two\""
62+
argument
63+
value: float_literal "3.0"

unified/extractor/tests/corpus/swift/control-flow/switch-expression-pattern.output

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,12 @@ top_level
333333
member_access_expr
334334
base: inferred_type_expr "."
335335
member_name_node: identifier "inferred"
336-
tuple_expr "(value, offset)"
336+
tuple_expr
337+
element:
338+
argument
339+
value: identifier "value"
340+
argument
341+
value: identifier "offset"
337342
array_literal
338343
element: identifier "value"
339344
map_literal "[key: value]"

unified/extractor/tests/corpus/swift/operators/parenthesised-expression.output

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ top_level
3939
block
4040
stmt:
4141
binary_expr
42-
left: tuple_expr "(a + b)"
42+
left:
43+
binary_expr
44+
left: identifier "a"
45+
operator: infix_operator "+"
46+
right: identifier "b"
4347
operator: infix_operator "*"
4448
right: identifier "c"

unified/ql/test/library-tests/BasicTest/test.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ identifier
129129
| test.swift:74:10:74:17 | isSorted | isSorted |
130130
| test.swift:74:24:74:27 | Bool | Bool |
131131
| test.swift:75:13:75:13 | i | i |
132+
| test.swift:75:23:75:27 | count | count |
132133
| test.swift:76:16:76:19 | self | self |
133134
| test.swift:76:21:76:21 | i | i |
134135
| test.swift:76:26:76:29 | self | self |

0 commit comments

Comments
 (0)