diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java index f19d9f6251..59f22a0cf3 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java @@ -1207,6 +1207,9 @@ public class Check { mask = (flags & RECORD) != 0 ? LocalRecordFlags : LocalClassFlags; if ((flags & RECORD) != 0) { implicit = STATIC; + if (sym.owner.kind == TYP) { + log.error(pos, Errors.RecordDeclarationNotAllowedInInnerClasses); + } } if ((sym.owner.flags_field & STATIC) == 0 && (flags & ENUM) != 0) { diff --git a/test/langtools/tools/javac/records/RecordCompilationTests.java b/test/langtools/tools/javac/records/RecordCompilationTests.java index 33c443d184..bc204ab3ed 100644 --- a/test/langtools/tools/javac/records/RecordCompilationTests.java +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java @@ -417,6 +417,14 @@ public class RecordCompilationTests extends CompilationTestCase { " record RR(int x) { public int x() { return z; }};\n" + " }\n" + "}"); + // can be contained inside a lambda + assertOK(""" + class Outer { + Runnable run = () -> { + record TestRecord(int i) {} + }; + } + """); // Can't self-shadow assertFail("compiler.err.already.defined", @@ -488,6 +496,35 @@ public class RecordCompilationTests extends CompilationTestCase { " record R(int a) {}\n" + " }\n" + "}"); + assertFail("compiler.err.record.declaration.not.allowed.in.inner.classes", + """ + class Outer { + public void test() { + class Inner extends Outer { + record R(int i) {} + } + } + } + """); + assertFail("compiler.err.record.declaration.not.allowed.in.inner.classes", + """ + class Outer { + Runnable run = new Runnable() { + record TestRecord(int i) {} + public void run() {} + }; + } + """); + assertFail("compiler.err.record.declaration.not.allowed.in.inner.classes", + """ + class Outer { + void m() { + record A() { + record B() { } + } + } + } + """); } public void testReceiverParameter() {