Skip to content

Commit

Permalink
Correctly type T.super (#6066)
Browse files Browse the repository at this point in the history
Fixes #6060.
  • Loading branch information
smillst committed Jul 3, 2023
1 parent 1823e66 commit 7435d61
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions checker/tests/tainting/TaintingIssue6060.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Spliterator;
import java.util.function.Consumer;
import org.checkerframework.checker.tainting.qual.Untainted;

public interface TaintingIssue6060<R> extends Iterable<@Untainted R> {

default Spliterator<@Untainted R> spliterator() {
return Iterable.super.spliterator();
}

default Spliterator<R> spliterator2() {
// :: error: (return)
return Iterable.super.spliterator();
}

default Spliterator<R> spliterator3() {
// :: error: (return)
return this.spliterator();
}

// :: error: (override.param)
default void forEach(Consumer<? super R> action) {
Iterable.super.forEach(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,23 @@ public AnnotatedDeclaredType getEnclosingType(TypeElement typeElement, Tree tree
return thisType;
}

/**
* Returns the {@link AnnotatedTypeMirror} of the enclosing type at the location of {@code tree}
* that is a subtype of {@code typeElement}.
*
* @param typeElement super type of the enclosing type to return
* @param tree location to use
* @return the enclosing type at the location of {@code tree} that is a subtype of {@code
* typeElement}
*/
public AnnotatedDeclaredType getEnclosingSubType(TypeElement typeElement, Tree tree) {
AnnotatedDeclaredType thisType = getSelfType(tree);
while (!isSubtype(thisType.getUnderlyingType(), typeElement.asType())) {
thisType = thisType.getEnclosingType();
}
return thisType;
}

/**
* Returns true if the erasure of {@code type1} is a Java subtype of the erasure of {@code type2}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
Expand Down Expand Up @@ -246,6 +247,7 @@ public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree tree, AnnotatedTyp
}
switch (ElementUtils.getKindRecordAsClass(elt)) {
case METHOD:
case CONSTRUCTOR: // x0.super() in anoymous classes
case PACKAGE: // "java.lang" in new java.lang.Short("2")
case CLASS: // o instanceof MyClass.InnerClass
case ENUM:
Expand All @@ -260,6 +262,14 @@ public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree tree, AnnotatedTyp
// Tree is "MyClass.this", where "MyClass" may be the innermost enclosing type or any
// outer type.
return f.getEnclosingType(TypesUtils.getTypeElement(TreeUtils.typeOf(tree)), tree);
} else if (tree.getIdentifier().contentEquals("super")) {
// Tree is "MyClass.super", where "MyClass" may be the innermost enclosing type or any
// outer type.
TypeMirror superTypeMirror = TreeUtils.typeOf(tree);
TypeElement superTypeElement = TypesUtils.getTypeElement(superTypeMirror);
AnnotatedDeclaredType thisType = f.getEnclosingSubType(superTypeElement, tree);
return AnnotatedTypes.asSuper(
f, thisType, AnnotatedTypeMirror.createType(superTypeMirror, f, false));
} else {
// tree must be a field access, so get the type of the expression, and then call
// asMemberOf.
Expand Down
13 changes: 13 additions & 0 deletions framework/tests/Issue6060.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Spliterator;
import java.util.function.Consumer;

public interface Issue6060<R> extends Iterable<R> {

default Spliterator<R> spliterator() {
return Iterable.super.spliterator();
}

default void forEach(Consumer<? super R> action) {
Iterable.super.forEach(action);
}
}

0 comments on commit 7435d61

Please sign in to comment.