Step 21: union mismatch bytecode throw

This commit is contained in:
sergeych committed 2026-02-09 12:13:33 +03:00
1 parent b49f291bff
commit 9d508e219f
3 files changed
+24 -14

No files matched your search

@@ -4341,15 +4341,6 @@ class Compiler(
}
}
private class UnionTypeMismatchStatement(
private val message: String,
override val pos: Pos
) : Statement() {
override suspend fun execute(scope: Scope): Obj {
throw ScriptError(pos, message)
}
}
private fun resolveMemberHostForUnion(typeDecl: TypeDecl, memberName: String, pos: Pos): ObjClass {
val receiverClass = when (typeDecl) {
TypeDecl.TypeAny, TypeDecl.TypeNullableAny -> Obj.rootObjectType
@@ -4392,8 +4383,10 @@ class Compiler(
resolveMemberHostForUnion(option, memberName, pos)
}
val unionName = typeDeclName(union)
val failStmt = UnionTypeMismatchStatement("value is not $unionName", pos)
var current: ObjRef = net.sergeych.lyng.obj.StatementRef(failStmt)
val errorMessage = ObjString("value is not $unionName").asReadonly
val throwExpr = ExpressionStatement(ConstRef(errorMessage), pos)
val throwStmt = ThrowStatement(throwExpr, pos)
var current: ObjRef = net.sergeych.lyng.obj.StatementRef(throwStmt)
for (option in options.asReversed()) {
val typeRef = net.sergeych.lyng.obj.TypeDeclRef(option, pos)
val cond = BinaryOpRef(BinOp.IS, left, typeRef)
@@ -17,6 +17,7 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.ExecutionError
import net.sergeych.lyng.Script
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.Source
@@ -24,6 +25,7 @@ import net.sergeych.lyng.eval
import net.sergeych.lyng.obj.toInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class BytecodeRecentOpsTest {
@@ -221,6 +223,21 @@ class BytecodeRecentOpsTest {
)
}
@Test
fun unionMemberDispatchMismatch() = runTest {
val err = assertFailsWith<ExecutionError> {
eval(
"""
class A { fun who() = "A" }
class B { fun who() = "B" }
val x: A | B = 1
x.who()
""".trimIndent()
)
}
assertTrue(err.message?.contains("value is not A | B") == true)
}
@Test
fun objectReceiverMemberError() = runTest {
val failed = try {