Skip to content

Commit b7c67d2

Browse files
cknittclaude
andauthored
Remove OCaml attribute aliases and the features only OCaml used (#8928)
* Remove OCaml attribute aliases and the features only OCaml used Why: The compiler still accepted OCaml spellings of builtin attributes that nothing in ReScript produces, and some of them only half worked: `@ocaml.unboxed` changed the type's representation but was ignored by untagged variants. `@boxed`, `@deprecated_mutable` and `%extension_constructor` are OCaml features with no use in ReScript. The `ocaml.*` spellings are now rejected with the bare name to use instead. `@boxed` and `@deprecated_mutable` are reported as unused attributes, and `%extension_constructor` as an uninterpreted extension. With it go the typed tree node `Texp_extension_constructor`, the predefined type `extension_constructor`, the error "A type cannot be boxed and unboxed at the same time." and the deprecated-mutation checks. The CMT magic number is bumped. Diagnostic added (removed OCaml spelling): Example: @ocaml.unboxed type t = Name(string) Before (diagnostic): (none, the type is unboxed) After (diagnostic): `@ocaml.unboxed` is no longer supported, use `@unboxed` instead. Diagnostic modified (boxed and unboxed): Example: @unboxed @boxed type t = One(int) Before (diagnostic): A type cannot be boxed and unboxed at the same time. After (diagnostic): Warning number 101 Unused attribute: @boxed This attribute has no effect here. Diagnostic modified (extension constructor): Example: exception A let _ = %extension_constructor(A) Before (diagnostic): (none, the constructor's slot) After (diagnostic): Uninterpreted extension 'extension_constructor'. Typedtree removed (Texp_extension_constructor): Before (typed): expression Texp_extension_constructor "A" After (typed): (removed) Breaking: `@ocaml.deprecated`, `@ocaml.unboxed`, `@ocaml.immediate` and `@ocaml.warnerror` are errors; use `@deprecated`, `@unboxed`, `@immediate` and `@warnerror`. `@ocaml.boxed`, `@ocaml.deprecated_mutable` and `@ocaml.text` are errors; remove them (use a doc comment for `@ocaml.text`). `@boxed` and `@deprecated_mutable` are unused attributes (warning 101, an error by default); remove them. `%extension_constructor` and the type `extension_constructor` are gone. Review: Why keep `@ocaml.ppwarning`? ppxlib's driver emits `[@@@ocaml.ppwarning]` for lint warnings, so PPXs built with ppxlib still produce it. Review: Why keep `@immediate`, `@warnerror` and `@ppwarning`? `@immediate` on an abstract type makes comparisons on it compile to `===`/`<`; `@warnerror` turns warnings into errors in a scope; `@ppwarning` is how PPXs report warnings. Review: Does removing a typed tree constructor break reading older CMTs? The CMT magic is bumped; older CMTs were already unreadable since `Texp_dict` was inserted mid-variant. Area: compiler/ml, compiler/frontend, analysis Output: unchanged for programs that use none of the removed spellings Tests: super_errors fixtures for the removed spellings, unused `@boxed`/`@deprecated_mutable` and `%extension_constructor`; three obsolete fixtures removed; `@ocaml.unboxed` in the reanalyze and gentype tests switched to `@unboxed` PPX: v0 wire unchanged; `ocaml.error`, `ocaml.warning`, `ocaml.ppwarning` and `ocaml.explicit_arity` still accepted Editor: hover no longer reads `@ocaml.text` as documentation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> * Read warning and error attributes only by their bare names Why: `ocaml.warning`, `ocaml.ppwarning` and `ocaml.error` were still read everywhere because PPXs built on OCaml tooling emit them (ppxlib reports errors as `[%ocaml.error]` and lint warnings as `[@@@ocaml.ppwarning]`). Renaming them once, where PPX output enters the compiler, lets every other reader use the bare name. The PPX bridge (`Ast_mapper_from0`) renames the three names in its default attribute and extension mappers, so they are renamed at any depth, payloads included. `Ast_mapper.extension_of_error` emits `error`, and the internal `ocaml.warn_on_literal_pattern` that `Predef` puts on `Failure` and `Invalid_argument` is now `warn_on_literal_pattern`. Written by hand in source, the prefixed spellings are rejected like the other removed aliases; the check now also sees attributes on constants. AST modified (PPX output): Example: %%ocaml.error("ppx error") let x = @ocaml.warning("-3") 1 Before (parse): structure_item Pstr_extension "ocaml.error" attribute "ocaml.warning" After (parse): structure_item Pstr_extension "error" attribute "warning" Diagnostic added (written by hand): Example: @@ocaml.warning("-32") Before (diagnostic): (none, warning 32 disabled) After (diagnostic): `@ocaml.warning` is no longer supported, use `@warning` instead. Breaking: `@ocaml.warning`, `@ocaml.ppwarning` and `%ocaml.error` in source are errors; use `@warning`, `@ppwarning` and `%error`. A hand-written `@ocaml.warn_on_literal_pattern` no longer has an effect. PPX output that uses the prefixed names keeps working. Review: Can a PPX's `ocaml.warning` still reach the error check? Only if the bridge skipped an attribute position; the ounit test covers structure and signature items, type declarations, expressions and attribute payloads, and the ast-mapping fixture covers the parser round trip. Review: Does warning 52 still fire on `Failure("boom")`? Yes, a new super_errors fixture covers it. Area: compiler/ml, compiler/frontend, analysis Output: unchanged Tests: ounit tests for the bridge renaming and `extension_of_error`; ast-mapping fixture OcamlPrefixedBuiltins; super_errors fixtures for hand-written `@ocaml.warning`, `@ocaml.ppwarning`, `%ocaml.error` and warning 52 on `Failure`; the reanalyze test's `@@ocaml.warning` switched to `@@warning` PPX: v0 wire unchanged; `ocaml.warning`, `ocaml.ppwarning` and `ocaml.error` from a PPX are renamed to the bare names; `ocaml.explicit_arity` still read by the bridge Editor: reanalyze reads only `@warning` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> * Ignore `@boxed` and `@deprecated_mutable` like any unknown attribute Why: Reporting them as unused attributes (warning 101, an error by default) broke builds over attributes that never changed anything in ReScript. Diagnostic removed (unused boxed): Example: @boxed type t = One(int) Before (diagnostic): Warning number 101 Unused attribute: @boxed This attribute has no effect here. After (diagnostic): (none, the attribute is ignored) Breaking: `@boxed` and `@deprecated_mutable` are no longer interpreted and are silently ignored; remove them. Area: compiler/frontend Output: unchanged Tests: the unused_boxed_deprecated_mutable super_errors fixture is removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> * Reject removed `ocaml.*` spellings before external PPXs and bump the CMI magic Why: With a `-ppx`, the source tree went through the PPX bridge, which renames `ocaml.warning`, `ocaml.ppwarning` and `ocaml.error` to the bare names, before the removed-spelling check ran, so hand-written `@@ocaml.warning("-32")` was silently accepted. Removing the predefined type `extension_constructor` shifted the stamps of later predefined types, but `.cmi` files from before the change were still accepted. `Cmd_ppx_apply` now runs the removed-spelling check from `Bs_ast_invariant` on the source before handing it to external PPXs; the check after the PPXs is unchanged. The CMI magic number is bumped to `Caml1999I039`. Diagnostic added (with an external PPX): Example: @@ocaml.warning("-32") let value = 1 Before (diagnostic): (none, accepted as `@@warning("-32")`) After (diagnostic): `@ocaml.warning` is no longer supported, use `@warning` instead. Breaking: `.cmi` files built by an earlier compiler are rejected; rebuild them. Area: compiler/core, compiler/frontend, compiler/ext Output: unchanged Tests: ounit test ppx_removed_ocaml_spelling compiles `@@ocaml.warning`, `@ocaml.ppwarning` in an interface and `%ocaml.error` through an identity PPX and expects the errors, and `@@warning` to compile PPX: v0 wire unchanged; source is checked before it is sent to a PPX, and PPX output is still renamed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> * Remove redundant tests for the removed OCaml spellings The removed `ocaml.*` attributes share one table lookup, which the `@ocaml.unboxed` fixture already covers. Warning 52 on predefined exceptions is covered by `warning_52_fragile_literal_pattern`, and the bridge renaming by the ounit test. Area: tests Output: unchanged Tests: removed super_errors fixtures removed_ocaml_deprecated, removed_ocaml_boxed, removed_ocaml_warning, removed_ocaml_ppwarning and warning_52_failure_literal_pattern, and the ast-mapping fixture OcamlPrefixedBuiltins, each with its expected file Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f79ec1f commit b7c67d2

