Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2918,13 +2918,12 @@ private TargetTypeAndAssignmentKind getTargetTypeFromParentContext(
&& !isQualifierOf(parentConstructorCall, expressionTree)) {
Type parentClassType = getTreeType(parentConstructorCall, parentState, calledFromDataflow);
if (parentClassType != null) {
Symbol parentCtorSymbol = ASTHelpers.getSymbol(parentConstructorCall);
Type parentCtorType =
TypeSubstitutionUtils.memberType(
state.getTypes(), parentClassType, parentCtorSymbol, config);
return new TargetTypeAndAssignmentKind(
getFormalParameterTypeForArgument(
parentConstructorCall, parentCtorType.asMethodType(), expressionTree),
parentConstructorCall,
constructorTypeAtCall(
parentConstructorCall, parentClassType, parentState, calledFromDataflow),
expressionTree),
false);
}
}
Expand Down Expand Up @@ -3614,11 +3613,14 @@ private static com.sun.tools.javac.util.List<Type> convertTreesToTypes(
}

/**
* Substitutes the type arguments from a generic method invocation into the method's type.
* Substitutes the type arguments of a call to a generic method or constructor into its type: the
* explicit type arguments where the call has them, and otherwise, for a method call, the inferred
* ones. A constructor call without explicit type arguments keeps the constructor's type
* variables.
*
* @param tree the method invocation tree
* @param forAllType the generic method type
* @param path the path to the invocation tree, or null if not available
* @param tree the method invocation or constructor call tree
* @param forAllType the generic method or constructor type
* @param path the path to the call tree, or null if not available
* @param state the visitor state
* @param calledFromDataflow whether this method is being called from dataflow analysis
* @return the substituted method type with library models applied
Expand Down Expand Up @@ -3926,14 +3928,16 @@ private CallAndContext getDirectCallContextForInference(
getTreeType(parentConstructorCall, state.withPath(parentPath), calledFromDataflow);
}
if (parentClassType != null) {
Symbol.MethodSymbol parentCtorSymbol = ASTHelpers.getSymbol(parentConstructorCall);
Type parentCtorType =
TypeSubstitutionUtils.memberType(
state.getTypes(), parentClassType, parentCtorSymbol, config);
return new CallAndContext(
call,
getFormalParameterTypeForArgument(
parentConstructorCall, parentCtorType.asMethodType(), call),
parentConstructorCall,
constructorTypeAtCall(
parentConstructorCall,
parentClassType,
state.withPath(parentPath),
calledFromDataflow),
call),
false);
}
}
Expand All @@ -3943,10 +3947,39 @@ private CallAndContext getDirectCallContextForInference(
}

/**
* Returns the resolved method type at a call site after receiver and inferred type-argument
* substitutions and handler-provided models have been applied. Unlike {@link
* #getExecutableTypeForInference}, this method substitutes the method type variables with
* inferred type arguments.
* Returns the type of the constructor that a constructor call invokes, as a member of the class
* type it instantiates, with the call's explicit type arguments substituted. A call without
* explicit type arguments keeps the constructor's type variables.
*
* @param constructorCall the constructor call
* @param classType the class type the call instantiates
* @param state the visitor state, with the path to the constructor call
* @param calledFromDataflow whether this method is being called from dataflow analysis
* @return the constructor type at the call
*/
private Type.MethodType constructorTypeAtCall(
NewClassTree constructorCall,
Type classType,
VisitorState state,
boolean calledFromDataflow) {
Symbol.MethodSymbol constructorSymbol = ASTHelpers.getSymbol(constructorCall);
Type constructorType =
TypeSubstitutionUtils.memberType(state.getTypes(), classType, constructorSymbol, config);
if (!constructorCall.getTypeArguments().isEmpty()
&& constructorType instanceof Type.ForAll forAllType) {
return substituteTypeArgsInGenericMethodType(
constructorCall, forAllType, state.getPath(), state, calledFromDataflow);
}
return constructorType.asMethodType();
}

/**
* Returns the resolved method type at a call site after receiver and type-argument substitutions
* and handler-provided models have been applied. The type arguments are those of a method call,
* explicit or inferred, and the explicit ones of a constructor call; a constructor call without
* explicit type arguments keeps the constructor's type variables. Unlike {@link
* #getExecutableTypeForInference}, this method substitutes the method type variables with the
* call's type arguments.
*/
private Type.MethodType getInvokedMethodTypeAtCall(
Symbol.MethodSymbol methodSymbol,
Expand All @@ -3961,7 +3994,9 @@ private Type.MethodType getInvokedMethodTypeAtCall(
invokedMethodType =
TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol, config);
}
if (tree instanceof MethodInvocationTree
if ((tree instanceof MethodInvocationTree
|| (tree instanceof NewClassTree newClassTree
&& !newClassTree.getTypeArguments().isEmpty()))
&& invokedMethodType instanceof Type.ForAll forAllType) {
return substituteTypeArgsInGenericMethodType(
tree, forAllType, path, state, calledFromDataflow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,58 @@ static Foo createWithTypeArgPositive() {
.doTest();
}

@Test
public void explicitTypeArgumentsInstantiateAGenericConstructorAsTheyDoAGenericMethod() {
makeHelper()
.addSourceLines(
"Test.java",
"""
package com.uber;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
static class Box<E extends @Nullable Object> {}
static class Consumer {
<U extends @Nullable Object> Consumer(Box<U> input) {}
}
static <U extends @Nullable Object> void consume(Box<U> input) {}
void nullableTypeArgumentToConstructor(Box<@Nullable String> nullable) {
new <@Nullable String>Consumer(nullable);
}
void nullableTypeArgumentToMethod(Box<@Nullable String> nullable) {
Test.<@Nullable String>consume(nullable);
}
void nonNullTypeArgumentToConstructor(Box<@Nullable String> nullable) {
// BUG: Diagnostic contains: incompatible types: Box<@Nullable String> cannot be converted to Box<String>
new <String>Consumer(nullable);
}
void nonNullTypeArgumentToMethod(Box<@Nullable String> nullable) {
// BUG: Diagnostic contains: incompatible types: Box<@Nullable String> cannot be converted to Box<String>
Test.<String>consume(nullable);
}
void nullableTypeArgumentToConstructorWithANonNullBox(Box<String> nonNull) {
// BUG: Diagnostic contains: incompatible types: Box<String> cannot be converted to Box<@Nullable String>
new <@Nullable String>Consumer(nonNull);
}
void inferredTypeArgumentToConstructor(Box<@Nullable String> nullable) {
// BUG: Diagnostic contains: incompatible types: Box<@Nullable String> cannot be converted to Box<U>
new Consumer(nullable);
}
static <T extends @Nullable Object> Box<T> make() {
return new Box<>();
}
void nullableTypeArgumentToConstructorWithAGenericCall() {
new <@Nullable String>Consumer(make());
}
void nullableTypeArgumentToMethodWithAGenericCall() {
Test.<@Nullable String>consume(make());
}
}
""")
.doTest();
}

@Test
public void inferGenericConstructorTypeVariable() {
makeHelper()
Expand Down
Loading