Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix viewpoint adaptation of type variables #799

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
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 @@ -8,6 +8,7 @@
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedPrimitiveType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType;
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.ElementUtils;
import org.plumelib.util.IPair;
Expand Down Expand Up @@ -284,7 +285,12 @@ protected AnnotatedTypeMirror combineAnnotationWithType(
AnnotatedTypeMirror resLower =
combineAnnotationWithType(receiverAnnotation, atv.getLowerBound());
mappings.put(atv.getLowerBound(), resLower);

// The values of the mappings are the viewpoint adapted lower and upper bounds,
// and we wish to replace the old bounds of atv with the new mappings.
// However, we need to first remove the primary annotations of atv, otherwise
// in later replacement, the primary annotations would override our computed
// new mappings (see method fixupBoundAnnotations).
atv.clearAnnotations();
AnnotatedTypeMirror result =
AnnotatedTypeCopierWithReplacement.replace(atv, mappings);

Expand Down Expand Up @@ -404,7 +410,16 @@ private AnnotatedTypeMirror substituteTVars(AnnotatedTypeMirror lhs, AnnotatedTy

// Base case where actual type argument is extracted
if (lhs.getKind() == TypeKind.DECLARED) {
// Replace type variable with its actual type argument
rhs = getTypeVariableSubstitution((AnnotatedDeclaredType) lhs, atv);
Ao-senXiong marked this conversation as resolved.
Show resolved Hide resolved
// If the type variable use is annotated, the upperbound and lowerbound annotation
// on the type variable are the same. Replace the primary annotation of the
// substituted result (rhs) with annotation on type variable use.
if (AnnotationUtils.areSame(
atv.getLowerBound().getAnnotations(),
atv.getUpperBound().getAnnotations())) {
rhs.replaceAnnotations(atv.getLowerBound().getAnnotations());
}
}
} else if (rhs.getKind() == TypeKind.DECLARED) {
AnnotatedDeclaredType adt = (AnnotatedDeclaredType) rhs.shallowCopy();
Expand Down
31 changes: 31 additions & 0 deletions framework/tests/viewpointtest/AnnoOnTypeVariableUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import viewpointtest.quals.*;

@SuppressWarnings("cast.unsafe.constructor.invocation")
class AnnoOnTypeVariableUse<E> {
@ReceiverDependentQual E rdq;
@A E a;
@B E b;
E c;

void test() {
AnnoOnTypeVariableUse<@B Element> d = new @A AnnoOnTypeVariableUse<>();
// d.element = @A |> @RDQ = @A
d.rdq = new @A Element();
// :: error: (assignment.type.incompatible)
d.rdq = new @B Element();
// d.a = @A |> @A = @A
d.a = new @A Element();
// :: error: (assignment.type.incompatible)
d.a = new @B Element();
// d.b = @A |> @B = @B
d.b = new @B Element();
// :: error: (assignment.type.incompatible)
d.b = new @A Element();
// :: error: (assignment.type.incompatible)
d.c = new @A Element();
// d.c = @B type argument subsitution with unannotated field
d.c = new @B Element();
}

class Element {}
}
Loading