Skip to content
Permalink
Browse files
8244513: [lworld] Typing of conditional expressions involving values.
  • Loading branch information
Srikanth Adayapalam committed Aug 17, 2020
1 parent 77af809 commit f72ec3c37f69709a473989d91df302144242a4f7
@@ -4039,23 +4039,6 @@ public Type lub(Type... ts) {
final int CLASS_BOUND = 2;

int[] kinds = new int[ts.length];

boolean haveValues = false;
boolean haveRefs = false;
for (int i = 0 ; i < ts.length ; i++) {
if (ts[i].isValue())
haveValues = true;
else
haveRefs = true;
}
if (haveRefs && haveValues) {
System.arraycopy(ts, 0, ts = new Type[ts.length], 0, ts.length);
for (int i = 0; i < ts.length; i++) {
if (ts[i].isValue())
ts[i] = ts[i].referenceProjection();
}
}

int boundkind = UNKNOWN_BOUND;
for (int i = 0 ; i < ts.length ; i++) {
Type t = ts[i];
@@ -2072,9 +2072,11 @@ Type condType(List<DiagnosticPosition> positions, List<Type> condTypes) {
}
}

// Those were all the cases that could result in a primitive
// Those were all the cases that could result in a primitive. See if primitive boxing and inline
// narrowing conversions bring about a convergence.
condTypes = condTypes.stream()
.map(t -> t.isPrimitive() ? types.boxedClass(t).type : t)
.map(t -> t.isPrimitive() ? types.boxedClass(t).type
: t.isReferenceProjection() ? t.valueProjection() : t)
.collect(List.collector());

for (Type type : condTypes) {
@@ -2085,10 +2087,10 @@ Type condType(List<DiagnosticPosition> positions, List<Type> condTypes) {
Iterator<DiagnosticPosition> posIt = positions.iterator();

condTypes = condTypes.stream()
.map(t -> chk.checkNonVoid(posIt.next(), t))
.map(t -> chk.checkNonVoid(posIt.next(), t.isValue() ? t.referenceProjection() : t))
.collect(List.collector());

// both are known to be reference types. The result is
// both are known to be reference types (or projections). The result is
// lub(thentype,elsetype). This cannot fail, as it will
// always be possible to infer "Object" if nothing better.
return types.lub(condTypes.stream().map(t -> t.baseType()).collect(List.collector()));
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

/*
* @test
* @bug 8244513
* @summary Test conditional expression typing involving inlines.
* @run main ConditionalInlineTypeTest
*/

public class ConditionalInlineTypeTest {

static inline class V {}

public static void main(String [] args) {

var r1 = args.length == 0 ? new V() : new V();
System.out.println(r1.getClass());

var r2 = args.length == 0 ? (V.ref) new V() : (V.ref) new V();
System.out.println(r2.getClass());

int npe = 0;
try {
var r3 = args.length != 0 ? new V() : (V.ref) null;
System.out.println(r3.getClass());
} catch (NullPointerException e) {
npe++;
}
try {
var r4 = args.length == 0 ? (V.ref) null : new V();
System.out.println(r4.getClass());
} catch (NullPointerException e) {
npe++;
}
if (npe != 2) {
throw new AssertionError("NPEs = " + npe);
}
}
}
@@ -0,0 +1,36 @@
/*
* @test /nodynamiccopyright/
* @bug 8244513
* @summary Test conditional expression typing involving inlines.
* @compile/fail/ref=ConditionalTypeTest.out -XDrawDiagnostics ConditionalTypeTest.java
*/

final class ConditionalTypeTest {
interface I {}
static inline class Node implements I {}
static void foo(int i) {
var ret1 = (i == 0) ? new XNodeWrapper() : new Node();
ret1 = "String cannot be assigned to I";
var ret2 = (i == 0) ? 10 : new XNodeWrapper();
ret2 = "String can be assigned to I";
var ret3 = (i == 0) ? new XNodeWrapper() : 10;
ret3 = "String can be assigned to Object";
var ret4 = (i == 0) ? new XNodeWrapper() : new ConditionalTypeTest();
ret4 = "String can be assigned to Object";
var ret5 = (i == 0) ? Integer.valueOf(10) : new ConditionalTypeTest();
ret5 = "String can be assigned to Object";

var ret6 = (i == 0) ? new Node() : new Node();
ret6 = "String cannot be assigned to Node";

var ret7 = (i == 0) ? (Node.ref) new Node() : (Node.ref) null;
ret7 = "String cannot be assigned to Node.ref";

var ret8 = (i == 0) ? new Node() : (Node.ref) null;
ret8 = "String cannot be assigned to Node";

var ret9 = (i == 0) ? (Node.ref) new Node() : new Node();
ret9 = "String cannot be assigned to Node";
}
static inline class XNodeWrapper implements I {}
}
@@ -0,0 +1,6 @@
ConditionalTypeTest.java:13:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, ConditionalTypeTest.I)
ConditionalTypeTest.java:24:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, ConditionalTypeTest.Node)
ConditionalTypeTest.java:27:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, ConditionalTypeTest.Node$ref)
ConditionalTypeTest.java:30:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, ConditionalTypeTest.Node)
ConditionalTypeTest.java:33:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, ConditionalTypeTest.Node)
5 errors

0 comments on commit f72ec3c

Please sign in to comment.