Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -2236,8 +2236,7 @@ public Void visitMethodInvocation(MethodInvocationTree tree, Void p) {
List<AnnotatedTypeMirror> typeargs = mType.typeArgs;

List<AnnotatedTypeParameterBounds> paramBounds =
CollectionsPlume.mapList(
AnnotatedTypeVariable::getBounds, invokedMethod.getTypeVariables());
atypeFactory.methodTypeVariableBoundsFromUse(tree, invokedMethod);

ExecutableElement method = invokedMethod.getElement();
CharSequence methodName = ElementUtils.getSimpleDescription(method);
Expand Down Expand Up @@ -2638,8 +2637,7 @@ public Void visitNewClass(NewClassTree tree, Void p) {
checkVarargs(constructorType, tree);

List<AnnotatedTypeParameterBounds> paramBounds =
CollectionsPlume.mapList(
AnnotatedTypeVariable::getBounds, constructorType.getTypeVariables());
atypeFactory.constructorTypeVariableBoundsFromUse(tree, constructorType);

checkTypeArguments(
tree,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,116 @@ public List<AnnotatedTypeParameterBounds> typeVariablesFromUse(
return res;
}

/**
* Returns the method type-variable bounds adapted to the viewpoint of a method invocation.
*
* @param tree a method invocation
* @param invokedMethod the type of the invoked method
* @return the adapted method type parameter bounds
*/
public List<AnnotatedTypeParameterBounds> methodTypeVariableBoundsFromUse(
MethodInvocationTree tree, AnnotatedExecutableType invokedMethod) {
List<AnnotatedTypeParameterBounds> bounds =
CollectionsPlume.mapList(
AnnotatedTypeVariable::getBounds, invokedMethod.getTypeVariables());

AnnotatedTypeMirror receiverType = getMethodReceiverType(tree);
if (viewpointAdapter != null && receiverType != null) {
viewpointAdapter.viewpointAdaptTypeParameterBounds(receiverType, bounds);
}
return bounds;
}

/**
* Returns the constructor type-variable bounds adapted to the viewpoint of a constructor
* invocation.
*
* @param tree a constructor invocation
* @param invokedConstructor the type of the invoked constructor
* @return the adapted constructor type parameter bounds
*/
public List<AnnotatedTypeParameterBounds> constructorTypeVariableBoundsFromUse(
NewClassTree tree, AnnotatedExecutableType invokedConstructor) {
List<AnnotatedTypeParameterBounds> bounds =
CollectionsPlume.mapList(
AnnotatedTypeVariable::getBounds, invokedConstructor.getTypeVariables());

if (viewpointAdapter != null) {
AnnotatedTypeMirror receiverType = getConstructorReceiverType(tree);
viewpointAdapter.viewpointAdaptTypeParameterBounds(receiverType, bounds);
}
return bounds;
}

/**
* Returns the receiver type used to viewpoint-adapt a constructor invocation.
*
* @param tree a constructor invocation tree
* @return the receiver type
* @see #getReceiverType(ExpressionTree)
* @see #getMethodReceiverType(MethodInvocationTree)
*/
public AnnotatedDeclaredType getConstructorReceiverType(NewClassTree tree) {
// Get the annotations written on the new class tree.
AnnotatedDeclaredType type =
(AnnotatedDeclaredType) toAnnotatedType(TreeUtils.typeOf(tree), false);
if (!TreeUtils.isDiamondTree(tree)) {
if (tree.getClassBody() == null) {
type.setTypeArguments(getExplicitNewClassClassTypeArgs(tree));
}
} else {
type = getAnnotatedType(TypesUtils.getTypeElement(type.underlyingType));
// Add explicit annotations below.
type.clearAnnotations();
}

AnnotationMirrorSet explicitAnnos = getExplicitNewClassAnnos(tree);
type.addAnnotations(explicitAnnos);

// Get the enclosing type of the constructor, if one exists.
// this.new InnerClass()
AnnotatedDeclaredType enclosingType = (AnnotatedDeclaredType) getReceiverType(tree);
if (enclosingType != null && enclosingType.isFrozen()) {
// getReceiverType may return a shared frozen cache value; it is embedded in `type` and
// mutated by addComputedTypeAnnotations below, so copy it first.
enclosingType = enclosingType.deepCopy();
}
type.setEnclosingType(enclosingType);

// Add computed annotations to the type.
addComputedTypeAnnotations(tree, type);

return type;
}

/**
* Returns the receiver type used to viewpoint-adapt a method invocation.
*
* @param tree a method invocation tree
* @return the receiver type, or null if the invocation has no receiver
* @see #getReceiverType(ExpressionTree)
* @see #getConstructorReceiverType(NewClassTree)
*/
public @Nullable AnnotatedTypeMirror getMethodReceiverType(MethodInvocationTree tree) {
ExecutableElement methodElt = TreeUtils.elementFromUse(tree);
if (ElementUtils.isStatic(methodElt)) {
return null;
}

AnnotatedTypeMirror receiverType = getReceiverType(tree);
if (receiverType == null
&& (TreeUtils.isSuperConstructorCall(tree)
|| TreeUtils.isThisConstructorCall(tree))) {
// super() and this() calls don't have a receiver, but they should be view-point adapted
// as if "this" is the receiver.
receiverType = getSelfType(tree);
}
if (receiverType != null && receiverType.getKind() == TypeKind.DECLARED) {
receiverType = applyCaptureConversion(receiverType);
}
return receiverType;
}

/**
* Creates and returns an AnnotatedNullType qualified with {@code annotations}.
*
Expand Down Expand Up @@ -2638,6 +2748,8 @@ private boolean isSameType(TypeMirror type1, TypeMirror type2) {
*
* @param expression the expression for which to determine the receiver type
* @return the type of the receiver of expression
* @see #getMethodReceiverType(MethodInvocationTree)
* @see #getConstructorReceiverType(NewClassTree)
*/
public final @Nullable AnnotatedTypeMirror getReceiverType(ExpressionTree expression) {
AnnotatedTypeMirror receiverType;
Expand Down Expand Up @@ -2759,17 +2871,7 @@ public ParameterizedExecutableType methodFromUseWithoutTypeArgInference(
protected ParameterizedExecutableType methodFromUse(
MethodInvocationTree tree, boolean inferTypeArgs) {
ExecutableElement methodElt = TreeUtils.elementFromUse(tree);
AnnotatedTypeMirror receiverType = getReceiverType(tree);
if (receiverType == null
&& (TreeUtils.isSuperConstructorCall(tree)
|| TreeUtils.isThisConstructorCall(tree))) {
// super() and this() calls don't have a receiver, but they should be view-point adapted
// as if "this" is the receiver.
receiverType = getSelfType(tree);
}
if (receiverType != null && receiverType.getKind() == TypeKind.DECLARED) {
receiverType = applyCaptureConversion(receiverType);
}
AnnotatedTypeMirror receiverType = getMethodReceiverType(tree);

ParameterizedExecutableType result =
methodFromUse(tree, methodElt, receiverType, inferTypeArgs);
Expand Down Expand Up @@ -3497,34 +3599,7 @@ public AnnotatedTypeMirror getResultingTypeOfConstructorMemberReference(
*/
protected ParameterizedExecutableType constructorFromUse(
NewClassTree tree, boolean inferTypeArgs) {
// Get the annotations written on the new class tree.
AnnotatedDeclaredType type =
(AnnotatedDeclaredType) toAnnotatedType(TreeUtils.typeOf(tree), false);
if (!TreeUtils.isDiamondTree(tree)) {
if (tree.getClassBody() == null) {
type.setTypeArguments(getExplicitNewClassClassTypeArgs(tree));
}
} else {
type = getAnnotatedType(TypesUtils.getTypeElement(type.underlyingType));
// Add explicit annotations below.
type.clearAnnotations();
}

AnnotationMirrorSet explicitAnnos = getExplicitNewClassAnnos(tree);
type.addAnnotations(explicitAnnos);

// Get the enclosing type of the constructor, if one exists.
// this.new InnerClass()
AnnotatedDeclaredType enclosingType = (AnnotatedDeclaredType) getReceiverType(tree);
if (enclosingType != null && enclosingType.isFrozen()) {
// getReceiverType may return a shared frozen cache value; it is embedded in `type` and
// mutated by addComputedTypeAnnotations below, so copy it first.
enclosingType = enclosingType.deepCopy();
}
type.setEnclosingType(enclosingType);

// Add computed annotations to the type.
addComputedTypeAnnotations(tree, type);
AnnotatedDeclaredType type = getConstructorReceiverType(tree);

ExecutableElement ctor = TreeUtils.elementFromUse(tree);
AnnotatedExecutableType con = getAnnotatedType(ctor); // get unsubstituted type
Expand Down Expand Up @@ -3623,9 +3698,9 @@ protected ParameterizedExecutableType constructorFromUse(
addDefaultAnnotations(returnType);
con.setReturnType(returnType);
}
if (enclosingType != null) {
if (type.getEnclosingType() != null) {
// Reset the enclosing type because it can be substituted incorrectly.
((AnnotatedDeclaredType) con.getReturnType()).setEnclosingType(enclosingType);
((AnnotatedDeclaredType) con.getReturnType()).setEnclosingType(type.getEnclosingType());
}
if (type.isUnderlyingTypeRaw() || TypesUtils.isRaw(TreeUtils.typeOf(tree))) {
((AnnotatedDeclaredType) con.getReturnType()).setIsUnderlyingTypeRaw();
Expand Down
19 changes: 19 additions & 0 deletions framework/tests/viewpointtest/ConstructorTypeVariableBounds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import viewpointtest.quals.*;

public class ConstructorTypeVariableBounds {
static class MyClass {
<T extends @ReceiverDependentQual Object> MyClass(T t) {}
}

void test(@Top Object top, @A Object a, @B Object b, @Bottom Object bottom) {

// :: error: (type.argument.type.incompatible) :: error: (new.class.type.invalid)
new <@Top Object>@Top MyClass(top);

// :: warning: (cast.unsafe.constructor.invocation)
new <@A Object>@A MyClass(a);

// :: warning: (cast.unsafe.constructor.invocation)
new <@B Object>@B MyClass(b);
}
}
72 changes: 72 additions & 0 deletions framework/tests/viewpointtest/MethodTypeVariableBounds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import viewpointtest.quals.*;

public class MethodTypeVariableBounds {
static class Methods {
<T extends @ReceiverDependentQual Object> void noArg() {}

<T extends @ReceiverDependentQual Object> void withArg(T t) {}
}

void topReceiver(
@Top Methods methods,
@Top Object top,
@A Object a,
@B Object b,
@Bottom Object bottom) {
// @Top viewpoint-adapts @ReceiverDependentQual to @Lost, so only @Bottom is within the
// adapted method type parameter bound.
// :: error: (type.argument.type.incompatible)
methods.noArg();

// :: error: (type.argument.type.incompatible)
methods.<@Top Object>withArg(top);

// :: error: (type.argument.type.incompatible)
methods.<@A Object>withArg(a);

// :: error: (type.argument.type.incompatible)
methods.<@B Object>withArg(b);

methods.<@Bottom Object>withArg(bottom);

// :: error: (type.arguments.not.inferred)
methods.withArg(top);

// :: error: (type.arguments.not.inferred)
methods.withArg(a);

// :: error: (type.arguments.not.inferred)
methods.withArg(b);

methods.withArg(bottom);
}

void aReceiver(
@A Methods methods, @Top Object top, @A Object a, @B Object b, @Bottom Object bottom) {
// @A viewpoint-adapts @ReceiverDependentQual to @A, so @A and @Bottom are within the
// adapted method type parameter bound.
// :: error: (type.argument.type.incompatible)
methods.noArg();

// :: error: (type.argument.type.incompatible)
methods.<@Top Object>withArg(top);

methods.<@A Object>withArg(a);

// :: error: (type.argument.type.incompatible)
methods.<@B Object>withArg(b);

methods.<@Bottom Object>withArg(bottom);

// :: error: (type.arguments.not.inferred)
methods.withArg(top);

// :: error: (type.arguments.not.inferred)
methods.withArg(a);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this line should not issue an error.

Let me think about how to combine this with #1850.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The test cases are from #1850 and I didn't touch them. The point of this branch is extracting the logic... which isn't re-used anymore (as it was in that earlier version of #1850), but it might still be nice to extract.


// :: error: (type.arguments.not.inferred)
methods.withArg(b);

methods.withArg(bottom);
}
}
Loading