Skip to content
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
3 changes: 2 additions & 1 deletion src/main/grammars/MoveParser.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ private LogicalEqExpr_items ::=
| LessEqualsBinExpr | LessBinExpr | GreaterEqualsBinExpr | GreaterBinExpr

private ControlFlowExpr_items ::= IfExpr | LoopExpr | MatchExpr | WhileExpr | ForExpr
private UnaryExpr_items ::= CopyExpr | MoveExpr | DerefExpr | BangExpr
private UnaryExpr_items ::= CopyExpr | MoveExpr | DerefExpr | BangExpr | MinusExpr
| ReturnExpr | ContinueExpr | BreakExpr | AbortExpr
private AtomExpr ::= AnnotatedExpr
| TupleLitOrParenExpr
Expand Down Expand Up @@ -999,6 +999,7 @@ PartialImplyBinOp ::= <<mslOnly lt_eqeq_gt>>

BangExpr ::= '!' Expr
DerefExpr ::= '*' Expr
MinusExpr ::= '-' Expr

CopyExpr ::= copy Expr
MoveExpr ::= move Expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import org.move.lang.core.types.ty.Ty
import org.move.lang.core.types.ty.TyAdt
import org.move.lang.core.types.ty.TyReference

val INTEGER_TYPE_IDENTIFIERS = setOf("u8", "u16", "u32", "u64", "u128", "u256")
val INTEGER_TYPE_IDENTIFIERS = setOf(
"u8", "u16", "u32", "u64", "u128", "u256",
"i8", "i16", "i32", "i64", "i128", "i256"
)
val SPEC_INTEGER_TYPE_IDENTIFIERS = INTEGER_TYPE_IDENTIFIERS + setOf("num")
val SPEC_ONLY_PRIMITIVE_TYPES = setOf("num")
val PRIMITIVE_TYPE_IDENTIFIERS = INTEGER_TYPE_IDENTIFIERS + setOf("bool")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,16 @@ class MvSyntaxErrorAnnotator: MvAnnotatorBase() {
@Suppress("CompanionObjectInExtension")
companion object {
private val INTEGER_WITH_SUFFIX_REGEX =
Regex("([0-9a-zA-Z_]+)(u[0-9]{1,4})")
Regex("([0-9a-zA-Z_]+)([ui][0-9]{1,4})")
private val ACCEPTABLE_INTEGER_SYMBOLS =
setOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_')
private val ACCEPTABLE_INTEGER_SUFFIXES =
setOf("u8", "u16", "u32", "u64", "u128", "u256")
setOf(
"u8", "u16", "u32", "u64", "u128", "u256",
"i8", "i16", "i32", "i64", "i128", "i256",
)
private val HEX_INTEGER_WITH_SUFFIX_REGEX =
Regex("([0-9a-zA-Z_]+)*(u[0-9]{1,4})")
Regex("([0-9a-zA-Z_]+)*([ui][0-9]{1,4})")
private val ACCEPTABLE_HEX_SYMBOLS =
setOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
private val ACCEPTABLE_HEX_INTEGER_SYMBOLS = ACCEPTABLE_HEX_SYMBOLS + setOf('_')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ class TypePsiWalker(
expr.expr?.inferType(TyBool)
TyBool
}
is MvMinusExpr -> {
expr.expr?.inferTypeCoercableTo(TyInteger(TyInteger.Kind.NoPrecision))
?: TyInteger(TyInteger.Kind.NoPrecision)
}

is MvIfExpr -> inferIfExprTy(expr, expected)
is MvWhileExpr -> inferWhileExpr(expr)
Expand Down
24 changes: 13 additions & 11 deletions src/main/kotlin/org/move/lang/core/types/ty/TyPrimitive.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@ import com.intellij.psi.PsiElement
import org.move.ide.presentation.tyToString


abstract class TyPrimitive(val name: String) : Ty() {
abstract class TyPrimitive(val name: String): Ty() {
override fun abilities() = setOf(Ability.DROP, Ability.COPY, Ability.STORE)
}

object TyBool : TyPrimitive("bool") {
object TyBool: TyPrimitive("bool") {
override fun toString(): String = tyToString(this)
}

object TyAddress : TyPrimitive("address") {
object TyAddress: TyPrimitive("address") {
override fun toString(): String = tyToString(this)
}

object TySigner : TyPrimitive("signer") {
object TySigner: TyPrimitive("signer") {
override fun abilities() = setOf(Ability.DROP)
override fun toString(): String = tyToString(this)
}

object TyUnit : TyPrimitive("()") {
object TyUnit: TyPrimitive("()") {
override fun abilities() = Ability.none()
override fun toString(): String = tyToString(this)
}

object TyNever : TyPrimitive("()") {
object TyNever: TyPrimitive("()") {
override fun abilities() = Ability.none()
override fun toString(): String = "<never>"
}

object TyNum : TyPrimitive("num") {
object TyNum: TyPrimitive("num") {
override fun abilities() = Ability.all()
override fun toString(): String = tyToString(this)
}
Expand All @@ -42,7 +42,7 @@ object TySpecBv: TyPrimitive("bv") {

}

data class TyInteger(val kind: Kind) : TyPrimitive(kind.name.lowercase()) {
data class TyInteger(val kind: Kind): TyPrimitive(kind.name.lowercase()) {
override fun abilities() = Ability.all()

// fun ulongRange(): ULongRange? {
Expand All @@ -59,10 +59,10 @@ data class TyInteger(val kind: Kind) : TyPrimitive(kind.name.lowercase()) {

companion object {
fun fromName(name: String): TyInteger =
Kind.values().find { it.name == name }?.let(::TyInteger)!!
Kind.entries.find { it.name == name }?.let(::TyInteger)!!

fun fromSuffixedLiteral(literal: PsiElement): TyInteger? =
Kind.values().find { literal.text.endsWith(it.name) }?.let(::TyInteger)
Kind.entries.find { literal.text.endsWith(it.name) }?.let(::TyInteger)

val DEFAULT_KIND = Kind.NoPrecision
val DEFAULT = default()
Expand All @@ -73,7 +73,9 @@ data class TyInteger(val kind: Kind) : TyPrimitive(kind.name.lowercase()) {

@Suppress("EnumEntryName")
enum class Kind {
NoPrecision, u8, u16, u32, u64, u128, u256, num
NoPrecision, num,
u8, u16, u32, u64, u128, u256,
i8, i16, i32, i64, i128, i256
}

override fun toString(): String = tyToString(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class HighlightingAnnotatorTest: AnnotatorTestCase(HighlightingAnnotator::class)
script {
fun main(s: &<BUILTIN_TYPE>signer</BUILTIN_TYPE>,
val: <PRIMITIVE_TYPE>u8</PRIMITIVE_TYPE>,
val_i8: <PRIMITIVE_TYPE>i8</PRIMITIVE_TYPE>,
val2: <PRIMITIVE_TYPE>u64</PRIMITIVE_TYPE>,
val3: <PRIMITIVE_TYPE>u128</PRIMITIVE_TYPE>,
val4: <PRIMITIVE_TYPE>u16</PRIMITIVE_TYPE>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class InvalidIntegerTest : AnnotatorTestCase(MvSyntaxErrorAnnotator::class) {
fun main() {
1;
1u8; 1u16; 1u32; 1u64; 1u128; 1u256;
1i8; 1i16; 1i32; 1i64; 1i128; 1i256;
0x123456789abcdef;
0x1; 0xff; 0xFFF; 0xACACAFFF;
0x1f1fu128;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1717,4 +1717,31 @@ module 0x1::pool {
}
}
} """)

fun `test signed integers`() = checkByText("""
module 0x1::mod {
fun call(_a: i8, _b: i8, _c: i8) {}
fun main() {
let _a: i8 = 1;
let _a: i8 = 1i8;
let _a: i8 = -1i8;
let _a: i8 = <error descr="Incompatible type 'i64', expected 'i8'">1i64</error>;
let _a: i8 = <error descr="Incompatible type 'i64', expected 'i8'">-1i64</error>;
1i8 + 1i8;
<error descr="Incompatible arguments to '+': 'i8' and 'i64'">1i8 + 1i64</error>;
call(-1, 1i8, <error descr="Incompatible type 'i64', expected 'i8'">1i64</error>);
}
}
""")

fun `test minus expr`() = checkByText("""
module 0x1::mod {
fun main() {
let _a = -1;
let _a: i8 = -1;
-<error descr="Incompatible type 'bool', expected 'integer'">true</error>;
--1;
}
}
""")
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CompleteParsingTest: MvParsingTestCase("complete") {
fun `test assign bin expr`() = doTest()
fun `test lambdas`() = doTest()
// fun `test new lambdas`() = doTest()
fun `test signed integers`() = doTest()

fun doTest() {
super.doTest(true, true)
Expand Down
39 changes: 39 additions & 0 deletions src/test/kotlin/org/move/lang/types/ExpressionTypesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2507,4 +2507,43 @@ module 0x1::main {
}
}
""")

fun `test signed integer i8`() = testExpr("""
module 0x1::main {
fun main(a: i8) {
a;
//^ i8
}
}
""")

fun `test signed integer i8 from expr`() = testExpr("""
module 0x1::main {
fun main() {
let a = 1i8;
a;
//^ i8
}
}
""")

fun `test signed integer i8 from plus expr`() = testExpr("""
module 0x1::main {
fun main() {
let a = 1i8 + 1i8;
a;
//^ i8
}
}
""")

fun `test signed integer i8 from unary minus expr`() = testExpr("""
module 0x1::main {
fun main() {
let a = -1i8;
a;
//^ i8
}
}
""")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module 0x1::signed_integers {
fun main() {
1i8;
-1i8;
-1;
-1i128;
-0xff;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
FILE
MvModuleImpl(MODULE)
PsiElement(module)('module')
PsiWhiteSpace(' ')
MvAddressRefImpl(ADDRESS_REF)
PsiElement(DIEM_ADDRESS)('0x1')
PsiElement(::)('::')
PsiElement(IDENTIFIER)('signed_integers')
PsiWhiteSpace(' ')
PsiElement({)('{')
PsiWhiteSpace('\n ')
MvFunctionImpl(FUNCTION)
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('main')
MvFunctionParameterListImpl(FUNCTION_PARAMETER_LIST)
PsiElement(()('(')
PsiElement())(')')
PsiWhiteSpace(' ')
MvCodeBlockImpl(CODE_BLOCK)
PsiElement({)('{')
PsiWhiteSpace('\n ')
MvExprStmtImpl(EXPR_STMT)
MvLitExprImpl(LIT_EXPR)
PsiElement(INTEGER_LITERAL)('1i8')
PsiElement(;)(';')
PsiWhiteSpace('\n ')
MvExprStmtImpl(EXPR_STMT)
MvMinusExprImpl(MINUS_EXPR)
PsiElement(-)('-')
MvLitExprImpl(LIT_EXPR)
PsiElement(INTEGER_LITERAL)('1i8')
PsiElement(;)(';')
PsiWhiteSpace('\n ')
MvExprStmtImpl(EXPR_STMT)
MvMinusExprImpl(MINUS_EXPR)
PsiElement(-)('-')
MvLitExprImpl(LIT_EXPR)
PsiElement(INTEGER_LITERAL)('1')
PsiElement(;)(';')
PsiWhiteSpace('\n ')
MvExprStmtImpl(EXPR_STMT)
MvMinusExprImpl(MINUS_EXPR)
PsiElement(-)('-')
MvLitExprImpl(LIT_EXPR)
PsiElement(INTEGER_LITERAL)('1i128')
PsiElement(;)(';')
PsiWhiteSpace('\n ')
MvExprStmtImpl(EXPR_STMT)
MvMinusExprImpl(MINUS_EXPR)
PsiElement(-)('-')
MvLitExprImpl(LIT_EXPR)
PsiElement(HEX_INTEGER_LITERAL)('0xff')
PsiElement(;)(';')
PsiWhiteSpace('\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')