Skip to content

Commit fc3994e

Browse files
paldepindCopilot
andcommitted
Unified: Extract all conditional compilation branches in switch cases and members
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2ce1abc6-e036-4c6c-8479-2d0e052d4cc6
1 parent 001c897 commit fc3994e

6 files changed

Lines changed: 88 additions & 17 deletions

File tree

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ yeast::query_match_schema! {
1919

2020
use swift_output_query::query_match as output_query_match;
2121

22+
yeast::query_match_schema! {
23+
swift_input_query,
24+
"swift_node_types.yml",
25+
}
26+
27+
use swift_input_query::query_match as input_query_match;
28+
2229
const STRING_VALUE_TABLE: &str = "unified_string_literal_interpreted";
2330

2431
/// User context propagated from outer rules down to the inner rules that
@@ -1218,15 +1225,24 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
12181225
// `ifConfigClause` for each branch.
12191226

12201227
// When a conditional compilation block is not in a context where it
1221-
// should be translated into an if expression we simply extract the
1222-
// first branch (for now).
1228+
// should be translated into an if expression (e.g., switch cases,
1229+
// members, or attributes) the elements of all branches are inserted
1230+
// directly into the parent.
12231231
rule!(
1224-
(ifConfigDecl
1225-
clauses: (ifConfigClause elements: _* @items)
1226-
clauses: _* @@rest)
1232+
(ifConfigDecl clauses: _* @@clauses)
12271233
where !ctx.conditional_compilation_as_expr
12281234
=>
1229-
_* { items }
1235+
_* {
1236+
clauses
1237+
.into_iter()
1238+
.flat_map(|clause| {
1239+
input_query_match!(
1240+
clause,
1241+
(ifConfigClause elements: _* @elements) => { elements },
1242+
)
1243+
})
1244+
.collect::<Vec<_>>()
1245+
}
12301246
),
12311247
// A conditional compilation block is translated into `if_expr` nodes
12321248
// with branches occurring nested within the else branch.

‎unified/extractor/tests/corpus/swift/conditional-compilation/conditional-compilation-in-switch-cases.output‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ top_level source="⟨body⟩"
170170
body:
171171
block source="{\n ⟨stmt⟩\n}"
172172
stmt:
173-
switch_expr source="switch ⟨value⟩ {\n #if DEBUG\n ⟨case⟩\n ⟨case⟩\n #elseif RELEASE\n case 2: break\n #else\n case 3: break\n #endif\n ⟨case⟩\n }"
173+
switch_expr source="switch ⟨value⟩ {\n #if DEBUG\n ⟨case⟩\n ⟨case⟩\n #elseif RELEASE\n ⟨case⟩\n #else\n ⟨case⟩\n #endif\n ⟨case⟩\n }"
174174
value: identifier "x" source="x"
175175
case:
176176
switch_case source="case ⟨pattern⟩: ⟨body⟩"
@@ -183,6 +183,16 @@ top_level source="⟨body⟩"
183183
body:
184184
block source="⟨stmt⟩"
185185
stmt: break_expr "break" source="break"
186+
switch_case source="case ⟨pattern⟩: ⟨body⟩"
187+
pattern: int_literal "2" source="2"
188+
body:
189+
block source="⟨stmt⟩"
190+
stmt: break_expr "break" source="break"
191+
switch_case source="case ⟨pattern⟩: ⟨body⟩"
192+
pattern: int_literal "3" source="3"
193+
body:
194+
block source="⟨stmt⟩"
195+
stmt: break_expr "break" source="break"
186196
switch_case source="default: ⟨body⟩"
187197
body:
188198
block source="⟨stmt⟩"

‎unified/extractor/tests/corpus/swift/conditional-compilation/conditional-compilation-with-elseif-in-class-body.output‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ top_level source="⟨body⟩"
122122
body:
123123
block source="⟨stmt⟩"
124124
stmt:
125-
class_like_declaration source="⟨modifier⟩ ⟨name_node⟩ {\n #if DEBUG\n ⟨member⟩\n #elseif RELEASE\n init(x: String) {}\n #else\n init() {}\n #endif\n}"
125+
class_like_declaration source="⟨modifier⟩ ⟨name_node⟩ {\n #if DEBUG\n ⟨member⟩\n #elseif RELEASE\n ⟨member⟩\n #else\n ⟨member⟩\n #endif\n}"
126126
modifier: modifier "class" source="class"
127127
name_node: identifier "C" source="C"
128128
member:
@@ -134,3 +134,14 @@ top_level source="⟨body⟩"
134134
type: identifier "Int" source="Int"
135135
pattern: identifier "x" source="x"
136136
body: block "{}" source="{}"
137+
constructor_declaration source="⟨name_node⟩(⟨parameter⟩) ⟨body⟩"
138+
name_node: identifier "init" source="init"
139+
parameter:
140+
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
141+
external_name_node: identifier "x" source="x"
142+
type: identifier "String" source="String"
143+
pattern: identifier "x" source="x"
144+
body: block "{}" source="{}"
145+
constructor_declaration source="⟨name_node⟩() ⟨body⟩"
146+
name_node: identifier "init" source="init"
147+
body: block "{}" source="{}"

‎unified/ql/test/library-tests/constructors/test.expected‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ implicitReceiverParameter
2626
| constructors.swift:54:9:56:9 | self | constructors.swift:54:9:56:9 | AccessorDeclaration |
2727
| constructors.swift:57:9:59:9 | self | constructors.swift:57:9:59:9 | AccessorDeclaration |
2828
| constructors.swift:67:9:67:25 | self | constructors.swift:67:9:67:25 | ConditionalConstructor.init |
29+
| constructors.swift:69:9:69:28 | self | constructors.swift:69:9:69:28 | ConditionalConstructor.init |
2930
| constructors.swift:74:1:74:0 | self | constructors.swift:74:1:74:0 | ConditionalFields.init |
3031
| constructors.swift:85:1:85:0 | self | constructors.swift:85:1:85:0 | UnequalConditionalFields.init |
3132
| constructors.swift:96:1:96:0 | self | constructors.swift:96:1:96:0 | NestedConditionalFields.init |
@@ -37,13 +38,20 @@ defaultConstructorParameter
3738
| constructors.swift:39:1:39:0 | S1.init | 4 | constructors.swift:39:1:39:0 | f7 [init default constructor] | f7 | true |
3839
| constructors.swift:39:1:39:0 | S1.init | 5 | constructors.swift:39:1:39:0 | f8 [init default constructor] | f8 | true |
3940
| constructors.swift:74:1:74:0 | ConditionalFields.init | 1 | constructors.swift:74:1:74:0 | first [init default constructor] | first | false |
40-
| constructors.swift:74:1:74:0 | ConditionalFields.init | 2 | constructors.swift:74:1:74:0 | third [init default constructor] | third | false |
41+
| constructors.swift:74:1:74:0 | ConditionalFields.init | 2 | constructors.swift:74:1:74:0 | second [init default constructor] | second | false |
42+
| constructors.swift:74:1:74:0 | ConditionalFields.init | 3 | constructors.swift:74:1:74:0 | third [init default constructor] | third | false |
4143
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 1 | constructors.swift:85:1:85:0 | alpha [init default constructor] | alpha | false |
42-
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 2 | constructors.swift:85:1:85:0 | omega [init default constructor] | omega | false |
44+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 2 | constructors.swift:85:1:85:0 | alpha [init default constructor] | alpha | false |
45+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 3 | constructors.swift:85:1:85:0 | beta [init default constructor] | beta | false |
46+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 4 | constructors.swift:85:1:85:0 | gamma [init default constructor] | gamma | false |
47+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 5 | constructors.swift:85:1:85:0 | omega [init default constructor] | omega | false |
4348
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 1 | constructors.swift:96:1:96:0 | before [init default constructor] | before | false |
4449
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 2 | constructors.swift:96:1:96:0 | selected [init default constructor] | selected | false |
45-
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 3 | constructors.swift:96:1:96:0 | afterNested [init default constructor] | afterNested | false |
46-
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 4 | constructors.swift:96:1:96:0 | after [init default constructor] | after | false |
50+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 3 | constructors.swift:96:1:96:0 | selected [init default constructor] | selected | false |
51+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 4 | constructors.swift:96:1:96:0 | afterNested [init default constructor] | afterNested | false |
52+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 5 | constructors.swift:96:1:96:0 | ignoredElseIf [init default constructor] | ignoredElseIf | false |
53+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 6 | constructors.swift:96:1:96:0 | ignoredElse [init default constructor] | ignoredElse | false |
54+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 7 | constructors.swift:96:1:96:0 | after [init default constructor] | after | false |
4755
| constructors.swift:113:1:113:0 | OptionalConditionalFields.init | 1 | constructors.swift:113:1:113:0 | first [init default constructor] | first | false |
4856
| constructors.swift:113:1:113:0 | OptionalConditionalFields.init | 2 | constructors.swift:113:1:113:0 | last [init default constructor] | last | false |
4957
defaultConstructorParameterType
@@ -52,12 +60,23 @@ defaultConstructorParameterType
5260
| constructors.swift:39:1:39:0 | S1.init | 4 | constructors.swift:39:1:39:0 | f7 [init default constructor] | f7 | constructors.swift:53:13:53:18 | Double |
5361
| constructors.swift:39:1:39:0 | S1.init | 5 | constructors.swift:39:1:39:0 | f8 [init default constructor] | f8 | constructors.swift:61:13:61:16 | GenericTypeExpr |
5462
| constructors.swift:74:1:74:0 | ConditionalFields.init | 1 | constructors.swift:74:1:74:0 | first [init default constructor] | first | constructors.swift:76:20:76:22 | Int |
55-
| constructors.swift:74:1:74:0 | ConditionalFields.init | 2 | constructors.swift:74:1:74:0 | third [init default constructor] | third | constructors.swift:80:16:80:19 | Bool |
63+
| constructors.swift:74:1:74:0 | ConditionalFields.init | 2 | constructors.swift:74:1:74:0 | second [init default constructor] | second | constructors.swift:78:21:78:26 | String |
64+
| constructors.swift:74:1:74:0 | ConditionalFields.init | 3 | constructors.swift:74:1:74:0 | third [init default constructor] | third | constructors.swift:80:16:80:19 | Bool |
5665
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 1 | constructors.swift:85:1:85:0 | alpha [init default constructor] | alpha | constructors.swift:87:20:87:22 | Int |
57-
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 2 | constructors.swift:85:1:85:0 | omega [init default constructor] | omega | constructors.swift:93:16:93:19 | Bool |
66+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 1 | constructors.swift:85:1:85:0 | alpha [init default constructor] | alpha | constructors.swift:89:20:89:25 | String |
67+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 2 | constructors.swift:85:1:85:0 | alpha [init default constructor] | alpha | constructors.swift:87:20:87:22 | Int |
68+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 2 | constructors.swift:85:1:85:0 | alpha [init default constructor] | alpha | constructors.swift:89:20:89:25 | String |
69+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 3 | constructors.swift:85:1:85:0 | beta [init default constructor] | beta | constructors.swift:90:19:90:24 | String |
70+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 4 | constructors.swift:85:1:85:0 | gamma [init default constructor] | gamma | constructors.swift:91:20:91:25 | String |
71+
| constructors.swift:85:1:85:0 | UnequalConditionalFields.init | 5 | constructors.swift:85:1:85:0 | omega [init default constructor] | omega | constructors.swift:93:16:93:19 | Bool |
5872
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 1 | constructors.swift:96:1:96:0 | before [init default constructor] | before | constructors.swift:97:17:97:19 | Int |
5973
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 2 | constructors.swift:96:1:96:0 | selected [init default constructor] | selected | constructors.swift:100:27:100:32 | String |
60-
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 3 | constructors.swift:96:1:96:0 | afterNested [init default constructor] | afterNested | constructors.swift:104:26:104:29 | Bool |
61-
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 4 | constructors.swift:96:1:96:0 | after [init default constructor] | after | constructors.swift:110:16:110:19 | Bool |
74+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 2 | constructors.swift:96:1:96:0 | selected [init default constructor] | selected | constructors.swift:102:27:102:32 | Double |
75+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 3 | constructors.swift:96:1:96:0 | selected [init default constructor] | selected | constructors.swift:100:27:100:32 | String |
76+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 3 | constructors.swift:96:1:96:0 | selected [init default constructor] | selected | constructors.swift:102:27:102:32 | Double |
77+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 4 | constructors.swift:96:1:96:0 | afterNested [init default constructor] | afterNested | constructors.swift:104:26:104:29 | Bool |
78+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 5 | constructors.swift:96:1:96:0 | ignoredElseIf [init default constructor] | ignoredElseIf | constructors.swift:106:28:106:32 | Float |
79+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 6 | constructors.swift:96:1:96:0 | ignoredElse [init default constructor] | ignoredElse | constructors.swift:108:26:108:31 | Double |
80+
| constructors.swift:96:1:96:0 | NestedConditionalFields.init | 7 | constructors.swift:96:1:96:0 | after [init default constructor] | after | constructors.swift:110:16:110:19 | Bool |
6281
| constructors.swift:113:1:113:0 | OptionalConditionalFields.init | 1 | constructors.swift:113:1:113:0 | first [init default constructor] | first | constructors.swift:115:20:115:22 | Int |
6382
| constructors.swift:113:1:113:0 | OptionalConditionalFields.init | 2 | constructors.swift:113:1:113:0 | last [init default constructor] | last | constructors.swift:117:15:117:18 | Bool |

‎unified/ql/test/library-tests/type-inference/constructors.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ struct S2 {
100100
#endif
101101
}
102102

103-
S2(f: { x in x }) // $ target=S2.init type=x:Int
103+
S2(f: { x in x }) // $ target=S2.init type=x:Int $ SPURIOUS: type=x:String

‎unified/ql/test/library-tests/type-inference/type-inference.expected‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ inferCertainType
325325
| constructors.swift:97:9:97:9 | f | Args | {EXTERNAL LOCATION} | Tuple1 |
326326
| constructors.swift:97:9:97:9 | f | Args.T0 | {EXTERNAL LOCATION} | Int |
327327
| constructors.swift:97:9:97:9 | f | Return | {EXTERNAL LOCATION} | Int |
328+
| constructors.swift:99:9:99:9 | f | | {EXTERNAL LOCATION} | Function |
329+
| constructors.swift:99:9:99:9 | f | Args | {EXTERNAL LOCATION} | Tuple1 |
330+
| constructors.swift:99:9:99:9 | f | Args.T0 | {EXTERNAL LOCATION} | String |
331+
| constructors.swift:99:9:99:9 | f | Return | {EXTERNAL LOCATION} | String |
328332
| constructors.swift:103:4:103:16 | Argument | | {EXTERNAL LOCATION} | Function |
329333
| constructors.swift:103:4:103:16 | Argument | Args | {EXTERNAL LOCATION} | Tuple1 |
330334
| constructors.swift:103:7:103:16 | FunctionExpr | | {EXTERNAL LOCATION} | Function |
@@ -2676,19 +2680,30 @@ inferType
26762680
| constructors.swift:97:9:97:9 | f | Args | {EXTERNAL LOCATION} | Tuple1 |
26772681
| constructors.swift:97:9:97:9 | f | Args.T0 | {EXTERNAL LOCATION} | Int |
26782682
| constructors.swift:97:9:97:9 | f | Return | {EXTERNAL LOCATION} | Int |
2683+
| constructors.swift:99:9:99:9 | f | | {EXTERNAL LOCATION} | Function |
2684+
| constructors.swift:99:9:99:9 | f | Args | {EXTERNAL LOCATION} | Tuple1 |
2685+
| constructors.swift:99:9:99:9 | f | Args.T0 | {EXTERNAL LOCATION} | String |
2686+
| constructors.swift:99:9:99:9 | f | Return | {EXTERNAL LOCATION} | String |
26792687
| constructors.swift:103:1:103:2 | S2 | | constructors.swift:95:1:101:1 | S2 |
26802688
| constructors.swift:103:1:103:17 | S2(...) | | constructors.swift:95:1:101:1 | S2 |
26812689
| constructors.swift:103:4:103:16 | Argument | | {EXTERNAL LOCATION} | Function |
26822690
| constructors.swift:103:4:103:16 | Argument | Args | {EXTERNAL LOCATION} | Tuple1 |
26832691
| constructors.swift:103:4:103:16 | Argument | Args.T0 | {EXTERNAL LOCATION} | Int |
2692+
| constructors.swift:103:4:103:16 | Argument | Args.T0 | {EXTERNAL LOCATION} | String |
26842693
| constructors.swift:103:4:103:16 | Argument | Return | {EXTERNAL LOCATION} | Int |
2694+
| constructors.swift:103:4:103:16 | Argument | Return | {EXTERNAL LOCATION} | String |
26852695
| constructors.swift:103:7:103:16 | FunctionExpr | | {EXTERNAL LOCATION} | Function |
26862696
| constructors.swift:103:7:103:16 | FunctionExpr | Args | {EXTERNAL LOCATION} | Tuple1 |
26872697
| constructors.swift:103:7:103:16 | FunctionExpr | Args.T0 | {EXTERNAL LOCATION} | Int |
2698+
| constructors.swift:103:7:103:16 | FunctionExpr | Args.T0 | {EXTERNAL LOCATION} | String |
26882699
| constructors.swift:103:7:103:16 | FunctionExpr | Return | {EXTERNAL LOCATION} | Int |
2700+
| constructors.swift:103:7:103:16 | FunctionExpr | Return | {EXTERNAL LOCATION} | String |
26892701
| constructors.swift:103:9:103:9 | x | | {EXTERNAL LOCATION} | Int |
2702+
| constructors.swift:103:9:103:9 | x | | {EXTERNAL LOCATION} | String |
26902703
| constructors.swift:103:14:103:14 | Block | | {EXTERNAL LOCATION} | Int |
2704+
| constructors.swift:103:14:103:14 | Block | | {EXTERNAL LOCATION} | String |
26912705
| constructors.swift:103:14:103:14 | x | | {EXTERNAL LOCATION} | Int |
2706+
| constructors.swift:103:14:103:14 | x | | {EXTERNAL LOCATION} | String |
26922707
| fields.swift:2:7:2:20 | inheritedField | | {EXTERNAL LOCATION} | Int |
26932708
| fields.swift:2:29:2:29 | 0 | | {EXTERNAL LOCATION} | Int |
26942709
| fields.swift:6:7:6:17 | storedField | | {EXTERNAL LOCATION} | String |

0 commit comments

Comments
 (0)