Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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

Large diffs are not rendered by default.

41 changes: 35 additions & 6 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
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.ExhaustivenessResult;
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;
Expand Down Expand Up @@ -696,9 +697,20 @@ public void visitSwitch(JCSwitch tree) {
tree.isExhaustive = tree.hasUnconditionalPattern ||
TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases);
if (exhaustiveSwitch) {
tree.isExhaustive |= exhaustiveness.exhausts(tree.selector, tree.cases);
if (!tree.isExhaustive) {
log.error(tree, Errors.NotExhaustiveStatement);
ExhaustivenessResult exhaustivenessResult = exhaustiveness.exhausts(tree.selector, tree.cases);

tree.isExhaustive = exhaustivenessResult.exhaustive();

if (!tree.isExhaustive) {
if (exhaustivenessResult.notExhaustiveDetails().isEmpty()) {
log.error(tree, Errors.NotExhaustiveStatement);
} else {
List<JCDiagnostic> details =
convertNotExhaustiveDetails(exhaustivenessResult);
log.error(tree, Errors.NotExhaustiveStatementDetails(details));
}
}
}
}
if (!tree.hasUnconditionalPattern && !exhaustiveSwitch) {
Expand Down Expand Up @@ -735,16 +747,33 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases)) {
tree.isExhaustive = true;
} else {
tree.isExhaustive = exhaustiveness.exhausts(tree.selector, tree.cases);
}
ExhaustivenessResult exhaustivenessResult = exhaustiveness.exhausts(tree.selector, tree.cases);

tree.isExhaustive = exhaustivenessResult.exhaustive();

if (!tree.isExhaustive) {
log.error(tree, Errors.NotExhaustive);
if (!tree.isExhaustive) {
if (exhaustivenessResult.notExhaustiveDetails().isEmpty()) {
log.error(tree, Errors.NotExhaustive);
} else {
List<JCDiagnostic> details =
convertNotExhaustiveDetails(exhaustivenessResult);
log.error(tree, Errors.NotExhaustiveDetails(details));
}
}
}

alive = prevAlive;
alive = alive.or(resolveYields(tree, prevPendingExits));
}

private List<JCDiagnostic> convertNotExhaustiveDetails(ExhaustivenessResult exhaustivenessResult) {
return exhaustivenessResult.notExhaustiveDetails()
.stream()
.sorted()
.map(detail -> diags.fragment(Fragments.NotExhaustiveDetail(detail)))
.collect(List.collector());
}

public void visitTry(JCTry tree) {
ListBuffer<PendingExit> prevPendingExits = pendingExits;
pendingExits = new ListBuffer<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,20 @@ compiler.err.not.exhaustive=\
compiler.err.not.exhaustive.statement=\
the switch statement does not cover all possible input values

# 0: list of diagnostic
compiler.err.not.exhaustive.details=\
the switch expression does not cover all possible input values\n\
missing patterns: {0}

# 0: list of diagnostic
compiler.err.not.exhaustive.statement.details=\
the switch statement does not cover all possible input values\n\
missing patterns: {0}

# 0: string
compiler.misc.not.exhaustive.detail=\
\n{0}

compiler.err.initializer.must.be.able.to.complete.normally=\
initializer must be able to complete normally

Expand Down
10 changes: 7 additions & 3 deletions test/langtools/tools/javac/diags/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,13 @@ boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, L
*/
private static void scanForKeys(JCDiagnostic d, Set<String> keys) {
keys.add(d.getCode());
for (Object o: d.getArgs()) {
if (o instanceof JCDiagnostic) {
scanForKeys((JCDiagnostic) o, keys);
List<Object> todoArgs = new ArrayList<>(Arrays.asList(d.getArgs()));
while (!todoArgs.isEmpty()) {
Object o = todoArgs.removeLast();
if (o instanceof JCDiagnostic sd) {
scanForKeys(sd, keys);
} else if (o instanceof List l) {
todoArgs.addAll(l);
}
}
for (JCDiagnostic sd: d.getSubdiagnostics())
Expand Down
3 changes: 2 additions & 1 deletion test/langtools/tools/javac/diags/examples/NotExhaustive.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, 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 @@ -22,6 +22,7 @@
*/

// key: compiler.err.not.exhaustive
// options: -XDexhaustivityTimeout=0

class NotExhaustive {
int t(int i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025, 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.not.exhaustive.detail
// options: -XDexhaustivityTimeout=-1

class NotExhaustiveDetails {
int t(int i) {
return switch (i) {
case 0 -> -1;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025, 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 @@ -22,6 +22,7 @@
*/

// key: compiler.err.not.exhaustive.statement
// options: -XDexhaustivityTimeout=0

class NotExhaustive {
void t(Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025, 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.statement.details
// key: compiler.misc.not.exhaustive.detail
// options: -XDexhaustivityTimeout=-1

class NotExhaustiveDetails {
void t(Object o) {
switch (o) {
case String s -> System.err.println("String of length: " + s.length());
};
}
}
1 change: 1 addition & 0 deletions test/langtools/tools/javac/patterns/Exhaustiveness.java
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,7 @@ private void doTest(Path base, String[] libraryCode, String testCode, boolean st
"-Xlint:-preview",
"--class-path", libClasses.toString(),
"-XDshould-stop.at=FLOW",
"-XDexhaustivityTimeout=0",
stopAtFlow ? "-XDshould-stop.ifNoError=FLOW"
: "-XDnoop")
.outdir(classes)
Expand Down
Loading