8187962: Optimistic types ignore JavaAdapter return types

Reviewed-by: sundar, attila
This commit is contained in:
Hannes Wallnöfer 2017-09-27 14:56:19 +02:00
parent 26f4eca08c
commit 047f60b678
5 changed files with 59 additions and 2 deletions

View File

@ -273,7 +273,8 @@ abstract class CompilationPhase {
private static final class LocalVariableTypeCalculationPhase extends CompilationPhase {
@Override
FunctionNode transform(final Compiler compiler, final CompilationPhases phases, final FunctionNode fn) {
final FunctionNode newFunctionNode = transformFunction(fn, new LocalVariableTypesCalculator(compiler));
final FunctionNode newFunctionNode = transformFunction(fn, new LocalVariableTypesCalculator(compiler,
compiler.getReturnType()));
final ScriptEnvironment senv = compiler.getScriptEnvironment();
final PrintWriter err = senv.getErr();

View File

@ -619,6 +619,10 @@ public final class Compiler implements Loggable {
return types == null ? null : types.get(fn, pos);
}
Type getReturnType() {
return types == null || !isOnDemandCompilation() ? Type.UNKNOWN : types.getReturnType();
}
/**
* Do a compilation job
*

View File

@ -405,10 +405,15 @@ final class LocalVariableTypesCalculator extends SimpleNodeVisitor {
// variables).
private final Deque<Label> catchLabels = new ArrayDeque<>();
LocalVariableTypesCalculator(final Compiler compiler) {
private LocalVariableTypesCalculator(final Compiler compiler) {
this.compiler = compiler;
}
LocalVariableTypesCalculator(final Compiler compiler, final Type returnType) {
this(compiler);
this.returnType = returnType;
}
private JumpTarget createJumpTarget(final Label label) {
assert !jumpTargets.containsKey(label);
final JumpTarget jumpTarget = new JumpTarget();

View File

@ -117,6 +117,15 @@ public final class TypeMap {
return null;
}
/**
* Get the return type required for the call site we're compiling for. This only determines
* whether object return type is required or not.
* @return Type.OBJECT for call sites with object return types, Type.UNKNOWN for everything else
*/
Type getReturnType() {
return returnType.isObject() ? Type.OBJECT : Type.UNKNOWN;
}
@Override
public String toString() {
return toString("");

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2017, 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.
*/
/**
* JDK-8187962: Optimistic types ignore JavaAdapter return types
*
* @test
* @run
*/
function iterator() {
return null;
}
var list = new java.util.List() { iterator: function() { return iterator(); }};
var result = list.iterator();
Assert.assertEquals(result, null);