Skip to content

Commit de3f519

Browse files
hltjBernard Blaser
authored andcommitted
8258897: wrong translation of capturing local classes inside nested lambdas
Co-authored-by: Bernard Blaser <bsrbnd@openjdk.org> Reviewed-by: jlahoda
1 parent d7efb4c commit de3f519

File tree

4 files changed

+192
-2
lines changed

4 files changed

+192
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,13 @@ public Symbol baseSymbol() {
20682068
};
20692069
break;
20702070
case LOCAL_VAR:
2071-
ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym);
2071+
ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym) {
2072+
@Override
2073+
public Symbol baseSymbol() {
2074+
//keep mapping with original symbol
2075+
return sym;
2076+
}
2077+
};
20722078
((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
20732079
// If sym.data == ElementKind.EXCEPTION_PARAMETER,
20742080
// set ret.data = ElementKind.EXCEPTION_PARAMETER too.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ JCExpression access(Symbol sym, JCExpression tree, JCExpression enclOp, boolean
12131213
//sym is a local variable - check the lambda translation map to
12141214
//see if sym has been translated to something else in the current
12151215
//scope (by LambdaToMethod)
1216-
Symbol translatedSym = lambdaTranslationMap.get(sym);
1216+
Symbol translatedSym = lambdaTranslationMap.get(sym.baseSymbol());
12171217
if (translatedSym != null) {
12181218
tree = make.at(tree.pos).Ident(translatedSym);
12191219
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8258897
27+
* @summary Checks translation of capturing local classes inside nested lambdas
28+
* @run main CaptureVariables
29+
*/
30+
31+
import java.util.function.Supplier;
32+
33+
public class CaptureVariables {
34+
static Supplier<Integer> supplier1 = () -> {
35+
boolean b0 = false;
36+
int i0 = 6;
37+
boolean b1 = false;
38+
String s0 = "hello";
39+
40+
class Local {
41+
int i = s0.length() + i0;
42+
}
43+
44+
return ((Supplier<Integer>) () -> new Local().i).get();
45+
};
46+
47+
static Supplier<Integer> supplier2 = () -> {
48+
boolean b0 = false;
49+
int i0 = 6;
50+
boolean b1 = false;
51+
String s0 = "hello";
52+
53+
class Local {
54+
int i = s0.length() + i0;
55+
}
56+
57+
return ((Supplier<Integer>) () -> ((Supplier<Integer>) () -> new Local().i).get()).get();
58+
};
59+
60+
Supplier<Integer> supplier3 = () -> {
61+
boolean b0 = false;
62+
int i0 = 6;
63+
boolean b1 = false;
64+
String s0 = "hello";
65+
66+
class Local {
67+
int i = s0.length() + i0;
68+
}
69+
70+
return ((Supplier<Integer>) () -> new Local().i).get();
71+
};
72+
73+
Supplier<Integer> supplier4 = () -> {
74+
boolean b0 = false;
75+
int i0 = 6;
76+
boolean b1 = false;
77+
String s0 = "hello";
78+
79+
class Local {
80+
int i = s0.length() + i0;
81+
}
82+
83+
return ((Supplier<Integer>) () -> ((Supplier<Integer>) () -> new Local().i).get()).get();
84+
};
85+
86+
public static void main(String[] args) {
87+
assert supplier1.get() == 11;
88+
assert supplier2.get() == 11;
89+
assert new CaptureVariables().supplier3.get() == 11;
90+
assert new CaptureVariables().supplier4.get() == 11;
91+
}
92+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8258897
27+
* @summary Checks translation of capturing local classes inside nested lambdas
28+
* @run main CaptureVariablesAnonymous
29+
*/
30+
31+
import java.util.function.Supplier;
32+
33+
public class CaptureVariablesAnonymous {
34+
static Supplier<Integer> supplier1 = () -> {
35+
boolean b0 = false;
36+
int i0 = 6;
37+
boolean b1 = false;
38+
String s0 = "hello";
39+
40+
class Local {
41+
int i = s0.length() + i0;
42+
}
43+
44+
return ((Supplier<Integer>) () -> new Local() {}.i).get();
45+
};
46+
47+
static Supplier<Integer> supplier2 = () -> {
48+
boolean b0 = false;
49+
int i0 = 6;
50+
boolean b1 = false;
51+
String s0 = "hello";
52+
53+
class Local {
54+
int i = s0.length() + i0;
55+
}
56+
57+
return ((Supplier<Integer>) () -> ((Supplier<Integer>) () -> new Local() {}.i).get()).get();
58+
};
59+
60+
Supplier<Integer> supplier3 = () -> {
61+
boolean b0 = false;
62+
int i0 = 6;
63+
boolean b1 = false;
64+
String s0 = "hello";
65+
66+
class Local {
67+
int i = s0.length() + i0;
68+
}
69+
70+
return ((Supplier<Integer>) () -> new Local() {}.i).get();
71+
};
72+
73+
Supplier<Integer> supplier4 = () -> {
74+
boolean b0 = false;
75+
int i0 = 6;
76+
boolean b1 = false;
77+
String s0 = "hello";
78+
79+
class Local {
80+
int i = s0.length() + i0;
81+
}
82+
83+
return ((Supplier<Integer>) () -> ((Supplier<Integer>) () -> new Local() {}.i).get()).get();
84+
};
85+
86+
public static void main(String[] args) {
87+
assert supplier1.get() == 11;
88+
assert supplier2.get() == 11;
89+
assert new CaptureVariablesAnonymous().supplier3.get() == 11;
90+
assert new CaptureVariablesAnonymous().supplier4.get() == 11;
91+
}
92+
}

0 commit comments

Comments
 (0)