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

Don't erase the type argument of receiver #793

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions checker/jtreg/nullness/MethodReceiverTypeErrorMessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @test
* @summary Test case for method receiver type in error message. If it is a non-raw type, it should be printed as such.
*
* @compile/fail/ref=MethodReceiverTypeErrorMessageTest.out -XDrawDiagnostics -processor org.checkerframework.checker.nullness.NullnessChecker MethodReceiverTypeErrorMessageTest.java
*/
public class MethodReceiverTypeErrorMessageTest<T> {

MethodReceiverTypeErrorMessageTest() {
foo();
}

void foo() {}
}

class sub extends MethodReceiverTypeErrorMessageTest<String> {
sub() {
foo();
}
}
7 changes: 7 additions & 0 deletions checker/jtreg/nullness/MethodReceiverTypeErrorMessageTest.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MethodReceiverTypeErrorMessageTest.java:10:12: compiler.err.proc.messager: [method.invocation.invalid] call to foo() not allowed on the given receiver.
found : @UnderInitialization(MethodReceiverTypeErrorMessageTest.class) MethodReceiverTypeErrorMessageTest<T extends @Initialized Object>
required: @Initialized MethodReceiverTypeErrorMessageTest<T extends @Initialized Object>
MethodReceiverTypeErrorMessageTest.java:18:12: compiler.err.proc.messager: [method.invocation.invalid] call to foo() not allowed on the given receiver.
found : @UnderInitialization(sub.class) sub
required: @Initialized MethodReceiverTypeErrorMessageTest<@Initialized String>
2 errors
19 changes: 19 additions & 0 deletions checker/tests/nullness/ReceiverTypeArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

public class ReceiverTypeArgs {
static class Box<T> {
T item;

public Box(T item) {
this.item = item;
}

void test(Box<@NonNull T> this) {}
}

private static void foo(Box<@Nullable String> box) {
// :: error: (method.invocation.invalid)
box.test();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3772,8 +3772,8 @@ protected void checkMethodInvocability(
return;
}

AnnotatedTypeMirror methodReceiver = method.getReceiverType().getErased();
AnnotatedTypeMirror treeReceiver = methodReceiver.shallowCopy(false);
AnnotatedDeclaredType methodReceiver = method.getReceiverType();
AnnotatedDeclaredType treeReceiver = methodReceiver.shallowCopy(false);
AnnotatedTypeMirror rcv = atypeFactory.getReceiverType(tree);
Ao-senXiong marked this conversation as resolved.
Show resolved Hide resolved

treeReceiver.addAnnotations(rcv.getEffectiveAnnotations());
Expand All @@ -3782,10 +3782,10 @@ protected void checkMethodInvocability(
// The diagnostic can be a bit misleading because the check is of the receiver but
// `tree` is the entire method invocation (where the receiver might be implicit).
commonAssignmentCheckStartDiagnostic(methodReceiver, treeReceiver, tree);
boolean success = typeHierarchy.isSubtype(treeReceiver, methodReceiver);
commonAssignmentCheckEndDiagnostic(success, null, methodReceiver, treeReceiver, tree);
boolean success = typeHierarchy.isSubtype(rcv, methodReceiver);
commonAssignmentCheckEndDiagnostic(success, null, methodReceiver, rcv, tree);
if (!success) {
reportMethodInvocabilityError(tree, treeReceiver, methodReceiver);
reportMethodInvocabilityError(tree, rcv, methodReceiver);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static boolean isSelfAccess(ExpressionTree tree) {
* @param tree an expression tree
* @return the outermost non-parenthesized tree enclosed by the given tree
*/
@SuppressWarnings("interning:return.type.incompatible") // pol ymorphism implementation
@SuppressWarnings("interning:return.type.incompatible") // polymorphism implementation
public static @PolyInterned ExpressionTree withoutParens(@PolyInterned ExpressionTree tree) {
ExpressionTree t = tree;
while (t.getKind() == Tree.Kind.PARENTHESIZED) {
Expand Down
Loading