8337980: Javac allows invocation of an inherited instance method from a static method

Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Reviewed-by: mcimadamore, jlahoda
This commit is contained in:
Archie Cobbs 2024-10-09 16:03:55 +00:00
parent 950e3a7587
commit 38c1d65148
5 changed files with 61 additions and 20 deletions

View File

@ -1853,6 +1853,10 @@ public class Resolve {
bestSoFar,
allowBoxing,
useVarargs);
if (bestSoFar.kind == AMBIGUOUS) {
AmbiguityError a_err = (AmbiguityError)bestSoFar.baseSymbol();
bestSoFar = a_err.mergeAbstracts(site);
}
return bestSoFar;
}
// where
@ -2757,7 +2761,7 @@ public class Resolve {
return lookupMethod(env, pos, env.enclClass.sym, resolveMethodCheck,
new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) {
@Override
Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
return findFun(env, name, argtypes, typeargtypes,
phase.isBoxingRequired(),
phase.isVarargsRequired());
@ -2789,7 +2793,7 @@ public class Resolve {
List<Type> typeargtypes) {
return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) {
@Override
Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
return findMethod(env, site, name, argtypes, typeargtypes,
phase.isBoxingRequired(),
phase.isVarargsRequired());
@ -2913,7 +2917,7 @@ public class Resolve {
List<Type> typeargtypes) {
return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
@Override
Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
return findConstructor(pos, env, site, argtypes, typeargtypes,
phase.isBoxingRequired(),
phase.isVarargsRequired());
@ -2972,7 +2976,7 @@ public class Resolve {
return lookupMethod(env, pos, site.tsym, resolveMethodCheck,
new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
@Override
Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
return findDiamond(pos, env, site, argtypes, typeargtypes,
phase.isBoxingRequired(),
phase.isVarargsRequired());
@ -3503,18 +3507,6 @@ public class Resolve {
super(name, site, argtypes, typeargtypes, maxPhase);
}
@Override
final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
Symbol sym = doLookup(env, phase);
if (sym.kind == AMBIGUOUS) {
AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
sym = a_err.mergeAbstracts(site);
}
return sym;
}
abstract Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase);
@Override
Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
if (sym.kind.isResolutionError()) {
@ -3561,10 +3553,6 @@ public class Resolve {
abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym);
Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
if (sym.kind == AMBIGUOUS) {
AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
sym = a_err.mergeAbstracts(site);
}
//skip error reporting
return sym;
}

View File

@ -0,0 +1,23 @@
/*
* @test /nodynamiccopyright/
* @bug 8337980
* @summary Test compiler crash due to failure to resolve method ambiguity
* @compile/fail/ref=MethodAmbiguityCrash1.out -XDrawDiagnostics MethodAmbiguityCrash1.java
*/
public class MethodAmbiguityCrash1 {
public interface A {
int op();
}
public abstract static class B {
abstract int op();
}
public abstract static class C extends B implements A {
public static int test() {
return op(); // compile should fail here
}
}
}

View File

@ -0,0 +1,2 @@
MethodAmbiguityCrash1.java:20:20: compiler.err.non-static.cant.be.ref: kindname.method, op()
1 error

View File

@ -0,0 +1,26 @@
/*
* @test /nodynamiccopyright/
* @bug 8337980
* @summary Test compiler crash due to failure to resolve method ambiguity
* @compile/fail/ref=MethodAmbiguityCrash2.out -XDrawDiagnostics MethodAmbiguityCrash2.java
*/
public class MethodAmbiguityCrash2 {
public interface A {
int op();
}
public abstract static class B {
public abstract int op();
}
public abstract static class C extends B implements A {
public C(int x) {
}
public C() {
this(op()); // compile should fail here
}
}
}

View File

@ -0,0 +1,2 @@
MethodAmbiguityCrash2.java:23:18: compiler.err.cant.ref.before.ctor.called: op()
1 error