Skip to content

Commit

Permalink
java.lang.Record is a super type of all records. (#6070)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
smillst committed Jul 3, 2023
1 parent 7435d61 commit 5338585
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
3 changes: 3 additions & 0 deletions checker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ task ainferTestCheckerGenerateStubs(type: Test) {
delete('tests/ainfer-testchecker/annotated/UsesAnonymous.java')
delete('tests/ainfer-testchecker/annotated/AnonymousClassWithField.java')

// This test outputs a warning about records.
delete('tests/ainfer-testchecker/annotated/all-systems/java17/Issue6069.java')

// This test causes an error when its corresponding stub file is read, because the test
// contains an annotation definition. The stub file parser does not support reading
// files that define annotations; this test can be reinstated if the stub parser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AinferTestCheckerJaifsGenerationTest(List<File> testFiles) {
"ainfer-testchecker/non-annotated",
"-Ainfer=jaifs",
// The AFU's JAIF reading/writing libraries don't support records.
"-AskipDefs=SimpleRecord",
"-AskipDefs=SimpleRecord|MyRecord",
// Use a stub file here, even though this is a JAIF test. This test can't pass
// without an external file that specifies that a method is pure, and there is no
// way to directly pass a JAIF file (in a real WPI run, the JAIF file's annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AinferTestCheckerJaifsValidationTest(List<File> testFiles) {
AinferTestCheckerJaifsGenerationTest.class,
"-Awarns",
// The AFU's JAIF reading/writing libraries don't support records.
"-AskipDefs=TestPure|SimpleRecord");
"-AskipDefs=TestPure|SimpleRecord|MyRecord");
}

@Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public AinferTestCheckerStubsValidationTest(List<File> testFiles) {
// "-AstubDebug",
"-AmergeStubsWithSource",
"-Awarns",
"-AskipDefs=TestPure");
"-AskipDefs=TestPure|MyRecord");
}

@Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,20 @@ protected static AnnotatedDeclaredType createTypeOfObject(AnnotatedTypeFactory a
return objectType;
}

/**
* Create an {@link AnnotatedDeclaredType} with the underlying type of {@code java.lang.Record}.
* It includes any annotations placed by {@link AnnotatedTypeFactory#fromElement(Element)}.
*
* @param atypeFactory type factory to use
* @return AnnotatedDeclaredType for Record
*/
protected static AnnotatedDeclaredType createTypeOfRecord(AnnotatedTypeFactory atypeFactory) {
AnnotatedDeclaredType recordType =
atypeFactory.fromElement(atypeFactory.elements.getTypeElement("java.lang.Record"));
recordType.declaration = false;
return recordType;
}

/**
* Returns the result of calling {@code underlyingType.toString().hashcode()}. This method saves
* the result in a field so that it isn't recomputed each time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ private List<AnnotatedDeclaredType> supertypesFromTree(
atypeFactory.getAnnotatedTypeFromTypeTree(classTree.getExtendsClause());
supertypes.add(adt);
} else if (!ElementUtils.isObject(TreeUtils.elementFromDeclaration(classTree))) {
supertypes.add(AnnotatedTypeMirror.createTypeOfObject(atypeFactory));
if (classTree.getKind().name().contentEquals("RECORD")) {
supertypes.add(AnnotatedTypeMirror.createTypeOfRecord(atypeFactory));
} else {
supertypes.add(AnnotatedTypeMirror.createTypeOfObject(atypeFactory));
}
}

for (Tree implemented : classTree.getImplementsClause()) {
Expand Down
22 changes: 22 additions & 0 deletions framework/tests/all-systems/java17/Issue6069.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @below-java17-jdk-skip-test
package issue6069;

public class Issue6069 {
public interface MyInterface {
Record foo();
}

public static class MyClass implements MyInterface {

public MyRecord foo() {
return new MyRecord(42);
}

record MyRecord(int bar) {}

public static void main(String[] args) {
MyClass f = new MyClass();
System.out.println(f.foo());
}
}
}

0 comments on commit 5338585

Please sign in to comment.