Skip to content

Commit 25e0fc3

Browse files
committed
remove annotation and make it a warning instead
1 parent c3ab4fd commit 25e0fc3

36 files changed

Lines changed: 106 additions & 152 deletions

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#### :rocket: New Feature
2424

2525
- Add a first-class `taggedTemplate<'param, 'output>` builtin type and the `TaggedTemplate` stdlib module (`TaggedTemplate.make`). Tagged-template tags are now tracked through the type system, so they emit real JS tagged-template syntax across module boundaries, when passed as first-class values, and when constructed at runtime by a factory (e.g. `postgres`). https://github.com/rescript-lang/rescript/pull/8461
26+
- Make mutation of private record mutable fields a configurable warning instead of a hard error. https://github.com/rescript-lang/rescript/pull/8366
2627

2728
#### :bug: Bug fix
2829

@@ -83,7 +84,6 @@
8384
- Rewatch: feature-gated source directories. Tag a source entry with `"feature": "<name>"` and select with `--features a,b` (or per-dep in `dependencies` / `dev-dependencies`) to include optional slices of a package's source tree at build time. Top-level `features` map supports transitive implications. https://github.com/rescript-lang/rescript/pull/8379
8485
- Rewatch: improve watch output and add `--clear-screen` option. https://github.com/rescript-lang/rescript/pull/8373
8586
- Add `Dict.assignMany`, `Dict.concat`, `Dict.concatMany`, `Dict.concatAll`, `Array.concatAll` to the stdlib. https://github.com/rescript-lang/rescript/pull/8364
86-
- Allow mutation of private record fields with @allowMutation https://github.com/rescript-lang/rescript/pull/8366
8787

8888
#### :bug: Bug fix
8989

compiler/ext/warnings.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type t =
7474
| Bs_toplevel_expression_unit of
7575
(string * top_level_unit_help) option (* 109 *)
7676
| Bs_todo of string option (* 110 *)
77+
| Bs_private_record_mutation of string (* 111 *)
7778

7879
(* If you remove a warning, leave a hole in the numbering. NEVER change
7980
the numbers of existing warnings.
@@ -126,8 +127,9 @@ let number = function
126127
| Bs_integer_literal_overflow -> 107
127128
| Bs_toplevel_expression_unit _ -> 109
128129
| Bs_todo _ -> 110
130+
| Bs_private_record_mutation _ -> 111
129131

130-
let last_warning_number = 110
132+
let last_warning_number = 111
131133

132134
let letter_all =
133135
let rec loop i = if i = 0 then [] else i :: loop (i - 1) in
@@ -453,6 +455,8 @@ let message = function
453455
^ "\n\n\
454456
\ This code is not implemented yet and will crash at runtime. Make sure \
455457
you implement this before running the code."
458+
| Bs_private_record_mutation field_name ->
459+
Printf.sprintf "Mutation of private record field %S." field_name
456460

457461
let sub_locs = function
458462
| Deprecated (_, def, use, _) ->
@@ -564,6 +568,7 @@ let descriptions =
564568
);
565569
(109, "Toplevel expression has unit type");
566570
(110, "Todo found");
571+
(111, "Mutation of private record field");
567572
]
568573

569574
let help_warnings () =

compiler/ext/warnings.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type t =
6767
| Bs_toplevel_expression_unit of
6868
(string * top_level_unit_help) option (* 109 *)
6969
| Bs_todo of string option (* 110 *)
70+
| Bs_private_record_mutation of string (* 111 *)
7071

7172
val parse_options : bool -> string -> unit
7273

compiler/ml/builtin_attributes.ml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,6 @@ let immediate =
240240
| {txt = "ocaml.immediate" | "immediate"; _}, _ -> true
241241
| _ -> false)
242242

