From 35f4c968a498cb2bbcbe151bd2aeef4be2f97cb4 Mon Sep 17 00:00:00 2001 From: sergeych Date: Sun, 26 Apr 2026 21:42:36 +0300 Subject: [PATCH] T? ?: break inference fixed --- .../lyng/bytecode/BytecodeCompiler.kt | 21 +++++++++++++++++-- .../kotlin/net/sergeych/lyng/OptTest.kt | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt index 74ae496..99e456f 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt @@ -4473,6 +4473,20 @@ class BytecodeCompiler( } private fun compileElvis(ref: ElvisRef): CompiledValue? { + fun isAbruptControlRef(candidate: ObjRef): Boolean { + var stmt = (candidate as? StatementRef)?.statement ?: return false + while (stmt is BytecodeStatement) { + stmt = stmt.original + } + return when (stmt) { + is net.sergeych.lyng.BreakStatement, + is net.sergeych.lyng.ContinueStatement, + is net.sergeych.lyng.ReturnStatement, + is net.sergeych.lyng.ThrowStatement -> true + else -> false + } + } + val leftValue = compileRefWithFallback(ref.left, null, Pos.builtIn) ?: return null val leftObj = ensureObjSlot(leftValue) val resultSlot = allocSlot() @@ -4489,9 +4503,12 @@ class BytecodeCompiler( emitMove(leftObj, resultSlot) builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel))) builder.mark(rightLabel) + val rightIsAbruptControl = isAbruptControlRef(ref.right) val rightValue = compileRefWithFallback(ref.right, null, Pos.builtIn) ?: return null - val rightObj = ensureObjSlot(rightValue) - emitMove(rightObj, resultSlot) + if (!rightIsAbruptControl) { + val rightObj = ensureObjSlot(rightValue) + emitMove(rightObj, resultSlot) + } builder.mark(endLabel) updateSlotType(resultSlot, SlotType.OBJ) return CompiledValue(resultSlot, SlotType.OBJ) diff --git a/lynglib/src/commonTest/kotlin/net/sergeych/lyng/OptTest.kt b/lynglib/src/commonTest/kotlin/net/sergeych/lyng/OptTest.kt index b7e0db7..1cee9d1 100644 --- a/lynglib/src/commonTest/kotlin/net/sergeych/lyng/OptTest.kt +++ b/lynglib/src/commonTest/kotlin/net/sergeych/lyng/OptTest.kt @@ -148,6 +148,7 @@ class OptTest { val x = t(cnt++) ?: break assertEquals(100, x) assertEquals(100, needInt(x)) + assertEquals(101, x + 1) } assert( t(3) == null ) assert( cnt == 4 )