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

Apply String declaration bounds to String conversion nodes #207

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -75,6 +75,7 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/**
Expand Down Expand Up @@ -1355,7 +1356,20 @@ public TransferResult<V, S> visitNarrowingConversion(
public TransferResult<V, S> visitStringConversion(
StringConversionNode n, TransferInput<V, S> p) {
TransferResult<V, S> result = super.visitStringConversion(n, p);
result.setResultValue(p.getValueOfSubNode(n.getOperand()));
TypeMirror strType = n.getType();

Set<AnnotationMirror> operandAnnos = p.getValueOfSubNode(n.getOperand()).getAnnotations();
if (operandAnnos.isEmpty() && n.getOperand().getType().getKind() == TypeKind.TYPEVAR) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about wildcards?
Can you add test cases for both type variables and wildcards?
This change has no impact for the existing type systems, so can you add a simple test type system that illustrates the problem? How would a type system writer e.g. ensure that the operand type is not a supertype of the String declaration bound?

// use effective annotation if we didn't find any annotations in the value of
// a type variable
Tree operandTree = n.getOperand().getTree();
operandAnnos =
analysis.atypeFactory.getAnnotatedType(operandTree).getEffectiveAnnotations();
}

Set<AnnotationMirror> resultAnnos =
analysis.atypeFactory.getAnnoOrTypeBound(strType, operandAnnos);
result.setResultValue(analysis.createAbstractValue(resultAnnos, strType));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5729,4 +5729,32 @@ public boolean isImmutable(TypeMirror type) {
}
return ImmutableTypes.isImmutable(TypeAnnotationUtils.unannotatedType(type).toString());
}

/**
* Returns the qualifiers in {@code annos} that are below the qualifier upper bound of {@code
* type}. If a qualifier in {@code annos} is above the bound, then the bound is added to the
* result instead.
*
* @param type java type that specifies the qualifier upper bound
* @param annos annotations to add to the {@link AnnotatedTypeMirror} of type
* @return the modified {@code annos} by applying the rules above
*/
public Set<AnnotationMirror> getAnnoOrTypeBound(
TypeMirror type, Set<? extends AnnotationMirror> annos) {
Set<AnnotationMirror> boundAnnos = getTypeDeclarationBounds(type);
Set<AnnotationMirror> results = AnnotationUtils.createAnnotationSet();

for (AnnotationMirror anno : annos) {
AnnotationMirror boundAnno =
qualHierarchy.findAnnotationInSameHierarchy(boundAnnos, anno);
assert boundAnno != null;

if (!qualHierarchy.isSubtype(anno, boundAnno)) {
results.add(boundAnno);
} else {
results.add(anno);
}
}
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
import org.checkerframework.framework.type.QualifierHierarchy;
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.CollectionUtils;
import org.checkerframework.javacutil.Pair;
import org.checkerframework.javacutil.TreePathUtil;
Expand Down Expand Up @@ -337,16 +336,8 @@ private boolean hasPrimaryAnnotationInAllHierarchies(AnnotatedTypeMirror type) {
* @param annos annotations to add to type
*/
private void addAnnoOrBound(AnnotatedTypeMirror type, Set<? extends AnnotationMirror> annos) {
Set<AnnotationMirror> boundAnnos =
atypeFactory.getQualifierUpperBounds().getBoundQualifiers(type.getUnderlyingType());
Set<AnnotationMirror> annosToAdd = AnnotationUtils.createAnnotationSet();
for (AnnotationMirror boundAnno : boundAnnos) {
AnnotationMirror anno = qualHierarchy.findAnnotationInSameHierarchy(annos, boundAnno);
if (anno != null && !qualHierarchy.isSubtype(anno, boundAnno)) {
annosToAdd.add(boundAnno);
}
}
Set<AnnotationMirror> annosToAdd =
atypeFactory.getAnnoOrTypeBound(type.getUnderlyingType(), annos);
type.addMissingAnnotations(annosToAdd);
type.addMissingAnnotations(annos);
}
}