Skip to content

Commit

Permalink
Fixed check on complex assignments; added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlint committed Oct 19, 2024
1 parent a16c88d commit 37c29b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,15 @@ private void checkAssignment(Statement current, (Assignable) `( <Assignable arg>
//collect(arg, c);
}

private bool assignCompatible(AType lhs, AType rhs)
= comparable(lhs, rhs)
|| (isSetAType(lhs) && comparable(getSetElementType(lhs), rhs))
|| (isListAType(lhs) && comparable(getListElementType(lhs), rhs))
;

private AType computeAssignmentRhsType(Statement current, AType lhsType, str operator, AType rhsType, Solver s){
checkNonVoid(current, rhsType, s, "Righthand side of assignment");
s.requireComparable(lhsType, rhsType, error(current, "Cannot assign righthand side of type %t to lefthand side of type %t", rhsType, lhsType));
s.requireTrue(assignCompatible(lhsType, rhsType), error(current, "Cannot assign righthand side of type %t to lefthand side of type %t", rhsType, lhsType));

resultType = avalue();
switch(operator){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ test bool IntAddAssignError() = unexpectedType("{int n = 3; n += true;}");
test bool StrAddAssign() = checkOK("{str s = \"a\"; s += \"b\";}");
test bool StrAddAssignError1() = unexpectedType("{str s = \"a\"; s += 3;}");

test bool SetStrAddAssign() = checkOK("{set[str] s = {\"a\"}; s += {\"b\"};}");
test bool SetStrAddAssign1() = checkOK("{set[str] s = {\"a\"}; s += {\"b\"};}");
test bool SetStrAddAssign2() = checkOK("{set[str] s = {\"a\"}; s += \"b\";}");
test bool SetStrAddAssignError1() = unexpectedType("{set[str] s = {\"a\"}; s += 3;}");
test bool SetStrAddAssignError2() = unexpectedType("{set[str] s = {\"a\"}; s += [\"b\"];}");

test bool ListStrAddAssign1() = checkOK("{list[str] s = [\"a\"]; s += [\"b\"];}");
test bool ListStrAddAssign2() = checkOK("{list[str] s = [\"a\"]; s += \"b\";}");
test bool ListStrAddAssignError1() = unexpectedType("{list[str] s = [\"a\"]; s += 3;}");
test bool ListStrAddAssignError2() = unexpectedType("{list[str] s = [\"a\"]; s += {\"b\"};}");

test bool IntMullAssign() = checkOK("{int n = 3; n *= 3;}");
test bool IntMullAssignError() = unexpectedType("{int n = 3; n *= true;}");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module lang::rascalcore::compile::Examples::Tst4

void main(){
set[str] s = {"a"};
s += 3;
}

// test bool Stat3() = checkOK("value zz = { n = 1; n = true; }; ");

// value main() { n = 1; n = true; return n; }

void main()
{ n = 1; n = 1.5; n + 2;}
// void main()
// { n = 1; n = 1.5; n + 2;}

// void main(){
// n = 1;
Expand Down

0 comments on commit 37c29b2

Please sign in to comment.