KT-4960 Redeclaration is not reported for type parameters of interfaces

When resolving a class body for a class without a primary constructor
(e.g., an interface), no checks were performed for redeclarations
in the corresponding class header.
Creating & initializing a lexical scope of an appropriate kind will do it.
Note that since class has no primary constructor, only type parameters
could be redeclared (and that's KT-4960).
This commit is contained in:
Dmitry Petrov
2017-03-28 15:59:33 +03:00
parent eb0c0a8869
commit 5ccfbcbe22
6 changed files with 62 additions and 2 deletions
@@ -268,6 +268,9 @@ public class BodyResolver {
primaryConstructor == null
? null
: FunctionDescriptorUtil.getFunctionInnerScope(scopeForConstructorResolution, primaryConstructor, trace, overloadChecker);
if (primaryConstructor == null) {
checkRedeclarationsInClassHeaderWithoutPrimaryConstructor(descriptor, scopeForConstructorResolution);
}
ExpressionTypingServices typeInferrer = expressionTypingServices; // TODO : flow
Map<KtTypeReference, KotlinType> supertypes = Maps.newLinkedHashMap();
@@ -396,6 +399,25 @@ public class BodyResolver {
checkSupertypeList(descriptor, supertypes, ktClass);
}
private void checkRedeclarationsInClassHeaderWithoutPrimaryConstructor(
@NotNull final ClassDescriptor descriptor, @NotNull LexicalScope scopeForConstructorResolution
) {
// Initializing a scope will report errors if any.
new LexicalScopeImpl(
scopeForConstructorResolution, descriptor, true, null, LexicalScopeKind.CLASS_HEADER,
new TraceBasedLocalRedeclarationChecker(trace, overloadChecker),
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
// If a class has no primary constructor, it still can have type parameters declared in header.
for (TypeParameterDescriptor typeParameter : descriptor.getDeclaredTypeParameters()) {
handler.addClassifierDescriptor(typeParameter);
}
return Unit.INSTANCE;
}
});
}
private void resolveConstructorCallForEnumEntryWithoutInitializer(
@NotNull KtEnumEntry ktEnumEntry,
@NotNull ClassDescriptor enumEntryDescriptor,
@@ -0,0 +1,6 @@
interface Test1<<!REDECLARATION!>T<!>, <!REDECLARATION!>T<!>>
interface Test2<<!REDECLARATION!>X<!>, Y, <!REDECLARATION!>X<!>>
class Outer<T> {
interface TestNested<T>
}
@@ -0,0 +1,26 @@
package
public final class Outer</*0*/ T> {
public constructor Outer</*0*/ T>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public interface TestNested</*0*/ T> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public interface Test1</*0*/ T, /*1*/ T> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface Test2</*0*/ X, /*1*/ Y, /*2*/ X> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -15949,6 +15949,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("interfaceTypeParameters.kt")
public void testInterfaceTypeParameters() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/redeclarations/interfaceTypeParameters.kt");
doTest(fileName);
}
@TestMetadata("kt2418.kt")
public void testKt2418() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/redeclarations/kt2418.kt");
@@ -1,5 +1,5 @@
// "Remove type arguments" "true"
interface Foo<T, T> {
interface Foo<T1, T2> {
fun f() {}
}
@@ -1,5 +1,5 @@
// "Remove type arguments" "true"
interface Foo<T, T> {
interface Foo<T1, T2> {
fun f() {}
}