Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment expr returns unit type #282

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -898,21 +898,22 @@ class TypeInferenceWalker(
private fun inferBinaryExprTy(binaryExpr: MvBinaryExpr): Ty {
return when (binaryExpr.binaryOp.op) {
"<", ">", "<=", ">=" -> inferOrderingBinaryExprTy(binaryExpr)
"+", "-", "*", "/", "%" -> inferArithmeticBinaryExprTy(binaryExpr)
"==", "!=" -> inferEqualityBinaryExprTy(binaryExpr)
"||", "&&", "==>", "<==>" -> inferLogicBinaryExprTy(binaryExpr)
"^", "|", "&" -> inferBitOpsExprTy(binaryExpr)
"<<", ">>" -> inferBitShiftsExprTy(binaryExpr)

"+=", "-=", "*=", "/=", "%=" -> inferArithmeticBinaryExprTy(binaryExpr)
"|=", "^=", "&=" -> inferBitOpsExprTy(binaryExpr)
">>=", "<<=" -> inferBitShiftsExprTy(binaryExpr)
"+", "-", "*", "/", "%" -> inferArithmeticBinaryExprTy(binaryExpr, false)
"^", "|", "&" -> inferBitOpsExprTy(binaryExpr, false)
"<<", ">>" -> inferBitShiftsExprTy(binaryExpr, false)

"+=", "-=", "*=", "/=", "%=" -> inferArithmeticBinaryExprTy(binaryExpr, true)
"|=", "^=", "&=" -> inferBitOpsExprTy(binaryExpr, true)
">>=", "<<=" -> inferBitShiftsExprTy(binaryExpr, true)

else -> TyUnknown
}
}

private fun inferArithmeticBinaryExprTy(binaryExpr: MvBinaryExpr): Ty {
private fun inferArithmeticBinaryExprTy(binaryExpr: MvBinaryExpr, compoundAssigment: Boolean): Ty {
val leftExpr = binaryExpr.left
val rightExpr = binaryExpr.right
val op = binaryExpr.binaryOp.op
Expand Down Expand Up @@ -943,7 +944,7 @@ class TypeInferenceWalker(
typeErrorEncountered = true
}
}
return if (typeErrorEncountered) TyUnknown else leftTy
return if (typeErrorEncountered) TyUnknown else (if (compoundAssigment) TyUnit else leftTy)
}

private fun inferEqualityBinaryExprTy(binaryExpr: MvBinaryExpr): Ty {
Expand Down Expand Up @@ -1009,26 +1010,26 @@ class TypeInferenceWalker(
return TyBool
}

private fun inferBitOpsExprTy(binaryExpr: MvBinaryExpr): Ty {
private fun inferBitOpsExprTy(binaryExpr: MvBinaryExpr, compoundAssigment: Boolean): Ty {
val leftExpr = binaryExpr.left
val rightExpr = binaryExpr.right

val leftTy = leftExpr.inferTypeCoercableTo(TyInteger.DEFAULT)
if (rightExpr != null) {
rightExpr.inferTypeCoercableTo(leftTy)
}
return leftTy
return if (compoundAssigment) TyUnit else leftTy
}

private fun inferBitShiftsExprTy(binaryExpr: MvBinaryExpr): Ty {
private fun inferBitShiftsExprTy(binaryExpr: MvBinaryExpr, compoundAssigment: Boolean): Ty {
val leftExpr = binaryExpr.left
val rightExpr = binaryExpr.right

val leftTy = leftExpr.inferTypeCoercableTo(TyInteger.DEFAULT)
if (rightExpr != null) {
rightExpr.inferTypeCoercableTo(TyInteger.U8)
}
return leftTy
return if (compoundAssigment) TyUnit else leftTy
}

private fun Ty.supportsArithmeticOp(): Boolean {
Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/org/move/lang/types/ExpressionTypesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2430,4 +2430,14 @@ module 0x1::main {
}
}
""")

fun `test compound assignment returns unit type`() = testExpr("""
module 0x1::m {
fun main() {
let a = 1;
(a += 1)
//^ ()
}
}
""")
}