improved type inference in plugin

This commit is contained in:
sergeych committed 2026-01-12 08:19:52 +01:00
1 parent f6deabaa38
commit 1efa96a990
12 files changed
+276 -125

No files matched your search

@@ -458,4 +458,19 @@ class MiniAstTest {
val className = DocLookupUtils.simpleClassNameOf(type)
assertEquals("List", className)
}
@Test
fun miniAst_captures_fun_with_type_and_default() = runTest {
val code = """
fun foo(a: Int, b: String = "ok"): Bool { true }
""".trimIndent()
val (_, sink) = compileWithMini(code)
val mini = sink.build()
assertNotNull(mini)
val fn = mini.declarations.filterIsInstance<MiniFunDecl>().firstOrNull { it.name == "foo" }
assertNotNull(fn)
assertEquals(2, fn.params.size)
assertEquals("a", fn.params[0].name)
assertEquals("b", fn.params[1].name)
}
}
@@ -0,0 +1,55 @@
/*
* 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.
*
*/
package net.sergeych.lyng.miniast
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.binding.Binder
import kotlin.test.Test
import kotlin.test.assertEquals
class ParamTypeInferenceTest {
@Test
fun testParameterTypeInference() = runTest {
val code = """
class A {
fun foo(p: String) {
p.
}
}
fun bar(q: Int) {
q.
}
""".trimIndent()
val sink = MiniAstBuilder()
Compiler.compileWithMini(code.trimIndent(), sink)
val mini = sink.build()!!
val binding = Binder.bind(code, mini)
val dotPosQ = code.indexOf("q.") + 1
val receiverClassQ = DocLookupUtils.guessReceiverClassViaMini(mini, code, dotPosQ, listOf("lyng.stdlib"), binding)
assertEquals("Int", receiverClassQ, "Should infer type of parameter q in top-level function")
val dotPosP = code.indexOf("p.") + 1
val receiverClassP = DocLookupUtils.guessReceiverClassViaMini(mini, code, dotPosP, listOf("lyng.stdlib"), binding)
assertEquals("String", receiverClassP, "Should infer type of parameter p in member function")
}
}