Search in sources :

Example 1 with DiagnosticPosition

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

the class Resolve method resolveDiamond.

/**
 * Resolve constructor using diamond inference.
 *  @param pos       The position to use for error reporting.
 *  @param env       The environment current at the constructor invocation.
 *  @param site      The type of class for which a constructor is searched.
 *                   The scope of this class has been touched in attribution.
 *  @param argtypes  The types of the constructor invocation's value
 *                   arguments.
 *  @param typeargtypes  The types of the constructor invocation's type
 *                   arguments.
 */
Symbol resolveDiamond(DiagnosticPosition pos, Env<AttrContext> env, Type site, List<Type> argtypes, List<Type> typeargtypes) {
    Symbol sym = startResolution();
    List<MethodResolutionPhase> steps = methodResolutionSteps;
    while (steps.nonEmpty() && steps.head.isApplicable(boxingEnabled, varargsEnabled) && sym.kind >= ERRONEOUS) {
        currentStep = steps.head;
        sym = resolveConstructor(pos, env, site, argtypes, typeargtypes, steps.head.isBoxingRequired(), env.info.varArgs = steps.head.isVarargsRequired());
        methodResolutionCache.put(steps.head, sym);
        steps = steps.tail;
    }
    if (sym.kind >= AMBIGUOUS) {
        final JCDiagnostic details = sym.kind == WRONG_MTH ? ((InapplicableSymbolError) sym).explanation : null;
        Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {

            @Override
            JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
                String key = details == null ? "cant.apply.diamond" : "cant.apply.diamond.1";
                return diags.create(dkind, log.currentSource(), pos, key, diags.fragment("diamond", site.tsym), details);
            }
        };
        MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
        sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes);
        env.info.varArgs = errPhase.isVarargsRequired();
    }
    return sym;
}
Also used : DiagnosticType(com.sun.tools.javac.util.JCDiagnostic.DiagnosticType) Type(com.sun.tools.javac.code.Type) MethodResolutionPhase(com.sun.tools.javac.comp.Resolve.MethodResolutionPhase) Symbol(com.sun.tools.javac.code.Symbol) DiagnosticType(com.sun.tools.javac.util.JCDiagnostic.DiagnosticType) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition) LocalizedString(com.sun.tools.javac.api.Formattable.LocalizedString)

Example 2 with DiagnosticPosition

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

the class Attr method selectSym.

private Symbol selectSym(JCFieldAccess tree, Symbol location, Type site, Env<AttrContext> env, Type pt, int pkind) {
    DiagnosticPosition pos = tree.pos();
    Name name = tree.name;
    switch(site.tag) {
        case PACKAGE:
            return rs.access(rs.findIdentInPackage(env, site.tsym, name, pkind), pos, location, site, name, true);
        case ARRAY:
        case CLASS:
            if (pt.tag == METHOD || pt.tag == FORALL) {
                return rs.resolveQualifiedMethod(pos, env, location, site, name, pt.getParameterTypes(), pt.getTypeArguments());
            } else if (name == names._this || name == names._super) {
                return rs.resolveSelf(pos, env, site.tsym, name);
            } else if (name == names._class) {
                // In this case, we have already made sure in
                // visitSelect that qualifier expression is a type.
                Type t = syms.classType;
                List<Type> typeargs = allowGenerics ? List.of(types.erasure(site)) : List.<Type>nil();
                t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
                return new VarSymbol(STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
            } else {
                // We are seeing a plain identifier as selector.
                Symbol sym = rs.findIdentInType(env, site, name, pkind);
                if ((pkind & ERRONEOUS) == 0)
                    sym = rs.access(sym, pos, location, site, name, true);
                return sym;
            }
        case WILDCARD:
            throw new AssertionError(tree);
        case TYPEVAR:
            // Normally, site.getUpperBound() shouldn't be null.
            // It should only happen during memberEnter/attribBase
            // when determining the super type which *must* beac
            // done before attributing the type variables.  In
            // other words, we are seeing this illegal program:
            // class B<T> extends A<T.foo> {}
            Symbol sym = (site.getUpperBound() != null) ? selectSym(tree, location, capture(site.getUpperBound()), env, pt, pkind) : null;
            if (sym == null) {
                log.error(pos, "type.var.cant.be.deref");
                return syms.errSymbol;
            } else {
                // Ceylon: relax the rules for private methods in wildcards, damnit, we want the private
                // method to be called, not any subtype's method we can't possibly know about, this is really
                // a lame Java decision.
                Symbol sym2 = (!sourceLanguage.isCeylon() && (sym.flags() & Flags.PRIVATE) != 0) ? rs.new AccessError(env, site, sym) : sym;
                rs.access(sym2, pos, location, site, name, true);
                return sym;
            }
        case ERROR:
            // preserve identifier names through errors
            return types.createErrorType(name, site.tsym, site).tsym;
        default:
            // .class is allowed for these.
            if (name == names._class) {
                // In this case, we have already made sure in Select that
                // qualifier expression is a type.
                Type t = syms.classType;
                Type arg = types.boxedClass(site).type;
                t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym);
                return new VarSymbol(STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
            } else {
                log.error(pos, "cant.deref", site);
                return syms.errSymbol;
            }
    }
}
Also used : 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) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition) 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) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Kinds.kindName(com.sun.tools.javac.code.Kinds.kindName) Name(com.sun.tools.javac.util.Name)

