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

8268663: Crash when guards contain boolean expression #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
37 changes: 35 additions & 2 deletions test/langtools/tools/javac/patterns/Guards.java
Original file line number Diff line number Diff line change
@@ -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,11 @@ void run(Function<Object, String> convert) {
assertEquals("any", convert.apply(""));
}

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

String typeTestPatternSwitchTest(Object o) {
switch (o) {
case Integer i && i == 0: return "zero";
Expand Down Expand Up @@ -84,6 +92,31 @@ 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";
Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle Jun 14, 2021

Choose a reason for hiding this comment

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

hi, I would recommend adding more tests with || in the condition, and with more than one case in the switch with conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, Vicente. I've added tests with || in d9e902b . One thing to note is that the tests are a bit different when writing something like: case String s && s.isEmpty() || o2 instanceof Number n -> n is not available here, as there is a path on which o2 is not a Number.

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 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 {
return "any";
}
}

String testPatternInGuard(Object o) {
if (o instanceof (CharSequence cs && cs instanceof String s)) {
return s;
Expand Down