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

Commit

Permalink
Browse files Browse the repository at this point in the history
8268663: Crash when guards contain boolean expression
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jun 15, 2021
1 parent 4d8b5c7 commit 35d867d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
Expand Up @@ -2392,6 +2392,10 @@ private void handleSwitch(JCTree tree, JCExpression selector,
JCCase c = l.head;
for (JCCaseLabel pat : c.labels) {
scan(pat);
if (inits.isReset()) {
inits.assign(initsWhenTrue);
uninits.assign(uninitsWhenTrue);
}
}
if (l.head.stats.isEmpty() &&
l.tail.nonEmpty() &&
Expand Down
42 changes: 40 additions & 2 deletions test/langtools/tools/javac/patterns/Guards.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@

/*
* @test
* @bug 8262891
* @bug 8262891 8268663
* @summary Check guards implementation.
* @compile --enable-preview -source ${jdk.version} Guards.java
* @run main/othervm --enable-preview Guards
Expand All @@ -43,6 +43,9 @@ void run() {
run(this::testBooleanSwitchExpression);
assertEquals("a", testPatternInGuard("a"));
assertEquals(null, testPatternInGuard(1));
runIfTrue(this::typeGuardIfTrueIfStatement);
runIfTrue(this::typeGuardIfTrueSwitchExpression);
runIfTrue(this::typeGuardIfTrueSwitchStatement);
}

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

void runIfTrue(Function<Object, String> convert) {
assertEquals("true", convert.apply(0));
assertEquals("second", convert.apply(2));
assertEquals("any", convert.apply(""));
}

String typeTestPatternSwitchTest(Object o) {
switch (o) {
case Integer i && i == 0: return "zero";
Expand Down Expand Up @@ -84,6 +93,35 @@ String testBooleanSwitchExpression(Object o) {
}
}

String typeGuardIfTrueSwitchStatement(Object o) {
Object o2 = "";
switch (o) {
case Integer i && i == 0 && i < 1 && o2 instanceof String s: o = s + String.valueOf(i); return "true";
case Integer i && i == 0 || i > 1: o = String.valueOf(i); return "second";
case Object x: return "any";
}
}

String typeGuardIfTrueSwitchExpression(Object o) {
Object o2 = "";
return switch (o) {
case Integer i && i == 0 && i < 1 && o2 instanceof String s: o = s + String.valueOf(i); yield "true";
case Integer i && i == 0 || i > 1: o = String.valueOf(i); yield "second";
case Object x: yield "any";
};
}

String typeGuardIfTrueIfStatement(Object o) {
Object o2 = "";
if (o != null && o instanceof (Integer i && i == 0 && i < 1) && (o = i) != null && o2 instanceof String s) {
return s != null ? "true" : null;
} else if (o != null && o instanceof (Integer i && i == 0 || i > 1) && (o = i) != null) {
return "second";
} else {
return "any";
}
}

String testPatternInGuard(Object o) {
if (o instanceof (CharSequence cs && cs instanceof String s)) {
return s;
Expand Down
12 changes: 12 additions & 0 deletions test/langtools/tools/javac/patterns/SwitchErrors.java
Expand Up @@ -190,4 +190,16 @@ void sealedNonAbstract(SealedNonAbstract obj) {
}
sealed class SealedNonAbstract permits A {}
final class A extends SealedNonAbstract {}
Object guardWithMatchingStatement(Object o1, Object o2) {
switch (o1) {
case String s && s.isEmpty() || o2 instanceof Number n: return n;
default: return null;
}
}
Object guardWithMatchingExpression(Object o1, Object o2) {
return switch (o1) {
case String s && s.isEmpty() || o2 instanceof Number n -> n;
default -> null;
};
}
}
4 changes: 3 additions & 1 deletion test/langtools/tools/javac/patterns/SwitchErrors.out
Expand Up @@ -28,6 +28,8 @@ SwitchErrors.java:166:18: compiler.err.flows.through.from.pattern
SwitchErrors.java:171:27: compiler.err.flows.through.to.pattern
SwitchErrors.java:177:18: compiler.err.flows.through.to.pattern
SwitchErrors.java:183:13: compiler.err.pattern.dominated
SwitchErrors.java:195:76: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
SwitchErrors.java:201:71: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
SwitchErrors.java:32:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:38:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:44:9: compiler.err.not.exhaustive.statement
Expand All @@ -41,4 +43,4 @@ SwitchErrors.java:127:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:187:9: compiler.err.not.exhaustive.statement
- compiler.note.preview.filename: SwitchErrors.java, DEFAULT
- compiler.note.preview.recompile
41 errors
43 errors

1 comment on commit 35d867d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.