Skip to content

Commit a97ded0

Browse files
committed
unified: Desugar capture declaration list
1 parent ea1c52e commit a97ded0

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
795795
body: (block stmt: {body}))
796796
),
797797
// A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional
798-
// ownership specifier (`weak`/`unowned`) becomes a modifier; the
798+
// ownership specifier (`weak`/`unowned`) becomes a modifier and a unary_expr; the
799799
// captured name becomes the bound `name_node`; an explicit capture
800800
// initializer (`[y = expr]`) becomes the bound value.
801801
rule!(
@@ -807,7 +807,18 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
807807
(variable_declaration
808808
modifier: (modifier #{spec})?
809809
pattern: (identifier #{name})
810-
value: {val})
810+
value: {
811+
// Expand [x] into [x = x]
812+
let value = match val {
813+
Some(val) => val,
814+
None => tree!((identifier #{name})),
815+
};
816+
// Expand [weak x] into a unary_expr, to represent the boxing in Optional.same
817+
match spec {
818+
Some(spec) => tree!((unary_expr operator: (prefix_operator #{spec}) operand: {value})),
819+
None => value,
820+
}
821+
})
811822
),
812823
// A closure parameter clause (`(x: Int, y)`) unwraps to its parameters.
813824
rule!((closureParameterClause parameters: _* @params) => parameter* { params }),

‎unified/extractor/tests/corpus/swift/closures/closure-with-capture-list.output‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ top_level source="⟨body⟩"
7070
value:
7171
function_expr source="{ [⟨capture_declaration⟩] in ⟨body⟩ }"
7272
capture_declaration:
73-
variable_declaration source="⟨modifier⟩ ⟨pattern⟩"
73+
variable_declaration source="⟨modifier⟩⟨value⟩⟨pattern⟩"
7474
modifier: modifier "weak" source="weak"
7575
pattern: identifier "self" source="self"
76+
value:
77+
unary_expr source="⟨operator⟩ ⟨operand⟩"
78+
operand: identifier "self" source="self"
79+
operator: prefix_operator "weak" source="weak"
7680
body:
7781
block source="⟨stmt⟩"
7882
stmt:

0 commit comments

Comments
 (0)