-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error message for match on structs
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
1 parent
151a2f5
commit 7369194
Showing
6 changed files
with
245 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
^ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.