mirror of
https://github.com/openjdk/jdk.git
synced 2026-07-19 23:48:50 +00:00
8076538: Verify error at runtime due to incorrect classification of a lambda as being instance capturing
Reviewed-by: mcimadamore
This commit is contained in:
parent
9ced0d90d5
commit
63b337e82e
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2015, 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
|
||||
@ -1197,10 +1197,13 @@ public class LambdaToMethod extends TreeTranslator {
|
||||
//if a class is defined within a lambda, the lambda must capture
|
||||
//its enclosing instance (if any)
|
||||
TranslationContext<?> localContext = context();
|
||||
while (localContext != null) {
|
||||
if (localContext.tree.getTag() == LAMBDA) {
|
||||
final TypeSymbol outerInstanceSymbol = tree.sym.type.getEnclosingType().tsym;
|
||||
while (localContext != null && !localContext.owner.isStatic()) {
|
||||
if (localContext.tree.hasTag(LAMBDA)) {
|
||||
JCTree block = capturedDecl(localContext.depth, outerInstanceSymbol);
|
||||
if (block == null) break;
|
||||
((LambdaTranslationContext)localContext)
|
||||
.addSymbol(tree.sym.type.getEnclosingType().tsym, CAPTURED_THIS);
|
||||
.addSymbol(outerInstanceSymbol, CAPTURED_THIS);
|
||||
}
|
||||
localContext = localContext.prev;
|
||||
}
|
||||
@ -1236,7 +1239,7 @@ public class LambdaToMethod extends TreeTranslator {
|
||||
}
|
||||
} else if (tree.sym.owner.kind == TYP) {
|
||||
TranslationContext<?> localContext = context();
|
||||
while (localContext != null) {
|
||||
while (localContext != null && !localContext.owner.isStatic()) {
|
||||
if (localContext.tree.hasTag(LAMBDA)) {
|
||||
JCTree block = capturedDecl(localContext.depth, tree.sym);
|
||||
if (block == null) break;
|
||||
@ -1312,10 +1315,15 @@ public class LambdaToMethod extends TreeTranslator {
|
||||
boolean isLocal = def.isLocal();
|
||||
if ((inReferencedClass && isLocal || lambdaNewClassFilter(context(), tree))) {
|
||||
TranslationContext<?> localContext = context();
|
||||
while (localContext != null) {
|
||||
if (localContext.tree.getTag() == LAMBDA) {
|
||||
final TypeSymbol outerInstanceSymbol = tree.type.getEnclosingType().tsym;
|
||||
while (localContext != null && !localContext.owner.isStatic()) {
|
||||
if (localContext.tree.hasTag(LAMBDA)) {
|
||||
if (outerInstanceSymbol != null) {
|
||||
JCTree block = capturedDecl(localContext.depth, outerInstanceSymbol);
|
||||
if (block == null) break;
|
||||
}
|
||||
((LambdaTranslationContext)localContext)
|
||||
.addSymbol(tree.type.getEnclosingType().tsym, CAPTURED_THIS);
|
||||
.addSymbol(outerInstanceSymbol, CAPTURED_THIS);
|
||||
}
|
||||
localContext = localContext.prev;
|
||||
}
|
||||
@ -1404,7 +1412,7 @@ public class LambdaToMethod extends TreeTranslator {
|
||||
// A select of this or super means, if we are in a lambda,
|
||||
// we much have an instance context
|
||||
TranslationContext<?> localContext = context();
|
||||
while (localContext != null) {
|
||||
while (localContext != null && !localContext.owner.isStatic()) {
|
||||
if (localContext.tree.hasTag(LAMBDA)) {
|
||||
JCClassDecl clazz = (JCClassDecl)capturedDecl(localContext.depth, tree.sym);
|
||||
if (clazz == null) break;
|
||||
@ -1579,7 +1587,7 @@ public class LambdaToMethod extends TreeTranslator {
|
||||
switch (block.tree.getTag()) {
|
||||
case CLASSDEF:
|
||||
ClassSymbol clazz = ((JCClassDecl)block.tree).sym;
|
||||
if (sym.isMemberOf(clazz, types)) {
|
||||
if (clazz.isSubClass(sym, types) || sym.isMemberOf(clazz, types)) {
|
||||
return currentDepth > depth ? null : block.tree;
|
||||
}
|
||||
break;
|
||||
|
||||
124
langtools/test/tools/javac/lambda/NestedCapture04.java
Normal file
124
langtools/test/tools/javac/lambda/NestedCapture04.java
Normal file
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8076538
|
||||
* @summary Verify error at runtime due to incorrect classification of a lambda as being instance capturing
|
||||
* @run main NestedCapture04
|
||||
*/
|
||||
public class NestedCapture04 {
|
||||
|
||||
public static interface Ftype {
|
||||
int get(int v);
|
||||
}
|
||||
|
||||
public static class A {
|
||||
static int counter = 0;
|
||||
}
|
||||
public static Ftype x0;
|
||||
public static void main(String[] args) throws Throwable {
|
||||
doit();
|
||||
}
|
||||
public static Object doit() throws Throwable {
|
||||
Ftype x0_ =
|
||||
(int y0) -> {
|
||||
A.counter++;
|
||||
Ftype x1 = (int y1) -> {
|
||||
A.counter++;
|
||||
class Cltype2 {
|
||||
Cltype2 meth(Cltype2 w) {
|
||||
A.counter++;
|
||||
class Cltype3 {
|
||||
class Inclass3 {
|
||||
public int iv;
|
||||
Inclass3() { iv = 0; }
|
||||
Inclass3 clmeth(Inclass3 a) {
|
||||
A.counter++;
|
||||
class Cltype4 {
|
||||
Cltype4 (Cltype4 z) {
|
||||
Ftype x5 = (int y5) -> {
|
||||
A.counter++;
|
||||
class Cltype6 {
|
||||
Cltype6 meth(Cltype6 w) {
|
||||
A.counter++;
|
||||
class Cltype7 {
|
||||
class Inclass7 {
|
||||
public int iv;
|
||||
Inclass7() { iv = 0; }
|
||||
Inclass7 clmeth(Inclass7 a) {
|
||||
A.counter++;
|
||||
class Cltype8 {
|
||||
Cltype8 (Cltype8 z) {
|
||||
Ftype x9 = (int y9) -> {
|
||||
A.counter++;
|
||||
return y9;
|
||||
};
|
||||
x9.get(2);
|
||||
if ( z == null) {
|
||||
A.counter++;
|
||||
return;
|
||||
}
|
||||
A.counter+=100;
|
||||
}
|
||||
}
|
||||
Cltype8 v = new Cltype8(null);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
Cltype7.Inclass7 c = new Cltype7().new Inclass7();
|
||||
c.clmeth((Cltype7.Inclass7)null);
|
||||
return w;
|
||||
}
|
||||
}
|
||||
Cltype6 v = new Cltype6().meth(new Cltype6());
|
||||
return y5;
|
||||
};
|
||||
x5.get(2);
|
||||
if ( z == null) {
|
||||
A.counter++;
|
||||
return;
|
||||
}
|
||||
A.counter+=100;
|
||||
}
|
||||
}
|
||||
Cltype4 v = new Cltype4(null);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
Cltype3.Inclass3 c = new Cltype3().new Inclass3();
|
||||
c.clmeth((Cltype3.Inclass3)null);
|
||||
return w;
|
||||
}
|
||||
}
|
||||
Cltype2 v = new Cltype2().meth(new Cltype2());
|
||||
return y1;
|
||||
};
|
||||
x1.get(2);
|
||||
return y0;
|
||||
};
|
||||
return x0 = x0_;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user