changeset 56995:be8b67a276dc records-and-sealed

moving previous negative tests to RecordCompilationTest, fixing reported bugs
author vromero
date Fri, 16 Aug 2019 11:00:47 -0400
parents 7f0d4f15e62b
children 7ddffaf8b702 6fbc84eefe66
files src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java test/langtools/lib/combo/tools/javac/combo/JavacTemplateTestBase.java test/langtools/tools/javac/records/BadUseOfRecordKeywordTest.java test/langtools/tools/javac/records/BadUseOfRecordKeywordTest.out test/langtools/tools/javac/records/RecordCompilationTests.java test/langtools/tools/javac/records/allow_static_fields/AllowStaticFieldsInRecordsTest.java test/langtools/tools/javac/records/fields/DatumCanNotDeclaredFieldsWithSameName.java test/langtools/tools/javac/records/fields/DatumCanNotDeclaredFieldsWithSameName.out test/langtools/tools/javac/records/fields/NoAddInstanceFieldsCanBeDeclaredInDatumTest.java test/langtools/tools/javac/records/fields/NoAddInstanceFieldsCanBeDeclaredInDatumTest.out test/langtools/tools/javac/records/records_cant_be_abstract/RecordsCantBeAbstractTest.java test/langtools/tools/javac/records/records_cant_be_abstract/RecordsCantBeAbstractTest.out test/langtools/tools/javac/records/records_cant_have_extends_clause/RecordsCantHaveExtendsClause.java test/langtools/tools/javac/records/records_cant_have_extends_clause/RecordsCantHaveExtendsClause.out
diffstat 14 files changed, 11 insertions(+), 145 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Aug 15 19:02:11 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Aug 16 11:00:47 2019 -0400
@@ -3330,6 +3330,13 @@
                 log.warning(pos, Warnings.RestrictedTypeNotAllowedPreview(name, Source.JDK13));
             }
         }
+        if (name == names.record) {
+            if (Feature.RECORDS.allowedInSource(source)) {
+                return true;
+            } else if (shouldWarn) {
+                log.warning(pos, Warnings.RestrictedTypeNotAllowedPreview(name, Source.JDK14));
+            }
+        }
         return false;
     }
 
--- a/test/langtools/lib/combo/tools/javac/combo/JavacTemplateTestBase.java	Thu Aug 15 19:02:11 2019 -0400
+++ b/test/langtools/lib/combo/tools/javac/combo/JavacTemplateTestBase.java	Fri Aug 16 11:00:47 2019 -0400
@@ -208,7 +208,7 @@
         if (!diags.errorsFound())
             fail("Expected failed compilation: " + key);
         if (!diags.containsErrorKey(key))
-            fail("Expected compilation error " + key);
+            fail(String.format("Expected compilation error with %s, found %s", key, diags.keys()));
     }
 
     /** Assert that a previous call to compile() failed with a specific error key */
--- a/test/langtools/tools/javac/records/BadUseOfRecordKeywordTest.java	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary check that record is not used as a type name
- * @compile/fail/ref=BadUseOfRecordKeywordTest.out -XDrawDiagnostics BadUseOfRecordKeywordTest.java
- */
-
-class BadUseOfRecordKeywordTest {
-
-    interface record {
-        void m();
-    }
-
-    class record {}
-
-    enum record {A, B}
-
-    record record(int x);
-}
--- a/test/langtools/tools/javac/records/BadUseOfRecordKeywordTest.out	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-BadUseOfRecordKeywordTest.java:9:15: compiler.err.record.not.allowed: record
-BadUseOfRecordKeywordTest.java:13:11: compiler.err.record.not.allowed: record
-BadUseOfRecordKeywordTest.java:15:10: compiler.err.record.not.allowed: record
-BadUseOfRecordKeywordTest.java:17:12: compiler.err.record.not.allowed: record
-BadUseOfRecordKeywordTest.java:17:25: compiler.err.expected: '{'
-BadUseOfRecordKeywordTest.java:18:2: compiler.err.premature.eof
-6 errors
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Aug 15 19:02:11 2019 -0400
+++ b/test/langtools/tools/javac/records/RecordCompilationTests.java	Fri Aug 16 11:00:47 2019 -0400
@@ -107,12 +107,10 @@
         // @@@ assertFail("compiler.err.restricted.type.not.allowed.here", "record R(record x) { }");
         assertFail("compiler.err.record.cant.declare.field.modifiers", "record R(public String foo) { }");
         assertFail("compiler.err.record.cant.declare.field.modifiers", "record R(private String foo) { }");
-        // @@@ Duplicates RecordCantBeAbstractTest
         assertFail("compiler.err.mod.not.allowed.here", "abstract record R(String foo) { }");
         assertFail("compiler.err.illegal.combination.of.modifiers", "non-sealed record R(String foo) { }");
         assertFail("compiler.err.repeated.modifier", "public public record R(String foo) { }");
         assertFail("compiler.err.repeated.modifier", "private private record R(String foo) { }");
-        // @@@ Duplicates DatumCanNotDeclaredFieldsWithSameName
         assertFail("compiler.err.record.cant.declare.duplicate.fields", "record R(int x, int x) {}");
     }
 
