Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8268663: Crash when guards contain boolean expression
Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed Jun 15, 2021
1 parent 4d8b5c7 commit 35d867df1f2064e867dcf1030c498bfa23568054
@@ -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() &&
@@ -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
@@ -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
@@ -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) {
@@ -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";
@@ -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;
@@ -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;
};
}
}
@@ -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
@@ -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

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 35d867d Jun 15, 2021

Please sign in to comment.