8058199: Code generation problem with javac skipping a checkcast instruction

TransTypes sometimes ignores inferred signatures when emitting synthetic casts

Reviewed-by: vromero
This commit is contained in:
Maurizio Cimadamore 2014-10-14 12:00:39 +01:00
parent 8cc03657e9
commit 8f96799dd5
2 changed files with 101 additions and 8 deletions

View File

@ -67,12 +67,16 @@ public class TransTypes extends TreeTranslator {
private Symtab syms;
private TreeMaker make;
private Enter enter;
private boolean allowInterfaceBridges;
private Types types;
private final Resolve resolve;
private final CompileStates compileStates;
/** Switch: is complex graph inference supported? */
private final boolean allowGraphInference;
/** Switch: are default methods supported? */
private final boolean allowInterfaceBridges;
protected TransTypes(Context context) {
context.put(transTypesKey, this);
compileStates = CompileStates.instance(context);
@ -81,11 +85,12 @@ public class TransTypes extends TreeTranslator {
syms = Symtab.instance(context);
enter = Enter.instance(context);
overridden = new HashMap<>();
Source source = Source.instance(context);
allowInterfaceBridges = source.allowDefaultMethods();
types = Types.instance(context);
make = TreeMaker.instance(context);
resolve = Resolve.instance(context);
Source source = Source.instance(context);
allowInterfaceBridges = source.allowDefaultMethods();
allowGraphInference = source.allowGraphInference();
}
/** A hashtable mapping bridge methods to the methods they override after
@ -654,7 +659,11 @@ public class TransTypes extends TreeTranslator {
tree.meth = translate(tree.meth, null);
Symbol meth = TreeInfo.symbol(tree.meth);
Type mt = meth.erasure(types);
List<Type> argtypes = mt.getParameterTypes();
boolean useInstantiatedPtArgs =
allowGraphInference && !types.isSignaturePolymorphic((MethodSymbol)meth.baseSymbol());
List<Type> argtypes = useInstantiatedPtArgs ?
tree.meth.type.getParameterTypes() :
mt.getParameterTypes();
if (meth.name == names.init && meth.owner == syms.enumSym)
argtypes = argtypes.tail.tail;
if (tree.varargsElement != null)
@ -675,14 +684,23 @@ public class TransTypes extends TreeTranslator {
public void visitNewClass(JCNewClass tree) {
if (tree.encl != null)
tree.encl = translate(tree.encl, erasure(tree.encl.type));
Type erasedConstructorType = tree.constructorType != null ?
erasure(tree.constructorType) :
null;
List<Type> argtypes = erasedConstructorType != null && allowGraphInference ?
erasedConstructorType.getParameterTypes() :
tree.constructor.erasure(types).getParameterTypes();
tree.clazz = translate(tree.clazz, null);
if (tree.varargsElement != null)
tree.varargsElement = types.erasure(tree.varargsElement);
tree.args = translateArgs(
tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
tree.args, argtypes, tree.varargsElement);
tree.def = translate(tree.def, null);
if (tree.constructorType != null)
tree.constructorType = erasure(tree.constructorType);
if (erasedConstructorType != null)
tree.constructorType = erasedConstructorType;
tree.type = erasure(tree.type);
result = tree;
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2014, 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 8058199
* @summary Code generation problem with javac skipping a checkcast instruction
*/
public class T8058199 {
final static String SYNTHETIC_CAST_TYPE = "[Ljava.lang.String;";
@SuppressWarnings("unchecked")
<Z> Z[] makeArr(Z z) { return (Z[])new Object[1]; }
<U> void check(U u) { }
void testMethod() {
test(() -> check(makeArr("")));
}
void testNewDiamond() {
class Check<X> {
Check(X x) { }
}
test(()-> new Check<>(makeArr("")));
}
void testNewGeneric() {
class Check {
<Z> Check(Z z) { }
}
test(()-> new Check(makeArr("")));
}
private void test(Runnable r) {
try {
r.run();
throw new AssertionError("Missing synthetic cast");
} catch (ClassCastException cce) {
if (!cce.getMessage().contains(SYNTHETIC_CAST_TYPE)) {
throw new AssertionError("Bad type in synthetic cast", cce);
}
}
}
public static void main(String[] args) {
T8058199 test = new T8058199();
test.testMethod();
test.testNewDiamond();
test.testNewGeneric();
}
}