Example 3 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project checker-framework by typetools.

the class InternalUtils method compareDiagnosticPosition.

/**
 * Compares tree1 to tree2 by the position at which a diagnostic (e.g., an error message) for the
 * tree should be printed.
 */
public static int compareDiagnosticPosition(Tree tree1, Tree tree2) {
    DiagnosticPosition pos1 = (DiagnosticPosition) tree1;
    DiagnosticPosition pos2 = (DiagnosticPosition) tree2;
    int preferred = Integer.compare(pos1.getPreferredPosition(), pos2.getPreferredPosition());
    if (preferred != 0) {
        return preferred;
    }
    return Integer.compare(pos1.getStartPosition(), pos2.getStartPosition());
}
Also used : DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)

Example 4 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project checker-framework by typetools.

the class SourceChecker method logBugInCF.

/**
 * Log (that is, print) an internal error in the framework or a checker.
 *
 * @param ce the internal error to output
 */
private void logBugInCF(BugInCF ce) {
    StringJoiner msg = new StringJoiner(LINE_SEPARATOR);
    if (ce.getCause() != null && ce.getCause() instanceof OutOfMemoryError) {
        msg.add(String.format("OutOfMemoryError (max memory = %d, total memory = %d, free memory = %d)", Runtime.getRuntime().maxMemory(), Runtime.getRuntime().totalMemory(), Runtime.getRuntime().freeMemory()));
    } else {
        msg.add(ce.getMessage());
        boolean noPrintErrorStack = (processingEnv != null && processingEnv.getOptions() != null && processingEnv.getOptions().containsKey("noPrintErrorStack"));
        msg.add("; The Checker Framework crashed.  Please report the crash.");
        if (noPrintErrorStack) {
            msg.add(" To see the full stack trace, don't invoke the compiler with -AnoPrintErrorStack");
        } else {
            if (this.currentRoot != null && this.currentRoot.getSourceFile() != null) {
                msg.add("Compilation unit: " + this.currentRoot.getSourceFile().getName());
            }
            if (this.visitor != null) {
                DiagnosticPosition pos = (DiagnosticPosition) this.visitor.lastVisited;
                if (pos != null) {
                    DiagnosticSource source = new DiagnosticSource(this.currentRoot.getSourceFile(), null);
                    int linenr = source.getLineNumber(pos.getStartPosition());
                    int col = source.getColumnNumber(pos.getStartPosition(), true);
                    String line = source.getLine(pos.getStartPosition());
                    msg.add("Last visited tree at line " + linenr + " column " + col + ":");
                    msg.add(line);
                }
            }
        }
    }
    msg.add("Exception: " + ce.getCause() + "; " + UtilPlume.stackTraceToString(ce.getCause()));
    boolean printClasspath = ce.getCause() instanceof NoClassDefFoundError;
    Throwable cause = ce.getCause().getCause();
    while (cause != null) {
        msg.add("Underlying Exception: " + cause + "; " + UtilPlume.stackTraceToString(cause));
        printClasspath |= cause instanceof NoClassDefFoundError;
        cause = cause.getCause();
    }
    if (printClasspath) {
        msg.add("Classpath:");
        for (URI uri : new ClassGraph().getClasspathURIs()) {
            msg.add(uri.toString());
        }
    }
    printMessage(msg.toString());
}
Also used : DiagnosticSource(com.sun.tools.javac.util.DiagnosticSource) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition) ClassGraph(io.github.classgraph.ClassGraph) URI(java.net.URI) StringJoiner(java.util.StringJoiner)

