Compare commits

...

4 Commits

Author SHA1 Message Date
Jan Lahoda
a164fd1f05 Cleanup. 2026-01-27 17:27:53 +01:00
Jan Lahoda
1c17012fa5 Fixing tests. 2026-01-27 17:24:34 +01:00
Jan Lahoda
ca43f533f7 Renaming checkTimeout, as suggested. 2026-01-27 16:33:26 +01:00
Jan Lahoda
3a3fd48628 Cleanup: using fragments as suggested. 2026-01-27 16:15:21 +01:00
18 changed files with 120 additions and 76 deletions

View File

@ -83,7 +83,6 @@ public interface MessageType {
FILE("file", "File", "java.io"),
FILE_OBJECT("file object", "JavaFileObject", "javax.tools"),
PATH("path", "Path", "java.nio.file"),
PATTERN("pattern", "PatternDescription", "com.sun.tools.javac.comp.ExhaustivenessComputer"),
NAME("name", "Name", "com.sun.tools.javac.util"),
LONG("long", "long", null),
NUMBER("number", "int", null),

View File

@ -95,7 +95,7 @@ public class ExhaustivenessComputer {
try {
computedMaxBaseChecks = Long.parseLong(baseChecks);
} catch (NumberFormatException _) {
//ignore invalid values and use the default timeout
//ignore invalid values and use the default maximum number of checks
}
}
@ -675,7 +675,7 @@ public class ExhaustivenessComputer {
}
private boolean isBpCovered(Type componentType, PatternDescription newNested) {
checkTimeout();
reportCheck();
if (newNested instanceof BindingPattern bp) {
Type seltype = types.erasure(componentType);
@ -688,7 +688,7 @@ public class ExhaustivenessComputer {
return false;
}
protected void checkTimeout() {
protected void reportCheck() {
if (baseChecks != (-1) &&
++baseChecks > maxBaseChecks) {
throw new TooManyChecksException(null);
@ -1045,9 +1045,9 @@ public class ExhaustivenessComputer {
reducedAdded.remove(current);
Set<PatternDescription> combinedPatterns =
Stream.concat(basePatterns.stream(),
replace(inMissingPatterns, toExpand, reducedAdded).stream())
Set<PatternDescription> combinedPatterns =
Stream.concat(basePatterns.stream(),
replace(inMissingPatterns, toExpand, reducedAdded).stream())
.collect(Collectors.toSet());
if (computeCoverage(selectorType, combinedPatterns, PatternEquivalence.LOOSE).covered()) {

View File

@ -50,10 +50,15 @@ import static com.sun.tools.javac.code.Flags.BLOCK;
import static com.sun.tools.javac.code.Kinds.Kind.*;
import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
import static com.sun.tools.javac.code.TypeTag.VOID;
import com.sun.tools.javac.comp.ExhaustivenessComputer.BindingPattern;
import com.sun.tools.javac.comp.ExhaustivenessComputer.EnumConstantPattern;
import com.sun.tools.javac.comp.ExhaustivenessComputer.ExhaustivenessResult;
import com.sun.tools.javac.comp.ExhaustivenessComputer.PatternDescription;
import com.sun.tools.javac.comp.ExhaustivenessComputer.RecordPattern;
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
import static com.sun.tools.javac.tree.JCTree.Tag.*;
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
import java.util.Arrays;
/** This pass implements dataflow analysis for Java programs though
* different AST visitor steps. Liveness analysis (see AliveAnalyzer) checks that
@ -768,14 +773,31 @@ public class Flow {
List<JCDiagnostic> details =
exhaustivenessResult.notExhaustiveDetails()
.stream()
.sorted((pd1, pd2) -> pd1.toString().compareTo(pd2.toString()))
.map(detail -> diags.fragment(Fragments.NotExhaustiveDetail(detail)))
.map(this::patternToDiagnostic)
.sorted((d1, d2) -> d1.toString()
.compareTo(d2.toString()))
.collect(List.collector());
JCDiagnostic main = diags.error(null, log.currentSource(), pos, errorKey);
JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, details);
log.report(d);
}
private JCDiagnostic patternToDiagnostic(PatternDescription desc) {
Type patternType = types.erasure(desc.type());
return diags.fragment(switch (desc) {
case BindingPattern _ ->
Fragments.BindingPattern(patternType);
case RecordPattern rp ->
Fragments.RecordPattern(patternType,
Arrays.stream(rp.nested())
.map(this::patternToDiagnostic)
.toList());
case EnumConstantPattern ep ->
Fragments.EnumConstantPattern(patternType,
ep.enumConstant());
});
}
public void visitTry(JCTry tree) {
ListBuffer<PendingExit> prevPendingExits = pendingExits;
pendingExits = new ListBuffer<>();

View File

@ -40,7 +40,6 @@
# number an integer
# option name the name of a command line option
# path a path
# patter a pattern/pattern description
# profile a profile name
# source a source version number, such as 1.5, 1.6, 1.7, taken from a com.sun.tools.javac.code.Source
# source version a source version number, such as 1.5, 1.6, 1.7, taken from a javax.lang.model.SourceVersion
@ -1485,9 +1484,17 @@ compiler.err.not.exhaustive.statement.details=\
the switch statement does not cover all possible input values\n\
missing patterns:
# 0: pattern
compiler.misc.not.exhaustive.detail=\
{0}
# 0: type
compiler.misc.binding.pattern=\
{0} _
# 0: type, 1: list of diagnostic
compiler.misc.record.pattern=\
{0}({1})
# 0: type, 1: name
compiler.misc.enum.constant.pattern=\
{0}.{1}
compiler.err.initializer.must.be.able.to.complete.normally=\
initializer must be able to complete normally

View File

@ -234,18 +234,6 @@ public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter
return messages.getLocalizedString(l, "compiler.misc.tree.tag." +
StringUtils.toLowerCase(tag.name()));
}
else if (arg instanceof BindingPattern bp) {
return formatArgument(d, bp.type(), l) + " _";
}
else if (arg instanceof RecordPattern rp) {
return formatArgument(d, rp.type(), l) +
Arrays.stream(rp.nested())
.map(pd -> formatArgument(d, pd, l))
.collect(Collectors.joining(", ", "(", ")"));
}
else if (arg instanceof EnumConstantPattern ep) {
return formatArgument(d, ep.type(), l) + "." + ep.enumConstant();
}
else {
return String.valueOf(arg);
}

View File

@ -212,7 +212,6 @@ public class RichDiagnosticFormatter extends
* @param arg the argument to be translated
*/
protected void preprocessArgument(Object arg) {
//TODO: preprocess for patterns
if (arg instanceof Type type) {
preprocessType(type);
}
@ -230,17 +229,6 @@ public class RichDiagnosticFormatter extends
preprocessArgument(o);
}
}
else if (arg instanceof BindingPattern bp) {
preprocessArgument(bp.type());
}
else if (arg instanceof RecordPattern rp) {
preprocessArgument(rp.type());
Arrays.stream(rp.nested())
.forEach(this::preprocessArgument);
}
else if (arg instanceof EnumConstantPattern ep) {
preprocessArgument(ep.type());
}
}
/**

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2026, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.not.exhaustive.details
// key: compiler.misc.binding.pattern
class BindingPattern {
int t(Object o) {
return switch (o) {
case String s -> 0;
};
}
}

View File

@ -22,13 +22,16 @@
*/
// key: compiler.err.not.exhaustive.details
// key: compiler.misc.not.exhaustive.detail
// options: -XDexhaustivityTimeout=-1
// key: compiler.misc.enum.constant.pattern
class NotExhaustiveDetails {
int t(int i) {
int t(I i) {
return switch (i) {
case 0 -> -1;
case R r -> -1;
case E.A -> -1;
};
}
sealed interface I {}
enum E implements I {A, B}
record R(E e) implements I {}
}

View File

@ -22,7 +22,7 @@
*/
// key: compiler.err.not.exhaustive
// options: -XDexhaustivityTimeout=0
// options: -XDexhaustivityMaxBaseChecks=0
class NotExhaustive {
int t(int i) {

View File

@ -22,7 +22,7 @@
*/
// key: compiler.err.not.exhaustive.statement
// options: -XDexhaustivityTimeout=0
// options: -XDexhaustivityMaxBaseChecks=0
class NotExhaustive {
void t(Object o) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2026, 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
@ -22,13 +22,17 @@
*/
// key: compiler.err.not.exhaustive.statement.details
// key: compiler.misc.not.exhaustive.detail
// options: -XDexhaustivityTimeout=-1
// key: compiler.misc.record.pattern
// key: compiler.misc.binding.pattern
class NotExhaustiveDetails {
void t(Object o) {
switch (o) {
case String s -> System.err.println("String of length: " + s.length());
class RecordPattern {
void t(R r) {
switch (r) {
case R(C1 _) -> {}
};
}
sealed interface I {}
record C1() implements I {}
record C2() implements I {}
record R(I i) {}
}

View File

@ -206,8 +206,8 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
}
}
""",
"lib.R(lib.A _, lib.A _)",
"lib.R(lib.B _, lib.B _)");
"lib.R(lib.A _,lib.A _)",
"lib.R(lib.B _,lib.B _)");
}
@Test
@ -230,8 +230,8 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
record Root(Base b1, Base b2, Base b3) {}
}
""",
"test.Test.Root(test.Test.R2 _, test.Test.Base _, test.Test.Base _)",
"test.Test.Root(test.Test.R3 _, test.Test.Base _, test.Test.Base _)");
"test.Test.Root(test.Test.R2 _,test.Test.Base _,test.Test.Base _)",
"test.Test.Root(test.Test.R3 _,test.Test.Base _,test.Test.Base _)");
}
@Test
@ -271,7 +271,7 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
record Root(Base b1, Base b2, Base b3) {}
}
""",
"test.Test.Root(test.Test.R2 _, test.Test.R2(test.Test.R2 _, test.Test.R2 _), test.Test.R2(test.Test.R2 _, test.Test.R2 _))");
"test.Test.Root(test.Test.R2 _,test.Test.R2(test.Test.R2 _,test.Test.R2 _),test.Test.R2(test.Test.R2 _,test.Test.R2 _))");
}
@Test
@ -308,7 +308,7 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
record NestedBaseC() implements NestedBase {}
}
""",
"test.Test.Triple(test.Test.A _, test.Test.C(test.Test.Nested _, test.Test.NestedBaseC _), test.Test.C(test.Test.Nested _, test.Test.NestedBaseC _))");
"test.Test.Triple(test.Test.A _,test.Test.C(test.Test.Nested _,test.Test.NestedBaseC _),test.Test.C(test.Test.Nested _,test.Test.NestedBaseC _))");
}
@Test
@ -348,10 +348,10 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
record Root(Base b1, Base b2, Base b3) {}
}
""",
"test.Test.Root(test.Test.R2 _, test.Test.R2(test.Test.Base _, test.Test.R2 _), test.Test.R2(test.Test.R2 _, test.Test.Base _))");
//ideally, the result would be as follow, but it is difficult to split Base on two distinct places:
// "test.Test.Root(test.Test.R2 _, test.Test.R2(test.Test.R1 _, test.Test.R2 _), test.Test.R2(test.Test.R2 _, test.Test.R1 _))",
// "test.Test.Root(test.Test.R2 _, test.Test.R2(test.Test.R2 _, test.Test.R2 _), test.Test.R2(test.Test.R2 _, test.Test.R2 _))");
"test.Test.Root(test.Test.R2 _,test.Test.R2(test.Test.Base _,test.Test.R2 _),test.Test.R2(test.Test.R2 _,test.Test.Base _))");
//ideally,the result would be as follow,but it is difficult to split Base on two distinct places:
// "test.Test.Root(test.Test.R2 _,test.Test.R2(test.Test.R1 _,test.Test.R2 _),test.Test.R2(test.Test.R2 _,test.Test.R1 _))",
// "test.Test.Root(test.Test.R2 _,test.Test.R2(test.Test.R2 _,test.Test.R2 _),test.Test.R2(test.Test.R2 _,test.Test.R2 _))");
}
@Test
@ -388,10 +388,10 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
record NestedBaseC() implements NestedBase {}
}
""",
"test.Test.Triple(test.Test.A _, test.Test.C(test.Test.Nested _, test.Test.NestedBaseA _), test.Test.C _)",
"test.Test.Triple(test.Test.A _,test.Test.C(test.Test.Nested _,test.Test.NestedBaseA _),test.Test.C _)",
//the following could be:
//test.Test.Triple(test.Test.A _, test.Test.C(test.Test.Nested _, test.Test.NestedBaseC _), test.Test.C(test.Test.Nested _, test.Test.NestedBaseC _))
"test.Test.Triple(test.Test.A _, test.Test.C(test.Test.Nested _, test.Test.NestedBaseC _), test.Test.C _)");
//test.Test.Triple(test.Test.A _,test.Test.C(test.Test.Nested _,test.Test.NestedBaseC _),test.Test.C(test.Test.Nested _,test.Test.NestedBaseC _))
"test.Test.Triple(test.Test.A _,test.Test.C(test.Test.Nested _,test.Test.NestedBaseC _),test.Test.C _)");
}
@Test
@ -410,7 +410,7 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
}
public record R(R r1, R r2, R r3, Object o) {}
""",
"test.R(test.R _, test.R _, test.R(test.R _, test.R _, test.R _, java.lang.Object _), java.lang.Object _)");
"test.R(test.R _,test.R _,test.R(test.R _,test.R _,test.R _,java.lang.Object _),java.lang.Object _)");
}
@Test
@ -527,7 +527,7 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
}
}
""",
"Test.Pair(Test.Pair(Test.Pair _, Test.Base _), Test.Pair(Test.Pair(Test.Pair _, Test.Base _), Test.Base _))");
"Test.Pair(Test.Pair(Test.Pair _,Test.Base _),Test.Pair(Test.Pair(Test.Pair _,Test.Base _),Test.Base _))");
}
private void doTest(Path base, String[] libraryCode, String testCode, String... expectedMissingPatterns) throws IOException {
@ -576,7 +576,7 @@ public class ExhaustivenessConvenientErrors extends TestRunner {
if (d instanceof JCDiagnostic.MultilineDiagnostic diag) {
diag.getSubdiagnostics()
.stream()
.map(fragment -> fragment.getArgs()[0].toString())
.map(fragment -> fragment.toString())
.forEach(missingPatterns::add);
}
}

View File

@ -103,19 +103,19 @@ public class PrimitiveInstanceOfComboTest extends ComboInstance<PrimitiveInstanc
ComboTask task1 = newCompilationTask()
.withSourceFromTemplate(test1.replace("#{TYPE1}", type1.code).replace("#{TYPE2}", type2.code))
.withOption("--enable-preview")
.withOption("-XDexhaustivityTimeout=0")
.withOption("-XDexhaustivityMaxBaseChecks=0")
.withOption("-source").withOption(JAVA_VERSION);
ComboTask task2 = newCompilationTask()
.withSourceFromTemplate(test2.replace("#{TYPE1}", type1.code).replace("#{TYPE2}", type2.code))
.withOption("--enable-preview")
.withOption("-XDexhaustivityTimeout=0")
.withOption("-XDexhaustivityMaxBaseChecks=0")
.withOption("-source").withOption(JAVA_VERSION);
ComboTask task3 = newCompilationTask()
.withSourceFromTemplate(test3.replace("#{TYPE1}", type1.code).replace("#{TYPE2}", type2.code))
.withOption("--enable-preview")
.withOption("-XDexhaustivityTimeout=0")
.withOption("-XDexhaustivityMaxBaseChecks=0")
.withOption("-source").withOption(JAVA_VERSION);
task1.generate(result1 -> {

View File

@ -2,7 +2,7 @@
* @test /nodynamiccopyright/
* @summary Retain exhaustiveness properties of switches with a constant selector
* @enablePreview
* @compile/fail/ref=PrimitivePatternsSwitchConstants.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityTimeout=0 PrimitivePatternsSwitchConstants.java
* @compile/fail/ref=PrimitivePatternsSwitchConstants.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityMaxBaseChecks=0 PrimitivePatternsSwitchConstants.java
*/
public class PrimitivePatternsSwitchConstants {
void testConstExpressions() {

View File

@ -3,7 +3,7 @@
* @bug 8304487 8325653 8332463
* @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)
* @enablePreview
* @compile/fail/ref=PrimitivePatternsSwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityTimeout=0 PrimitivePatternsSwitchErrors.java
* @compile/fail/ref=PrimitivePatternsSwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityMaxBaseChecks=0 PrimitivePatternsSwitchErrors.java
*/
public class PrimitivePatternsSwitchErrors {
record R_int(int x) {}

View File

@ -2,7 +2,7 @@
* @test /nodynamiccopyright/
* @bug 8262891 8269146 8269113 8348928
* @summary Verify errors related to pattern switches.
* @compile/fail/ref=SwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityTimeout=0 SwitchErrors.java
* @compile/fail/ref=SwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityMaxBaseChecks=0 SwitchErrors.java
*/
public class SwitchErrors {

View File

@ -26,9 +26,9 @@
* @bug 8318913
* @summary Verify no error is when compiling a class whose permitted types are not exported
* @modules jdk.compiler
* @compile/fail/ref=NonExportedPermittedTypes.out -XDrawDiagnostics -XDexhaustivityTimeout=0 NonExportedPermittedTypes.java
* @compile/fail/ref=NonExportedPermittedTypes.out --release 21 -XDrawDiagnostics -XDexhaustivityTimeout=0 NonExportedPermittedTypes.java
* @compile/fail/ref=NonExportedPermittedTypes.out --release ${jdk.version} -XDrawDiagnostics -XDexhaustivityTimeout=0 NonExportedPermittedTypes.java
* @compile/fail/ref=NonExportedPermittedTypes.out -XDrawDiagnostics -XDexhaustivityMaxBaseChecks=0 NonExportedPermittedTypes.java
* @compile/fail/ref=NonExportedPermittedTypes.out --release 21 -XDrawDiagnostics -XDexhaustivityMaxBaseChecks=0 NonExportedPermittedTypes.java
* @compile/fail/ref=NonExportedPermittedTypes.out --release ${jdk.version} -XDrawDiagnostics -XDexhaustivityMaxBaseChecks=0 NonExportedPermittedTypes.java
*/

View File

@ -2,7 +2,7 @@
* @test /nodynamiccopyright/
* @bug 8206986
* @summary Verify behavior of not exhaustive switch expressions.
* @compile/fail/ref=ExpressionSwitchNotExhaustive.out -XDrawDiagnostics -XDexhaustivityTimeout=0 ExpressionSwitchNotExhaustive.java
* @compile/fail/ref=ExpressionSwitchNotExhaustive.out -XDrawDiagnostics -XDexhaustivityMaxBaseChecks=0 ExpressionSwitchNotExhaustive.java
*/
public class ExpressionSwitchNotExhaustive {