Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit 35d867d

Browse files
committed
8268663: Crash when guards contain boolean expression
Reviewed-by: vromero
1 parent 4d8b5c7 commit 35d867d

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,10 @@ private void handleSwitch(JCTree tree, JCExpression selector,
23922392
JCCase c = l.head;
23932393
for (JCCaseLabel pat : c.labels) {
23942394
scan(pat);
2395+
if (inits.isReset()) {
2396+
inits.assign(initsWhenTrue);
2397+
uninits.assign(uninitsWhenTrue);
2398+
}
23952399
}
23962400
if (l.head.stats.isEmpty() &&
23972401
l.tail.nonEmpty() &&

test/langtools/tools/javac/patterns/Guards.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8262891
26+
* @bug 8262891 8268663
2727
* @summary Check guards implementation.
2828
* @compile --enable-preview -source ${jdk.version} Guards.java
2929
* @run main/othervm --enable-preview Guards
@@ -43,6 +43,9 @@ void run() {
4343
run(this::testBooleanSwitchExpression);
4444
assertEquals("a", testPatternInGuard("a"));
4545
assertEquals(null, testPatternInGuard(1));
46+
runIfTrue(this::typeGuardIfTrueIfStatement);
47+
runIfTrue(this::typeGuardIfTrueSwitchExpression);
48+
runIfTrue(this::typeGuardIfTrueSwitchStatement);
4649
}
4750

4851
void run(Function<Object, String> convert) {
@@ -52,6 +55,12 @@ void run(Function<Object, String> convert) {
5255
assertEquals("any", convert.apply(""));
5356
}
5457

58+
void runIfTrue(Function<Object, String> convert) {
59+
assertEquals("true", convert.apply(0));
60+
assertEquals("second", convert.apply(2));
61+
assertEquals("any", convert.apply(""));
62+
}
63+
5564
String typeTestPatternSwitchTest(Object o) {
5665
switch (o) {
5766
case Integer i && i == 0: return "zero";
@@ -84,6 +93,35 @@ String testBooleanSwitchExpression(Object o) {
8493
}
8594
}
8695

96+
String typeGuardIfTrueSwitchStatement(Object o) {
97+
Object o2 = "";
98+
switch (o) {
99+
case Integer i && i == 0 && i < 1 && o2 instanceof String s: o = s + String.valueOf(i); return "true";
100+
case Integer i && i == 0 || i > 1: o = String.valueOf(i); return "second";
101+
case Object x: return "any";
102+
}
103+
}
104+
105+
String typeGuardIfTrueSwitchExpression(Object o) {
106+
Object o2 = "";
107+
return switch (o) {
108+
case Integer i && i == 0 && i < 1 && o2 instanceof String s: o = s + String.valueOf(i); yield "true";
109+
case Integer i && i == 0 || i > 1: o = String.valueOf(i); yield "second";
110+
case Object x: yield "any";
111+
};
112+
}
113+
114+
String typeGuardIfTrueIfStatement(Object o) {
115+
Object o2 = "";
116+
if (o != null && o instanceof (Integer i && i == 0 && i < 1) && (o = i) != null && o2 instanceof String s) {
117+
return s != null ? "true" : null;
118+
} else if (o != null && o instanceof (Integer i && i == 0 || i > 1) && (o = i) != null) {
119+
return "second";
120+
} else {
121+
return "any";
122+
}
123+
}
124+
87125
String testPatternInGuard(Object o) {
88126
if (o instanceof (CharSequence cs && cs instanceof String s)) {
89127
return s;

test/langtools/tools/javac/patterns/SwitchErrors.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,16 @@ void sealedNonAbstract(SealedNonAbstract obj) {
190190
}
191191
sealed class SealedNonAbstract permits A {}
192192
final class A extends SealedNonAbstract {}
193+
Object guardWithMatchingStatement(Object o1, Object o2) {
194+
switch (o1) {
195+
case String s && s.isEmpty() || o2 instanceof Number n: return n;
196+
default: return null;
197+
}
198+
}
199+
Object guardWithMatchingExpression(Object o1, Object o2) {
200+
return switch (o1) {
201+
case String s && s.isEmpty() || o2 instanceof Number n -> n;
202+
default -> null;
203+
};
204+
}
193205
}

test/langtools/tools/javac/patterns/SwitchErrors.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ SwitchErrors.java:166:18: compiler.err.flows.through.from.pattern
2828
SwitchErrors.java:171:27: compiler.err.flows.through.to.pattern
2929
SwitchErrors.java:177:18: compiler.err.flows.through.to.pattern
3030
SwitchErrors.java:183:13: compiler.err.pattern.dominated
31+
SwitchErrors.java:195:76: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
32+
SwitchErrors.java:201:71: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
3133
SwitchErrors.java:32:9: compiler.err.not.exhaustive.statement
3234
SwitchErrors.java:38:9: compiler.err.not.exhaustive.statement
3335
SwitchErrors.java:44:9: compiler.err.not.exhaustive.statement
@@ -41,4 +43,4 @@ SwitchErrors.java:127:9: compiler.err.not.exhaustive.statement
4143
SwitchErrors.java:187:9: compiler.err.not.exhaustive.statement
4244
- compiler.note.preview.filename: SwitchErrors.java, DEFAULT
4345
- compiler.note.preview.recompile
44-
41 errors
46+
43 errors

0 commit comments

Comments
 (0)