Thu, 08 Aug 2013 11:49:16 +0100
8019486: javac, generates erroneous LVT for a test case with lambda code
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Wed Aug 07 16:09:31 2013 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Thu Aug 08 11:49:16 2013 +0100 1.3 @@ -473,7 +473,7 @@ 1.4 //non-void to non-void conversion: 1.5 // return (TYPE)BODY; 1.6 JCExpression retExpr = transTypes.coerce(attrEnv, expr, restype); 1.7 - return make.Block(0, List.<JCStatement>of(make.Return(retExpr))); 1.8 + return make.at(retExpr).Block(0, List.<JCStatement>of(make.Return(retExpr))); 1.9 } 1.10 } 1.11
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/T8019486/WrongLVTForLambdaTest.java Thu Aug 08 11:49:16 2013 +0100 2.3 @@ -0,0 +1,111 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. Oracle designates this 2.11 + * particular file as subject to the "Classpath" exception as provided 2.12 + * by Oracle in the LICENSE file that accompanied this code. 2.13 + * 2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.17 + * version 2 for more details (a copy is included in the LICENSE file that 2.18 + * accompanied this code). 2.19 + * 2.20 + * You should have received a copy of the GNU General Public License version 2.21 + * 2 along with this work; if not, write to the Free Software Foundation, 2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.23 + * 2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.25 + * or visit www.oracle.com if you need additional information or have any 2.26 + * questions. 2.27 + */ 2.28 + 2.29 +/* 2.30 + * @test 2.31 + * @bug 8019486 2.32 + * @summary javac, generates erroneous LVT for a test case with lambda code 2.33 + * @library /tools/javac/lib 2.34 + * @build ToolBox 2.35 + * @run main WrongLVTForLambdaTest 2.36 + */ 2.37 + 2.38 +import java.io.File; 2.39 +import java.nio.file.Paths; 2.40 + 2.41 +import com.sun.tools.classfile.ClassFile; 2.42 +import com.sun.tools.classfile.Code_attribute; 2.43 +import com.sun.tools.classfile.LineNumberTable_attribute; 2.44 +import com.sun.tools.classfile.Method; 2.45 +import com.sun.tools.javac.util.Assert; 2.46 + 2.47 +public class WrongLVTForLambdaTest { 2.48 + 2.49 + static final String testSource = 2.50 + /* 01 */ "import java.util.List;\n" + 2.51 + /* 02 */ "import java.util.Arrays;\n" + 2.52 + /* 03 */ "import java.util.stream.Collectors;\n" + 2.53 + /* 04 */ "\n" + 2.54 + /* 05 */ "public class Foo {\n" + 2.55 + /* 06 */ " void bar(int value) {\n" + 2.56 + /* 07 */ " final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" + 2.57 + /* 08 */ " final List<Integer> numbersPlusOne = \n" + 2.58 + /* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" + 2.59 + /* 10 */ " }\n" + 2.60 + /* 11 */ "}"; 2.61 + 2.62 + static final int[][] expectedLNT = { 2.63 + // {line-number, start-pc}, 2.64 + {9, 0}, //number -> number / 1 2.65 + }; 2.66 + 2.67 + static final String methodToLookFor = "lambda$0"; 2.68 + 2.69 + public static void main(String[] args) throws Exception { 2.70 + new WrongLVTForLambdaTest().run(); 2.71 + } 2.72 + 2.73 + void run() throws Exception { 2.74 + compileTestClass(); 2.75 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 2.76 + "Foo.class").toUri()), methodToLookFor); 2.77 + } 2.78 + 2.79 + void compileTestClass() throws Exception { 2.80 + ToolBox.JavaToolArgs javacSuccessArgs = 2.81 + new ToolBox.JavaToolArgs().setSources(testSource); 2.82 + ToolBox.javac(javacSuccessArgs); 2.83 + } 2.84 + 2.85 + void checkClassFile(final File cfile, String methodToFind) throws Exception { 2.86 + ClassFile classFile = ClassFile.read(cfile); 2.87 + boolean methodFound = false; 2.88 + for (Method method : classFile.methods) { 2.89 + if (method.getName(classFile.constant_pool).equals(methodToFind)) { 2.90 + methodFound = true; 2.91 + Code_attribute code = (Code_attribute) method.attributes.get("Code"); 2.92 + LineNumberTable_attribute lnt = 2.93 + (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); 2.94 + Assert.check(lnt.line_number_table_length == expectedLNT.length, 2.95 + "The LineNumberTable found has a length different to the expected one"); 2.96 + int i = 0; 2.97 + for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { 2.98 + Assert.check(entry.line_number == expectedLNT[i][0] && 2.99 + entry.start_pc == expectedLNT[i][1], 2.100 + "LNT entry at pos " + i + " differ from expected." + 2.101 + "Found " + entry.line_number + ":" + entry.start_pc + 2.102 + ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); 2.103 + i++; 2.104 + } 2.105 + } 2.106 + } 2.107 + Assert.check(methodFound, "The seek method was not found"); 2.108 + } 2.109 + 2.110 + void error(String msg) { 2.111 + throw new AssertionError(msg); 2.112 + } 2.113 + 2.114 +}