From 298d344c316affb67bb600b3fe04111bf9ca6dc2 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 12 Sep 2013 17:40:23 +0400 Subject: [PATCH] check constants with Unit return type as usual --- .../CompileTimeConstantResolver.java | 12 +++--- compiler/testData/diagnostics/tests/Basic.kt | 4 +- .../diagnostics/tests/FunctionReturnTypes.kt | 40 +++++++++---------- .../kt770.kt351.kt735_StatementType.kt | 2 +- .../tests/regressions/CoercionToUnit.kt | 4 +- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java index 95180e47926..bed06c06c24 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java @@ -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); } } diff --git a/compiler/testData/diagnostics/tests/Basic.kt b/compiler/testData/diagnostics/tests/Basic.kt index 68cd06d9776..c005cd17aaa 100644 --- a/compiler/testData/diagnostics/tests/Basic.kt +++ b/compiler/testData/diagnostics/tests/Basic.kt @@ -1,9 +1,9 @@ fun foo(u : Unit) : Int = 1 fun test() : Int { - foo(1) + foo(1) val a : () -> Unit = { - foo(1) + foo(1) } return 1 - "1" } diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt index 83e48288483..3fdeb3487de 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt @@ -3,7 +3,7 @@ fun none() {} fun unitEmptyInfer() {} fun unitEmpty() : Unit {} fun unitEmptyReturn() : Unit {return} -fun unitIntReturn() : Unit {return 1} +fun unitIntReturn() : Unit {return 1} fun unitUnitReturn() : Unit {return Unit.VALUE} fun test1() : Any = {return} fun test2() : Any = @a {return@a 1} @@ -13,7 +13,7 @@ fun test5(): Any = @{ return fun test6(): Any = {return 1} fun bbb() { - return 1 + return 1 } fun foo(expr: StringBuilder): Int { @@ -26,8 +26,8 @@ fun foo(expr: StringBuilder): Int { fun unitShort() : Unit = Unit.VALUE -fun unitShortConv() : Unit = 1 -fun unitShortNull() : Unit = null +fun unitShortConv() : Unit = 1 +fun unitShortNull() : Unit = null fun intEmpty() : Int {} fun intShortInfer() = 1 @@ -40,7 +40,7 @@ fun intString(): Int = "s" fun intFunctionLiteral(): Int = { 10 } fun blockReturnUnitMismatch() : Int {return} -fun blockReturnValueTypeMismatch() : Int {return 3.4} +fun blockReturnValueTypeMismatch() : Int {return 3.4} fun blockReturnValueTypeMatch() : Int {return 1} fun blockReturnValueTypeMismatchUnit() : Int {return Unit.VALUE} @@ -51,7 +51,7 @@ fun blockAndAndMismatch1() : Int { return true && false } fun blockAndAndMismatch2() : Int { - (return true) && (return false) + (return true) && (return false) } fun blockAndAndMismatch3() : Int { @@ -61,10 +61,10 @@ fun blockAndAndMismatch4() : Int { return true || false } fun blockAndAndMismatch5() : Int { - (return true) || (return false) + (return true) || (return false) } fun blockReturnValueTypeMatch1() : Int { - return if (1 > 2) 1.0 else 2.0 + return if (1 > 2) 1.0 else 2.0 } fun blockReturnValueTypeMatch2() : Int { return if (1 > 2) 1 @@ -74,18 +74,18 @@ fun blockReturnValueTypeMatch3() : Int { } fun blockReturnValueTypeMatch4() : Int { if (1 > 2) - return 1.0 - else return 2.0 + return 1.0 + else return 2.0 } fun blockReturnValueTypeMatch5() : Int { if (1 > 2) - return 1.0 - return 2.0 + return 1.0 + return 2.0 } fun blockReturnValueTypeMatch6() : Int { if (1 > 2) - else return 1.0 - return 2.0 + else return 1.0 + return 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 1.0 + else return 1.0 } fun blockNoReturnIfValDeclaration(): Int { val x = 1 @@ -140,21 +140,21 @@ class A() { } fun illegalConstantBody(): Int = "s" fun illegalConstantBlock(): String { - return 1 + return 1 } fun illegalIfBody(): Int = - if (1 < 2) 'a' else { 1.0 } + if (1 < 2) 'a' else { 1.0 } fun illegalIfBlock(): Boolean { if (1 < 2) return false - else { return 1 } + else { return 1 } } fun illegalReturnIf(): Char { - return if (1 < 2) 'a' else { 1 } + return if (1 < 2) 'a' else { 1 } } fun returnNothing(): Nothing { - throw 1 + throw 1 } fun f(): Int { if (1 < 2) { return 1 } else returnNothing() diff --git a/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt b/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt index e7ba9273d79..b7bd29d8726 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt @@ -106,7 +106,7 @@ fun testImplicitCoercion() { val h = if (false) 4 else {} bar(if (true) { - 4 + 4 } else { z = 342 diff --git a/compiler/testData/diagnostics/tests/regressions/CoercionToUnit.kt b/compiler/testData/diagnostics/tests/regressions/CoercionToUnit.kt index 8cf346de150..ba04871cefc 100644 --- a/compiler/testData/diagnostics/tests/regressions/CoercionToUnit.kt +++ b/compiler/testData/diagnostics/tests/regressions/CoercionToUnit.kt @@ -1,9 +1,9 @@ fun foo(u : Unit) : Int = 1 fun test() : Int { - foo(1) + foo(1) val a : () -> Unit = { - foo(1) + foo(1) } return 1 }