Skip to content

Commit 318bc88

Browse files
committed
Unified: Handle Swift default with an ignore pattern
1 parent 824bba2 commit 318bc88

15 files changed

Lines changed: 41 additions & 26 deletions

‎unified/extractor/ast_types.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ named:
387387

388388
# A single `case ...:` (or `default:`) entry in a switch.
389389
# An entry with multiple `case p1, p2:` patterns uses an `or_pattern`.
390-
# A `default:` entry has no pattern.
390+
# A `default:` entry uses an `ignore_pattern`.
391391
switch_case:
392392
modifier*: modifier
393-
pattern?: expr
393+
pattern: expr
394394
body: block
395395

396396
# Evaluate 'expr' and match its result against 'pattern', and return true if it matches.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
918918
),
919919
// A `switch` statement. Each `switchCase` becomes a `switch_case` with a
920920
// pattern (or an `or_pattern` for comma-separated `case a, b:`) and a
921-
// body; a `default:` case has a body but no pattern. The case items and
921+
// body; a `default:` case uses an `ignore_pattern`. The case items and
922922
// body are auto-translated; the Rust block only picks the pattern shape
923923
// by arity (the query engine can't branch on list length).
924924
rule!(
@@ -934,9 +934,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
934934
body: (block stmt: {body}))
935935
),
936936
rule!(
937-
(switchCase label: (switchDefaultLabel) statements: _* @body)
937+
(switchCase label: (switchDefaultLabel defaultKeyword: @@keyword) statements: _* @body)
938938
=>
939-
(switch_case body: (block stmt: {body}))
939+
(switch_case
940+
pattern: (ignore_pattern #{keyword})
941+
body: (block stmt: {body}))
940942
),
941943
// A single case item unwraps to its pattern, possibly boxed in conditional_pattern
942944
rule!(

‎unified/extractor/tests/corpus/swift/control-flow/binding-modifier-does-not-leak-to-sibling.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ top_level source="⟨body⟩"
105105
argument:
106106
argument source="⟨value⟩"
107107
value: string_literal "\"matched\"" source="\"matched\""
108-
switch_case source="default:\n ⟨body⟩"
108+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
109+
pattern: ignore_pattern "default" source="default"
109110
body:
110111
block source="⟨stmt⟩"
111112
stmt: break_expr "break" source="break"

‎unified/extractor/tests/corpus/swift/control-flow/fallthrough.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ top_level source="⟨body⟩"
104104
body:
105105
block source="⟨stmt⟩"
106106
stmt: unsupported_node "fallthrough" source="fallthrough"
107-
switch_case source="default:\n ⟨body⟩"
107+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
108+
pattern: ignore_pattern "default" source="default"
108109
body:
109110
block source="⟨stmt⟩"
110111
stmt: break_expr "break" source="break"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ top_level source="⟨body⟩"
286286
body:
287287
block source="⟨stmt⟩"
288288
stmt: break_expr "break" source="break"
289-
switch_case source="default:\n ⟨body⟩"
289+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
290+
pattern: ignore_pattern "default" source="default"
290291
body:
291292
block source="⟨stmt⟩"
292293
stmt: break_expr "break" source="break"

‎unified/extractor/tests/corpus/swift/control-flow/switch-case-item-where-clauses.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ top_level source="⟨body⟩"
200200
argument:
201201
argument source="⟨value⟩"
202202
value: string_literal "\"non-positive\"" source="\"non-positive\""
203-
switch_case source="default:\n ⟨body⟩"
203+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
204+
pattern: ignore_pattern "default" source="default"
204205
body:
205206
block source="⟨stmt⟩"
206207
stmt:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ top_level source="⟨body⟩"
393393
argument:
394394
argument source="⟨value⟩"
395395
value: identifier "fallback" source="fallback"
396-
switch_case source="default:\n ⟨body⟩"
396+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
397+
pattern: ignore_pattern "default" source="default"
397398
body:
398399
block source="⟨stmt⟩"
399400
stmt: break_expr "break" source="break"

‎unified/extractor/tests/corpus/swift/control-flow/switch-statement.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ top_level source="⟨body⟩"
151151
argument:
152152
argument source="⟨value⟩"
153153
value: string_literal "\"two or three\"" source="\"two or three\""
154-
switch_case source="default:\n ⟨body⟩"
154+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
155+
pattern: ignore_pattern "default" source="default"
155156
body:
156157
block source="⟨stmt⟩"
157158
stmt:

‎unified/extractor/tests/corpus/swift/types/binding-modifier-does-not-leak-into-accessor-body.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ top_level source="var p: Int {\n ⟨body⟩\n}"
114114
stmt:
115115
return_expr source="return ⟨value⟩"
116116
value: int_literal "1" source="1"
117-
switch_case source="default:\n ⟨body⟩"
117+
switch_case source="⟨pattern⟩:\n ⟨body⟩"
118+
pattern: ignore_pattern "default" source="default"
118119
body:
119120
block source="⟨stmt⟩"
120121
stmt:

‎unified/extractor/tests/corpus/swift/variables/binding-modifier-does-not-leak-into-initializer.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ top_level source="⟨body⟩"
7777
body:
7878
block source="⟨stmt⟩"
7979
stmt: int_literal "1" source="1"
80-
switch_case source="default: ⟨body⟩"
80+
switch_case source="⟨pattern⟩: ⟨body⟩"
81+
pattern: ignore_pattern "default" source="default"
8182
body:
8283
block source="⟨stmt⟩"
8384
stmt: int_literal "2" source="2"

0 commit comments

Comments
 (0)