Skip to content

Commit c4bd8fd

Browse files
paldepindCopilot
andcommitted
Unified: Treat Unified Swift switches as exhaustive
Use the shared exhaustive-switch handling. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4a9b52f8-d07b-4692-8fb9-409f0efd64f2
1 parent b1ef638 commit c4bd8fd

4 files changed

Lines changed: 9 additions & 10 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ private module Input implements InputSig1, InputSig2 {
325325

326326
class CallableContext = Void;
327327

328+
// Swift switches are exhaustive.
329+
predicate isExhaustive(Ast::Switch switch) { any() }
330+
328331
predicate beginAbruptCompletion(
329332
AstNode ast, PreControlFlowNode n, AbruptCompletion c, boolean always
330333
) {

‎unified/ql/test/library-tests/controlflow/basicblock-slices.expected‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@
501501
| 618 | cfg.swift:618:7:618:14 | Block | 'Block -V 1 -^ ReturnExpr' |
502502
| 619 | cfg.swift:619:5:620:14 | SwitchCase | 'SwitchCase -V false' |
503503
| 620 | cfg.swift:620:7:620:14 | Block | 'Block -V 0 -^ ReturnExpr' |
504-
| 622 | cfg.swift:622:11:622:11 | 1 | '1 -^ - ... -^ ReturnExpr' |
505504
| 625 | cfg.swift:625:1:635:1 | func exhaustiveEnumSwitch | 'func exhaustiveEnumSwitch -V exhaustiveEnumSwitch' |
506505
| 625 | cfg.swift:625:29:625:33 | value | 'value -> flag -> Block' |
507506
| 626 | cfg.swift:626:3:633:3 | SwitchExpr | 'SwitchExpr -V value' |
@@ -511,4 +510,3 @@
511510
| 630 | cfg.swift:630:7:630:14 | Block | 'Block -V 2 -^ ReturnExpr' |
512511
| 631 | cfg.swift:631:5:632:14 | SwitchCase | 'SwitchCase -V . -^ ... .error2 -> . -^ ... .error3 -^ OrPattern' |
513512
| 632 | cfg.swift:632:7:632:14 | Block | 'Block -V 3 -^ ReturnExpr' |
514-
| 634 | cfg.swift:634:11:634:11 | 1 | '1 -^ - ... -^ ReturnExpr' |

‎unified/ql/test/library-tests/controlflow/cfg.expected‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,10 @@ bbStep
152152
| cfg.swift:600:13:600:18 | ... < ... | '... < ... : true -> Block(+0)' |
153153
| cfg.swift:617:10:617:13 | true | 'true : match -> Block(+1)' |
154154
| cfg.swift:617:10:617:13 | true | 'true : no-match -> SwitchCase(+2)' |
155-
| cfg.swift:619:10:619:14 | false | 'false : match -> Block(+1)' |
156-
| cfg.swift:619:10:619:14 | false | 'false : no-match -> 1(+3)' |
157155
| cfg.swift:627:10:627:27 | ConditionalPattern | 'ConditionalPattern : match -> Block(+1)' |
158156
| cfg.swift:627:10:627:27 | ConditionalPattern | 'ConditionalPattern : no-match -> SwitchCase(+2)' |
159157
| cfg.swift:629:10:629:16 | ... .error1 | '... .error1 : match -> Block(+1)' |
160158
| cfg.swift:629:10:629:16 | ... .error1 | '... .error1 : no-match -> SwitchCase(+2)' |
161-
| cfg.swift:631:10:631:25 | OrPattern | 'OrPattern : match -> Block(+1)' |
162-
| cfg.swift:631:10:631:25 | OrPattern | 'OrPattern : no-match -> 1(+3)' |
163159
noCfg
164160
| cfg.swift:47:42:47:47 | String |
165161
| cfg.swift:53:34:53:34 | _ |
@@ -179,6 +175,8 @@ noCfg
179175
| cfg.swift:440:5:440:8 | init |
180176
| cfg.swift:506:25:506:27 | Int |
181177
| cfg.swift:564:49:564:51 | Int |
178+
| cfg.swift:622:3:622:11 | ReturnExpr |
179+
| cfg.swift:634:3:634:11 | ReturnExpr |
182180
nonSimple
183181
| cfg.swift:12:17:12:25 | withParam | 'withParam -? Block' |
184182
| cfg.swift:35:5:35:5 | CatchClause | 'CatchClause -V MyError -^ ... .error1 -> isZero -> Argument -V x -^ isZero(...) -? MyError -^ ... .error2 -^ ConditionalPattern -^ OrPattern' |

‎unified/ql/test/library-tests/controlflow/cfg.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ func exhaustiveBoolSwitch(_ value: Bool) -> Int {
616616
switch value {
617617
case true: // $ bbStep='true : match -> Block(+1)' bbStep='true : no-match -> SwitchCase(+2)'
618618
return 1
619-
case false: // $ bbStep='false : match -> Block(+1)' bbStep='false : no-match -> 1(+3)'
619+
case false:
620620
return 0
621621
}
622-
return -1
622+
return -1 // $ noCfg
623623
}
624624

625625
func exhaustiveEnumSwitch(_ value: MyError, _ flag: Bool) -> Int {
@@ -628,8 +628,8 @@ func exhaustiveEnumSwitch(_ value: MyError, _ flag: Bool) -> Int {
628628
return 1
629629
case .error1: // $ bbStep='... .error1 : match -> Block(+1)' bbStep='... .error1 : no-match -> SwitchCase(+2)'
630630
return 2
631-
case .error2, .error3: // $ bbStep='OrPattern : match -> Block(+1)' bbStep='OrPattern : no-match -> 1(+3)'
631+
case .error2, .error3:
632632
return 3
633633
}
634-
return -1
634+
return -1 // $ noCfg
635635
}

0 commit comments

Comments
 (0)