Skip to content

Commit

Permalink
Use reference type for "expected an array or a tuple" error
Browse files Browse the repository at this point in the history
  • Loading branch information
lochana-chathura committed Apr 7, 2022
1 parent 2f6ee41 commit ab9f63b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1775,17 +1775,17 @@ private BType checkArrayType(BLangListConstructorExpr listConstructor, BArrayTyp

BLangExpression spreadOpExpr = ((BLangListConstructorSpreadOpExpr) expr).expr;
BType spreadOpType = checkExpr(spreadOpExpr, this.env);
spreadOpType = Types.getReferredType(spreadOpType);
BType spreadOpReferredType = Types.getReferredType(spreadOpType);

switch (spreadOpType.tag) {
switch (spreadOpReferredType.tag) {
case TypeTags.ARRAY:
BType spreadOpeType = ((BArrayType) spreadOpType).eType;
BType spreadOpeType = ((BArrayType) spreadOpReferredType).eType;
if (types.typeIncompatible(spreadOpExpr.pos, spreadOpeType, eType)) {
return symTable.semanticError;
}
break;
case TypeTags.TUPLE:
BTupleType spreadOpTuple = (BTupleType) spreadOpType;
BTupleType spreadOpTuple = (BTupleType) spreadOpReferredType;
List<BType> tupleTypes = spreadOpTuple.tupleTypes;
for (BType tupleMemberType : tupleTypes) {
if (types.typeIncompatible(spreadOpExpr.pos, tupleMemberType, eType)) {
Expand Down Expand Up @@ -1880,11 +1880,11 @@ private BType checkTupleType(BLangListConstructorExpr listConstructor, BTupleTyp

BLangExpression spreadOpExpr = ((BLangListConstructorSpreadOpExpr) expr).expr;
BType spreadOpType = checkExpr(spreadOpExpr, this.env);
spreadOpType = Types.getReferredType(spreadOpType);
BType spreadOpReferredType = Types.getReferredType(spreadOpType);

switch (spreadOpType.tag) {
switch (spreadOpReferredType.tag) {
case TypeTags.ARRAY:
BArrayType spreadOpArray = (BArrayType) spreadOpType;
BArrayType spreadOpArray = (BArrayType) spreadOpReferredType;
if (spreadOpArray.state == BArrayState.CLOSED) {
for (int i = 0; i < spreadOpArray.size && nonRestTypeIndex < memberTypeSize;
i++, nonRestTypeIndex++) {
Expand Down Expand Up @@ -1913,7 +1913,7 @@ private BType checkTupleType(BLangListConstructorExpr listConstructor, BTupleTyp
}
break;
case TypeTags.TUPLE:
BTupleType spreadOpTuple = (BTupleType) spreadOpType;
BTupleType spreadOpTuple = (BTupleType) spreadOpReferredType;
int spreadOpMemberTypeSize = spreadOpTuple.tupleTypes.size();

if (isFixedLengthTuple(spreadOpTuple)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ public void testSpreadOperatorNegative() {
"invalid usage of spread operator: fixed member expected for 'string'", 170, 41);
BAssertUtil.validateError(resultNegative, i++,
"invalid usage of spread operator: fixed member expected for 'string'", 173, 38);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'int', found 'string'", 187, 23);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'int', found 'string'", 189, 23);
BAssertUtil.validateError(resultNegative, i++,
"invalid usage of spread operator: fixed length list expected", 190, 24);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'string', found 'int'", 192, 20);
"invalid usage of spread operator: fixed length list expected", 192, 24);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'string', found 'int'", 194, 20);
BAssertUtil.validateError(resultNegative, i++,
"invalid usage of spread operator: fixed length list expected", 195, 17);
"invalid usage of spread operator: fixed length list expected", 197, 17);
BAssertUtil.validateError(resultNegative, i++,
"incompatible types: expected an array or a tuple, found 'Qux'", 200, 26);
Assert.assertEquals(resultNegative.getErrorCount(), i);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ type Bar [int, string];

type Baz int[]|string|boolean;

type Qux int;

function testSpreadOpWithListTypeBeingTypeRef() {
IntArr _ = [1, ...[2, "s"]]; // error

Expand All @@ -193,6 +195,9 @@ function testSpreadOpWithListTypeBeingTypeRef() {

Foo t = [3, ...["s"]]; // OK
Bar _ = [...t]; // error

Qux x = 3;
IntArr _ = [1, 2, ...x]; // error
}

function testTypeCheckingForArrayArrayCompatibilityPositive() {
Expand Down

0 comments on commit ab9f63b

Please sign in to comment.