@@ -134,19 +132,17 @@
     }
 
     public void testRestrictedIdentifiers() {
-        // @@@ Duplicates BadUseOfRecordKeywordTest
         for (String s : List.of("interface record { void m(); }",
                 "@interface record { }",
                 "class record { }",
                 "record record(int x) { }",
                 "enum record { A, B }",
                 "class R<record> { }")) {
-            assertFail("compiler.err.record.not.allowed", s);
+            assertFail("compiler.err.restricted.type.not.allowed", s);
         }
     }
 
     public void testValidMembers() {
-        // @@@ Duplicates NoAddInstanceFieldsCanBeDeclaredInDatumTest (partially; rest is duplicated in testFieldDeclarations)
         for (String s : List.of("record X(int j) { }",
                 "interface I { }",
                 "static { }",
@@ -164,7 +160,6 @@
     }
 
     public void testBadExtends() {
-        // @@@ Duplicate RecordCantHaveExtendsTest
         assertFail("compiler.err.expected", "record R(int x) extends Object { }");
         assertFail("compiler.err.expected", "record R(int x) {}\n"
                 + "record R2(int x) extends R { }");
@@ -174,12 +169,11 @@
 
     public void testNoExtendRecord() {
         // @@@ Not finished, waiting on j.l.Record
-//        assertFail("",
-//                   "class R extends Record { public String toString() { return null; } public int hashCode() { return 0; } public boolean equals(Object o) { return false; } } }");
+        assertFail("compiler.err.invalid.supertype.record",
+                   "class R extends Record { public String toString() { return null; } public int hashCode() { return 0; } public boolean equals(Object o) { return false; } } }");
     }
 
     public void testFieldDeclarations() {
-        // @@@ Duplicates AllowStaticFieldsInRecordTest
         // static fields are OK
         assertOK("public record R(int x) {\n" +
                 "    static int I = 1;\n" +
--- a/test/langtools/tools/javac/records/allow_static_fields/AllowStaticFieldsInRecordsTest.java	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2017, 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
- * @summary smoke test to check that static fields are allowed in records
- * @compile AllowStaticFieldsInRecordsTest.java
- */
-
-public record AllowStaticFieldsInRecordsTest(int x) {
-    static int I = 1;
-    static final String S = "Hello World!";
-    static private Object O = null;
-    static protected Object O2 = null;
-}
--- a/test/langtools/tools/javac/records/fields/DatumCanNotDeclaredFieldsWithSameName.java	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke negative test for datum classes
- * @compile/fail/ref=DatumCanNotDeclaredFieldsWithSameName.out -XDrawDiagnostics DatumCanNotDeclaredFieldsWithSameName.java
- */
-
-public class DatumCanNotDeclaredFieldsWithSameName {
-    record D1(int x, int x) { }
-}
--- a/test/langtools/tools/javac/records/fields/DatumCanNotDeclaredFieldsWithSameName.out	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-DatumCanNotDeclaredFieldsWithSameName.java:8:26: compiler.err.record.cant.declare.duplicate.fields
-1 error
--- a/test/langtools/tools/javac/records/fields/NoAddInstanceFieldsCanBeDeclaredInDatumTest.java	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke negative test for datum classes
- * @compile/fail/ref=NoAddInstanceFieldsCanBeDeclaredInDatumTest.out -XDrawDiagnostics NoAddInstanceFieldsCanBeDeclaredInDatumTest.java
- */
-
-public class NoAddInstanceFieldsCanBeDeclaredInDatumTest {
-    record Bad1(int i) {
-        int y;
-    }
-
-    record Good1(int i) {
-        interface I {}
-    }
-
-    record Good2(int i) {
-        static {}
-    }
-
-    record Good3(int i) {
-        enum E {A, B}
-    }
-
-    record Good4(int i) {
-        {}
-    }
-}
--- a/test/langtools/tools/javac/records/fields/NoAddInstanceFieldsCanBeDeclaredInDatumTest.out	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-NoAddInstanceFieldsCanBeDeclaredInDatumTest.java:9:13: compiler.err.record.fields.must.be.in.header
-1 error
--- a/test/langtools/tools/javac/records/records_cant_be_abstract/RecordsCantBeAbstractTest.java	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke negative test for datum classes
- * @compile/fail/ref=RecordsCantBeAbstractTest.out -XDrawDiagnostics RecordsCantBeAbstractTest.java
- */
-
-abstract record RecordsCantBeAbstractTest(int i) {}
--- a/test/langtools/tools/javac/records/records_cant_be_abstract/RecordsCantBeAbstractTest.out	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-RecordsCantBeAbstractTest.java:7:1: compiler.err.record.cant.be.abstract
-1 error
--- a/test/langtools/tools/javac/records/records_cant_have_extends_clause/RecordsCantHaveExtendsClause.java	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary check that a datum can inherit from DataClass or an abstract datum class
- * @compile/fail/ref=RecordsCantHaveExtendsClause.out -XDrawDiagnostics RecordsCantHaveExtendsClause.java
- */
-
-import java.io.Serializable;
-
-class RecordsCantHaveExtendsClause {
-
-    // even Object which is the implicit super class for records
-    record R1(int x) extends Object {}
-
-    record R2(int y) {}
-
-    // can't extend other records either
-    record R3(int y, int x) extends R2(y) {}
-
-    // records can implement interfaces
-    record R4() implements Serializable, Runnable {
-        public void run() {}
-    }
-}
--- a/test/langtools/tools/javac/records/records_cant_have_extends_clause/RecordsCantHaveExtendsClause.out	Thu Aug 15 19:02:11 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-RecordsCantHaveExtendsClause.java:12:21: compiler.err.expected: '{'
-RecordsCantHaveExtendsClause.java:17:28: compiler.err.expected: '{'
-2 errors