Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
haewiful committed Feb 22, 2025
1 parent 461c309 commit 48bb542
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ public class InferTypeVisitor extends Types.DefaultTypeVisitor<@Nullable Map<Typ
@Override
public @Nullable Map<Type, Type> visitClassType(Type.ClassType rhsType, Type lhsType) {
Map<Type, Type> genericNullness = new HashMap<>();
// for each type parameter, call accept with this visitor and add all results to one map
com.sun.tools.javac.util.List<Type> rhsTypeArguments = rhsType.getTypeArguments();
com.sun.tools.javac.util.List<Type> lhsTypeArguments =
((Type.ClassType) lhsType).getTypeArguments();
// get the inferred type for each type arguments and add them to genericNullness
for (int i = 0; i < rhsTypeArguments.size(); i++) {
Type rhsTypeArg = rhsTypeArguments.get(i);
Type lhsTypeArg = lhsTypeArguments.get(i);
// get the inferred type for each type arguments and add them to genericNullness
Map<Type, Type> map = rhsTypeArg.accept(this, lhsTypeArg);
if (map != null) {
genericNullness.putAll(map);
Expand All @@ -36,7 +35,7 @@ public class InferTypeVisitor extends Types.DefaultTypeVisitor<@Nullable Map<Typ
}

@Override
public Map<Type, Type> visitTypeVar(Type.TypeVar rhsType, Type lhsType) { // type variable itself
public Map<Type, Type> visitTypeVar(Type.TypeVar rhsType, Type lhsType) {
Map<Type, Type> genericNullness = new HashMap<>();
Boolean isLhsNullable =
Nullness.hasNullableAnnotation(lhsType.getAnnotationMirrors().stream(), config);
Expand All @@ -45,7 +44,7 @@ public Map<Type, Type> visitTypeVar(Type.TypeVar rhsType, Type lhsType) { // typ
Nullness.hasNullableAnnotation(upperBound.getAnnotationMirrors().stream(), config);
if (!isLhsNullable) { // lhsType is NonNull, we can just use this
genericNullness.put(rhsType, lhsType);
} else if (isRhsNullable) { // lhsType & rhsType is Nullable, can use lhs for inference
} else if (isRhsNullable) { // lhsType & rhsType are Nullable, can use lhs for inference
genericNullness.put(rhsType, lhsType);
} else { // rhs can't be nullable, use upperbound
genericNullness.put(rhsType, upperBound);
Expand All @@ -55,6 +54,7 @@ public Map<Type, Type> visitTypeVar(Type.TypeVar rhsType, Type lhsType) { // typ

@Override
public @Nullable Map<Type, Type> visitArrayType(Type.ArrayType rhsType, Type lhsType) {
// unwrap the type of the array and call accept on it
Type rhsComponentType = rhsType.elemtype;
Type lhsComponentType = ((Type.ArrayType) lhsType).elemtype;
Map<Type, Type> genericNullness = rhsComponentType.accept(this, lhsComponentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ public void genericInferenceOnAssignmentsWithArrays() {
" Foo<Foo<@Nullable Object>[]> f8 = Foo.test1Nonnull(null);",
" Foo<Object>[] f9 = Foo.test2Null(new Object());",
" Foo<@Nullable Object>[] f10 = Foo.test2Null(new Object());",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
" Foo<Object>[] f11 = Foo.test2Null(null);",
" Foo<@Nullable Object>[] f12 = Foo.test2Null(null);",
" Foo<Object>[] f13 = Foo.test2Nonnull(new Object());",
" // BUG: Diagnostic contains: due to mismatched nullability of type parameters",
" Foo<@Nullable Object>[] f14 = Foo.test2Nonnull(new Object());",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
" Foo<Object>[] f15 = Foo.test2Nonnull(null);",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
" Foo<@Nullable Object>[] f16 = Foo.test2Nonnull(null);",
" }",
" }")
Expand Down

0 comments on commit 48bb542

Please sign in to comment.