check constants with Unit return type as usual
This commit is contained in:
+6
-6
@@ -109,7 +109,7 @@ public class CompileTimeConstantResolver {
|
||||
if (value == null) {
|
||||
return ErrorValue.create(INT_LITERAL_OUT_OF_RANGE.on(expression));
|
||||
}
|
||||
if (noExpectedTypeOrUnitOrError(expectedType)) {
|
||||
if (noExpectedTypeOrError(expectedType)) {
|
||||
if (Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE) {
|
||||
return new IntValue(value.intValue());
|
||||
}
|
||||
@@ -198,7 +198,7 @@ public class CompileTimeConstantResolver {
|
||||
) {
|
||||
String text = expression.getText();
|
||||
try {
|
||||
if (noExpectedTypeOrUnitOrError(expectedType)
|
||||
if (noExpectedTypeOrError(expectedType)
|
||||
|| JetTypeChecker.INSTANCE.isSubtypeOf(builtIns.getDoubleType(), expectedType)) {
|
||||
return new DoubleValue(Double.parseDouble(text));
|
||||
}
|
||||
@@ -221,7 +221,7 @@ public class CompileTimeConstantResolver {
|
||||
JetType nativeType,
|
||||
JetConstantExpression expression
|
||||
) {
|
||||
if (!noExpectedTypeOrUnitOrError(expectedType)
|
||||
if (!noExpectedTypeOrError(expectedType)
|
||||
&& !JetTypeChecker.INSTANCE.isSubtypeOf(nativeType, expectedType)) {
|
||||
|
||||
return ErrorValue.create(CONSTANT_EXPECTED_TYPE_MISMATCH.on(expression, title, expectedType));
|
||||
@@ -346,13 +346,13 @@ public class CompileTimeConstantResolver {
|
||||
public CompileTimeConstant<?> getNullValue(
|
||||
@NotNull JetConstantExpression expression, @NotNull JetType expectedType
|
||||
) {
|
||||
if (noExpectedTypeOrUnitOrError(expectedType) || expectedType.isNullable()) {
|
||||
if (noExpectedTypeOrError(expectedType) || expectedType.isNullable()) {
|
||||
return NullValue.NULL;
|
||||
}
|
||||
return ErrorValue.create(NULL_FOR_NONNULL_TYPE.on(expression, expectedType));
|
||||
}
|
||||
|
||||
private static boolean noExpectedTypeOrUnitOrError(JetType expectedType) {
|
||||
return TypeUtils.noExpectedType(expectedType) || KotlinBuiltIns.getInstance().isUnit(expectedType) || ErrorUtils.isErrorType(expectedType);
|
||||
private static boolean noExpectedTypeOrError(JetType expectedType) {
|
||||
return TypeUtils.noExpectedType(expectedType) || ErrorUtils.isErrorType(expectedType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
fun foo(<!UNUSED_PARAMETER!>u<!> : Unit) : Int = 1
|
||||
|
||||
fun test() : Int {
|
||||
foo(<!TYPE_MISMATCH!>1<!>)
|
||||
foo(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||
val <!UNUSED_VARIABLE!>a<!> : () -> Unit = {
|
||||
foo(<!TYPE_MISMATCH!>1<!>)
|
||||
foo(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||
}
|
||||
return 1 <!NONE_APPLICABLE!>-<!> "1"
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ fun none() {}
|
||||
fun unitEmptyInfer() {}
|
||||
fun unitEmpty() : Unit {}
|
||||
fun unitEmptyReturn() : Unit {return}
|
||||
fun unitIntReturn() : Unit {return <!TYPE_MISMATCH!>1<!>}
|
||||
fun unitIntReturn() : Unit {return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>}
|
||||
fun unitUnitReturn() : Unit {return Unit.VALUE}
|
||||
fun test1() : Any = {<!RETURN_NOT_ALLOWED, RETURN_TYPE_MISMATCH!>return<!>}
|
||||
fun test2() : Any = @a {<!RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED!>return@a 1<!>}
|
||||
@@ -13,7 +13,7 @@ fun test5(): Any = @{ <!RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED!>return
|
||||
fun test6(): Any = {<!RETURN_NOT_ALLOWED!>return 1<!>}
|
||||
|
||||
fun bbb() {
|
||||
return <!TYPE_MISMATCH!>1<!>
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
|
||||
}
|
||||
|
||||
fun foo(<!UNUSED_PARAMETER!>expr<!>: StringBuilder): Int {
|
||||
@@ -26,8 +26,8 @@ fun foo(<!UNUSED_PARAMETER!>expr<!>: StringBuilder): Int {
|
||||
|
||||
|
||||
fun unitShort() : Unit = Unit.VALUE
|
||||
fun unitShortConv() : Unit = <!TYPE_MISMATCH!>1<!>
|
||||
fun unitShortNull() : Unit = <!TYPE_MISMATCH!>null<!>
|
||||
fun unitShortConv() : Unit = <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
|
||||
fun unitShortNull() : Unit = <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
|
||||
fun intEmpty() : Int {<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
|
||||
fun intShortInfer() = 1
|
||||
@@ -40,7 +40,7 @@ fun intString(): Int = <!TYPE_MISMATCH!>"s"<!>
|
||||
fun intFunctionLiteral(): Int = <!TYPE_MISMATCH!>{ 10 }<!>
|
||||
|
||||
fun blockReturnUnitMismatch() : Int {<!RETURN_TYPE_MISMATCH!>return<!>}
|
||||
fun blockReturnValueTypeMismatch() : Int {return <!ERROR_COMPILE_TIME_VALUE!>3.4<!>}
|
||||
fun blockReturnValueTypeMismatch() : Int {return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>3.4<!>}
|
||||
fun blockReturnValueTypeMatch() : Int {return 1}
|
||||
fun blockReturnValueTypeMismatchUnit() : Int {return <!TYPE_MISMATCH!>Unit.VALUE<!>}
|
||||
|
||||
@@ -51,7 +51,7 @@ fun blockAndAndMismatch1() : Int {
|
||||
return <!TYPE_MISMATCH!>true && false<!>
|
||||
}
|
||||
fun blockAndAndMismatch2() : Int {
|
||||
<!UNREACHABLE_CODE!>(return <!ERROR_COMPILE_TIME_VALUE!>true<!>) && (return <!ERROR_COMPILE_TIME_VALUE!>false<!>)<!>
|
||||
<!UNREACHABLE_CODE!>(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) && (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)<!>
|
||||
}
|
||||
|
||||
fun blockAndAndMismatch3() : Int {
|
||||
@@ -61,10 +61,10 @@ fun blockAndAndMismatch4() : Int {
|
||||
return <!TYPE_MISMATCH!>true || false<!>
|
||||
}
|
||||
fun blockAndAndMismatch5() : Int {
|
||||
<!UNREACHABLE_CODE!>(return <!ERROR_COMPILE_TIME_VALUE!>true<!>) || (return <!ERROR_COMPILE_TIME_VALUE!>false<!>)<!>
|
||||
<!UNREACHABLE_CODE!>(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) || (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)<!>
|
||||
}
|
||||
fun blockReturnValueTypeMatch1() : Int {
|
||||
return if (1 > 2) <!TYPE_MISMATCH!>1.0<!> else <!TYPE_MISMATCH!>2.0<!>
|
||||
return if (1 > 2) <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!> else <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2.0<!>
|
||||
}
|
||||
fun blockReturnValueTypeMatch2() : Int {
|
||||
return <!TYPE_MISMATCH!>if (1 > 2) 1<!>
|
||||
@@ -74,18 +74,18 @@ fun blockReturnValueTypeMatch3() : Int {
|
||||
}
|
||||
fun blockReturnValueTypeMatch4() : Int {
|
||||
if (1 > 2)
|
||||
return <!ERROR_COMPILE_TIME_VALUE!>1.0<!>
|
||||
else return <!ERROR_COMPILE_TIME_VALUE!>2.0<!>
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!>
|
||||
else return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2.0<!>
|
||||
}
|
||||
fun blockReturnValueTypeMatch5() : Int {
|
||||
if (1 > 2)
|
||||
return <!ERROR_COMPILE_TIME_VALUE!>1.0<!>
|
||||
return <!ERROR_COMPILE_TIME_VALUE!>2.0<!>
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!>
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2.0<!>
|
||||
}
|
||||
fun blockReturnValueTypeMatch6() : Int {
|
||||
if (1 > 2)
|
||||
else return <!ERROR_COMPILE_TIME_VALUE!>1.0<!>
|
||||
return <!ERROR_COMPILE_TIME_VALUE!>2.0<!>
|
||||
else return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!>
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2.0<!>
|
||||
}
|
||||
fun blockReturnValueTypeMatch7() : Int {
|
||||
if (1 > 2)
|
||||
@@ -113,7 +113,7 @@ fun blockReturnValueTypeMatch11() : Int {
|
||||
fun blockReturnValueTypeMatch12() : Int {
|
||||
if (1 > 2)
|
||||
return 1
|
||||
else return <!ERROR_COMPILE_TIME_VALUE!>1.0<!>
|
||||
else return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!>
|
||||
}
|
||||
fun blockNoReturnIfValDeclaration(): Int {
|
||||
val <!UNUSED_VARIABLE!>x<!> = 1
|
||||
@@ -140,21 +140,21 @@ class A() {
|
||||
}
|
||||
fun illegalConstantBody(): Int = <!TYPE_MISMATCH!>"s"<!>
|
||||
fun illegalConstantBlock(): String {
|
||||
return <!ERROR_COMPILE_TIME_VALUE!>1<!>
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
|
||||
}
|
||||
fun illegalIfBody(): Int =
|
||||
if (1 < 2) <!TYPE_MISMATCH!>'a'<!> else { <!TYPE_MISMATCH!>1.0<!> }
|
||||
if (1 < 2) <!CONSTANT_EXPECTED_TYPE_MISMATCH!>'a'<!> else { <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!> }
|
||||
fun illegalIfBlock(): Boolean {
|
||||
if (1 < 2)
|
||||
return false
|
||||
else { return <!ERROR_COMPILE_TIME_VALUE!>1<!> }
|
||||
else { return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!> }
|
||||
}
|
||||
fun illegalReturnIf(): Char {
|
||||
return if (1 < 2) 'a' else { <!TYPE_MISMATCH!>1<!> }
|
||||
return if (1 < 2) 'a' else { <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!> }
|
||||
}
|
||||
|
||||
fun returnNothing(): Nothing {
|
||||
throw <!ERROR_COMPILE_TIME_VALUE!>1<!>
|
||||
throw <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
|
||||
}
|
||||
fun f(): Int {
|
||||
if (1 < 2) { return 1 } else returnNothing()
|
||||
|
||||
+1
-1
@@ -106,7 +106,7 @@ fun testImplicitCoercion() {
|
||||
val <!UNUSED_VARIABLE!>h<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (false) 4 else {}<!>
|
||||
|
||||
bar(if (true) {
|
||||
<!TYPE_MISMATCH!>4<!>
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!>
|
||||
}
|
||||
else {
|
||||
z = <!UNUSED_VALUE!>342<!>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
fun foo(<!UNUSED_PARAMETER!>u<!> : Unit) : Int = 1
|
||||
|
||||
fun test() : Int {
|
||||
foo(<!TYPE_MISMATCH!>1<!>)
|
||||
foo(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||
val <!UNUSED_VARIABLE!>a<!> : () -> Unit = {
|
||||
foo(<!TYPE_MISMATCH!>1<!>)
|
||||
foo(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user