243-
let has_allow_mutation attr =
244-
List.exists
245-
(function
246-
| {txt = "allowMutation"; _}, _ -> true
247-
| _ -> false)
248-
attr
249-
250243
(* The "ocaml.boxed (default)" and "ocaml.unboxed (default)"
251244
attributes cannot be input by the user, they are added by the
252245
compiler when applying the default setting. This is done to record

compiler/ml/builtin_attributes.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,5 @@ val explicit_arity : Parsetree.attributes -> bool
9393

9494
val immediate : Parsetree.attributes -> bool
9595

96-
val has_allow_mutation : Parsetree.attributes -> bool
97-
9896
val has_unboxed : Parsetree.attributes -> bool
9997
val has_boxed : Parsetree.attributes -> bool

compiler/ml/typecore.ml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ type error =
5757
string * Longident.t * (Path.t * Path.t) * (Path.t * Path.t) list
5858
| Undefined_method of type_expr * string * string list option
5959
| Private_type of type_expr
60-
| Private_label of Longident.t * type_expr
6160
| Not_subtype of
6261
Ctype.type_pairs * Ctype.type_pairs * Ctype.subtype_context option
6362
| Too_many_arguments of bool * type_expr
@@ -316,6 +315,13 @@ let extract_concrete_record env ty =
316315
match extract_concrete_typedecl env ty with
317316
| p0, p, {type_kind = Type_record (fields, repr)} -> (p0, p, fields, repr)
318317
| _ -> raise Not_found
318+
319+
let is_private_record_field env label =
320+
match extract_concrete_typedecl env label.lbl_res with
321+
| _, _, {type_kind = Type_record _; type_private = Private} -> true
322+
| _ -> false
323+
| exception Not_found -> false
324+
319325
let extract_concrete_variant env ty =
320326
match extract_concrete_typedecl env ty with
321327
| p0, p, {type_kind = Type_variant cstrs} -> (p0, p, cstrs)
@@ -2952,6 +2958,9 @@ and type_expect_ ?deprecated_context ~context ?in_function ?(recarg = Rejected)
29522958
unify_exp ~context:None env record ty_record;
29532959
if label.lbl_mut = Immutable then
29542960
raise (Error (loc, env, Label_not_mutable lid.txt));
2961+
if label.lbl_private = Private && is_private_record_field env label then
2962+
Location.prerr_warning lid.loc
2963+
(Warnings.Bs_private_record_mutation (Longident.last lid.txt));
29552964
Builtin_attributes.check_deprecated_mutable lid.loc label.lbl_attributes
29562965
(Longident.last lid.txt);
29572966
rue
@@ -3636,24 +3645,8 @@ and type_label_exp ~call_context create env loc ty_expected
36363645
end_def ();
36373646
(* Generalize information merged from ty_expected *)
36383647
generalize_structure ty_arg);
3639-
(if label.lbl_private = Private then
3640-
if create then raise (Error (loc, env, Private_type ty_expected))
3641-
else
3642-
let allow_private_assignment =
3643-
match extract_concrete_typedecl env label.lbl_res with
3644-
| ( _,
3645-
_,
3646-
{
3647-
type_kind = Type_record _;
3648-
type_private = Private;
3649-
type_attributes;
3650-
} ) ->
3651-
Builtin_attributes.has_allow_mutation type_attributes
3652-
| _ -> false
3653-
| exception Not_found -> false
3654-
in
3655-
if not allow_private_assignment then
3656-
raise (Error (lid.loc, env, Private_label (lid.txt, ty_expected))));
3648+
if label.lbl_private = Private && create then
3649+
raise (Error (loc, env, Private_type ty_expected));
36573650
let arg =
36583651
let snap = if vars = [] then None else Some (Btype.snapshot ()) in
36593652
let field_name = Longident.last lid.txt in
@@ -4842,9 +4835,6 @@ let report_error env loc ppf error =
48424835
"In this type, the locally bound module name %s escapes its scope" id
48434836
| Private_type ty ->
48444837
fprintf ppf "Cannot create values of the private type %a" type_expr ty
4845-
| Private_label (lid, ty) ->
4846-
fprintf ppf "Cannot assign field %a of the private type %a" longident lid
4847-
type_expr ty
48484838
| Not_a_variant_type lid ->
48494839
fprintf ppf "The type %a@ is not a variant type" longident lid
48504840
| Incoherent_label_order ->

compiler/ml/typecore.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ type error =
9090
string * Longident.t * (Path.t * Path.t) * (Path.t * Path.t) list
9191
| Undefined_method of type_expr * string * string list option
9292
| Private_type of type_expr
93-
| Private_label of Longident.t * type_expr
9493
| Not_subtype of
9594
Ctype.type_pairs * Ctype.type_pairs * Ctype.subtype_context option
9695
| Too_many_arguments of bool * type_expr

compiler/ml/typedecl.ml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,6 @@ let transl_declaration ~type_record_as_object ~untagged_wfc env sdecl id =
387387
( sdecl.ptype_loc,
388388
Invalid_attribute
389389
"@notUndefined can only be used on abstract types" )));
390-
(if Builtin_attributes.has_allow_mutation sdecl.ptype_attributes then
391-
match (sdecl.ptype_private, sdecl.ptype_kind) with
392-
| Private, Ptype_record _ -> ()
393-
| _ ->
394-
raise
395-
(Error
396-
( sdecl.ptype_loc,
397-
Invalid_attribute
398-
"@allowMutation can only be used on private record types" )));
399390

400391
(* Bind type parameters *)
401392
reset_type_variables ();

tests/build_tests/super_errors/expected/allowMutation_private_abstract_attribute.res.expected

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/build_tests/super_errors/expected/allowMutation_private_record_construction.res.expected

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)