Skip to content

Commit 094fb15

Browse files
authoredAug 27, 2024··
Disallow non-variant spreads in variants (#6980)
* disallow non-variant spreads in variants * changelog * undo formatting in changelog * run make lib * changelog * fix * changelog
1 parent cfd0a34 commit 094fb15

7 files changed

+29
-2
lines changed
 

‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953
3131
- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963
3232
- Fix incorrect printing of external with `@as` attribute and `_` placholder (fixed argument). https://github.com/rescript-lang/rescript-compiler/pull/6970
33+
- Disallow spreading anything but regular variants inside of other variants. https://github.com/rescript-lang/rescript-compiler/pull/6980
3334

3435
#### :house: Internal
3536

@@ -2607,4 +2608,4 @@ Features:
26072608
26082609
# 1.0.0
26092610
2610-
Initial release
2611+
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
@@ -2142,6 +2142,8 @@ let report_error ppf = function
21422142
^ other_variant_text
21432143
^ ". Both variants must have the same @tag attribute configuration, or no \
21442144
@tag attribute at all")
2145+
| Variant_spread_fail Variant_type_spread.InvalidType ->
2146+
fprintf ppf "@[This type is not a valid type to spread. It's only possible to spread other variants.@]"
21452147
| Variant_spread_fail Variant_type_spread.CouldNotFindType ->
21462148
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.@]"
21472149
| 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)
Please sign in to comment.