Skip to content

Commit 9f59b2c

Browse files
tausbnCopilot
andcommitted
Unified: Refine Swift block locations
Translate implicit accessor blocks through a dedicated rule and keep closure delimiters on function expressions rather than their body blocks. Also inline the synthetic enum constructor body. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 90e8424 commit 9f59b2c

13 files changed

Lines changed: 42 additions & 54 deletions

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ impl SwiftContext {
6262
}
6363
}
6464

65-
fn block_with_anchor(
66-
ctx: &mut yeast::build::BuildCtx<'_, SwiftContext>,
67-
statements: Vec<yeast::Id>,
68-
anchor: Option<yeast::Id>,
69-
) -> yeast::Id {
70-
match anchor {
71-
Some(anchor) => tree_at!(ctx, anchor, (block stmt: {statements})),
72-
None => tree!(ctx, (block stmt: {statements})),
73-
}
74-
}
75-
7665
/// Build a freshly-created `chained_declaration` modifier node if
7766
/// `ctx.is_chained`, else `None`. Used by inner declaration rules to
7867
/// emit the chained tag for non-first children of a flattening outer
@@ -301,6 +290,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
301290
rule!((tupleExpr elements: _* @elements) => (tuple_expr element: {elements})),
302291
// A code block contains its statements directly.
303292
rule!((codeBlock statements: _* @stmts) => (block stmt: {stmts})),
293+
rule!((accessorBlock accessors: (codeBlockItem)* @stmts) => (block stmt: {stmts})),
304294
// ---- Properties with accessors ----
305295
// A computed property with an implicit getter (`var a: T { <stmts> }`)
306296
// becomes a single `accessor_declaration` of kind `get`. This form is
@@ -313,14 +303,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
313303
bindings: (patternBinding
314304
pattern: (identifierPattern identifier: @@name)
315305
typeAnnotation: (typeAnnotation type: @ty)
316-
accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body) @@accessor_block))
306+
accessorBlock: (accessorBlock accessors: (codeBlockItem)+) @body))
317307
=>
318308
(accessor_declaration
319309
modifier: (modifier #{spec})
320310
name_node: (identifier #{name})
321311
type: {ty}
322-
accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", accessor_block)}
323-
body: {block_with_anchor(&mut ctx, body, Some(accessor_block))})
312+
accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", body)}
313+
body: {body})
324314
),
325315
// A property with an explicit accessor block. swift-syntax makes both
326316
// shapes plain `accessorDecl`s, so they are told apart by the presence
@@ -485,11 +475,10 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
485475
parameterClause: (enumCaseParameterClause parameters: _* @params) @@clause)
486476
=>
487477
class_like_declaration {
488-
let body = tree!((block));
489478
let constructor = tree_spanning!(
490479
ctx,
491480
[name, clause],
492-
(constructor_declaration parameter: {params} body: {body})
481+
(constructor_declaration parameter: {params} body: (block))
493482
);
494483
tree!((class_like_declaration
495484
modifier: {ctx.outer_modifiers.clone()}
@@ -803,14 +792,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
803792
capture: (closureCaptureClause items: _* @captures)?
804793
parameterClause: _* @params
805794
returnClause: (returnClause type: @ret)?)?
806-
statements: _* @body) @@closure
795+
statements: _* @body)
807796
=>
808797
(function_expr
809798
modifier: {attrs}
810799
capture_declaration: {captures}
811800
parameter: {params}
812801
return_type: {ret}
813-
body: {block_with_anchor(&mut ctx, body, Some(closure))})
802+
body: (block stmt: {body}))
814803
),
815804
// A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional
816805
// ownership specifier (`weak`/`unowned`) becomes a modifier; the

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ top_level source="⟨body⟩"
6868
modifier: modifier "let" source="let"
6969
pattern: identifier "f" source="f"
7070
value:
71-
function_expr source="⟨body⟩⟨capture_declaration⟩"
71+
function_expr source="{ [⟨capture_declaration⟩] in ⟨body⟩ }"
7272
capture_declaration:
7373
variable_declaration source="⟨modifier⟩ ⟨pattern⟩"
7474
modifier: modifier "weak" source="weak"
7575
pattern: identifier "self" source="self"
7676
body:
77-
block source="{ [weak self] in ⟨stmt⟩ }"
77+
block source="⟨stmt⟩"
7878
stmt:
7979
call_expr source="⟨callee⟩()"
8080
callee:

