Skip to content

Commit 7435d61

Browse files
authored
Correctly type T.super (#6066)
Fixes #6060.
1 parent 1823e66 commit 7435d61

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Spliterator;
2+
import java.util.function.Consumer;
3+
import org.checkerframework.checker.tainting.qual.Untainted;
4+
5+
public interface TaintingIssue6060<R> extends Iterable<@Untainted R> {
6+
7+
default Spliterator<@Untainted R> spliterator() {
8+
return Iterable.super.spliterator();
9+
}
10+
11+
default Spliterator<R> spliterator2() {
12+
// :: error: (return)
13+
return Iterable.super.spliterator();
14+
}
15+
16+
default Spliterator<R> spliterator3() {
17+
// :: error: (return)
18+
return this.spliterator();
19+
}
20+
21+
// :: error: (override.param)
22+
default void forEach(Consumer<? super R> action) {
23+
Iterable.super.forEach(action);
24+
}
25+
}

framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,23 @@ public AnnotatedDeclaredType getEnclosingType(TypeElement typeElement, Tree tree
21832183
return thisType;
21842184
}
21852185

2186+
/**
2187+
* Returns the {@link AnnotatedTypeMirror} of the enclosing type at the location of {@code tree}
2188+
* that is a subtype of {@code typeElement}.
2189+
*
2190+
* @param typeElement super type of the enclosing type to return
2191+
* @param tree location to use
2192+
* @return the enclosing type at the location of {@code tree} that is a subtype of {@code
2193+
* typeElement}
2194+
*/
2195+
public AnnotatedDeclaredType getEnclosingSubType(TypeElement typeElement, Tree tree) {
2196+
AnnotatedDeclaredType thisType = getSelfType(tree);
2197+
while (!isSubtype(thisType.getUnderlyingType(), typeElement.asType())) {
2198+
thisType = thisType.getEnclosingType();
2199+
}
2200+
return thisType;
2201+
}
2202+
21862203
/**
21872204
* Returns true if the erasure of {@code type1} is a Java subtype of the erasure of {@code type2}.
21882205
*

framework/src/main/java/org/checkerframework/framework/type/TypeFromExpressionVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import javax.lang.model.element.AnnotationMirror;
3030
import javax.lang.model.element.Element;
31+
import javax.lang.model.element.TypeElement;
3132
import javax.lang.model.type.TypeKind;
3233
import javax.lang.model.type.TypeMirror;
3334
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
@@ -246,6 +247,7 @@ public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree tree, AnnotatedTyp
246247
}
247248
switch (ElementUtils.getKindRecordAsClass(elt)) {
248249
case METHOD:
250+
case CONSTRUCTOR: // x0.super() in anoymous classes
249251
case PACKAGE: // "java.lang" in new java.lang.Short("2")
250252
case CLASS: // o instanceof MyClass.InnerClass
251253
case ENUM:
@@ -260,6 +262,14 @@ public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree tree, AnnotatedTyp
260262
// Tree is "MyClass.this", where "MyClass" may be the innermost enclosing type or any
261263
// outer type.
262264
return f.getEnclosingType(TypesUtils.getTypeElement(TreeUtils.typeOf(tree)), tree);
265+
} else if (tree.getIdentifier().contentEquals("super")) {
266+
// Tree is "MyClass.super", where "MyClass" may be the innermost enclosing type or any
267+
// outer type.
268+
TypeMirror superTypeMirror = TreeUtils.typeOf(tree);
269+
TypeElement superTypeElement = TypesUtils.getTypeElement(superTypeMirror);
270+
AnnotatedDeclaredType thisType = f.getEnclosingSubType(superTypeElement, tree);
271+
return AnnotatedTypes.asSuper(
272+
f, thisType, AnnotatedTypeMirror.createType(superTypeMirror, f, false));
263273
} else {
264274
// tree must be a field access, so get the type of the expression, and then call
265275
// asMemberOf.

framework/tests/Issue6060.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Spliterator;
2+
import java.util.function.Consumer;
3+
4+
public interface Issue6060<R> extends Iterable<R> {
5+
6+
default Spliterator<R> spliterator() {
7+
return Iterable.super.spliterator();
8+
}
9+
10+
default void forEach(Consumer<? super R> action) {
11+
Iterable.super.forEach(action);
12+
}
13+
}

0 commit comments

Comments
 (0)