Search in sources :

Example 1 with Warner

use of com.sun.tools.javac.util.Warner in project error-prone by google.

the class BlockTemplate method matchesStartingAtBeginning.

private Choice<List<BlockTemplateMatch>> matchesStartingAtBeginning(final JCBlock block, final int offset, final ImmutableList<? extends StatementTree> statements, final Context context) {
    if (statements.isEmpty()) {
        return Choice.none();
    }
    final JCStatement firstStatement = (JCStatement) statements.get(0);
    Choice<UnifierWithUnconsumedStatements> choice = Choice.of(UnifierWithUnconsumedStatements.create(new Unifier(context), statements));
    for (UStatement templateStatement : templateStatements()) {
        choice = choice.thenChoose(templateStatement);
    }
    return choice.thenChoose((UnifierWithUnconsumedStatements state) -> {
        Unifier unifier = state.unifier();
        Inliner inliner = unifier.createInliner();
        try {
            Optional<Unifier> checkedUnifier = typecheck(unifier, inliner, new Warner(firstStatement), expectedTypes(inliner), actualTypes(inliner));
            if (checkedUnifier.isPresent()) {
                int consumedStatements = statements.size() - state.unconsumedStatements().size();
                BlockTemplateMatch match = new BlockTemplateMatch(block, checkedUnifier.get(), offset, offset + consumedStatements);
                boolean verified = ExpressionTemplate.trueOrNull(ExpressionTemplate.PLACEHOLDER_VERIFIER.scan(templateStatements(), checkedUnifier.get()));
                if (!verified) {
                    return Choice.none();
                }
                return matchesStartingAnywhere(block, offset + consumedStatements, statements.subList(consumedStatements, statements.size()), context).transform(list -> list.prepend(match));
            }
        } catch (CouldNotResolveImportException e) {
        // fall through
        }
        return Choice.none();
    });
}
Also used : UnifierWithUnconsumedStatements(com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements) Warner(com.sun.tools.javac.util.Warner) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 2 with Warner

use of com.sun.tools.javac.util.Warner in project error-prone by google.

the class Template method callCheckMethod.

/**
 * Reflectively invoke Resolve.checkMethod(), which despite being package-private is apparently
 * the only useful entry-point into javac8's type inference implementation.
 */
private MethodType callCheckMethod(Warner warner, Inliner inliner, Object resultInfo, List<Type> actualArgTypes, MethodSymbol methodSymbol, Type site, Env<AttrContext> env) throws InferException {
    try {
        Method checkMethod;
        checkMethod = Resolve.class.getDeclaredMethod("checkMethod", Env.class, Type.class, Symbol.class, Class.forName(// ResultInfo is package-private
        "com.sun.tools.javac.comp.Attr$ResultInfo"), List.class, List.class, Warner.class);
        checkMethod.setAccessible(true);
        return (MethodType) checkMethod.invoke(Resolve.instance(inliner.getContext()), env, site, methodSymbol, resultInfo, actualArgTypes, /*freeTypeVariables=*/
        List.<Type>nil(), warner);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof Resolve.InapplicableMethodException) {
            throw new InferException(ImmutableList.of(((Resolve.InapplicableMethodException) e.getTargetException()).getDiagnostic()));
        }
        throw new LinkageError(e.getMessage(), e.getCause());
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) Method(java.lang.reflect.Method) Env(com.sun.tools.javac.comp.Env) Resolve(com.sun.tools.javac.comp.Resolve) InvocationTargetException(java.lang.reflect.InvocationTargetException) Warner(com.sun.tools.javac.util.Warner) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with Warner

use of com.sun.tools.javac.util.Warner in project ceylon-compiler by ceylon.

the class Attr method visitBinary.

public void visitBinary(JCBinary tree) {
    // Attribute arguments.
    Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env));
    Type right = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.rhs, env));
    // Find operator.
    Symbol operator = tree.operator = rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right);
    Type owntype = types.createErrorType(tree.type);
    if (operator.kind == MTH && !left.isErroneous() && !right.isErroneous()) {
        owntype = operator.type.getReturnType();
        int opc = chk.checkOperator(tree.lhs.pos(), (OperatorSymbol) operator, tree.getTag(), left, right);
        // If both arguments are constants, fold them.
        if (left.constValue() != null && right.constValue() != null) {
            Type ctype = cfolder.fold2(opc, left, right);
            if (ctype != null) {
                owntype = cfolder.coerce(ctype, owntype);
                // operands are constant identifiers.
                if (tree.lhs.type.tsym == syms.stringType.tsym) {
                    tree.lhs.type = syms.stringType;
                }
                if (tree.rhs.type.tsym == syms.stringType.tsym) {
                    tree.rhs.type = syms.stringType;
                }
            }
        }
        // castable to each other, (JLS???).
        if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) {
            if (!types.isCastable(left, right, new Warner(tree.pos()))) {
                log.error(tree.pos(), "incomparable.types", left, right);
            }
        }
        chk.checkDivZero(tree.rhs.pos(), operator, right);
    }
    result = check(tree, owntype, VAL, pkind, pt);
}
Also used : Warner(com.sun.tools.javac.util.Warner) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol) Lint(com.sun.tools.javac.code.Lint)

Example 4 with Warner

use of com.sun.tools.javac.util.Warner in project ceylon-compiler by ceylon.

the class Attr method checkMethod.

/**
 * Check that method arguments conform to its instantation.
 */
