Skip to content

Commit

Permalink
lambda type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Oct 24, 2024
1 parent e1db952 commit e68459d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.move.ide.presentation.hintText
import org.move.lang.core.psi.MvFunctionParameter
import org.move.lang.core.psi.MvLetStmt
import org.move.lang.core.psi.MvPatBinding
import org.move.lang.core.psi.MvSchemaFieldStmt
import org.move.lang.core.psi.ext.bindingOwner
import org.move.lang.core.psi.ext.endOffset
import org.move.lang.core.psi.ext.hasAncestor
Expand All @@ -31,8 +33,9 @@ class MvTypeInlayHintsProvider2: InlayHintsProvider {
// skip private variables
if (patBinding.name.startsWith("_")) return

// only show bindings for let statements
if (patBinding.bindingOwner !is MvLetStmt) return
// does not show hints for bindings with explicit type annotations
val owner = patBinding.bindingOwner
if (owner is MvFunctionParameter || owner is MvSchemaFieldStmt) return

val contextInferenceOwner = patBinding.inferenceOwner() ?: return

Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/org/move/lang/core/psi/ext/MvBindingPat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ val MvPatBinding.bindingOwner: PsiElement?
it is MvLetStmt
|| it is MvFunctionParameter
|| it is MvSchemaFieldStmt
|| it is MvLambdaParameter
}

sealed class RsBindingModeKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class TypeInferenceWalker(
is MvDerefExpr -> inferDerefExprTy(expr)
is MvLitExpr -> inferLitExprTy(expr, expected)
is MvTupleLitExpr -> inferTupleLitExprTy(expr, expected)
is MvLambdaExpr -> inferLambdaExpr(expr, expected)
is MvLambdaExpr -> inferLambdaExprTy(expr, expected)

is MvMoveExpr -> expr.expr?.inferType() ?: TyUnknown
is MvCopyExpr -> expr.expr?.inferType() ?: TyUnknown
Expand Down Expand Up @@ -379,7 +379,7 @@ class TypeInferenceWalker(
return TyReference(innerRefTy, mutability, ctx.msl)
}

private fun inferLambdaExpr(lambdaExpr: MvLambdaExpr, expected: Expectation): Ty {
private fun inferLambdaExprTy(lambdaExpr: MvLambdaExpr, expected: Expectation): Ty {
val bindings = lambdaExpr.parametersAsBindings
val lambdaTy =
(expected.onlyHasTy(this.ctx) as? TyLambda) ?: TyLambda.unknown(bindings.size)
Expand All @@ -398,10 +398,7 @@ class TypeInferenceWalker(
val baseFuncTy =
when (namedItem) {
is MvFunctionLike -> {
val itemTy = instantiatePath<TyFunction>(path, namedItem) ?: return TyUnknown
// val (itemTy, _) = instantiateMethodOrPath<TyFunction>(path, namedItem)
// ?: return TyUnknown
itemTy
instantiatePath<TyFunction>(path, namedItem) ?: return TyUnknown
}
is MvFieldsOwner -> {
val tupleFields = namedItem.tupleFields
Expand Down Expand Up @@ -658,9 +655,9 @@ class TypeInferenceWalker(
formalArgs: List<Ty>,
): List<Ty> {
val resolvedFormalRet = resolveTypeVarsIfPossible(formalRet)
val retTy = expectedRet.onlyHasTy(ctx) ?: return emptyList()
val expectedRetTy = expectedRet.onlyHasTy(ctx) ?: return emptyList()
return ctx.freezeUnification {
if (ctx.combineTypes(retTy, resolvedFormalRet).isOk) {
if (ctx.combineTypes(expectedRetTy, resolvedFormalRet).isOk) {
formalArgs.map { ctx.resolveTypeVarsIfPossible(it) }
} else {
emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ class InlayTypeHintsProvider2Test: DeclarativeInlayHintsProviderTestCase() {
}
}
""")


fun `test lambda expr`() = checkByText("""
module 0x1::m {
fun callback(elem: u8, ident: |u8|u8): u8 { ident(elem) }
fun main() {
callback(10, |elem/*<# : |u8 #>*/| elem + 1);
}
}
""")

private fun checkByText(@Language("Move") code: String) {
doTestProvider("main.move", code, MvTypeInlayHintsProvider2())
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/kotlin/org/move/lang/types/ExpressionTypesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2165,4 +2165,24 @@ module 0x1::main {
}
}
""")

fun `test lambda expr binding`() = testBinding("""
module 0x1::m {
fun callback(elem: u8, ident: |u8|u8): u8 { ident(elem) }
fun main() {
callback(10, |elem| elem + 1);
//^ u8
}
}
""")

fun `test lambda expr binding with inference`() = testBinding("""
module 0x1::m {
fun callback<E>(elem: E, ident: |E|E): E { ident(elem) }
fun main() {
callback(10u8, |elem| elem + 1);
//^ u8
}
}
""")
}

0 comments on commit e68459d

Please sign in to comment.