Skip to content

Commit f953a89

Browse files
committed
Add type checking for arithmetic operators
1 parent ef0d431 commit f953a89

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
- Add support for `long` numeric type.
55
- Add `stylesheet` to the valid types list.
6+
- Add type checking for arithmetic operators.
67

78
## [1.4.1] - 2023-10-16
89

src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/RsTypeInferenceVisitor.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV
153153
if (unfoldedActualType != unfoldedExpectedType) {
154154
if (reportError) {
155155
context.error(
156-
"Type mismatch: '%s' was given but '%s' was expected".format(
156+
TYPE_MISMATCH_ERROR.format(
157157
unfoldedActualType.representation,
158158
unfoldedExpectedType.representation
159159
)
@@ -511,6 +511,17 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV
511511

512512
o.right.typeHint = o.typeHint ?: RsPrimitiveType.INT
513513
o.right.accept(this)
514+
515+
val leftType = o.left.type.unfold()
516+
val rightType = o.right.type.unfold()
517+
518+
if (leftType == null || rightType == null || leftType is RsErrorType || rightType == RsErrorType) return
519+
520+
val operator = o.arithmeticOp
521+
522+
if (leftType != rightType) {
523+
o.error(INVALID_OPERATOR_ERROR.format(operator.text, leftType.representation, rightType.representation))
524+
}
514525
}
515526

516527
override fun visitArithmeticValueExpression(o: RsArithmeticValueExpression) {
@@ -665,10 +676,8 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV
665676
checkExpressionList(o, o.expressionList, expectedReturnList ?: emptyArray<RsType>())
666677
}
667678
companion object {
668-
private val ALLOWED_RELATIONAL_TYPES = arrayOf(
669-
RsPrimitiveType.INT,
670-
RsPrimitiveType.LONG
671-
)
679+
private const val TYPE_MISMATCH_ERROR = "Type mismatch: '%s' was given but '%s' was expected"
680+
private const val INVALID_OPERATOR_ERROR = "Operator '%s' cannot be applied to '%s', '%s'"
672681
}
673682
}
674683

0 commit comments

Comments
 (0)