From 9fa2f1999001dcda7bf2c8d0fe0d0864d8a5a30c Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Fri, 22 Mar 2024 21:48:12 +0000 Subject: [PATCH] Add test for guards in the presence of fall-through between pattern and constant cases --- .../library-tests/guards12/PrintAst.expected | 20 ++++++++++++++++++ java/ql/test/library-tests/guards12/Test.java | 9 ++++++++ .../library-tests/guards12/guard.expected | 21 +++++++++++++++---- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/java/ql/test/library-tests/guards12/PrintAst.expected b/java/ql/test/library-tests/guards12/PrintAst.expected index 55225509608..8920252fb29 100644 --- a/java/ql/test/library-tests/guards12/PrintAst.expected +++ b/java/ql/test/library-tests/guards12/PrintAst.expected @@ -82,3 +82,23 @@ Test.java: # 24| 0: [StringLiteral] "g" # 25| 3: [DefaultCase] default # 25| -1: [BlockStmt] { ... } +# 27| 5: [SwitchStmt] switch (...) +# 27| -1: [VarAccess] s +# 28| 0: [ConstCase] case ... +# 28| 0: [StringLiteral] "h" +# 29| 1: [PatternCase] case +# 29| -3: [EQExpr] ... == ... +# 29| 0: [VarAccess] len +# 29| 1: [IntegerLiteral] 4 +#-----| 0: (Pattern case declaration) +# 29| 0: [TypeAccess] String +# 29| 1: [LocalVariableDeclExpr] +# 30| 2: [ConstCase] case ... +# 30| 0: [StringLiteral] "i" +# 31| 3: [LocalVariableDeclStmt] var ...; +# 31| 0: [TypeAccess] String +# 31| 1: [LocalVariableDeclExpr] target +# 31| 0: [StringLiteral] "Shouldn't be controlled by any of those tests" +# 32| 4: [BreakStmt] break +# 33| 5: [DefaultCase] default +# 34| 6: [BreakStmt] break diff --git a/java/ql/test/library-tests/guards12/Test.java b/java/ql/test/library-tests/guards12/Test.java index 4dc09a31952..2c777467d4a 100644 --- a/java/ql/test/library-tests/guards12/Test.java +++ b/java/ql/test/library-tests/guards12/Test.java @@ -24,5 +24,14 @@ class Test { case "g" -> { } default -> { } } + switch (s) { + case "h": + case String _ when len == 4: + case "i": + String target = "Shouldn't be controlled by any of those tests"; + break; + default: + break; + } } } diff --git a/java/ql/test/library-tests/guards12/guard.expected b/java/ql/test/library-tests/guards12/guard.expected index 0efa2b54423..0980e891d84 100644 --- a/java/ql/test/library-tests/guards12/guard.expected +++ b/java/ql/test/library-tests/guards12/guard.expected @@ -1,8 +1,8 @@ hasBranchEdge -| Test.java:4:7:4:22 | case ... | Test.java:2:39:27:3 | { ... } | Test.java:4:7:4:22 | case ... | true | -| Test.java:5:7:5:17 | case ... | Test.java:2:39:27:3 | { ... } | Test.java:5:7:5:17 | case ... | true | -| Test.java:6:7:6:17 | case ... | Test.java:2:39:27:3 | { ... } | Test.java:6:7:6:17 | case ... | true | -| Test.java:7:7:7:16 | default | Test.java:2:39:27:3 | { ... } | Test.java:7:7:7:16 | default | true | +| Test.java:4:7:4:22 | case ... | Test.java:2:39:36:3 | { ... } | Test.java:4:7:4:22 | case ... | true | +| Test.java:5:7:5:17 | case ... | Test.java:2:39:36:3 | { ... } | Test.java:5:7:5:17 | case ... | true | +| Test.java:6:7:6:17 | case ... | Test.java:2:39:36:3 | { ... } | Test.java:6:7:6:17 | case ... | true | +| Test.java:7:7:7:16 | default | Test.java:2:39:36:3 | { ... } | Test.java:7:7:7:16 | default | true | | Test.java:10:7:10:22 | case ... | Test.java:3:9:3:21 | x | Test.java:10:7:10:22 | case ... | true | | Test.java:11:7:11:17 | case ... | Test.java:3:9:3:21 | x | Test.java:11:7:11:17 | case ... | true | | Test.java:12:7:12:17 | case ... | Test.java:3:9:3:21 | x | Test.java:12:7:12:17 | case ... | true | @@ -31,6 +31,16 @@ hasBranchEdge | Test.java:24:7:24:17 | case ... | Test.java:23:19:23:20 | s2 | Test.java:24:7:24:17 | case ... | true | | Test.java:25:7:25:16 | default | Test.java:23:7:23:37 | case | Test.java:25:7:25:16 | default | true | | Test.java:25:7:25:16 | default | Test.java:23:19:23:20 | s2 | Test.java:25:7:25:16 | default | true | +| Test.java:28:7:28:15 | case ... | Test.java:27:5:27:14 | switch (...) | Test.java:28:7:28:15 | case ... | true | +| Test.java:29:7:29:34 | case | Test.java:29:7:29:34 | case | Test.java:29:19:29:19 | | true | +| Test.java:29:7:29:34 | case | Test.java:29:7:29:34 | case | Test.java:30:7:30:15 | case ... | false | +| Test.java:29:7:29:34 | case | Test.java:29:7:29:34 | case | Test.java:33:7:33:14 | default | false | +| Test.java:29:26:29:33 | ... == ... | Test.java:29:19:29:19 | | Test.java:30:7:30:15 | case ... | false | +| Test.java:29:26:29:33 | ... == ... | Test.java:29:19:29:19 | | Test.java:30:7:30:15 | case ... | true | +| Test.java:29:26:29:33 | ... == ... | Test.java:29:19:29:19 | | Test.java:33:7:33:14 | default | false | +| Test.java:30:7:30:15 | case ... | Test.java:29:7:29:34 | case | Test.java:30:7:30:15 | case ... | true | +| Test.java:33:7:33:14 | default | Test.java:29:7:29:34 | case | Test.java:33:7:33:14 | default | true | +| Test.java:33:7:33:14 | default | Test.java:29:19:29:19 | | Test.java:33:7:33:14 | default | true | #select | Test.java:5:7:5:17 | case ... | Test.java:3:20:3:20 | s | Test.java:5:12:5:14 | "c" | true | false | Test.java:7:7:7:16 | default | | Test.java:5:7:5:17 | case ... | Test.java:3:20:3:20 | s | Test.java:5:12:5:14 | "c" | true | true | Test.java:5:7:5:17 | case ... | @@ -48,3 +58,6 @@ hasBranchEdge | Test.java:23:27:23:34 | ... == ... | Test.java:23:27:23:29 | len | Test.java:23:34:23:34 | 4 | true | true | Test.java:23:39:23:41 | { ... } | | Test.java:24:7:24:17 | case ... | Test.java:21:13:21:41 | ...?...:... | Test.java:24:12:24:14 | "g" | true | false | Test.java:25:7:25:16 | default | | Test.java:24:7:24:17 | case ... | Test.java:21:13:21:41 | ...?...:... | Test.java:24:12:24:14 | "g" | true | true | Test.java:24:7:24:17 | case ... | +| Test.java:28:7:28:15 | case ... | Test.java:27:13:27:13 | s | Test.java:28:12:28:14 | "h" | true | false | Test.java:33:7:33:14 | default | +| Test.java:28:7:28:15 | case ... | Test.java:27:13:27:13 | s | Test.java:28:12:28:14 | "h" | true | true | Test.java:28:7:28:15 | case ... | +| Test.java:30:7:30:15 | case ... | Test.java:27:13:27:13 | s | Test.java:30:12:30:14 | "i" | true | false | Test.java:33:7:33:14 | default |