Skip to content

Commit

Permalink
8325653: Erroneous exhaustivity analysis for primitive patterns
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
biboudis committed Feb 14, 2024
1 parent d003996 commit 0c2def0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
24 changes: 6 additions & 18 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2024, 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 Down Expand Up @@ -1297,24 +1297,12 @@ public void analyzeTree(Env<AttrContext> env, JCTree tree, TreeMaker make) {

private boolean isBpCovered(Type componentType, PatternDescription newNested) {
if (newNested instanceof BindingPattern bp) {
var seltype = types.erasure(componentType);
Type seltype = types.erasure(componentType);
Type pattype = types.erasure(bp.type);

if (seltype.isPrimitive()) {
if (types.isSameType(bp.type, types.boxedClass(seltype).type)) {
return true;
}

// if the target is unconditionally exact to the pattern, target is covered
if (types.isUnconditionallyExact(seltype, bp.type)) {
return true;
}
} else if (seltype.isReference() && bp.type.isPrimitive() && types.isCastable(seltype, bp.type)) {
return true;
} else {
if (types.isSubtype(seltype, types.erasure(bp.type))) {
return true;
}
}
return seltype.isPrimitive() ?
types.isUnconditionallyExact(seltype, pattype) :
(bp.type.isPrimitive() && types.isUnconditionallyExact(types.unboxedType(seltype), bp.type)) || types.isSubtype(seltype, pattype);
}
return false;
}
Expand Down
@@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8304487
* @bug 8304487 8325653
* @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)
* @enablePreview
* @compile/fail/ref=PrimitivePatternsSwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW PrimitivePatternsSwitchErrors.java
Expand Down Expand Up @@ -217,4 +217,28 @@ void nullAndPrimitive() {
default -> System.out.println("any other integral value");
}
}

public static int nonExhaustive4() {
Number n = Byte.valueOf((byte) 42);
return switch (n) { // Error - not exhaustive
case byte b when b == 42 -> 1;
case byte b -> -1 ;
};
}

public static int nonExhaustive5() {
Object n = 42;
return switch (n) { // Error - not exhaustive
case int b when b == 42 -> 1;
case int b -> -1 ;
};
}

public static int nonExhaustive6() {
Object n = 42;
return switch (n) { // Error - not exhaustive
case byte b -> -1 ;
case int b -> -2 ;
};
}
}
Expand Up @@ -32,6 +32,9 @@ PrimitivePatternsSwitchErrors.java:44:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:52:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:201:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:207:9: compiler.err.not.exhaustive.statement
PrimitivePatternsSwitchErrors.java:223:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:231:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:239:16: compiler.err.not.exhaustive
- compiler.note.preview.filename: PrimitivePatternsSwitchErrors.java, DEFAULT
- compiler.note.preview.recompile
34 errors
37 errors

1 comment on commit 0c2def0

@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.