Skip to content

Commit

Permalink
Only capture fields that are not on the LHS of assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst authored Oct 9, 2024
1 parent 238e276 commit 09f423a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.PrimitiveTypeTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.TypeCastTree;
import com.sun.source.tree.UnaryTree;
import com.sun.source.tree.WildcardTree;
import com.sun.source.util.TreePath;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -271,11 +273,24 @@ public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree tree, AnnotatedTyp
return AnnotatedTypes.asSuper(
f, thisType, AnnotatedTypeMirror.createType(superTypeMirror, f, false));
} else {
// tree must be a field access, so get the type of the (receiver) expression, and then call
// asMemberOf.
// tree must be a field access or an enum constant, so get the type of the (receiver)
// expression, and then call asMemberOf.
AnnotatedTypeMirror typeOfReceiver = f.getAnnotatedType(tree.getExpression());
typeOfReceiver = f.applyCaptureConversion(typeOfReceiver);
return f.applyCaptureConversion(AnnotatedTypes.asMemberOf(f.types, f, typeOfReceiver, elt));
AnnotatedTypeMirror typeOfFieldAccess =
AnnotatedTypes.asMemberOf(f.types, f, typeOfReceiver, elt);
TreePath path = f.getPath(tree);

// Only capture the type if this is not the left hand side of an assignment.
if (path != null && path.getParentPath().getLeaf().getKind() == Kind.ASSIGNMENT) {
AssignmentTree assignmentTree = (AssignmentTree) path.getParentPath().getLeaf();
@SuppressWarnings("interning:not.interned") // Looking for exact object.
boolean leftHandSide = assignmentTree.getExpression() != tree;
if (leftHandSide) {
return typeOfFieldAccess;
}
}
return f.applyCaptureConversion(typeOfFieldAccess);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public int hashCode() {

@Override
public TypeMirror getJavaType() {
return typeMirror;
return type.getUnderlyingType();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public int hashCode() {

@Override
public TypeMirror getJavaType() {
return properType;
return type.getUnderlyingType();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public UseOfVariable getErased() {

@Override
public TypeVariable getJavaType() {
return variable.typeVariableJava;
return variable.typeVariable.getUnderlyingType();
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions framework/tests/all-systems/Issue6825.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all") // Just check for crashes.
public class Issue6825 {
static class ClassA<T extends Number> {}
Expand All @@ -9,4 +12,13 @@ void method(Number n) {
var y = flag ? f : new ClassA<Number>();
var x = flag ? this.f : new ClassA<Number>();
}

static class SomeClass {}

private List<? extends SomeClass> typeParameters = null;

public Issue6825(Issue6825 other) {
this.typeParameters =
other.typeParameters == null ? null : new ArrayList<>(other.typeParameters);
}
}

0 comments on commit 09f423a

Please sign in to comment.