# HG changeset patch # Parent 255cae042245d61b1d96d43f87e29e5b640ab20d 8236005: local records shouldn't capture any non-static state from outer types diff -r 255cae042245 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java Mon Dec 16 15:33:03 2019 -0500 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java Mon Dec 16 16:50:20 2019 -0500 @@ -1489,7 +1489,13 @@ if (sym.exists()) { if (staticOnly && sym.kind == VAR && - sym.owner.kind == TYP && + // if it is a field + (sym.owner.kind == TYP || + // or it is a local variable but it is not declared inside of the static local type + // only allowed for records so far, then error + (sym.owner.kind == MTH) && + (env.enclClass.sym.flags() & STATIC) != 0 && + sym.enclClass() != env.enclClass.sym) && (sym.flags() & STATIC) == 0) return new StaticError(sym); else diff -r 255cae042245 test/langtools/tools/javac/records/RecordCompilationTests.java --- a/test/langtools/tools/javac/records/RecordCompilationTests.java Mon Dec 16 15:33:03 2019 -0500 +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java Mon Dec 16 16:50:20 2019 -0500 @@ -377,12 +377,23 @@ " }\n" + "}"); - // Capture locals from local record - assertOK("class R { \n" + + // Cant capture locals + assertFail("compiler.err.non-static.cant.be.ref", + "class R { \n" + " void m(int y) { \n" + " record RR(int x) { public int x() { return y; }};\n" + " }\n" + "}"); + + // or instance fields + assertFail("compiler.err.non-static.cant.be.ref", + "class R { \n" + + " int z = 0;\n" + + " void m() { \n" + + " record RR(int x) { public int x() { return z; }};\n" + + " }\n" + + "}"); + // can be contained inside a lambda assertOK(""" class Outer {