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

Untagged error messages #6290

Merged
merged 8 commits into from
Jun 7, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
#### :bug: Bug Fix

- Fix issue where uncurried type internals leak in type error. https://github.com/rescript-lang/rescript-compiler/pull/6264
- Improve error messages for untagged variant definitions https://github.com/rescript-lang/rescript-compiler/pull/6290


# 11.0.0-beta.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/UntaggedNonUnary1.res:2:1-27

1 │ @unboxed
2 │ type t = Tuple(int, string)
3 │

This untagged variant definition is invalid: Constructor Tuple has more than one argument.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/UntaggedNonUnary2.res:2:1-42

1 │ @unboxed
2 │ type t = Tuple(int, string) | Float(float)
3 │

This untagged variant definition is invalid: Constructor Tuple has more than one argument.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/UntaggedUnknown.res:2:10-31

1 │ @unboxed
2 │ type t = Tuple((float, string)) | Float(float)
3 │

This untagged variant definition is invalid: Case Tuple has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@unboxed
type t = Tuple(int, string)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@unboxed
type t = Tuple(int, string) | Float(float)
2 changes: 2 additions & 0 deletions jscomp/build_tests/super_errors/fixtures/UntaggedUnknown.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@unboxed
type t = Tuple((float, string)) | Float(float)
449 changes: 241 additions & 208 deletions jscomp/ml/ast_untagged_variants.ml

Large diffs are not rendered by default.

26 changes: 6 additions & 20 deletions jscomp/ml/typedecl.ml
Original file line number Diff line number Diff line change
@@ -304,38 +304,24 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
in
let raw_status = get_unboxed_from_attributes sdecl in

let checkUntaggedVariant = match sdecl.ptype_kind with
let checkUntaggedVariant() = match sdecl.ptype_kind with
| Ptype_variant cds -> Ext_list.for_all cds (function
| {pcd_args = Pcstr_tuple ([] | [_])} ->
(* at most one payload allowed for untagged variants *)
true
| {pcd_args = Pcstr_tuple (_::_::_); pcd_name={txt=name}} ->
Ast_untagged_variants.reportConstructorMoreThanOneArg ~loc:sdecl.ptype_loc ~name
| {pcd_args = Pcstr_record _} -> true
| _ -> false )
)
| _ -> false
in

if raw_status.unboxed && not raw_status.default && not checkUntaggedVariant then begin
if raw_status.unboxed && not raw_status.default && not (checkUntaggedVariant()) then begin
match sdecl.ptype_kind with
| Ptype_abstract ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute
"it is abstract"))
| Ptype_variant [{pcd_args = Pcstr_tuple []; _}] ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute
"its constructor has no argument"))
| Ptype_variant [{pcd_args = Pcstr_tuple [_]; _}] -> ()
| Ptype_variant [{pcd_args = Pcstr_tuple _; _}] ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute
"its constructor has more than one argument"))
| Ptype_variant [{pcd_args = Pcstr_record
[{pld_mutable=Immutable; _}]; _}] -> ()
| Ptype_variant [{pcd_args = Pcstr_record [{pld_mutable=Mutable; _}]; _}] ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute "it is mutable"))
| Ptype_variant [{pcd_args = Pcstr_record _; _}] ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute
"its constructor has more than one argument"))
| Ptype_variant _ ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute
"it has more than one constructor"))
| Ptype_variant _ -> ()
| Ptype_record [{pld_mutable=Immutable; _}] -> ()
| Ptype_record [{pld_mutable=Mutable; _}] ->
raise(Error(sdecl.ptype_loc, Bad_unboxed_attribute