Skip to content

Commit

Permalink
Fix some crashes involving records
Browse files Browse the repository at this point in the history
Fixes #2324, #2323, #2322

PiperOrigin-RevId: 373431735
  • Loading branch information
cushon authored and Error Prone Team committed May 12, 2021
1 parent cafdc69 commit de28b3e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1311,11 +1311,15 @@ public static Matcher<Tree> hasAnyAnnotation(List<? extends TypeMirror> mirrors)
private static final ImmutableSet<Kind> DECLARATION =
Sets.immutableEnumSet(Kind.LAMBDA_EXPRESSION, Kind.CLASS, Kind.ENUM, Kind.INTERFACE);

private static boolean isDeclaration(Kind kind) {
return DECLARATION.contains(kind) || kind.name().equals("RECORD");
}

public static boolean methodCallInDeclarationOfThrowingRunnable(VisitorState state) {
return stream(state.getPath())
// Find the nearest definitional context for this method invocation
// (i.e.: the nearest surrounding class or lambda)
.filter(t -> DECLARATION.contains(t.getKind()))
.filter(t -> isDeclaration(t.getKind()))
.findFirst()
.map(t -> isThrowingFunctionalInterface(getType(t), state))
.orElseThrow(VerifyException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public static boolean isAtLeast15() {
return MAJOR >= 15;
}

/** Returns true if the current runtime is JDK 16 or newer. */
public static boolean isAtLeast16() {
return MAJOR >= 16;
}

/**
* Returns the latest {@code --release} version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ private static boolean isInstance(Tree tree) {
case INTERFACE:
return false;
default:
if (tree.getKind().name().equals("RECORD")) {
return false;
}
throw new AssertionError("unknown member type:" + tree.getKind());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ public Violation visitType(Type type, Void s) {
case CLASS:
break;
default:
if (type.tsym.getKind().name().equals("RECORD")) {
break;
}
throw new AssertionError(String.format("Unexpected type kind %s", type.tsym.getKind()));
}
if (WellKnownMutability.isAnnotation(state, type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -286,4 +289,24 @@ public void inner() {
"}")
.doTest();
}

@Test
public void record() {
assumeTrue(RuntimeVersion.isAtLeast15());
compilationHelper
.addSourceLines(
"ExampleClass.java",
"package example;",
"public final class ExampleClass {",
" public record OtherRecord(String value) {}",
" public record SomeRecord(OtherRecord value) {",
" public static SomeRecord fromValue(final OtherRecord value) {",
" return new SomeRecord(value);",
" }",
" }",
" private ExampleClass() {",
" }",
"}")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -345,4 +348,18 @@ public void abstractClass_noPrivateConstructor() {
.expectUnchanged()
.doTest();
}

@Test
public void record() {
assumeTrue(RuntimeVersion.isAtLeast15());
testHelper
.addInputLines(
"ExampleUtilityClass.java",
"package example;",
"public final class ExampleUtilityClass {",
" public record SomeRecord(String value) {}",
"}")
.expectUnchanged()
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -285,4 +288,24 @@ public void defaultMethod() {
"}")
.doTest();
}

@Test
public void record() {
assumeTrue(RuntimeVersion.isAtLeast15());
testHelper
.addSourceLines(
"ExampleRecord.java",
"package example;",
"import java.io.IOException;",
"import java.nio.file.Files;",
"import java.nio.file.Path;",
"import java.util.stream.Stream;",
"record ExampleRecord(Path path) {",
" public Stream<Path> list() throws IOException {",
" // BUG: Diagnostic contains: should be closed",
" return Files.list(path);",
" }",
"}")
.doTest();
}
}

0 comments on commit de28b3e

Please sign in to comment.