47 files changed

Lines changed: 298 additions & 237 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎analysis/reanalyze/src/annotation.ml‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ let has_attribute check_text (attributes : Typedtree.attributes) =
8383
get_attribute_payload check_text attributes <> None
8484

8585
let is_ocaml_suppress_dead_warning attributes =
86-
match
87-
attributes
88-
|> get_attribute_payload (fun x -> x = "ocaml.warning" || x = "warning")
89-
with
86+
match attributes |> get_attribute_payload (fun x -> x = "warning") with
9087
| Some (StringPayload s) ->
9188
let numeric =
9289
match Str.search_forward (Str.regexp (Str.quote "-32")) s 0 with

‎analysis/reanalyze/src/arnold.ml‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,12 +1033,8 @@ module Compile = struct
10331033
| Texp_pack _ ->
10341034
not_implemented "Texp_pack";
10351035
assert false
1036-
| Texp_extension_constructor _ when true ->
1037-
not_implemented "Texp_extension_constructor";
1038-
assert false
1039-
| _ ->
1040-
(* ocaml 4.08: Texp_letop(_) | Texp_open(_) *)
1041-
not_implemented "Texp_letop(_) | Texp_open(_)";
1036+
| Texp_break | Texp_continue ->
1037+
not_implemented "Texp_break | Texp_continue";
10421038
assert false
10431039

