Skip to content

Commit 5338585

Browse files
authored
java.lang.Record is a super type of all records. (#6070)
* Record is a super type of all records. * Inference doesn't work with records. * Change skipDef. * Don't run inference on records. * Delete test. * Fix Javadoc.
1 parent 7435d61 commit 5338585

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

checker/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ task ainferTestCheckerGenerateStubs(type: Test) {
409409
delete('tests/ainfer-testchecker/annotated/UsesAnonymous.java')
410410
delete('tests/ainfer-testchecker/annotated/AnonymousClassWithField.java')
411411

412+
// This test outputs a warning about records.
413+
delete('tests/ainfer-testchecker/annotated/all-systems/java17/Issue6069.java')
414+
412415
// This test causes an error when its corresponding stub file is read, because the test
413416
// contains an annotation definition. The stub file parser does not support reading
414417
// files that define annotations; this test can be reinstated if the stub parser

checker/src/test/java/org/checkerframework/checker/test/junit/ainferrunners/AinferTestCheckerJaifsGenerationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AinferTestCheckerJaifsGenerationTest(List<File> testFiles) {
2626
"ainfer-testchecker/non-annotated",
2727
"-Ainfer=jaifs",
2828
// The AFU's JAIF reading/writing libraries don't support records.
29-
"-AskipDefs=SimpleRecord",
29+
"-AskipDefs=SimpleRecord|MyRecord",
3030
// Use a stub file here, even though this is a JAIF test. This test can't pass
3131
// without an external file that specifies that a method is pure, and there is no
3232
// way to directly pass a JAIF file (in a real WPI run, the JAIF file's annotations

checker/src/test/java/org/checkerframework/checker/test/junit/ainferrunners/AinferTestCheckerJaifsValidationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public AinferTestCheckerJaifsValidationTest(List<File> testFiles) {
2525
AinferTestCheckerJaifsGenerationTest.class,
2626
"-Awarns",
2727
// The AFU's JAIF reading/writing libraries don't support records.
28-
"-AskipDefs=TestPure|SimpleRecord");
28+
"-AskipDefs=TestPure|SimpleRecord|MyRecord");
2929
}
3030

3131
@Parameters

checker/src/test/java/org/checkerframework/checker/test/junit/ainferrunners/AinferTestCheckerStubsValidationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AinferTestCheckerStubsValidationTest(List<File> testFiles) {
2828
// "-AstubDebug",
2929
"-AmergeStubsWithSource",
3030
"-Awarns",
31-
"-AskipDefs=TestPure");
31+
"-AskipDefs=TestPure|MyRecord");
3232
}
3333

3434
@Parameters

framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeMirror.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,20 @@ protected static AnnotatedDeclaredType createTypeOfObject(AnnotatedTypeFactory a
919919
return objectType;
920920
}
921921

922+
/**
923+
* Create an {@link AnnotatedDeclaredType} with the underlying type of {@code java.lang.Record}.
924+
* It includes any annotations placed by {@link AnnotatedTypeFactory#fromElement(Element)}.
925+
*
926+
* @param atypeFactory type factory to use
927+
* @return AnnotatedDeclaredType for Record
928+
*/
929+
protected static AnnotatedDeclaredType createTypeOfRecord(AnnotatedTypeFactory atypeFactory) {
930+
AnnotatedDeclaredType recordType =
931+
atypeFactory.fromElement(atypeFactory.elements.getTypeElement("java.lang.Record"));
932+
recordType.declaration = false;
933+
return recordType;
934+
}
935+
922936
/**
923937
* Returns the result of calling {@code underlyingType.toString().hashcode()}. This method saves
924938
* the result in a field so that it isn't recomputed each time.

framework/src/main/java/org/checkerframework/framework/type/SupertypeFinder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ private List<AnnotatedDeclaredType> supertypesFromTree(
327327
atypeFactory.getAnnotatedTypeFromTypeTree(classTree.getExtendsClause());
328328
supertypes.add(adt);
329329
} else if (!ElementUtils.isObject(TreeUtils.elementFromDeclaration(classTree))) {
330-
supertypes.add(AnnotatedTypeMirror.createTypeOfObject(atypeFactory));
330+
if (classTree.getKind().name().contentEquals("RECORD")) {
331+
supertypes.add(AnnotatedTypeMirror.createTypeOfRecord(atypeFactory));
332+
} else {
333+
supertypes.add(AnnotatedTypeMirror.createTypeOfObject(atypeFactory));
334+
}
331335
}
332336

333337
for (Tree implemented : classTree.getImplementsClause()) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @below-java17-jdk-skip-test
2+
package issue6069;
3+
4+
public class Issue6069 {
5+
public interface MyInterface {
6+
Record foo();
7+
}
8+
9+
public static class MyClass implements MyInterface {
10+
11+
public MyRecord foo() {
12+
return new MyRecord(42);
13+
}
14+
15+
record MyRecord(int bar) {}
16+
17+
public static void main(String[] args) {
18+
MyClass f = new MyClass();
19+
System.out.println(f.foo());
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)