8157149: Inference: weird propagation of thrown inference variables

Overhaul of treatment of thrown inference variables

Reviewed-by: vromero
This commit is contained in:
Maurizio Cimadamore 2016-05-23 15:07:10 +01:00
parent 5b6c0e5152
commit 414c739476
6 changed files with 168 additions and 26 deletions

View File

@ -2666,7 +2666,7 @@ public class Attr extends JCTree.Visitor {
@Override
public boolean compatible(Type found, Type req, Warner warn) {
//return type must be compatible in both current context and assignment context
return chk.basicHandler.compatible(found, inferenceContext().asUndetVar(req), warn);
return chk.basicHandler.compatible(inferenceContext().asUndetVar(found), inferenceContext().asUndetVar(req), warn);
}
@Override

View File

@ -194,7 +194,7 @@ public class Infer {
//inject return constraints earlier
doIncorporation(inferenceContext, warn); //propagation
boolean shouldPropagate = resultInfo.checkContext.inferenceContext().free(resultInfo.pt);
boolean shouldPropagate = shouldPropagate(mt.getReturnType(), resultInfo, inferenceContext);
InferenceContext minContext = shouldPropagate ?
inferenceContext.min(roots(mt, deferredAttrContext), true, warn) :
@ -255,6 +255,13 @@ public class Infer {
}
}
//where
private boolean shouldPropagate(Type restype, Attr.ResultInfo target, InferenceContext inferenceContext) {
return target.checkContext.inferenceContext() != emptyContext && //enclosing context is a generic method
inferenceContext.free(restype) && //return type contains inference vars
(!inferenceContext.inferencevars.contains(restype) || //no eager instantiation is required (as per 18.5.2)
!needsEagerInstantiation((UndetVar)inferenceContext.asUndetVar(restype), target.pt, inferenceContext));
}
private List<Type> roots(MethodType mt, DeferredAttrContext deferredAttrContext) {
ListBuffer<Type> roots = new ListBuffer<>();
roots.add(mt.getReturnType());
@ -309,7 +316,7 @@ public class Infer {
*/
saved_undet = inferenceContext.save();
if (allowGraphInference && !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
boolean shouldPropagate = resultInfo.checkContext.inferenceContext().free(resultInfo.pt);
boolean shouldPropagate = shouldPropagate(getReturnType(), resultInfo, inferenceContext);
InferenceContext minContext = shouldPropagate ?
inferenceContext.min(roots(asMethodType(), null), false, warn) :
@ -394,8 +401,9 @@ public class Infer {
to = from.isPrimitive() ? from : syms.objectType;
} else if (qtype.hasTag(UNDETVAR)) {
if (resultInfo.pt.isReference()) {
to = generateReturnConstraintsUndetVarToReference(
tree, (UndetVar)qtype, to, resultInfo, inferenceContext);
if (needsEagerInstantiation((UndetVar)qtype, to, inferenceContext)) {
to = generateReferenceToTargetConstraint(tree, (UndetVar)qtype, to, resultInfo, inferenceContext);
}
} else {
if (to.isPrimitive()) {
to = generateReturnConstraintsPrimitive(tree, (UndetVar)qtype, to,
@ -439,9 +447,7 @@ public class Infer {
return types.boxedClass(to).type;
}
private Type generateReturnConstraintsUndetVarToReference(JCTree tree,
UndetVar from, Type to, Attr.ResultInfo resultInfo,
InferenceContext inferenceContext) {
private boolean needsEagerInstantiation(UndetVar from, Type to, InferenceContext inferenceContext) {
Type captureOfTo = types.capture(to);
/* T is a reference type, but is not a wildcard-parameterized type, and either
*/
@ -452,8 +458,7 @@ public class Infer {
for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
Type captureOfBound = types.capture(t);
if (captureOfBound != t) {
return generateReferenceToTargetConstraint(tree, from, to,
resultInfo, inferenceContext);
return true;
}
}
@ -467,8 +472,7 @@ public class Infer {
!inferenceContext.free(aLowerBound) &&
!inferenceContext.free(anotherLowerBound) &&
commonSuperWithDiffParameterization(aLowerBound, anotherLowerBound)) {
return generateReferenceToTargetConstraint(tree, from, to,
resultInfo, inferenceContext);
return true;
}
}
}
@ -483,12 +487,11 @@ public class Infer {
for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
Type sup = types.asSuper(t, to.tsym);
if (sup != null && sup.isRaw()) {
return generateReferenceToTargetConstraint(tree, from, to,
resultInfo, inferenceContext);
return true;
}
}
}
return to;
return false;
}
private boolean commonSuperWithDiffParameterization(Type t, Type s) {
@ -1475,21 +1478,16 @@ public class Infer {
//not a throws undet var
return false;
}
if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
.diff(t.getDeclaredBounds()).nonEmpty()) {
//not an unbounded undet var
return false;
}
Infer infer = inferenceContext.infer;
for (Type db : t.getDeclaredBounds()) {
for (Type db : t.getBounds(InferenceBound.UPPER)) {
if (t.isInterface()) continue;
if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
//declared bound is a supertype of RuntimeException
return true;
if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) == null) {
//upper bound is not a supertype of RuntimeException - give up
return false;
}
}
//declared bound is more specific then RuntimeException - give up
return false;
return true;
}
@Override

View File

@ -0,0 +1,28 @@
/*
* @test /nodynamiccopyright/
* @bug 8157149
* @summary Inference: weird propagation of thrown inference variables
*
* @compile/fail/ref=T8157149a.out -XDrawDiagnostics T8157149a.java
*/
import java.io.IOException;
class T8157149a {
<Z extends Throwable> Z m_T() throws Z { return null; }
<Z extends Exception> Z m_E() throws Z { return null; }
void test_T() {
Throwable t1 = m_T();
Exception t2 = m_T();
RuntimeException t3 = m_T();
IOException t4 = m_T(); //thrown not caught
}
void test_E() {
Throwable t1 = m_E();
Exception t2 = m_E();
RuntimeException t3 = m_E();
IOException t4 = m_E(); //thrown not caught
}
}

View File

@ -0,0 +1,3 @@
T8157149a.java:19:28: compiler.err.unreported.exception.need.to.catch.or.throw: java.io.IOException
T8157149a.java:26:28: compiler.err.unreported.exception.need.to.catch.or.throw: java.io.IOException
2 errors

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2016, 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 8157149
* @summary Inference: weird propagation of thrown inference variables
*
* @compile T8157149b.java
*/
class T8157149b {
void test() {
computeException1(this::computeThrowable);
computeException2(this::computeThrowable);
computeException1(() -> {
Integer integer = computeThrowable();
return integer;
});
computeException2(() -> {
Integer integer = computeThrowable();
return integer;
});
}
<T, E extends Exception> void computeException1(ThrowableComputable1<T, E> c) throws E {}
<T, E extends Exception> void computeException2(ThrowableComputable2<T, E> c) throws E {}
<E1 extends Throwable> Integer computeThrowable() throws E1 {
return 0;
}
interface ThrowableComputable1<T, E extends Throwable> {
T compute() throws E;
}
interface ThrowableComputable2<T, E extends Throwable> {
Integer compute() throws E;
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2016, 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 8157149
* @summary Inference: weird propagation of thrown inference variables
*
* @compile T8157149c.java
*/
class T8157149c {
interface I<T extends C<?, ?>, U> {
T m(U o);
}
static class C<T, U> {
C(T f) { }
}
<T, A> void m(I<C<T, Object>, A> o1, A o2) { }
void test(Object o) {
m(C::new, o);
}
}