backend: fix minor bug in evaluateWhen
Also add the corresponding test 'when9'.
This commit is contained in:
committed by
SvyatoslavScherbina
parent
0958d32605
commit
1f7738e052
+7
-2
@@ -1151,9 +1151,13 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
||||
if (!isNothing) // If "when" has "exit".
|
||||
bbExit = codegen.basicBlock() // Create basic block to process "exit".
|
||||
|
||||
val resultPhi = if (isUnit || isNothing) null else
|
||||
val hasNoValue = !isUnconditional(expression.branches.last())
|
||||
// (It is possible if IrWhen is used as statement).
|
||||
|
||||
val llvmType = codegen.getLLVMType(expression.type)
|
||||
val resultPhi = if (isUnit || isNothing || hasNoValue) null else
|
||||
codegen.appendingTo(bbExit!!) {
|
||||
codegen.phi(codegen.getLLVMType(expression.type))
|
||||
codegen.phi(llvmType)
|
||||
}
|
||||
|
||||
expression.branches.forEach { // Iterate through "when" branches (clauses).
|
||||
@@ -1167,6 +1171,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
||||
// FIXME: remove the hacks.
|
||||
isUnit -> codegen.theUnitInstanceRef.llvm
|
||||
isNothing -> codegen.kNothingFakeValue
|
||||
hasNoValue -> LLVMGetUndef(llvmType)!!
|
||||
else -> resultPhi!!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,6 +330,11 @@ task when8(type: RunKonanTest) {
|
||||
goldValue = "true\n"
|
||||
}
|
||||
|
||||
task when9(type: RunKonanTest) {
|
||||
goldValue = "Ok\n"
|
||||
source = "codegen/branching/when9.kt"
|
||||
}
|
||||
|
||||
task when_through(type: RunKonanTest) {
|
||||
source = "codegen/branching/when_through.kt"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
fun main(args: Array<String>) {
|
||||
foo(0)
|
||||
println("Ok")
|
||||
}
|
||||
|
||||
fun foo(x: Int) {
|
||||
when (x) {
|
||||
0 -> 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user