From 5ff1d721850114508e96e1462f7722f46564afdd Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 14 Jan 2020 07:07:43 -0500 Subject: [PATCH] 8236005: local records shouldn't capture any non-static state from any enclosing type Reviewed-by: mcimadamore --- .../com/sun/tools/javac/comp/Resolve.java | 10 ++++- .../javac/records/RecordCompilationTests.java | 40 +++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 8e8186147e..8e721255a5 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, 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 @@ -1489,7 +1489,13 @@ public class Resolve { 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 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 --git a/test/langtools/tools/javac/records/RecordCompilationTests.java b/test/langtools/tools/javac/records/RecordCompilationTests.java index abbaed887c..33c443d184 100644 --- a/test/langtools/tools/javac/records/RecordCompilationTests.java +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java @@ -377,10 +377,44 @@ public class RecordCompilationTests extends CompilationTestCase { " }\n" + "}"); - // Capture locals from local record + // 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" + + "}"); + + assertFail("compiler.err.non-static.cant.be.ref", + "class R { \n" + + " void m() {\n" + + " int y;\n" + + " record RR(int x) { public int x() { return y; }};\n" + + " }\n" + + "}"); + + // 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" + + "}"); + + // or type variables + assertFail("compiler.err.non-static.cant.be.ref", + "class R { \n" + + " void m() { \n" + + " record RR(T t) {};\n" + + " }\n" + + "}"); + + // but static fields are OK assertOK("class R { \n" + - " void m(int y) { \n" + - " record RR(int x) { public int x() { return y; }};\n" + + " static int z = 0;\n" + + " void m() { \n" + + " record RR(int x) { public int x() { return z; }};\n" + " }\n" + "}");