Skip to content

Commit

Permalink
[refactoring] Wrap calls to Types.subst and Types.memberType (#1115)
Browse files Browse the repository at this point in the history
This is in preparation to attempt to fix #1091. Just a refactoring, no
behavior changes.
  • Loading branch information
msridhar authored Dec 26, 2024
1 parent 17df87f commit 7cb6b98
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uber.nullaway.dataflow;

import static com.uber.nullaway.NullabilityUtil.castToNonNull;
import static com.uber.nullaway.Nullness.NONNULL;
import static com.uber.nullaway.Nullness.NULLABLE;

Expand All @@ -16,6 +17,7 @@
import com.uber.nullaway.Config;
import com.uber.nullaway.NullabilityUtil;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.generics.TypeSubstitutionUtils;
import com.uber.nullaway.handlers.Handler;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -104,7 +106,9 @@ private static NullnessStore lambdaInitialStore(
// This obtains the types of the functional interface method parameters with preserved
// annotations in case of generic type arguments. Only used in JSpecify mode.
List<Type> overridenMethodParamTypeList =
types.memberType(ASTHelpers.getType(code), fiMethodSymbol).getParameterTypes();
TypeSubstitutionUtils.memberType(
types, castToNonNull(ASTHelpers.getType(code)), fiMethodSymbol)
.getParameterTypes();
// If fiArgumentPositionNullness[i] == null, parameter position i is unannotated
Nullness[] fiArgumentPositionNullness = new Nullness[fiMethodParameters.size()];
boolean isFIAnnotated =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ public static void compareGenericTypeParameterNullabilityForCall(
enclosingType = methodSymbol.owner.type;
}
if (enclosingType != null) {
invokedMethodType = state.getTypes().memberType(enclosingType, methodSymbol);
invokedMethodType =
TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol);
}
}
// substitute type arguments for generic methods
Expand Down Expand Up @@ -694,7 +695,8 @@ public static void checkTypeParameterNullnessForMethodOverriding(
// Obtain type parameters for the overridden method within the context of the overriding
// method's class
Type methodWithTypeParams =
state.getTypes().memberType(overridingMethod.owner.type, overriddenMethod);
TypeSubstitutionUtils.memberType(
state.getTypes(), overridingMethod.owner.type, overriddenMethod);

checkTypeParameterNullnessForOverridingMethodReturnType(
tree, methodWithTypeParams, analysis, state);
Expand Down Expand Up @@ -772,7 +774,8 @@ public static Nullness getGenericMethodReturnTypeNullness(
// annotation should have been handled by the caller)
return Nullness.NONNULL;
}
Type overriddenMethodType = state.getTypes().memberType(enclosingType, method);
Type overriddenMethodType =
TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, method);
verify(
overriddenMethodType instanceof ExecutableType,
"expected ExecutableType but instead got %s",
Expand Down Expand Up @@ -873,7 +876,8 @@ private static Type substituteTypeArgsInGenericMethodType(

Type.ForAll forAllType = (Type.ForAll) methodSymbol.type;
Type.MethodType underlyingMethodType = (Type.MethodType) forAllType.qtype;
return state.getTypes().subst(underlyingMethodType, forAllType.tvars, explicitTypeArgs);
return TypeSubstitutionUtils.subst(
state.getTypes(), underlyingMethodType, forAllType.tvars, explicitTypeArgs);
}

/**
Expand Down Expand Up @@ -1007,7 +1011,7 @@ public static Nullness getGenericMethodParameterNullness(
// @Nullable annotation is handled elsewhere)
return Nullness.NONNULL;
}
Type methodType = state.getTypes().memberType(enclosingType, method);
Type methodType = TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, method);
Type paramType = methodType.getParameterTypes().get(parameterIndex);
return getTypeNullness(paramType, config);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.uber.nullaway.generics;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.List;

/** Utility method related to substituting type arguments for type variables. */
public class TypeSubstitutionUtils {

/**
* Returns the type of {@code sym} as a member of {@code t}.
*
* @param types the {@link Types} instance
* @param t the enclosing type
* @param sym the symbol
* @return the type of {@code sym} as a member of {@code t}
*/
public static Type memberType(Types types, Type t, Symbol sym) {
return types.memberType(t, sym);
}

/**
* Substitutes the types in {@code to} for the types in {@code from} in {@code t}.
*
* @param types the {@link Types} instance
* @param t the type to which to perform the substitution
* @param from the types that will be substituted out
* @param to the types that will be substituted in
* @return the type resulting from the substitution
*/
public static Type subst(Types types, Type t, List<Type> from, List<Type> to) {
return types.subst(t, from, to);
}
}

0 comments on commit 7cb6b98

Please sign in to comment.