Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent handling of function calls in consume expressions #3647

Merged
merged 5 commits into from
Sep 10, 2020
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
20 changes: 20 additions & 0 deletions .release-notes/3467.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Fix function calls in consume expressions

Function calls are not allowed in `consume` expressions. However, if the expression being `consume`d is a field access expression, whose LHS happens to be a function call,
the compiler fails after hitting an assertion.

For instance the following results in a compilation failure

```pony
let a = "I am a string"
let x:C iso! = consume a.chop(1)
```

but the following results in a compiler crash with an assertion failure

```pony
let a = "I am a string"
let x:C iso! = consume a.chop(1)._1
```

The fix ensures that both the scenarios are handled gracefully.
4 changes: 4 additions & 0 deletions src/libponyc/pass/refer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,10 @@ static bool refer_consume(pass_opt_t* opt, ast_t* ast)
"can't consume a let or embed field");
return false;
}
} else if (ast_id(left) == TK_CALL) {
ast_error(opt->check.errors, ast,
"consume expressions must specify a single identifier");
return false;
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions test/libponyc/refer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,15 @@ TEST_F(ReferTest, ConsumeParamVarSubfieldReassignSameExpressionReuse)
TEST_ERRORS_1(src,
"can't use a consumed local or field in an expression");
}

TEST_F(ReferTest, ConsumeTupleAccessorOfFunctionValResult)
{
const char* src =
"actor Main\n"
"new create(env: Env) =>\n"
"let a = \"I am a string\"\n"
"consume a.chop(1)._1";

TEST_ERRORS_1(src,
"consume expressions must specify a single identifier");
}