Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with implicit equals() methods in interfaces #898

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.auto.service.AutoService;
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -399,16 +400,31 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
if (!withinAnnotatedCode(state)) {
return Description.NO_MATCH;
}
final Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
if (methodSymbol == null) {
throw new RuntimeException("not expecting unresolved method here");
}
Symbol.MethodSymbol methodSymbol = getSymbolForMethodInvocation(tree, state);
handler.onMatchMethodInvocation(this, tree, state, methodSymbol);
// assuming this list does not include the receiver
List<? extends ExpressionTree> actualParams = tree.getArguments();
return handleInvocation(tree, state, methodSymbol, actualParams);
}

private static Symbol.MethodSymbol getSymbolForMethodInvocation(
MethodInvocationTree tree, VisitorState state) {
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
Verify.verify(methodSymbol != null, "not expecting unresolved method here");
// For interface methods, if the method is an implicit method corresponding to a method from
// java.lang.Object, use the symbol for the java.lang.Object method instead. We do this to
// properly treat the method as unannotated, which is particularly important for equals()
// methods. This is an adaptation to a change in JDK 18; see
// https://bugs.openjdk.org/browse/JDK-8272564
if (methodSymbol.owner.isInterface()) {
Symbol.MethodSymbol baseSymbol = (Symbol.MethodSymbol) methodSymbol.baseSymbol();
if (baseSymbol != methodSymbol && baseSymbol.owner == state.getSymtab().objectType.tsym) {
methodSymbol = baseSymbol;
}
}
return methodSymbol;
}
Comment on lines +419 to +426
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm understanding this wrong, but previously we would get a symbol from the interface, which would eventually be considered annotated in the caller (matchMethodInvocation and later handleInvocation).

Now that we have this special handling to resolve to the base symbol (i.e., from java.lang.Object), which would be considered unannotated (everything from the JDK is unannotated), and then NullAway won't think that equals must have a nonnull param. Right?

If so, this change looks good to me! (I also agree that this is the interim solution and the end goal is for NullAway to enforce nullable on equals, but that's for another PR)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuxincs yes, this is exactly correct.


@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
if (!withinAnnotatedCode(state)) {
Expand Down
16 changes: 16 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayCoreTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -985,4 +985,20 @@ public void cfgConstructionSymbolCompletionFailure() {
"}")
.doTest();
}

@Test
public void testDefaultEqualsInInterfaceTakesNullable() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, this test would fail in the :nullaway:testJdk21 task

defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class Test {",
" public interface AnInterface {}",
" public static boolean foo(AnInterface a, @Nullable AnInterface b) {",
" return a.equals(b);",
" }",
"}")
.doTest();
}
}