changeset 57456:255cae042245

8235778: No compilation error reported when a record is declared in a local class Reviewed-by: mcimadamore
author vromero
date Mon, 16 Dec 2019 15:33:03 -0500
parents b2e315938072
children 673bf1230322
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java test/langtools/tools/javac/records/RecordCompilationTests.java
diffstat 2 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Mon Dec 16 10:04:36 2019 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Mon Dec 16 15:33:03 2019 -0500
@@ -1207,6 +1207,9 @@
                 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) {
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java	Mon Dec 16 10:04:36 2019 -0800
+++ b/test/langtools/tools/javac/records/RecordCompilationTests.java	Mon Dec 16 15:33:03 2019 -0500
@@ -383,6 +383,14 @@
                 "        record RR(int x) { public int x() { return y; }};\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",
@@ -431,5 +439,34 @@
                 "        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() { }
+                        }
+                    }
+                }
+                """);
     }
 }