fix KT-9553 Invalid const initializers must be compile-time errors

#KT-9553 Fixed
This commit is contained in:
Michael Nedzelsky
2015-11-26 22:15:01 +03:00
parent 18f892892c
commit 9aae5e6457
3 changed files with 32 additions and 5 deletions
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
import org.jetbrains.kotlin.util.OperatorNameConventions
import java.math.BigInteger
import java.util.*
@@ -340,8 +341,26 @@ private class ConstantExpressionEvaluatorVisitor(
else null
}
override fun visitBinaryWithTypeRHSExpression(expression: KtBinaryExpressionWithTypeRHS, expectedType: KotlinType?): CompileTimeConstant<*>? =
evaluate(expression.getLeft(), expectedType)
override fun visitBinaryWithTypeRHSExpression(expression: KtBinaryExpressionWithTypeRHS, expectedType: KotlinType?): CompileTimeConstant<*>? {
val compileTimeConstant = evaluate(expression.left, expectedType)
if (compileTimeConstant != null) {
if (expectedType != null && !TypeUtils.noExpectedType(expectedType)) {
val constantType = when(compileTimeConstant) {
is TypedCompileTimeConstant<*> ->
compileTimeConstant.type
is IntegerValueTypeConstant ->
compileTimeConstant.getType(expectedType)
else ->
throw IllegalStateException("Unexpected compileTimeConstant class: ${compileTimeConstant.javaClass.canonicalName}")
}
if (!constantType.isSubtypeOf(expectedType)) return null
}
}
return compileTimeConstant
}
override fun visitBinaryExpression(expression: KtBinaryExpression, expectedType: KotlinType?): CompileTimeConstant<*>? {
val leftExpression = expression.getLeft() ?: return null
@@ -1,4 +1,4 @@
// !DIAGNOSTICS:-UNUSED_VARIABLE
// !DIAGNOSTICS:-UNUSED_VARIABLE,-CAST_NEVER_SUCCEEDS
import kotlin.reflect.KProperty
@@ -67,7 +67,11 @@ class Outer {
const val defaultGetter = 19
<!CONST_VAL_WITH_GETTER!>get<!>
const val nonConstInitializer = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>foo()<!>
const val nonConstInitializer1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>foo()<!>
const val nonConstInitializer2 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1 as String<!>
const val nonConstInitializer3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.0 as String<!>
const val nonConstInitializer4 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1 as Double<!>
const val nonConstInitializer5 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"2" as Int<!>
// ------------------
class Delegate {
@@ -2,7 +2,11 @@ package
public const val defaultGetter: kotlin.Int = 19
public const val delegated: kotlin.Int
public const val nonConstInitializer: kotlin.Int
public const val nonConstInitializer1: kotlin.Int
public const val nonConstInitializer2: kotlin.String
public const val nonConstInitializer3: kotlin.String
public const val nonConstInitializer4: kotlin.Double
public const val nonConstInitializer5: kotlin.Int
private val privateTopLevel: kotlin.Int = 3
public const var topLeveLVar: kotlin.Int
public const val topLevel: kotlin.Int = 0