Skip to content

Commit 95deed0

Browse files
committed
Fix type checking for expression lists
1 parent 68fceb3 commit 95deed0

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### Fixed
1616
- Fix a bug with script block indentation.
1717
- Fix missing indentation for file block.
18+
- Fix type checking for expression lists.
1819

1920
## [1.2.0] - 2023-08-18
2021

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ fun Iterable<RsType>.flatten(): Array<RsType> {
4444
return flattened.toTypedArray()
4545
}
4646

47-
fun List<RsType>.joined(): RsType {
47+
fun Collection<RsType>.joined(): RsType {
4848
if (size == 1) {
49-
return this[0]
49+
return first()
5050
}
51-
return RsTupleType(toTypedArray())
51+
return RsTupleType(flatten())
5252
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,29 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV
224224
}
225225
}
226226

227-
private fun checkExpressionList(expressionList: List<RsExpression>, parameterTypes: Array<RsType>) {
227+
private fun checkExpressionList(context: PsiElement, expressionList: List<RsExpression>, parameterTypes: Array<RsType>) {
228228
var index = 0
229-
expressionList.forEach {
229+
val actualTypes = expressionList.map{
230230
if (index < parameterTypes.size) {
231231
it.typeHint = parameterTypes[index]
232232
}
233233
if (it.type == null) {
234234
it.accept(this)
235235
}
236-
// TODO(Walied): Better error reporting here
237236
val actualType = it.type
238237
if (actualType != null) {
239-
val expectedTuple = (0 until actualType.size).map { tupleIndex ->
240-
val expectedIndex = index + tupleIndex
241-
if (expectedIndex < parameterTypes.size) parameterTypes[expectedIndex] else RsErrorType
242-
}.joined()
243-
checkTypeMismatch(it, actualType, expectedTuple)
244-
index += expectedTuple.size
238+
index += actualType.size
239+
actualType
245240
} else {
246241
index++
242+
RsErrorType
247243
}
248-
}
244+
}.joined()
245+
checkTypeMismatch(context, actualTypes, parameterTypes.toList().joined())
249246
}
250247

251248
fun checkArgumentList(argumentList: RsArgumentList, parameterTypes: Array<RsType>) {
252-
checkExpressionList(argumentList.expressionList, parameterTypes)
249+
checkExpressionList(argumentList, argumentList.expressionList, parameterTypes)
253250
}
254251

255252
override fun visitConditionExpression(o: RsConditionExpression) {
@@ -594,7 +591,7 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV
594591
?.typeNameList
595592
?.map { RsPrimitiveType.lookupReferencable(it.text) }
596593
?.toTypedArray<RsType>()
597-
checkExpressionList(o.expressionList, expectedReturnList ?: emptyArray<RsType>())
594+
checkExpressionList(o, o.expressionList, expectedReturnList ?: emptyArray<RsType>())
598595
}
599596
}
600597

0 commit comments

Comments
 (0)