KT-11892: explicitly qualified 'super' with a supertype that is overridden

by a different explicitly declared supertype is an error (as in Java)
(see http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.3)
This commit is contained in:
Dmitry Petrov
2016-07-01 15:51:54 +03:00
parent dbf38823b9
commit 07cb3e09b9
9 changed files with 158 additions and 5 deletions
@@ -574,6 +574,7 @@ public interface Errors {
DiagnosticFactory0<KtExpression> ABSTRACT_SUPER_CALL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> NOT_A_SUPERTYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<KtTypeReference, KotlinType> QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE = DiagnosticFactory1.create(ERROR);
// Conventions
@@ -330,6 +330,7 @@ public class DefaultErrorMessages {
MAP.put(ABSTRACT_SUPER_CALL, "Abstract member cannot be accessed directly");
MAP.put(NOT_A_SUPERTYPE, "Not an immediate supertype");
MAP.put(TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER, "Type arguments do not need to be specified in a 'super' qualifier");
MAP.put(QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE, "Explicitly qualified supertype is extended by another supertype ''{0}''", RENDER_TYPE);
MAP.put(USELESS_CAST, "No cast needed");
MAP.put(CAST_NEVER_SUCCEEDS, "This cast can never succeed");
MAP.put(DYNAMIC_NOT_ALLOWED, "Dynamic types are not allowed in this position");
@@ -479,6 +479,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
else if (redundantTypeArguments != null) {
context.trace.report(TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER.on(redundantTypeArguments));
}
if (result != null && (validClassifier || validType)) {
checkResolvedExplicitlyQualifiedSupertype(context.trace, result, supertypes, superTypeQualifier);
}
}
else {
if (UnqualifiedSuperKt.isPossiblyAmbiguousUnqualifiedSuper(expression, supertypes)) {
@@ -522,6 +526,27 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return result;
}
private static void checkResolvedExplicitlyQualifiedSupertype(
@NotNull BindingTrace trace,
@NotNull KotlinType result,
@NotNull Collection<KotlinType> supertypes,
@NotNull KtTypeReference superTypeQualifier
) {
if (supertypes.size() > 1) {
ClassifierDescriptor resultClassifierDescriptor = result.getConstructor().getDeclarationDescriptor();
for (KotlinType otherSupertype : supertypes) {
ClassifierDescriptor otherSupertypeClassifierDescriptor = otherSupertype.getConstructor().getDeclarationDescriptor();
if (otherSupertypeClassifierDescriptor == resultClassifierDescriptor) {
continue;
}
if (KotlinTypeChecker.DEFAULT.isSubtypeOf(otherSupertype, result)) {
trace.report(QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE.on(superTypeQualifier, otherSupertype));
break;
}
}
}
}
@NotNull // No class receivers
private LabelResolver.LabeledReceiverResolutionResult resolveToReceiver(
KtInstanceExpressionWithLabel expression,
@@ -0,0 +1,19 @@
interface IBase<T> {
fun foo() {}
fun bar() {}
}
interface IDerived<T> : IBase<T> {
override fun foo() {}
fun qux() {}
}
class Test : IDerived<String>, IBase<String> {
fun test() {
super<<!QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE!>IBase<!>>.foo()
super<<!QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE!>IBase<!>>.bar()
super<IDerived>.foo()
super<IDerived>.bar()
super<IDerived>.qux()
}
}
@@ -0,0 +1,29 @@
package
public interface IBase</*0*/ T> {
public open fun bar(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface IDerived</*0*/ T> : IBase<T> {
public open override /*1*/ /*fake_override*/ fun bar(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open fun qux(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Test : IDerived<kotlin.String>, IBase<kotlin.String> {
public constructor Test()
public open override /*2*/ /*fake_override*/ fun bar(): kotlin.Unit
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*2*/ /*fake_override*/ fun foo(): kotlin.Unit
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun qux(): kotlin.Unit
public final fun test(): kotlin.Unit
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,29 @@
interface IBase {
fun foo() {}
fun bar() {}
}
interface IDerived1 : IBase {
override fun foo() {}
fun qux() {}
}
interface IDerived2 : IBase {
override fun foo() {}
}
class Test : IDerived1, IBase, IDerived2 {
override fun foo() {}
fun test() {
super<<!QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE!>IBase<!>>.foo()
super<<!QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE!>IBase<!>>.bar()
super<IDerived1>.foo()
super<IDerived1>.bar()
super<IDerived1>.qux()
super<IDerived2>.foo()
super<IDerived2>.bar()
}
}
@@ -0,0 +1,37 @@
package
public interface IBase {
public open fun bar(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface IDerived1 : IBase {
public open override /*1*/ /*fake_override*/ fun bar(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open fun qux(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface IDerived2 : IBase {
public open override /*1*/ /*fake_override*/ fun bar(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Test : IDerived1, IBase, IDerived2 {
public constructor Test()
public open override /*3*/ /*fake_override*/ fun bar(): kotlin.Unit
public open override /*3*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*3*/ fun foo(): kotlin.Unit
public open override /*3*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun qux(): kotlin.Unit
public final fun test(): kotlin.Unit
public open override /*3*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -19365,12 +19365,24 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("genericQualifiedSuperOverridden.kt")
public void testGenericQualifiedSuperOverridden() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/thisAndSuper/genericQualifiedSuperOverridden.kt");
doTest(fileName);
}
@TestMetadata("notAccessibleSuperInTrait.kt")
public void testNotAccessibleSuperInTrait() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/thisAndSuper/notAccessibleSuperInTrait.kt");
doTest(fileName);
}
@TestMetadata("qualifiedSuperOverridden.kt")
public void testQualifiedSuperOverridden() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/thisAndSuper/qualifiedSuperOverridden.kt");
doTest(fileName);
}
@TestMetadata("QualifiedThis.kt")
public void testQualifiedThis() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/thisAndSuper/QualifiedThis.kt");
@@ -5,12 +5,12 @@ interface A {
fun foo(t: Int): Int = t + 1
}
interface B : A {
override fun bar(): Int = 3
interface B {
fun foo(t: Int): Int = t
fun bar(): Int = 3
}
class C : B, A {
override fun bar(): Int {
return super<B>.bar() + super<A>.bar()
}
@@ -23,7 +23,7 @@ class C : B, A {
fun box(): String {
val c = C()
if (c.foo(3) != 5) return "Trait super call fail. c.foo(3) is ${c.foo(3)}"
if (c.bar() != 5) return "Trait super call fail. c.bar() is ${c.bar()}"
if (c.foo(3) != 5) return "Interface super call fail. c.foo(3) is ${c.foo(3)}"
if (c.bar() != 5) return "Interface super call fail. c.bar() is ${c.bar()}"
return "OK"
}