8187962: Optimistic types ignore JavaAdapter return types
authorhannesw
Wed, 27 Sep 2017 14:56:19 +0200
changeset 47276 bfa048898f11
parent 47275 b4c8426fe105
child 47277 69c3639a49a6
8187962: Optimistic types ignore JavaAdapter return types Reviewed-by: sundar, attila
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CompilationPhase.java
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Compiler.java
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/TypeMap.java
test/nashorn/script/basic/JDK-8187962.js
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CompilationPhase.java	Wed Sep 27 11:38:21 2017 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CompilationPhase.java	Wed Sep 27 14:56:19 2017 +0200
@@ -273,7 +273,8 @@
     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();
 
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Compiler.java	Wed Sep 27 11:38:21 2017 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Compiler.java	Wed Sep 27 14:56:19 2017 +0200
@@ -619,6 +619,10 @@
         return types == null ? null : types.get(fn, pos);
     }
 
+    Type getReturnType() {
+        return types == null || !isOnDemandCompilation() ? Type.UNKNOWN : types.getReturnType();
+    }
+
     /**
      * Do a compilation job
      *
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java	Wed Sep 27 11:38:21 2017 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java	Wed Sep 27 14:56:19 2017 +0200
@@ -405,10 +405,15 @@
     // 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();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/TypeMap.java	Wed Sep 27 11:38:21 2017 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/TypeMap.java	Wed Sep 27 14:56:19 2017 +0200
@@ -117,6 +117,15 @@
         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("");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/nashorn/script/basic/JDK-8187962.js	Wed Sep 27 14:56:19 2017 +0200
@@ -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);
+