public Type checkMethod(Type site, Symbol sym, Env<AttrContext> env, final List<JCExpression> argtrees, List<Type> argtypes, List<Type> typeargtypes, boolean useVarargs) {
    // an unchecked warning if its argument types change under erasure.
    if (allowGenerics && (sym.flags() & STATIC) == 0 && (site.tag == CLASS || site.tag == TYPEVAR)) {
        Type s = types.asOuterSuper(site, sym.owner);
        if (s != null && s.isRaw() && !types.isSameTypes(sym.type.getParameterTypes(), sym.erasure(types).getParameterTypes())) {
            chk.warnUnchecked(env.tree.pos(), "unchecked.call.mbr.of.raw.type", sym, s);
        }
    }
    // Compute the identifier's instantiated type.
    // For methods, we need to compute the instance type by
    // Resolve.instantiate from the symbol's type as well as
    // any type arguments and value arguments.
    noteWarner.clear();
    Type owntype;
    // Added by Ceylon
    if (sym.kind == MTH && ((MethodSymbol) sym).isDynamic()) {
        owntype = sym.type;
    } else {
        owntype = rs.instantiate(env, site, sym, argtypes, typeargtypes, true, useVarargs, noteWarner);
    }
    boolean warned = noteWarner.hasNonSilentLint(LintCategory.UNCHECKED);
    // found the identifier in the first place.
    if (owntype == null) {
        if (!pt.isErroneous())
            log.error(env.tree.pos(), "internal.error.cant.instantiate", sym, site, Type.toString(pt.getParameterTypes()));
        owntype = types.createErrorType(site);
    } else {
        // System.out.println("call   : " + env.tree);
        // System.out.println("method : " + owntype);
        // System.out.println("actuals: " + argtypes);
        List<Type> formals = owntype.getParameterTypes();
        Type last = useVarargs ? formals.last() : null;
        if (sym.name == names.init && sym.owner == syms.enumSym)
            formals = formals.tail.tail;
        List<JCExpression> args = argtrees;
        while (formals.head != last) {
            JCTree arg = args.head;
            Warner warn = chk.convertWarner(arg.pos(), arg.type, formals.head);
            assertConvertible(arg, arg.type, formals.head, warn);
            warned |= warn.hasNonSilentLint(LintCategory.UNCHECKED);
            args = args.tail;
            formals = formals.tail;
        }
        if (useVarargs) {
            Type varArg = types.elemtype(last);
            while (args.tail != null) {
                JCTree arg = args.head;
                Warner warn = chk.convertWarner(arg.pos(), arg.type, varArg);
                assertConvertible(arg, arg.type, varArg, warn);
                warned |= warn.hasNonSilentLint(LintCategory.UNCHECKED);
                args = args.tail;
            }
        } else if ((sym.flags() & VARARGS) != 0 && allowVarargs) {
            // non-varargs call to varargs method
            Type varParam = owntype.getParameterTypes().last();
            Type lastArg = argtypes.last();
            if (types.isSubtypeUnchecked(lastArg, types.elemtype(varParam)) && !types.isSameType(types.erasure(varParam), types.erasure(lastArg)))
                log.warning(argtrees.last().pos(), "inexact.non-varargs.call", types.elemtype(varParam), varParam);
        }
        if (warned && sym.type.tag == FORALL) {
            chk.warnUnchecked(env.tree.pos(), "unchecked.meth.invocation.applied", kindName(sym), sym.name, rs.methodArguments(sym.type.getParameterTypes()), rs.methodArguments(argtypes), kindName(sym.location()), sym.location());
            // Don't erase the return type of the instantiated method type
            // for Ceylon #1095
            owntype = new MethodType(owntype.getParameterTypes(), sourceLanguage.isCeylon() && typeargtypes != null && !typeargtypes.isEmpty() ? owntype.getReturnType() : types.erasure(owntype.getReturnType()), types.erasure(owntype.getThrownTypes()), syms.methodClass);
        }
        if (useVarargs) {
            JCTree tree = env.tree;
            Type argtype = owntype.getParameterTypes().last();
            if (owntype.getReturnType().tag != FORALL || warned) {
                chk.checkVararg(env.tree.pos(), owntype.getParameterTypes(), sym);
            }
            Type elemtype = types.elemtype(argtype);
            switch(tree.getTag()) {
                case JCTree.APPLY:
                    ((JCMethodInvocation) tree).varargsElement = elemtype;
                    break;
                case JCTree.NEWCLASS:
                    ((JCNewClass) tree).varargsElement = elemtype;
                    break;
                default:
                    throw new AssertionError("" + tree);
            }
        }
    }
    return owntype;
}
Also used : Warner(com.sun.tools.javac.util.Warner) MethodType(com.sun.tools.javac.code.Type.MethodType) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

Warner (com.sun.tools.javac.util.Warner)4 Type (com.sun.tools.javac.code.Type)3 MethodType (com.sun.tools.javac.code.Type.MethodType)3 Symbol (com.sun.tools.javac.code.Symbol)2 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 ArrayType (com.sun.tools.javac.code.Type.ArrayType)2 ClassType (com.sun.tools.javac.code.Type.ClassType)2 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)2 WildcardType (com.sun.tools.javac.code.Type.WildcardType)2 ImmutableList (com.google.common.collect.ImmutableList)1 UnifierWithUnconsumedStatements (com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements)1 Lint (com.sun.tools.javac.code.Lint)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)1 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)1 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)1 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)1 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)1 Env (com.sun.tools.javac.comp.Env)1 Resolve (com.sun.tools.javac.comp.Resolve)1