10441040
and expression_opt ~ctx e_opt =

‎analysis/reanalyze/src/side_effects.ml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ let rec expr_no_side_effects (expr : Typedtree.expression) =
7878
| Texp_object_set _ -> false
7979
| Texp_letexception (_ec, e) -> e |> expr_no_side_effects
8080
| Texp_pack _ -> false
81-
| Texp_extension_constructor _ when true -> true
82-
| _ -> (* on ocaml 4.08: Texp_letop | Texp_open *) true
8381

8482
and expr_opt_no_side_effects eo =
8583
match eo with

‎analysis/src/process_attributes.ml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ open Shared_types
44
let rec find_doc_attribute attributes =
55
match attributes with
66
| [] -> None
7-
| ({Asttypes.txt = "ocaml.doc" | "ocaml.text" | "ns.doc" | "res.doc"}, payload)
8-
:: rest -> (
7+
| ({Asttypes.txt = "ocaml.doc" | "ns.doc" | "res.doc"}, payload) :: rest -> (
98
match Ast_payload.semantic_string_of_payload payload with
109
| Some doc -> Some doc
1110
| None -> find_doc_attribute rest)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Remove the OCaml attribute spellings `@ocaml.deprecated`, `@ocaml.unboxed`, `@ocaml.immediate`, `@ocaml.warnerror`, `@ocaml.warning`, `@ocaml.ppwarning`, `@ocaml.boxed`, `@ocaml.deprecated_mutable`, `@ocaml.text` and `%ocaml.error`, and the `%extension_constructor` extension. `@boxed` and `@deprecated_mutable` are no longer interpreted and are ignored. PPX output keeps working: the PPX bridge renames `ocaml.warning`, `ocaml.ppwarning` and `ocaml.error` to the bare names.

‎compiler/core/cmd_ppx_apply.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ let apply_rewriters_str ~tool_name ast =
108108
Ml_binary.ast0_roundtrip Ml_binary.Ml ast
109109
else ast
110110
| ppxs ->
111+
Bs_ast_invariant.check_removed_ocaml_spellings_on_structure ast;
111112
ast
112113
|> Ast_mapper.add_ppx_context_str ~tool_name
113114
|> rewrite Ml ppxs |> Ml_binary.ast0_to_structure
@@ -120,6 +121,7 @@ let apply_rewriters_sig ~tool_name ast =
120121
Ml_binary.ast0_roundtrip Ml_binary.Mli ast
121122
else ast
122123
| ppxs ->
124+
Bs_ast_invariant.check_removed_ocaml_spellings_on_signature ast;
123125
ast
124126
|> Ast_mapper.add_ppx_context_sig ~tool_name
125127
|> rewrite Mli ppxs |> Ml_binary.ast0_to_signature

‎compiler/ext/config.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let cmi_magic_number = "Caml1999I038"
1+
let cmi_magic_number = "Caml1999I039"
22

33
(* Magic numbers for marshaled values of the *current* parsetree, whose layout
44
changes across compiler versions. *)
@@ -13,7 +13,7 @@ and ast0_impl_magic_number = "Caml1999M022"
1313

1414
and ast0_intf_magic_number = "Caml1999N022"
1515

16-
and cmt_magic_number = "Caml1999T046"
16+
and cmt_magic_number = "Caml1999T047"
1717

1818
let get_load_path () = (Compiler_request_state.current ()).load_path
1919
let set_load_path path = (Compiler_request_state.current ()).load_path <- path

‎compiler/frontend/bs_ast_invariant.ml‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,34 @@ let is_checked_attribute txt =
3333
true
3434
| _ -> false
3535

36-
let warn_unused_attribute ((({txt; loc} as sloc), _) : Parsetree.attribute) =
36+
(* OCaml spellings of builtin attributes the compiler used to accept. *)
37+
let removed_ocaml_attribute txt =
38+
match txt with
39+
| "ocaml.deprecated" -> Some "use `@deprecated` instead."
40+
| "ocaml.unboxed" -> Some "use `@unboxed` instead."
41+
| "ocaml.immediate" -> Some "use `@immediate` instead."
42+
| "ocaml.warnerror" -> Some "use `@warnerror` instead."
43+
| "ocaml.boxed" -> Some "remove it: types are boxed unless marked `@unboxed`."
44+
| "ocaml.deprecated_mutable" -> Some "remove it."
45+
| "ocaml.text" -> Some "use a doc comment instead."
46+
| "ocaml.warning" -> Some "use `@warning` instead."
47+
| "ocaml.ppwarning" -> Some "use `@ppwarning` instead."
48+
| _ -> None
49+
50+
let check_removed_ocaml_attribute (({txt; loc}, _) : Parsetree.attribute) =
51+
match removed_ocaml_attribute txt with
52+
| Some hint ->
53+
Location.raise_errorf ~loc "`@%s` is no longer supported, %s" txt hint
54+
| None -> ()
55+
56+
let check_removed_ocaml_extension (({txt; loc}, _) : Parsetree.extension) =
57+
if txt = "ocaml.error" then
58+
Location.raise_errorf ~loc
59+
"`%%ocaml.error` is no longer supported, use `%%error` instead."
60+
61+
let warn_unused_attribute
62+
((({txt; loc} as sloc), _) as attr : Parsetree.attribute) =
63+
check_removed_ocaml_attribute attr;
3764
if
3865
is_checked_attribute txt && (not loc.loc_ghost)
3966
&& not (Used_attributes.is_used_attribute sloc)
@@ -71,6 +98,10 @@ let emit_external_warnings : iterator =
7198
"built-in type `%s` can not be redefined " txt;
7299
super.type_declaration self ptyp);
73100
attribute = (fun _ attr -> warn_unused_attribute attr);
101+
extension =
102+
(fun self ext ->
103+
check_removed_ocaml_extension ext;
104+
super.extension self ext);
74105
structure_item =
75106
(fun self str_item ->
76107
match str_item.pstr_desc with
@@ -85,7 +116,9 @@ let emit_external_warnings : iterator =
85116
expr =
86117
(fun self ({pexp_loc = loc} as a) ->
87118
match a.pexp_desc with
88-
| Pexp_constant const -> check_constant loc const
119+
| Pexp_constant const ->
120+
check_constant loc const;
121+
self.attributes self a.pexp_attributes
89122
| _ -> super.expr self a);
90123
label_declaration =
91124
(fun self lbl ->
@@ -136,7 +169,9 @@ let emit_external_warnings : iterator =
136169
pat =
137170
(fun self (pat : Parsetree.pattern) ->
138171
match pat.ppat_desc with
139-
| Ppat_constant constant -> check_constant pat.ppat_loc constant
172+
| Ppat_constant constant ->
173+
check_constant pat.ppat_loc constant;
174+
self.attributes self pat.ppat_attributes
140175
| _ -> super.pat self pat);
141176
}
142177

@@ -165,3 +200,25 @@ let emit_external_warnings_on_structure (stru : Parsetree.structure) =
165200

166201
let emit_external_warnings_on_signature (sigi : Parsetree.signature) =
167202
emit_external_warnings.signature emit_external_warnings sigi
203+
204+
(* The PPX bridge renames the [ocaml.]-prefixed builtins a PPX emits to their
205+
bare names, so source written with them is checked before it reaches the
206+
bridge. *)
207+
let removed_ocaml_spellings : iterator =
208+
{
209+
super with
210+
attribute =
211+
(fun self attr ->
212+
check_removed_ocaml_attribute attr;
213+
super.attribute self attr);
214+
extension =
215+
(fun self ext ->
216+
check_removed_ocaml_extension ext;
217+
super.extension self ext);
218+
}
219+
220+
let check_removed_ocaml_spellings_on_structure (stru : Parsetree.structure) =
221+
removed_ocaml_spellings.structure removed_ocaml_spellings stru
222+
223+
let check_removed_ocaml_spellings_on_signature (sigi : Parsetree.signature) =
224+
removed_ocaml_spellings.signature removed_ocaml_spellings sigi

‎compiler/frontend/bs_ast_invariant.mli‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ val iter_warnings_on_sigi : Parsetree.signature -> unit
3737
val emit_external_warnings_on_structure : Parsetree.structure -> unit
3838

3939
val emit_external_warnings_on_signature : Parsetree.signature -> unit
40+
41+
val check_removed_ocaml_spellings_on_structure : Parsetree.structure -> unit
42+
(** Reject the [ocaml.]-prefixed builtin attributes and extensions that are no
43+
longer supported. Run on source before external PPXs see it. *)
44+
45+
val check_removed_ocaml_spellings_on_signature : Parsetree.signature -> unit

‎compiler/ml/ast_mapper.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ let default_mapper =
640640
}
641641

642642
let rec extension_of_error {loc; msg; if_highlight; sub} =
643-
( {loc; txt = "ocaml.error"},
643+
( {loc; txt = "error"},
644644
PStr
645645
([
646646
Str.eval (Exp.constant (Const.string msg));

0 commit comments

Comments
 (0)