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

Improve error message on destructuring of non-tuple types #3753

Merged
merged 1 commit into from
May 5, 2021
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
41 changes: 41 additions & 0 deletions .release-notes/3753.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Improve error message when attempting to destructure non-tuple types

Sometimes, the compiler will infer that the return type of an expression is an union or an intersection of multiple types. If the user tries to destructure such result into a tuple, the compiler will emit an error, but it won't show the user what the inferred type is. This could be confusing to users, as they wouldn't know what went wrong with the code, unless they added explicit type annotations to the assigned variables.

Starting with this release, the compiler will now show what the inferred type is, so that the user can spot the problem without needing to explicitly annotate their code.

As an example, the following piece of Pony code:

```pony
actor Main
new create(env: Env) =>
(let str, let size) =
if true then
let str' = String(5) .> append("hello")
(str', USize(5))
else
("world", USize(5))
end
```

would fail to compile on previous releases with the following error message:

```
Error:
main.pony:3:25: can't destructure a union using assignment, use pattern matching instead
(let str, let size) =
^
```

Starting with this release, the error message will show the inferred type of the expression:

```
Error:
main.pony:3:25: can't destructure a union using assignment, use pattern matching instead
(let str, let size) =
^
Info:
main.pony:4:7: inferred type of expression: ((String ref, USize val^) | (String val, USize val^))
if true then
^
```
4 changes: 4 additions & 0 deletions src/libponyc/expr/operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,16 @@ bool expr_assign(pass_opt_t* opt, ast_t* ast)
ast_error(opt->check.errors, ast,
"can't destructure a union using assignment, use pattern matching "
"instead");
ast_error_continue(opt->check.errors, right,
"inferred type of expression: %s", ast_print_type(r_type));
break;

case TK_ISECTTYPE:
ast_error(opt->check.errors, ast,
"can't destructure an intersection using assignment, use pattern "
"matching instead");
ast_error_continue(opt->check.errors, right,
"inferred type of expression: %s", ast_print_type(r_type));
break;

default:
Expand Down