RFR: 8194743: Compiler implementation for Statements before super() [v14]
Archie Cobbs
acobbs at openjdk.org
Mon Sep 25 14:30:20 UTC 2023
On Mon, 25 Sep 2023 14:02:53 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> Oh I see. Hmm, I'll admit ignorance on how exactly these `Kind` enum error values like `STATICERR`, `HIDDEN`, etc. are used. Is there a better `Kind` choice for this error? Or does this warrant a new `Kind` value? In the latter case, there is a scary warning about ordering of the enum values so any advice appreciated. Thanks.
>
> The kind is used in some other parts of Resolve to determine whether some errors should be skipped in some places, or recovered from. I'd advise against creating a new error kind, which might end up invalidating some existing check which depend on STATICERR. E.g.:
>
>
> final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
> return phase.ordinal() > maxPhase.ordinal() ||
> !sym.kind.isResolutionError() || sym.kind == AMBIGUOUS || sym.kind == STATICERR;
> }
>
>
> E.g. you don't want overload resolution to try new candidates because you tried to call a method and you don't have a `this` - e.g. the compiler behavior should be similar as to when an instance method is called and no `this` is available (meaning: the found method is correct, and the error should be reported, no attempt should be made at recovery).
OK got it. So what if we did this instead?
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
index 295bb192a42..4c7c177163f 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
@@ -4576,7 +4587,11 @@ JCDiagnostic inaccessiblePackageReason(Env<AttrContext> env, PackageSymbol sym)
class StaticError extends InvalidSymbolError {
StaticError(Symbol sym) {
- super(STATICERR, sym, "static error");
+ this(sym, "static error");
+ }
+
+ StaticError(Symbol sym, String debugName) {
+ super(STATICERR, sym, debugName);
}
@Override
@@ -4595,6 +4610,32 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
}
}
+ /**
+ * Specialization of {@link InvalidSymbolError} for illegal
+ * early accesses within a constructor prologue.
+ */
+ class RefBeforeCtorCalledError extends StaticError {
+
+ RefBeforeCtorCalledError(Symbol sym) {
+ super(sym, "prologue error");
+ }
+
+ @Override
+ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
+ DiagnosticPosition pos,
+ Symbol location,
+ Type site,
+ Name name,
+ List<Type> argtypes,
+ List<Type> typeargtypes) {
+ Symbol errSym = ((sym.kind == TYP && sym.type.hasTag(CLASS))
+ ? types.erasure(sym.type).tsym
+ : sym);
+ return diags.create(dkind, log.currentSource(), pos,
+ "cant.ref.before.ctor.called", errSym);
+ }
+ }
+
/**
* InvalidSymbolError error class indicating that a pair of symbols
* (either methods, constructors or operands) are ambiguous
@@ -4708,7 +4749,7 @@ class BadMethodReferenceError extends StaticError {
boolean unboundLookup;
public BadMethodReferenceError(Symbol sym, boolean unboundLookup) {
- super(sym);
+ super(sym, "bad method ref error");
this.unboundLookup = unboundLookup;
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13656#discussion_r1335969649
More information about the compiler-dev
mailing list