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

Disallow non-variant spreads in variants #6980

Merged
merged 7 commits into from
Aug 27, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953
- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963
- Fix incorrect printing of external with `@as` attribute and `_` placholder (fixed argument). https://github.com/rescript-lang/rescript-compiler/pull/6970
- Disallow spreading anything but regular variants inside of other variants. https://github.com/rescript-lang/rescript-compiler/pull/6980

#### :house: Internal

Expand Down Expand Up @@ -2607,4 +2608,4 @@ Features:

# 1.0.0

Initial release
Initial release
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/variant_spread_abstract_type.res:2:15

1 │ type a
2 │ type b = | ...a | Other
3 │

This type is not a valid type to spread. It's only possible to spread other variants.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/variant_spread_extensible_variant.res:2:15

1 │ type a = ..
2 │ type b = | ...a | Other
3 │

This type is not a valid type to spread. It's only possible to spread other variants.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type a
type b = | ...a | Other
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type a = ..
type b = | ...a | Other
2 changes: 2 additions & 0 deletions jscomp/ml/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,8 @@ let report_error ppf = function
^ other_variant_text
^ ". Both variants must have the same @tag attribute configuration, or no \
@tag attribute at all")
| Variant_spread_fail Variant_type_spread.InvalidType ->
fprintf ppf "@[This type is not a valid type to spread. It's only possible to spread other variants.@]"
| Variant_spread_fail Variant_type_spread.CouldNotFindType ->
fprintf ppf "@[This type could not be found. It's only possible to spread variants that are known as the spread happens. This means for example that you can't spread variants in recursive definitions.@]"
| Variant_spread_fail Variant_type_spread.HasTypeParams ->
Expand Down
4 changes: 3 additions & 1 deletion jscomp/ml/variant_type_spread.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let mk_constructor_comes_from_spread_attr () : Parsetree.attribute =
type variant_type_spread_error =
| CouldNotFindType
| HasTypeParams
| InvalidType
| DuplicateConstructor of {
variant_with_overlapping_constructor: string;
overlapping_constructor_name: string;
Expand Down Expand Up @@ -31,6 +32,7 @@ let map_constructors ~(sdecl : Parsetree.type_declaration) ~all_constructors env
in

match type_decl with
| {type_kind = Type_variant [] } -> raise (VariantTypeSpreadError (loc.loc, InvalidType))
| {type_kind = Type_variant cstrs; type_attributes; type_params} ->
if List.length type_params > 0 then
raise (VariantTypeSpreadError (loc.loc, HasTypeParams));
Expand Down Expand Up @@ -83,7 +85,7 @@ let map_constructors ~(sdecl : Parsetree.type_declaration) ~all_constructors env
pcd_args = Pcstr_tuple [];
pcd_name = Location.mkloc cstr.cd_id.name cstr.cd_loc;
}))
| _ -> [c])
| _ -> raise (VariantTypeSpreadError (loc.loc, InvalidType)))
| _ ->
Hashtbl.add all_constructors c.pcd_name.txt ();
[c]
Expand Down