anotther compiler inference bug andlyng.io.console improvements

This commit is contained in:
sergeych committed 2026-04-03 11:55:25 +03:00
1 parent caad7d8ab9
commit fd6d05d568
7 files changed
+389 -130

No files matched your search

@@ -4674,6 +4674,7 @@ class Compiler(
?: nameObjClass[ref.name]
?: resolveClassByName(ref.name)
}
is ImplicitThisMemberRef -> resolveReceiverClassForMember(ref)
is ClassScopeMemberRef -> {
val targetClass = resolveClassByName(ref.ownerClassName()) ?: return null
inferFieldReturnClass(targetClass, ref.name)
@@ -4728,6 +4729,15 @@ class Compiler(
is FastLocalVarRef -> nameTypeDecl[ref.name]
?: lookupLocalTypeDeclByName(ref.name)
?: seedTypeDeclByName(ref.name)
is ImplicitThisMemberRef -> {
val typeName = ref.preferredThisTypeName() ?: currentImplicitThisTypeName()
val targetClass = typeName?.let { resolveClassByName(it) } ?: return null
targetClass.getInstanceMemberOrNull(ref.name, includeAbstract = true)?.typeDecl?.let { return it }
classFieldTypesByName[targetClass.className]?.get(ref.name)
?.let { return TypeDecl.Simple(it.className, false) }
classMethodReturnTypeDeclByName[targetClass.className]?.get(ref.name)?.let { return it }
null
}
is FieldRef -> {
val targetDecl = resolveReceiverTypeDecl(ref.target) ?: return null
val targetClass = resolveTypeDeclObjClass(targetDecl) ?: resolveReceiverClassForMember(ref.target)
@@ -7388,6 +7398,15 @@ class Compiler(
val mutable = param.accessType?.isMutable ?: false
declareSlotNameIn(classSlotPlan, param.name, mutable, isDelegated = false)
}
constructorArgsDeclaration?.let { ctorDecl ->
val classParamTypeMap = slotTypeByScopeId.getOrPut(classSlotPlan.id) { mutableMapOf() }
val classParamTypeDeclMap = slotTypeDeclByScopeId.getOrPut(classSlotPlan.id) { mutableMapOf() }
for (param in ctorDecl.params) {
val slot = classSlotPlan.slots[param.name]?.index ?: continue
classParamTypeDeclMap[slot] = param.type
resolveTypeDeclObjClass(param.type)?.let { classParamTypeMap[slot] = it }
}
}
val ctorForcedLocalSlots = LinkedHashMap<String, Int>()
if (constructorArgsDeclaration != null) {
val ctorDecl = constructorArgsDeclaration
@@ -8873,6 +8892,7 @@ class Compiler(
val targetClass = resolveReceiverClassForMember(directRef.target)
inferFieldReturnClass(targetClass, directRef.name)
}
is ImplicitThisMemberRef -> resolveReceiverClassForMember(directRef)
is CallRef -> {
val target = directRef.target
when {
@@ -0,0 +1,69 @@
/*
* 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.eval
import kotlin.test.Test
class InferredCtorFieldIntRegressionTest {
@Test
fun inferredIntFieldsFromCtorParamsStayIntInsideTypedCalls() = runTest {
eval(
"""
class GameState(
pieceId0: Int,
nextId0: Int,
next2Id0: Int,
px0: Int,
py0: Int,
) {
var pieceId = pieceId0
var nextId = nextId0
var next2Id = next2Id0
var rot = 0
var px = px0
var py = py0
var score = 0
var totalLines = 0
var level = 1
var running = true
var gameOver = false
var paused = false
}
fun canPlace(board: List<List<Int>>, boardW: Int, boardH: Int, pieceId: Int, rot: Int, px: Int, py: Int): Bool {
true
}
val board: List<List<Int>> = []
val boardW = 10
val boardH = 20
fun applyKeyInput(s: GameState, key: String) {
if (key == "ArrowLeft") {
if (canPlace(board, boardW, boardH, s.pieceId, s.rot, s.px - 1, s.py) == true) s.px--
}
}
val s = GameState(1, 2, 3, 4, 5)
applyKeyInput(s, "ArrowLeft")
assertEquals(3, s.px)
""".trimIndent()
)
}
}