Skip to content

Commit

Permalink
Improve error message for match on structs
Browse files Browse the repository at this point in the history
Since a struct doesn't have a type descriptor, it's impossible to match
against it, unless it's exactly the same struct type. Before, the
return type of matchtype.c:is_matchtype didn't convey enough information
to the caller about the reason why two types couldn't match. In most
cases, this is due to reference capabilities, so that's what the error
message shows.

By distinguishing the specific case of a match failure due to lack of
type descriptor, the compiler can give a friendlier error message to the
user.
  • Loading branch information
ergl authored and SeanTAllen committed May 3, 2021
1 parent 151a2f5 commit 7369194
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 82 deletions.
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

0 comments on commit 7369194

Please # to comment.