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 9 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 @@ -285,7 +286,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 @@ -408,6 +414,23 @@ private AnnotatedTypeMirror substituteTVars(AnnotatedTypeMirror lhs, AnnotatedTy
// Base case where actual type argument is extracted
if (lhs.getKind() == TypeKind.DECLARED) {
rhs = getTypeVariableSubstitution((AnnotatedDeclaredType) lhs, atv);
Ao-senXiong marked this conversation as resolved.
Show resolved Hide resolved
// A type annotation on a use of a generic type variable overrides/ignores any type
// qualifier (in the same type hierarchy) on the corresponding actual type argument.
// See https://eisop.github.io/cf/manual/manual.html#type-variable-use
// However, #getTypeVariableSubstitution will replace the type qualifier with the
// qualifier on type variable declaration.
// Here check if the types of the lower and upper bound are the same. If they are:
// (1) If the type variable use is annotated, replacing the primary annotation of
// the substituted result (rhs) with the previous annotated type qualifier.
// (2) If the type variable use is not annotated and the type parameter is declared
// with the same upper and lower bounds, and doing the same replace as (1) is safe
// because the qualifiers of the substituted result (rhs) and the old type variable
// use (atv) are the same.
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
26 changes: 26 additions & 0 deletions framework/tests/viewpointtest/AnnoOnTypeVariableUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import viewpointtest.quals.*;

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

void test() {
AnnoOnTypeVariableUse<@B Element> d = new @A AnnoOnTypeVariableUse<>();
// d.element = @A |> @RDQ = @A
d.element = new @A Element();
Ao-senXiong marked this conversation as resolved.
Show resolved Hide resolved
Ao-senXiong marked this conversation as resolved.
Show resolved Hide resolved
// d.a = @A |> @A = @A
d.a = new @A Element();
// :: error: (assignment.type.incompatible)
d.a = new @B Element();
// d.b = @B |> @B = @B
d.b = new @B Element();
// :: error: (assignment.type.incompatible)
d.b = new @A Element();
}
}

class Element {
int f = 1;
}
Loading