check constants with Unit return type as usual

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