Error on using extension function type as an upper bound

This commit is contained in:
Michael Nedzelsky
2015-10-28 18:46:09 +03:00
parent ee46ec1126
commit c318a13e6c
10 changed files with 70 additions and 5 deletions
@@ -259,6 +259,7 @@ public interface Errors {
DiagnosticFactory1<KtTypeReference, KotlinType> FINAL_UPPER_BOUND = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<KtTypeReference> DYNAMIC_UPPER_BOUND = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtNamedDeclaration, TypeParameterDescriptor> CONFLICTING_UPPER_BOUNDS =
DiagnosticFactory1.create(ERROR, DECLARATION_NAME);
@@ -391,6 +391,7 @@ public class DefaultErrorMessages {
MAP.put(UPPER_BOUND_VIOLATED, "Type argument is not within its bounds: should be subtype of ''{0}''", RENDER_TYPE, RENDER_TYPE);
MAP.put(FINAL_UPPER_BOUND, "''{0}'' is a final type, and thus a value of the type parameter is predetermined", RENDER_TYPE);
MAP.put(UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE, "Extension function type can not be used as an upper bound");
MAP.put(DYNAMIC_UPPER_BOUND, "Dynamic type can not be used as an upper bound");
MAP.put(USELESS_ELVIS, "Elvis operator (?:) always returns the left operand of non-nullable type {0}", RENDER_TYPE);
MAP.put(USELESS_ELVIS_ON_FUNCTION_LITERAL, "Left operand of elvis operator (?:) is function literal");
@@ -589,6 +589,9 @@ public class DescriptorResolver {
if (DynamicTypesKt.isDynamic(upperBoundType)) {
trace.report(DYNAMIC_UPPER_BOUND.on(upperBound));
}
if (KotlinBuiltIns.isExactExtensionFunctionType(upperBoundType)) {
trace.report(UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE.on(upperBound));
}
}
@NotNull
@@ -1,13 +1,13 @@
fun <E : String?, T : ((CharSequence).() -> Unit)?> foo(x: E, y: T) {
fun <E : String?, T : ((CharSequence) -> Unit)?> foo(x: E, y: T) {
if (x != null) {
<!DEBUG_INFO_SMARTCAST!>x<!>.<!UNSAFE_CALL, INVOKE_EXTENSION_ON_NOT_EXTENSION_FUNCTION!>y<!>()
<!UNSAFE_CALL!>y<!>(<!DEBUG_INFO_SMARTCAST!>x<!>)
}
if (y != null) {
x<!UNSAFE_CALL!>.<!><!INVOKE_EXTENSION_ON_NOT_EXTENSION_FUNCTION, DEBUG_INFO_SMARTCAST!>y<!>()
<!DEBUG_INFO_SMARTCAST!>y<!>(<!TYPE_MISMATCH!>x<!>)
}
if (x != null && y != null) {
<!DEBUG_INFO_SMARTCAST!>x<!>.<!INVOKE_EXTENSION_ON_NOT_EXTENSION_FUNCTION, DEBUG_INFO_SMARTCAST!>y<!>()
<!DEBUG_INFO_SMARTCAST!>y<!>(<!DEBUG_INFO_SMARTCAST!>x<!>)
}
}
@@ -1,3 +1,3 @@
package
public fun </*0*/ E : kotlin.String?, /*1*/ T : (kotlin.CharSequence.() -> kotlin.Unit)?> foo(/*0*/ x: E, /*1*/ y: T): kotlin.Unit
public fun </*0*/ E : kotlin.String?, /*1*/ T : ((kotlin.CharSequence) -> kotlin.Unit)?> foo(/*0*/ x: E, /*1*/ y: T): kotlin.Unit
@@ -0,0 +1,7 @@
fun <T: <!UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE!>Int.() -> String<!>> foo() {}
val <<!TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER!>T: <!UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE!>Int.() -> String<!><!>> bar = fun (x: Int): String { return x.toString() }
class A<T> where T : <!UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE!>Double.(Int) -> Unit<!>
interface B<T, U : <!UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE!>T.() -> Unit<!>>
@@ -0,0 +1,17 @@
package
public val </*0*/ T : kotlin.Int.() -> kotlin.String> bar: (kotlin.Int) -> kotlin.String
public fun </*0*/ T : kotlin.Int.() -> kotlin.String> foo(): kotlin.Unit
public final class A</*0*/ T : kotlin.Double.(kotlin.Int) -> kotlin.Unit> {
public constructor A</*0*/ T : kotlin.Double.(kotlin.Int) -> kotlin.Unit>()
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 B</*0*/ T, /*1*/ U : T.() -> kotlin.Unit> {
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
}
@@ -0,0 +1,7 @@
fun <T: (Int) -> String> foo() {}
val <<!TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER!>T: (kotlin.Int) -> kotlin.String<!>> bar = fun (x: Int): String { return x.toString() }
class A<T, U, V> where T : () -> Unit, U : (Int) -> Double, V : (T, U) -> U
interface B<T, U : (T) -> Unit>
@@ -0,0 +1,17 @@
package
public val </*0*/ T : (kotlin.Int) -> kotlin.String> bar: (kotlin.Int) -> kotlin.String
public fun </*0*/ T : (kotlin.Int) -> kotlin.String> foo(): kotlin.Unit
public final class A</*0*/ T : () -> kotlin.Unit, /*1*/ U : (kotlin.Int) -> kotlin.Double, /*2*/ V : (T, U) -> U> {
public constructor A</*0*/ T : () -> kotlin.Unit, /*1*/ U : (kotlin.Int) -> kotlin.Double, /*2*/ V : (T, U) -> U>()
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 B</*0*/ T, /*1*/ U : (T) -> kotlin.Unit> {
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
}
@@ -16563,6 +16563,18 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("extFunctionTypeAsUpperBound.kt")
public void testExtFunctionTypeAsUpperBound() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typeParameters/extFunctionTypeAsUpperBound.kt");
doTest(fileName);
}
@TestMetadata("functionTypeAsUpperBound.kt")
public void testFunctionTypeAsUpperBound() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typeParameters/functionTypeAsUpperBound.kt");
doTest(fileName);
}
@TestMetadata("misplacedConstraints.kt")
public void testMisplacedConstraints() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typeParameters/misplacedConstraints.kt");