8365676: javac incorrectly allows calling interface static method via type variable

Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Reviewed-by: vromero
This commit is contained in:
Chen Liang 2025-09-10 21:23:45 +00:00
parent 85715e1050
commit 85996572b6
3 changed files with 37 additions and 3 deletions

View File

@ -4569,9 +4569,19 @@ public class Attr extends JCTree.Visitor {
log.error(pos, Errors.TypeVarCantBeDeref);
return syms.errSymbol;
} else {
Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
rs.new AccessError(env, site, sym) :
sym;
// JLS 4.9 specifies the members are derived by inheritance.
// We skip inducing a whole class by filtering members that
// can never be inherited:
Symbol sym2;
if (sym.isPrivate()) {
// Private members
sym2 = rs.new AccessError(env, site, sym);
} else if (sym.owner.isInterface() && sym.kind == MTH && (sym.flags() & STATIC) != 0) {
// Interface static methods
sym2 = rs.new SymbolNotFoundError(ABSENT_MTH);
} else {
sym2 = sym;
}
rs.accessBase(sym2, pos, location, site, name, true);
return sym;
}

View File

@ -0,0 +1,20 @@
/*
* @test /nodynamiccopyright/
* @bug 8365676
* @summary Interface static methods should not be inherited by type variables
* @compile/fail/ref=T8365676.out -XDrawDiagnostics T8365676.java
*/
import java.text.Collator;
import java.util.Comparator;
class T8365676 {
// T and P should have equivalent members
<T extends Comparator<Object>, P extends Object & Comparator<Object>>
void test() {
Comparator.reverseOrder();
Collator.reverseOrder(); // Fails
P.reverseOrder(); // Fails
T.reverseOrder(); // Should fail
}
}

View File

@ -0,0 +1,4 @@
T8365676.java:16:17: compiler.err.cant.resolve.location.args: kindname.method, reverseOrder, , , (compiler.misc.location: kindname.class, java.text.Collator, null)
T8365676.java:17:10: compiler.err.cant.resolve.location.args: kindname.method, reverseOrder, , , (compiler.misc.location: kindname.type.variable.bound, java.lang.Object&java.util.Comparator<java.lang.Object>, null)
T8365676.java:18:10: compiler.err.cant.resolve.location.args: kindname.method, reverseOrder, , , (compiler.misc.location: kindname.type.variable.bound, T, null)
3 errors