Skip to content

Commit ae131a2

Browse files
zthcknitt
authored andcommitted
Disallow non-variant spreads in variants (#6980)
* disallow non-variant spreads in variants * changelog * undo formatting in changelog * run make lib * changelog * fix * changelog # Conflicts: # CHANGELOG.md
1 parent 3ed3dec commit ae131a2

7 files changed

+29
-2
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
1616
- Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953
1717
- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963
18+
- Disallow spreading anything but regular variants inside of other variants. https://github.com/rescript-lang/rescript-compiler/pull/6980
1819

1920
# 11.1.3
2021

@@ -2507,4 +2508,4 @@ Features:
25072508
25082509
# 1.0.0
25092510
2510-
Initial release
2511+
Initial release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_spread_abstract_type.res:2:15
4+
5+
1 │ type a
6+
2 │ type b = | ...a | Other
7+
3 │
8+
9+
This type is not a valid type to spread. It's only possible to spread other variants.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_spread_extensible_variant.res:2:15
4+
5+
1 │ type a = ..
6+
2 │ type b = | ...a | Other
7+
3 │
8+
9+
This type is not a valid type to spread. It's only possible to spread other variants.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type a
2+
type b = | ...a | Other
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type a = ..
2+
type b = | ...a | Other

jscomp/ml/typedecl.ml

+2
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,8 @@ let report_error ppf = function
21842184
^ other_variant_text
21852185
^ ". Both variants must have the same @tag attribute configuration, or no \
21862186
@tag attribute at all")
2187+
| Variant_spread_fail Variant_type_spread.InvalidType ->
2188+
fprintf ppf "@[This type is not a valid type to spread. It's only possible to spread other variants.@]"
21872189
| Variant_spread_fail Variant_type_spread.CouldNotFindType ->
21882190
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.@]"
21892191
| Variant_spread_fail Variant_type_spread.HasTypeParams ->

jscomp/ml/variant_type_spread.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let mk_constructor_comes_from_spread_attr () : Parsetree.attribute =
44
type variant_type_spread_error =
55
| CouldNotFindType
66
| HasTypeParams
7+
| InvalidType
78
| DuplicateConstructor of {
89
variant_with_overlapping_constructor: string;
910
overlapping_constructor_name: string;
@@ -31,6 +32,7 @@ let map_constructors ~(sdecl : Parsetree.type_declaration) ~all_constructors env
3132
in
3233

3334
match type_decl with
35+
| {type_kind = Type_variant [] } -> raise (VariantTypeSpreadError (loc.loc, InvalidType))
3436
| {type_kind = Type_variant cstrs; type_attributes; type_params} ->
3537
if List.length type_params > 0 then
3638
raise (VariantTypeSpreadError (loc.loc, HasTypeParams));
@@ -83,7 +85,7 @@ let map_constructors ~(sdecl : Parsetree.type_declaration) ~all_constructors env
8385
pcd_args = Pcstr_tuple [];
8486
pcd_name = Location.mkloc cstr.cd_id.name cstr.cd_loc;
8587
}))
86-
| _ -> [c])
88+
| _ -> raise (VariantTypeSpreadError (loc.loc, InvalidType)))
8789
| _ ->
8890
Hashtbl.add all_constructors c.pcd_name.txt ();
8991
[c]

0 commit comments

Comments
 (0)