Example 5 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project checker-framework by typetools.

the class SourceChecker method logCheckerError.

private void logCheckerError(CheckerError ce) {
    if (ce.getMessage() == null) {
        final String stackTrace = formatStackTrace(ce.getStackTrace());
        ErrorReporter.errorAbort("Null error message while logging Checker error.\nStack Trace:\n" + stackTrace);
    }
    StringBuilder msg = new StringBuilder(ce.getMessage());
    if ((processingEnv == null || processingEnv.getOptions() == null || processingEnv.getOptions().containsKey("printErrorStack")) && ce.getCause() != null) {
        if (this.currentRoot != null && this.currentRoot.getSourceFile() != null) {
            msg.append("\nCompilation unit: " + this.currentRoot.getSourceFile().getName());
        }
        if (this.visitor != null) {
            DiagnosticPosition pos = (DiagnosticPosition) this.visitor.lastVisited;
            DiagnosticSource source = new DiagnosticSource(this.currentRoot.getSourceFile(), null);
            int linenr = source.getLineNumber(pos.getStartPosition());
            int col = source.getColumnNumber(pos.getStartPosition(), true);
            String line = source.getLine(pos.getStartPosition());
            msg.append("\nLast visited tree at line " + linenr + " column " + col + ":\n" + line);
        }
        msg.append("\nException: " + ce.getCause().toString() + "; " + formatStackTrace(ce.getCause().getStackTrace()));
        Throwable cause = ce.getCause().getCause();
        while (cause != null) {
            msg.append("\nUnderlying Exception: " + (cause.toString() + "; " + formatStackTrace(cause.getStackTrace())));
            cause = cause.getCause();
        }
    } else {
        if (ce.userError) {
            msg.append('.');
        } else {
            msg.append("; The Checker Framework crashed.  Please report the crash.  To see " + "the full stack trace invoke the compiler with -AprintErrorStack");
        }
    }
    if (this.messager == null) {
        messager = processingEnv.getMessager();
    }
    this.messager.printMessage(javax.tools.Diagnostic.Kind.ERROR, msg);
}
Also used : DiagnosticSource(com.sun.tools.javac.util.DiagnosticSource) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)

Aggregations

DiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)8 Symbol (com.sun.tools.javac.code.Symbol)2 Type (com.sun.tools.javac.code.Type)2 DiagnosticSource (com.sun.tools.javac.util.DiagnosticSource)2 LocalizedString (com.sun.tools.javac.api.Formattable.LocalizedString)1 Kinds.kindName (com.sun.tools.javac.code.Kinds.kindName)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)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 ArrayType (com.sun.tools.javac.code.Type.ArrayType)1 ClassType (com.sun.tools.javac.code.Type.ClassType)1 MethodType (com.sun.tools.javac.code.Type.MethodType)1 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)1 WildcardType (com.sun.tools.javac.code.Type.WildcardType)1 MethodResolutionPhase (com.sun.tools.javac.comp.Resolve.MethodResolutionPhase)1 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1