8019983: Void returns combined with return with expression picked the wrong return type
authorlagergren
Fri, 05 Jul 2013 19:35:39 +0200
changeset 18854 8dd3bfd73623
parent 18853 25ba8264b427
child 18855 408663ef8f66
8019983: Void returns combined with return with expression picked the wrong return type Reviewed-by: sundar, jlaskey
nashorn/src/jdk/nashorn/internal/codegen/Attr.java
nashorn/test/script/basic/JDK-8019983.js
nashorn/test/script/basic/JDK-8019983.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/codegen/Attr.java	Fri Jul 05 15:10:47 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/Attr.java	Fri Jul 05 19:35:39 2013 +0200
@@ -749,6 +749,7 @@
     @Override
     public Node leaveReturnNode(final ReturnNode returnNode) {
         final Node expr = returnNode.getExpression();
+        final Type returnType;
 
         if (expr != null) {
             //we can't do parameter specialization if we return something that hasn't been typed yet
@@ -757,10 +758,12 @@
                 symbol.setType(Type.OBJECT);
             }
 
-            final Type returnType = Type.widest(returnTypes.pop(), symbol.getSymbolType());
-            returnTypes.push(returnType);
-            LOG.info("Returntype is now ", returnType);
+            returnType = Type.widest(returnTypes.pop(), symbol.getSymbolType());
+        } else {
+            returnType = Type.OBJECT; //undefined
         }
+        LOG.info("Returntype is now ", returnType);
+        returnTypes.push(returnType);
 
         end(returnNode);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019983.js	Fri Jul 05 19:35:39 2013 +0200
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2010, 2013, 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-8019983.js return without expression combined with return with expression should produce object
+ * return type (undefined)
+ * 
+ * @test
+ * @run
+ */
+
+
+function g() { 
+    switch(1) { 
+    case 0: 
+    case '': 
+    default:
+	return;
+    }
+    return 10;
+}
+print(g());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019983.js.EXPECTED	Fri Jul 05 19:35:39 2013 +0200
@@ -0,0 +1,1 @@
+undefined