From 97891a036b29ff1be51dce1dd3f1de3fc8462a01 Mon Sep 17 00:00:00 2001 From: Pierrick Couderc Date: Thu, 31 Oct 2024 15:38:48 +0100 Subject: [PATCH 1/2] Proto/Compiler: fix attributes reported as unused in OCaml5 --- .../compat_files/compiler_libs_ocaml4.ml | 2 ++ .../compat_files/compiler_libs_ocaml5.ml | 6 ++++++ .../compat/compiler_libs.mli | 4 ++++ src/lib_protocol_compiler/packer.ml | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml4.ml b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml4.ml index dfc499ec2f56..c32259f3d0bb 100644 --- a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml4.ml +++ b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml4.ml @@ -18,3 +18,5 @@ let make_persistent_signature ~filename ~cmi = Persistent_env.Persistent_signature.{filename; cmi} let compunit_name compunit = compunit.Cmo_format.cu_name + +let mark_attribute_used _ = () diff --git a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml index 74cceb0f7210..8fce82d4edca 100644 --- a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml +++ b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml @@ -18,3 +18,9 @@ let make_persistent_signature ~filename ~cmi = let compunit_name compunit = let (Cmo_format.Compunit cuname) = compunit.Cmo_format.cu_name in cuname + +let mark_attribute_used attribute = + Builtin_attributes.select_attributes + [(attribute.Parsetree.attr_name.txt, Mark_used_only)] + [attribute] + |> ignore diff --git a/src/lib_protocol_compiler/compat/compiler_libs.mli b/src/lib_protocol_compiler/compat/compiler_libs.mli index 2f3cdaba8b6c..6a8d6a83af53 100644 --- a/src/lib_protocol_compiler/compat/compiler_libs.mli +++ b/src/lib_protocol_compiler/compat/compiler_libs.mli @@ -28,3 +28,7 @@ val make_persistent_signature : (** [compunit_name cu] returns the name of the compilation unit *) val compunit_name : Cmo_format.compilation_unit -> string + +(** [mark_attribute_used attr] marks the attribute used, so that it doesn't + trigger warning 53. See [Packer.check_syntax]. *) +val mark_attribute_used : Parsetree.attribute -> unit diff --git a/src/lib_protocol_compiler/packer.ml b/src/lib_protocol_compiler/packer.ml index d52976c977d4..221213ae56cc 100644 --- a/src/lib_protocol_compiler/packer.ml +++ b/src/lib_protocol_compiler/packer.ml @@ -64,6 +64,24 @@ let check_syntax kind file = struct_item.pstr_loc | _should_be_fine -> default_iterator.structure_item iterator struct_item); + attribute = + (* Starting OCaml 5.2, attributes are accumulated during parsing and + marked as used during typechecking. As parsing only is not + supported by the OCaml compiler (and especially calling the + compiler in different mode during the same execution), there is no + rule to cleanup the table of unused builtin attributes if + typechecking is never reached. This shouldn't be an issue to parse + a file twice as the attributes are registered by their location, + thus the first occurence would be replaced. However in the case of + the protocol compiler the files are repacked into a single functor, + which effectively changes their location. As a result, the previous + locations are never cleaned up and the attributes will eventually be + declared as unused. As such they need to be marked as used. *) + (fun iterator attribute -> + (* The result is ignored, as when `Mark_used_only` is set it should + return nothing. *) + Compiler_libs.mark_attribute_used attribute ; + default_iterator.attribute iterator attribute); } in (match kind with -- GitLab From 9e84d05ebfe20868f0dcce78bfe2baee97c1556a Mon Sep 17 00:00:00 2001 From: Pierrick Couderc Date: Tue, 5 Nov 2024 14:02:12 +0100 Subject: [PATCH 2/2] Proto/Compiler: add OCaml 5.2 specific file --- manifest/product_octez.ml | 16 +++++++++++- .../compat_files/compiler_libs_ocaml5.ml | 6 +---- .../compat_files/compiler_libs_ocaml5_2.ml | 26 +++++++++++++++++++ src/lib_protocol_compiler/compat/dune | 7 ++++- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5_2.ml diff --git a/manifest/product_octez.ml b/manifest/product_octez.ml index c6783b36bd50..dab6e5dec91d 100644 --- a/manifest/product_octez.ml +++ b/manifest/product_octez.ml @@ -3411,7 +3411,21 @@ let octez_protocol_compiler_compat = [ S "copy"; S "compat_files/compiler_libs_ocaml5.ml"; S "%{target}"; ] - ~enabled_if:[S ">="; S "%{ocaml_version}"; S "5"]; + ~enabled_if: + [ + S "and"; + [S ">="; S "%{ocaml_version}"; S "5"]; + [S "<"; S "%{ocaml_version}"; S "5.2"]; + ]; + target_rule + "compiler_libs.ml" + ~action: + [ + S "copy"; + S "compat_files/compiler_libs_ocaml5_2.ml"; + S "%{target}"; + ] + ~enabled_if:[S ">="; S "%{ocaml_version}"; S "5.2"]; ] let _octez_protocol_compiler_cmis_of_cma = diff --git a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml index 8fce82d4edca..f3bfefc6d712 100644 --- a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml +++ b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5.ml @@ -19,8 +19,4 @@ let compunit_name compunit = let (Cmo_format.Compunit cuname) = compunit.Cmo_format.cu_name in cuname -let mark_attribute_used attribute = - Builtin_attributes.select_attributes - [(attribute.Parsetree.attr_name.txt, Mark_used_only)] - [attribute] - |> ignore +let mark_attribute_used _ = () diff --git a/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5_2.ml b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5_2.ml new file mode 100644 index 000000000000..8fce82d4edca --- /dev/null +++ b/src/lib_protocol_compiler/compat/compat_files/compiler_libs_ocaml5_2.ml @@ -0,0 +1,26 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* SPDX-FileCopyrightText: 2024 Nomadic Labs *) +(* *) +(*****************************************************************************) + +let default_persistent_signature_loader = + !Persistent_env.Persistent_signature.load + +let override_persistent_signature_loader f = + Persistent_env.Persistent_signature.load := f + +let make_persistent_signature ~filename ~cmi = + Persistent_env.Persistent_signature. + {filename; cmi; visibility = Load_path.Visible} + +let compunit_name compunit = + let (Cmo_format.Compunit cuname) = compunit.Cmo_format.cu_name in + cuname + +let mark_attribute_used attribute = + Builtin_attributes.select_attributes + [(attribute.Parsetree.attr_name.txt, Mark_used_only)] + [attribute] + |> ignore diff --git a/src/lib_protocol_compiler/compat/dune b/src/lib_protocol_compiler/compat/dune index 8eedaa5b79a7..a71283fe5804 100644 --- a/src/lib_protocol_compiler/compat/dune +++ b/src/lib_protocol_compiler/compat/dune @@ -28,4 +28,9 @@ (rule (target compiler_libs.ml) (action (copy compat_files/compiler_libs_ocaml5.ml %{target})) - (enabled_if (>= %{ocaml_version} 5))) + (enabled_if (and (>= %{ocaml_version} 5) (< %{ocaml_version} 5.2)))) + +(rule + (target compiler_libs.ml) + (action (copy compat_files/compiler_libs_ocaml5_2.ml %{target})) + (enabled_if (>= %{ocaml_version} 5.2))) -- GitLab