‎unified/extractor/tests/corpus/swift/closures/closure-with-explicit-parameters.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ top_level source="⟨body⟩"
7070
modifier: modifier "let" source="let"
7171
pattern: identifier "f" source="f"
7272
value:
73-
function_expr source="⟨body⟩⟨parameter⟩⟨return_type⟩"
73+
function_expr source="{ (⟨parameter⟩) -> ⟨return_type⟩ in ⟨body⟩ }"
7474
parameter:
7575
parameter source="⟨pattern⟩: ⟨type⟩"
7676
type: identifier "Int" source="Int"
7777
pattern: identifier "x" source="x"
7878
return_type: identifier "Int" source="Int"
7979
body:
80-
block source="{ (x: Int) -> Int in ⟨stmt⟩ }"
80+
block source="⟨stmt⟩"
8181
stmt:
8282
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
8383
left: identifier "x" source="x"

‎unified/extractor/tests/corpus/swift/closures/closure-with-shorthand-parameters.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ top_level source="⟨body⟩"
4747
modifier: modifier "let" source="let"
4848
pattern: identifier "f" source="f"
4949
value:
50-
function_expr source="⟨body⟩"
50+
function_expr source="{ ⟨body⟩ }"
5151
body:
52-
block source="{ ⟨stmt⟩ }"
52+
block source="⟨stmt⟩"
5353
stmt:
5454
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
5555
left: identifier "$0" source="$0"

‎unified/extractor/tests/corpus/swift/closures/multi-statement-closure.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ top_level source="⟨body⟩"
101101
modifier: modifier "let" source="let"
102102
pattern: identifier "f" source="f"
103103
value:
104-
function_expr source="⟨body⟩⟨parameter⟩⟨return_type⟩"
104+
function_expr source="{ (⟨parameter⟩) -> ⟨return_type⟩ in\n ⟨body⟩\n}"
105105
parameter:
106106
parameter source="⟨pattern⟩: ⟨type⟩"
107107
type: identifier "Int" source="Int"
108108
pattern: identifier "x" source="x"
109109
return_type: identifier "Int" source="Int"
110110
body:
111-
block source="{ (x: Int) -> Int in\n ⟨stmt⟩\n ⟨stmt⟩\n}"
111+
block source="⟨stmt⟩\n ⟨stmt⟩"
112112
stmt:
113113
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
114114
modifier: modifier "let" source="let"

‎unified/extractor/tests/corpus/swift/closures/trailing-closure.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ top_level source="⟨body⟩"
5151
argument:
5252
argument source="⟨value⟩"
5353
value:
54-
function_expr source="⟨body⟩"
54+
function_expr source="{ ⟨body⟩ }"
5555
body:
56-
block source="{ ⟨stmt⟩ }"
56+
block source="⟨stmt⟩"
5757
stmt:
5858
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
5959
left: identifier "$0" source="$0"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ top_level source="⟨body⟩"
158158
value: int_literal "1" source="1"
159159
argument source="⟨value⟩"
160160
value:
161-
function_expr source="⟨body⟩⟨parameter⟩⟨parameter⟩"
161+
function_expr source="{ ⟨parameter⟩, ⟨parameter⟩ in\n\t⟨body⟩\n}"
162162
parameter:
163163
parameter source="⟨pattern⟩"
164164
pattern: identifier "_" source="_"
165165
parameter source="⟨pattern⟩"
166166
pattern: identifier "count" source="count"
167167
body:
168-
block source="{ _, count in\n\t⟨stmt⟩\n}"
168+
block source="⟨stmt⟩"
169169
stmt:
170170
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
171171
left: identifier "count" source="count"

‎unified/extractor/tests/corpus/swift/types/function-type-with-convention-attribute.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ top_level source="⟨body⟩"
6767
pattern: identifier "callback" source="callback"
6868
type: unsupported_node "@convention(c) () -> Void" source="@convention(c) () -> Void"
6969
value:
70-
function_expr source="⟨body⟩"
71-
body: block "{}" source="{}"
70+
function_expr source="{}"
71+
body: block source=""

‎unified/extractor/tests/corpus/swift/types/function-type-with-sendable-attribute.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ top_level source="⟨body⟩"
6060
pattern: identifier "handler" source="handler"
6161
type: unsupported_node "@Sendable () -> Void" source="@Sendable () -> Void"
6262
value:
63-
function_expr source="⟨body⟩"
64-
body: block "{}" source="{}"
63+
function_expr source="{}"
64+
body: block source=""

‎unified/extractor/tests/location_tests.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ fn declaration_and_operator_tokens_keep_precise_ranges() {
118118
assert_has_span(&ast, source, "infix_operator", Some("is"), "is");
119119
}
120120

121+
#[test]
122+
fn closure_bodies_exclude_closure_delimiters() {
123+
let source = "let f = { (x: Int) -> Int in x * 2 }";
124+
let ast = desugar(source);
125+
126+
assert_has_span(&ast, source, "block", None, "x * 2");
127+
}
128+
121129
#[test]
122130
fn synthetic_optional_binding_nodes_anchor_to_binding_keyword() {
123131
let source = "if let value = optional {}";

0 commit comments

Comments
 (0)