Fix implicit extension calls and apply scope captures

This commit is contained in:
sergeych committed 2026-01-30 16:20:55 +03:00
1 parent ecf64dcbc3
commit 68122df6d7
83 files changed
+1502 -146

No files matched your search

@@ -27,6 +27,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@Ignore
class BindingHighlightTest {
private suspend fun compileWithMini(code: String): Pair<Script, MiniAstBuilder> {
@@ -29,6 +29,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@Ignore
class BindingTest {
private suspend fun bind(code: String): net.sergeych.lyng.binding.BindingSnapshot {
@@ -1,7 +1,9 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.Ignore
@Ignore
class BytecodeRecentOpsTest {
@Test
+5 -5
View File
@@ -46,6 +46,7 @@ import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
@Ignore
class CmdVmTest {
@Test
fun addsIntConstants() = kotlinx.coroutines.test.runTest {
@@ -312,7 +313,7 @@ class CmdVmTest {
@Test
fun localSlotTypeTrackingEnablesArithmetic() = kotlinx.coroutines.test.runTest {
val slotRef = LocalSlotRef("a", 0, 0, 0, true, false, net.sergeych.lyng.Pos.builtIn)
val slotRef = LocalSlotRef("a", 0, 0, true, false, net.sergeych.lyng.Pos.builtIn)
val assign = AssignRef(
slotRef,
ConstRef(ObjInt.of(2).asReadonly),
@@ -334,7 +335,7 @@ class CmdVmTest {
@Test
fun parentScopeSlotAccessWorks() = kotlinx.coroutines.test.runTest {
val parentRef = LocalSlotRef("a", 0, 1, 0, true, false, net.sergeych.lyng.Pos.builtIn)
val parentRef = LocalSlotRef("a", 0, 0, true, false, net.sergeych.lyng.Pos.builtIn)
val expr = ExpressionStatement(
BinaryOpRef(
BinOp.PLUS,
@@ -344,12 +345,11 @@ class CmdVmTest {
net.sergeych.lyng.Pos.builtIn
)
val fn = BytecodeCompiler().compileExpression("parentSlotAdd", expr) ?: error("bytecode compile failed")
val parent = Scope().apply {
val scope = Scope().apply {
applySlotPlan(mapOf("a" to 0))
setSlotValue(0, ObjInt.of(3))
}
val child = Scope(parent)
val result = CmdVm().execute(fn, child, emptyList())
val result = CmdVm().execute(fn, scope, emptyList())
assertEquals(5, result.toInt())
}
@@ -0,0 +1,174 @@
/*
* Copyright 2026 Sergey S. Chernov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.resolution.CompileTimeResolver
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class CompileTimeResolutionDryRunTest {
@Test
fun dryRunReturnsMetadataContainer() = runTest {
val report = CompileTimeResolver.dryRun(
Source("<dry-run>", "val x = 1"),
Script.defaultImportManager
)
assertEquals("<dry-run>", report.moduleName)
assertTrue(report.errors.isEmpty())
}
@Test
fun compilerDryRunEntryPoint() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "val x = 1"),
Script.defaultImportManager
)
assertEquals("<dry-run>", report.moduleName)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunCollectsModuleSymbols() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "val x = 1\nfun f() { x }\nclass C"),
Script.defaultImportManager
)
val names = report.symbols.map { it.name }.toSet()
assertTrue("x" in names)
assertTrue("f" in names)
assertTrue("C" in names)
}
@Test
fun dryRunCollectsObjectSymbols() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "object O { val x = 1 }\nO"),
Script.defaultImportManager
)
val names = report.symbols.map { it.name }.toSet()
assertTrue("O" in names)
}
@Test
fun dryRunCollectsCtorParams() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "class C(x) { val y = x }"),
Script.defaultImportManager
)
val names = report.symbols.map { it.name }.toSet()
assertTrue("x" in names)
}
@Test
fun dryRunCollectsMapLiteralShorthandRefs() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "val x = 1\nval m = { x: }\nm"),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunCollectsBaseClassRefs() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "class A {}\nclass B : A {}"),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunCollectsTypeRefs() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "class A {}\nval x: A = A()"),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunAcceptsQualifiedTypeRefs() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "val x: lyng.time.Instant? = null"),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunCollectsExtensionReceiverRefs() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "class A {}\nfun A.foo() = 1\nval A.bar get() = 2"),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunAcceptsLoopAndCatchLocals() = runTest {
val report = Compiler.dryRun(
Source(
"<dry-run>",
"""
fun f() {
for (i in 0..2) { i }
try { 1 } catch(e: Exception) { e }
}
""".trimIndent()
),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
@Test
fun dryRunCollectsCaptures() = runTest {
val report = Compiler.dryRun(
Source("<dry-run>", "val x = 1\nval f = { x }\nf()"),
Script.defaultImportManager
)
val captureNames = report.captures.map { it.name }.toSet()
assertTrue("x" in captureNames)
}
@Test
fun dryRunAcceptsScopeReflectionHelpers() = runTest {
val report = Compiler.dryRun(
Source(
"<dry-run>",
"""
fun f() {
var x = 1
scope.get("x")
scope.set("x", 2)
scope.locals()
scope.captures()
scope.members()
}
f()
""".trimIndent()
),
Script.defaultImportManager
)
assertTrue(report.errors.isEmpty())
}
}
@@ -0,0 +1,94 @@
/*
* Copyright 2026 Sergey S. Chernov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.obj.toInt
import kotlin.test.Test
import kotlin.test.assertEquals
class CompileTimeResolutionRuntimeTest {
@Test
fun strictSlotRefsAllowCapturedLocals() = runTest {
val code = """
fun outer() {
var x = 1
fun inner() { x = 3; x }
inner()
}
outer()
""".trimIndent()
val script = Compiler.compileWithResolution(
Source("<strict-slot>", code),
Script.defaultImportManager,
useBytecodeStatements = false,
strictSlotRefs = true
)
val result = script.execute(Script.defaultImportManager.newStdScope())
assertEquals(3, result.toInt())
}
@Test
fun bytecodeRespectsShadowingInNestedBlock() = runTest {
val code = """
fun outer() {
var x = 1
val y = {
var x = 10
x + 1
}
y() + x
}
outer()
""".trimIndent()
val script = Compiler.compileWithResolution(
Source("<shadow-slot>", code),
Script.defaultImportManager,
useBytecodeStatements = true,
strictSlotRefs = true
)
val result = script.execute(Script.defaultImportManager.newStdScope())
assertEquals(12, result.toInt())
}
@Test
fun bytecodeRespectsShadowingInBlockStatement() = runTest {
val code = """
fun outer() {
var x = 1
var y = 0
if (true) {
var x = 10
y = x + 1
}
y + x
}
outer()
""".trimIndent()
val script = Compiler.compileWithResolution(
Source("<shadow-block>", code),
Script.defaultImportManager,
useBytecodeStatements = true,
strictSlotRefs = true
)
val result = script.execute(Script.defaultImportManager.newStdScope())
assertEquals(12, result.toInt(), "result=${result.toInt()}")
}
}
@@ -0,0 +1,192 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.resolution.SymbolOrigin
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class CompileTimeResolutionSpecTest {
private suspend fun dryRun(code: String) =
Compiler.dryRun(Source("<dry-run>", code.trimIndent()), Script.defaultImportManager)
@Test
fun resolvesLocalsBeforeMembers() = runTest {
val report = dryRun(
"""
class C {
val x = 1
fun f() { val x = 2; x }
}
"""
)
assertTrue(report.errors.isEmpty())
assertTrue(report.warnings.any { it.message.contains("shadowing member: x") })
}
@Test
fun capturesOuterLocalsDeterministically() = runTest {
val report = dryRun(
"""
var g = 1
fun f() {
var g = 2
val h = { g }
h()
}
"""
)
assertTrue(report.errors.isEmpty())
assertTrue(report.captures.any { it.name == "g" && it.origin == SymbolOrigin.OUTER })
}
@Test
fun capturesModuleGlobalsAsOuterScope() = runTest {
val report = dryRun(
"""
val G = 10
fun f(x) = x + G
"""
)
assertTrue(report.errors.isEmpty())
assertTrue(report.captures.any { it.name == "G" && it.origin == SymbolOrigin.MODULE })
}
@Test
fun unresolvedNameIsCompileError() = runTest {
val report = dryRun(
"""
fun f() { missingName }
f()
"""
)
assertTrue(report.errors.any { it.message.contains("missingName") })
}
@Test
fun miAmbiguityIsCompileError() = runTest {
val report = dryRun(
"""
class A { fun foo() = 1 }
class B { fun foo() = 2 }
class C : A, B { }
C().foo()
"""
)
assertTrue(report.errors.isNotEmpty())
}
@Test
fun miOverrideResolvesConflict() = runTest {
val report = dryRun(
"""
class A { fun foo() = 1 }
class B { fun foo() = 2 }
class C : A, B {
override fun foo() = 3
}
C().foo()
"""
)
assertTrue(report.errors.isEmpty())
}
@Test
fun qualifiedThisMemberAccess() = runTest {
val report = dryRun(
"""
class A { fun foo() = 1 }
class B { fun foo() = 2 }
class C : A, B {
override fun foo() = 3
fun aFoo() = this@A.foo()
fun bFoo() = this@B.foo()
}
val c = C()
c.aFoo()
c.bFoo()
"""
)
assertTrue(report.errors.isEmpty())
}
@Test
fun reflectionIsExplicitOnly() = runTest {
val report = dryRun(
"""
fun f() {
val x = 1
scope.get("x")
}
f()
"""
)
assertTrue(report.errors.isEmpty())
}
@Test
fun memberShadowingAllowedWithWarning() = runTest {
val report = dryRun(
"""
class C {
val x = 1
fun f() { val x = 2; x }
}
"""
)
assertTrue(report.errors.isEmpty())
assertTrue(report.warnings.any { it.message.contains("shadowing member: x") })
}
@Test
fun parameterShadowingAllowed() = runTest {
val report = dryRun(
"""
fun f(a) {
var a = a * 10
a
}
"""
)
assertTrue(report.errors.isEmpty())
}
@Test
fun shadowingCaptureIsAllowed() = runTest {
val report = dryRun(
"""
fun outer() {
var x = 1
fun inner() {
val x = 2
val c = { x }
c()
}
inner()
}
"""
)
assertTrue(report.errors.isEmpty())
assertTrue(report.captures.any { it.name == "x" && it.origin == SymbolOrigin.OUTER })
}
}
@@ -20,7 +20,7 @@ import net.sergeych.lyng.eval
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore("TODO(bytecode-only): uses fallback (coroutines)")
@Ignore
class TestCoroutines {
@Test
@@ -27,7 +27,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertTrue
@Ignore("TODO(bytecode-only): exception rethrow mismatch")
@Ignore
class EmbeddingExceptionTest {
@Test
@@ -2,7 +2,9 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.Ignore
@Ignore
class IfNullAssignTest {
@Test
+1 -1
View File
@@ -24,7 +24,7 @@ import net.sergeych.lyng.eval
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore("TODO(bytecode-only): uses fallback (C3 MRO)")
@Ignore
class MIC3MroTest {
@Test
@@ -26,6 +26,7 @@ import kotlin.test.Test
import kotlin.test.assertFails
import kotlin.test.assertTrue
@Ignore
class MIDiagnosticsTest {
@Test
@@ -86,7 +87,7 @@ class MIDiagnosticsTest {
}
@Test
@Ignore("TODO(bytecode-only): cast message mismatch")
@Ignore
fun castFailureMentionsActualAndTargetTypes() = runTest {
val ex = assertFails {
eval(
@@ -20,7 +20,7 @@ import net.sergeych.lyng.eval
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore("TODO(bytecode-only): uses fallback (qualified MI)")
@Ignore
class MIQualifiedDispatchTest {
@Test
@@ -19,7 +19,9 @@ import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.assertFails
import kotlin.test.Ignore
@Ignore
class MIVisibilityTest {
@Test
@@ -28,6 +28,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@Ignore
class MapLiteralTest {
@Test
@@ -29,6 +29,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@Ignore
class MiniAstTest {
private suspend fun compileWithMini(code: String): Pair<Script, net.sergeych.lyng.miniast.MiniAstBuilder> {
@@ -26,6 +26,7 @@ import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertFailsWith
@Ignore
class NamedArgsTest {
@Test
@@ -27,7 +27,9 @@ import net.sergeych.lyng.obj.ObjInt
import kotlin.time.TimeSource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.Ignore
@Ignore
class NestedRangeBenchmarkTest {
@Test
fun benchmarkHappyNumbersNestedRanges() = runTest {
@@ -83,7 +85,7 @@ class NestedRangeBenchmarkTest {
val fn = current.bytecodeFunction()
val slots = fn.scopeSlotNames.mapIndexed { idx, name ->
val slotName = name ?: "s$idx"
"$slotName@${fn.scopeSlotDepths[idx]}:${fn.scopeSlotIndices[idx]}"
"$slotName@${fn.scopeSlotIndices[idx]}"
}
println("[DEBUG_LOG] [BENCH] nested-happy slots depth=$depth: ${slots.joinToString(", ")}")
val disasm = CmdDisassembler.disassemble(fn)
+1 -1
View File
@@ -27,7 +27,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
@Ignore("TODO(bytecode-only): uses fallback")
@Ignore
class OOTest {
@Test
fun testClassProps() = runTest {
@@ -6,6 +6,7 @@ import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertFailsWith
@Ignore
class ObjectExpressionTest {
@Test
@@ -7,6 +7,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@Ignore
class ReturnStatementTest {
@Test
@@ -1107,7 +1107,6 @@ class ScriptTest {
assertEquals(11, cxt.eval("x").toInt())
}
@Ignore("incremental enable")
@Test
fun testValVarConverting() = runTest {
eval(
@@ -1455,7 +1454,6 @@ class ScriptTest {
println(a)
}
@Ignore("incremental enable")
@Test
fun testLambdaWithIt1() = runTest {
eval(
@@ -1472,7 +1470,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testLambdaWithIt2() = runTest {
eval(
@@ -1485,7 +1482,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testLambdaWithIt3() = runTest {
eval(
@@ -1499,7 +1495,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testLambdaWithArgs() = runTest {
eval(
@@ -1517,7 +1512,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testCaptureLocals() = runTest {
eval(
@@ -1544,7 +1538,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testInstanceCallScopeIsCorrect() = runTest {
eval(
@@ -1575,7 +1568,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testAppliedScopes() = runTest {
eval(
@@ -1621,7 +1613,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testLambdaWithArgsEllipsis() = runTest {
eval(
@@ -1637,7 +1628,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testLambdaWithBadArgs() = runTest {
assertFails {
@@ -1653,7 +1643,6 @@ class ScriptTest {
}
}
@Ignore("incremental enable")
@Test
fun testWhileExecuteElseIfNotExecuted() = runTest {
assertEquals(
@@ -1668,7 +1657,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testIsPrimeSampleBug() = runTest {
eval(
@@ -1689,7 +1677,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testLambdaAsFnCallArg() = runTest {
eval(
@@ -1704,7 +1691,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testNewFnParser() = runTest {
eval(
@@ -1716,7 +1702,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testSpoilArgsBug() = runTest {
eval(
@@ -1736,7 +1721,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun testSpoilLamdaArgsBug() = runTest {
eval(
@@ -1756,7 +1740,6 @@ class ScriptTest {
)
}
@Ignore("incremental enable")
@Test
fun commentBlocksShouldNotAlterBehavior() = runTest {
eval(
@@ -20,6 +20,7 @@ import net.sergeych.lyng.eval
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore
class StdlibTest {
@Test
fun testIterableFilter() = runTest {
@@ -37,7 +37,7 @@ import kotlin.test.Test
*
*/
@Ignore("TODO(bytecode-only): uses fallback (MI tests)")
@Ignore
class TestInheritance {
@Test
@@ -20,6 +20,7 @@ import net.sergeych.lyng.eval
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore
class TypesTest {
@Test
@@ -20,6 +20,7 @@ import net.sergeych.lyng.eval
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore
class ValReassignRegressionTest {
@Test
@@ -21,7 +21,7 @@ import kotlinx.coroutines.test.runTest
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore("TODO(bytecode-only): uses fallback")
@Ignore
class DelegationTest {
@Test
@@ -4,6 +4,7 @@ import kotlinx.coroutines.test.runTest
import kotlin.test.Ignore
import kotlin.test.Test
@Ignore
class OperatorOverloadingTest {
@Test
fun testBinaryOverloading() = runTest {
@@ -30,6 +30,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
@Ignore
class TransientTest {
@Test
@@ -24,7 +24,9 @@ import kotlin.math.min
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.Ignore
@Ignore
class BlockReindentTest {
@Test
fun findMatchingOpen_basic() {
@@ -18,7 +18,9 @@ package net.sergeych.lyng.format
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.Ignore
@Ignore
class LyngFormatterTest {
@Test
@@ -20,7 +20,9 @@ package net.sergeych.lyng.highlight
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.Ignore
@Ignore
class CommentEolTest {
@Test
@@ -19,7 +19,9 @@ package net.sergeych.lyng.highlight
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlin.test.Ignore
@Ignore
class HighlightMappingTest {
private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> =
@@ -25,7 +25,9 @@ import net.sergeych.lyng.Pos
import net.sergeych.lyng.Source
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.Ignore
@Ignore
class SourceOffsetTest {
@Test