Search in sources :

Example 16 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class AtmLubVisitor method lubTypeArgument.

private void lubTypeArgument(AnnotatedTypeMirror type1, AnnotatedTypeMirror type2, AnnotatedTypeMirror lub) {
    // In lub(), asSuper is called on type1 and type2, but asSuper does not recur into type
    // arguments, so call asSuper on the type arguments so that they have the same underlying
    // type.
    final AnnotatedTypeMirror type1AsLub = AnnotatedTypes.asSuper(atypeFactory, type1, lub);
    final AnnotatedTypeMirror type2AsLub = AnnotatedTypes.asSuper(atypeFactory, type2, lub);
    // but Gen<@A ? super @B Object> is returned.)
    if (lub.getKind() == TypeKind.WILDCARD) {
        if (visited(lub)) {
            return;
        }
        AnnotatedWildcardType type1Wildcard = (AnnotatedWildcardType) type1AsLub;
        AnnotatedWildcardType type2Wildcard = (AnnotatedWildcardType) type2AsLub;
        AnnotatedWildcardType lubWildcard = (AnnotatedWildcardType) lub;
        if (type1Wildcard.isUninferredTypeArgument() || type2Wildcard.isUninferredTypeArgument()) {
            lubWildcard.setUninferredTypeArgument();
        }
        lubWildcard(type1Wildcard.getSuperBound(), type1Wildcard.getExtendsBound(), type2Wildcard.getSuperBound(), type2Wildcard.getExtendsBound(), lubWildcard.getSuperBound(), lubWildcard.getExtendsBound());
    } else if (lub.getKind() == TypeKind.TYPEVAR && TypesUtils.isCaptured((TypeVariable) lub.getUnderlyingType())) {
        if (visited(lub)) {
            return;
        }
        AnnotatedTypeVariable type1typevar = (AnnotatedTypeVariable) type1AsLub;
        AnnotatedTypeVariable type2typevar = (AnnotatedTypeVariable) type2AsLub;
        AnnotatedTypeVariable lubTypevar = (AnnotatedTypeVariable) lub;
        lubWildcard(type1typevar.getLowerBound(), type1typevar.getUpperBound(), type2typevar.getLowerBound(), type2typevar.getUpperBound(), lubTypevar.getLowerBound(), lubTypevar.getUpperBound());
    } else {
        // Don't add to visit history because that will happen in visitTypevar_Typevar or
        // visitWildcard_Wildcard if needed.
        visit(type1AsLub, type2AsLub, lub);
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 17 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class BaseTypeVisitor method checkTypeArguments.

/**
 * Checks that the annotations on the type arguments supplied to a type or a method invocation
 * are within the bounds of the type variables as declared, and issues the
 * "type.argument.type.incompatible" error if they are not.
 *
 * @param toptree the tree for error reporting, only used for inferred type arguments
 * @param paramBounds the bounds of the type parameters from a class or method declaration
 * @param typeargs the type arguments from the type or method invocation
 * @param typeargTrees the type arguments as trees, used for error reporting
 */
// TODO: see updated version below that performs more well-formedness checks.
protected void checkTypeArguments(Tree toptree, List<? extends AnnotatedTypeParameterBounds> paramBounds, List<? extends AnnotatedTypeMirror> typeargs, List<? extends Tree> typeargTrees) {
    // If there are no type variables, do nothing.
    if (paramBounds.isEmpty()) {
        return;
    }
    assert paramBounds.size() == typeargs.size() : "BaseTypeVisitor.checkTypeArguments: mismatch between type arguments: " + typeargs + " and type parameter bounds" + paramBounds;
    Iterator<? extends AnnotatedTypeParameterBounds> boundsIter = paramBounds.iterator();
    Iterator<? extends AnnotatedTypeMirror> argIter = typeargs.iterator();
    while (boundsIter.hasNext()) {
        AnnotatedTypeParameterBounds bounds = boundsIter.next();
        AnnotatedTypeMirror typeArg = argIter.next();
        if (isIgnoredUninferredWildcard(bounds.getUpperBound()) || isIgnoredUninferredWildcard(typeArg)) {
            continue;
        }
        if (shouldBeCaptureConverted(typeArg, bounds)) {
            continue;
        }
        AnnotatedTypeMirror paramUpperBound = bounds.getUpperBound();
        if (typeArg.getKind() == TypeKind.WILDCARD) {
            paramUpperBound = atypeFactory.widenToUpperBound(paramUpperBound, (AnnotatedWildcardType) typeArg);
        }
        if (typeargTrees == null || typeargTrees.isEmpty()) {
            // The type arguments were inferred and we mark the whole method.
            // The inference fails if we provide invalid arguments,
            // therefore issue an error for the arguments.
            // I hope this is less confusing for users.
            commonAssignmentCheck(paramUpperBound, typeArg, toptree, "type.argument.type.incompatible");
        } else {
            commonAssignmentCheck(paramUpperBound, typeArg, typeargTrees.get(typeargs.indexOf(typeArg)), "type.argument.type.incompatible");
        }
        if (!atypeFactory.getTypeHierarchy().isSubtype(bounds.getLowerBound(), typeArg)) {
            if (typeargTrees == null || typeargTrees.isEmpty()) {
                // The type arguments were inferred and we mark the whole method.
                checker.report(Result.failure("type.argument.type.incompatible", typeArg, bounds), toptree);
            } else {
                checker.report(Result.failure("type.argument.type.incompatible", typeArg, bounds), typeargTrees.get(typeargs.indexOf(typeArg)));
            }
        }
    }
}
Also used : AnnotatedTypeParameterBounds(org.checkerframework.framework.type.AnnotatedTypeParameterBounds) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 18 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class QualifierDefaults method getWildcardBoundType.

/**
 * @return the BoundType of annotatedWildcard. If it is unbounded, use the type parameter to
 *     which its an argument.
 */
public static BoundType getWildcardBoundType(final AnnotatedWildcardType annotatedWildcard, final AnnotatedTypeFactory typeFactory) {
    final WildcardType wildcard = (WildcardType) annotatedWildcard.getUnderlyingType();
    final BoundType boundType;
    if (wildcard.isUnbound() && wildcard.bound != null) {
        boundType = getTypeVarBoundType((TypeParameterElement) wildcard.bound.asElement(), typeFactory);
    } else {
        // note: isSuperBound will be true for unbounded and lowers, but the unbounded case is
        // already handled
        boundType = wildcard.isSuperBound() ? BoundType.LOWER : BoundType.UPPER;
    }
    return boundType;
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 19 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class AFReducingVisitor method visitDeclared_Declared.

// From the JLS Spec:
// If F has the form G<..., Yk-1,U, Yk+1, ...>, where U involves Tj
@Override
public Void visitDeclared_Declared(AnnotatedDeclaredType subtype, AnnotatedDeclaredType supertype, Set<AFConstraint> constraints) {
    if (subtype.wasRaw() || supertype.wasRaw()) {
        // The error will be caught in {@link DefaultTypeArgumentInference#infer} and
        // inference will be aborted, but type-checking will continue.
        ErrorReporter.errorAbort("Can't infer type arguments when raw types are involved.");
        return null;
    }
    if (!TypesUtils.isErasedSubtype(subtype.getUnderlyingType(), supertype.getUnderlyingType(), typeFactory.getContext().getTypeUtils())) {
        return null;
    }
    AnnotatedDeclaredType subAsSuper = DefaultTypeHierarchy.castedAsSuper(subtype, supertype);
    final List<AnnotatedTypeMirror> subTypeArgs = subAsSuper.getTypeArguments();
    final List<AnnotatedTypeMirror> superTypeArgs = supertype.getTypeArguments();
    for (int i = 0; i < subTypeArgs.size(); i++) {
        final AnnotatedTypeMirror subTypeArg = subTypeArgs.get(i);
        final AnnotatedTypeMirror superTypeArg = superTypeArgs.get(i);
        // Since we always have both bounds in the checker framework we always compare both
        if (superTypeArg.getKind() == TypeKind.WILDCARD) {
            final AnnotatedWildcardType superWc = (AnnotatedWildcardType) superTypeArg;
            if (subTypeArg.getKind() == TypeKind.WILDCARD) {
                final AnnotatedWildcardType subWc = (AnnotatedWildcardType) subTypeArg;
                TypeArgInferenceUtil.checkForUninferredTypes(subWc);
                addConstraint(subWc.getExtendsBound(), superWc.getExtendsBound(), constraints);
                addInverseConstraint(superWc.getSuperBound(), subWc.getSuperBound(), constraints);
            } else {
                addConstraint(subTypeArg, superWc.getExtendsBound(), constraints);
                addInverseConstraint(superWc.getSuperBound(), subTypeArg, constraints);
            }
        } else {
            // if F has the form G<..., Yk-1, U, Yk+1, ...>, where U is a type expression that
            // involves Tj
            addEqualityConstraint(subTypeArg, superTypeArg, constraints);
        }
    }
    return null;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)19 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)7 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)6 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)5 TypeMirror (javax.lang.model.type.TypeMirror)4 ArrayList (java.util.ArrayList)3 TypeElement (javax.lang.model.element.TypeElement)3 WildcardType (com.sun.tools.javac.code.Type.WildcardType)2 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)1 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 WildcardType (com.github.javaparser.ast.type.WildcardType)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Element (javax.lang.model.element.Element)1