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 for match on structs #3746

Merged
merged 1 commit into from
May 3, 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
55 changes: 55 additions & 0 deletions .release-notes/3746.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Improve error messages when matching on struct types

A struct type doesn't have a type descriptor, which means that they cannot be used in match or "as" statements. Before this change, the compiler would incorrectly warn that matching against a struct wasn't possible due to a violation of capabilities, which was confusing. With this release, the compiler will now show a more helpful error message, explicitly mentioning that struct types can't be used in union types.

As an example, the following piece of Pony code:

```pony
struct Rect

actor Main
new create(env: Env) =>
let a: (Rect | None) = None
match a
| let a': Rect => None
| None => None
end
```

would fail to compile on ponyc 0.40.0 with the following error message:

```
Error:
main.pony:7:7: this capture violates capabilities, because the match would need to differentiate by capability at runtime instead of matching on type alone
| let a': Rect => None
^
Info:
main.pony:5:18: the match type allows for more than one possibility with the same type as pattern type, but different capabilities. match type: (Rect ref | None val)
let a: (Rect | None) = None
^
main.pony:7:7: pattern type: Rect ref
| let a': Rect => None
^
main.pony:7:15: matching (Rect ref | None val) with Rect ref could violate capabilities
| let a': Rect => None
^
```

Starting with this release, the error message is:

```
Error:
main.pony:7:7: this capture cannot match, since the type Rect ref is a struct and lacks a type descriptor
| let a': Rect => None
^
Info:
main.pony:5:18: a struct cannot be part of a union type. match type: (Rect ref | None val)
let a: (Rect | None) = None
^
main.pony:7:7: pattern type: Rect ref
| let a': Rect => None
^
main.pony:7:15: matching (Rect ref | None val) with Rect ref is not possible, since a struct lacks a type descriptor
| let a': Rect => None
^
```
20 changes: 19 additions & 1 deletion src/libponyc/expr/match.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ bool expr_case(pass_opt_t* opt, ast_t* ast)
break;
}

case MATCHTYPE_DENY:
case MATCHTYPE_DENY_CAP:
{
errorframe_t frame = NULL;
ast_error_frame(&frame, pattern,
Expand All @@ -547,6 +547,24 @@ bool expr_case(pass_opt_t* opt, ast_t* ast)
ok = false;
break;
}

case MATCHTYPE_DENY_NODESC:
{
errorframe_t frame = NULL;
ast_error_frame(&frame, pattern,
"this capture cannot match, since the type %s "
"is a struct and lacks a type descriptor",
ast_print_type(pattern_type));
ast_error_frame(&frame, match_type,
"a struct cannot be part of a union type. match type: %s",
ast_print_type(match_type));
ast_error_frame(&frame, pattern, "pattern type: %s",
ast_print_type(pattern_type));
errorframe_append(&frame, &info);
errorframe_report(&frame, opt->check.errors);
ok = false;
break;
}
}

if(ast_id(guard) != TK_NONE)
Expand Down
18 changes: 17 additions & 1 deletion src/libponyc/expr/operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ static bool add_as_type(pass_opt_t* opt, ast_t* ast, ast_t* expr,

ast_t* expr_type = ast_type(expr);
errorframe_t info = NULL;
if(is_matchtype(expr_type, type, &info, opt) == MATCHTYPE_DENY)
matchtype_t is_match = is_matchtype(expr_type, type, &info, opt);
if(is_match == MATCHTYPE_DENY_CAP)
{
errorframe_t frame = NULL;
ast_error_frame(&frame, ast,
Expand All @@ -512,6 +513,21 @@ static bool add_as_type(pass_opt_t* opt, ast_t* ast, ast_t* expr,
errorframe_append(&frame, &info);
errorframe_report(&frame, opt->check.errors);

return false;
} else if(is_match == MATCHTYPE_DENY_NODESC){
errorframe_t frame = NULL;
ast_error_frame(&frame, ast,
"matching variable of type %s with %s is not possible, "
"since a struct lacks a type descriptor",
ast_print_type(expr_type), ast_print_type(type));
ast_error_frame(&frame, type,
"match type: %s", ast_print_type(type));
ast_error_frame(&frame, expr,
"a struct cannot be part of a union type. "
"pattern type: %s", ast_print_type(expr_type));
errorframe_append(&frame, &info);
errorframe_report(&frame, opt->check.errors);

return false;
}

Expand Down
Loading