From 53499cd971ea2a184f6d1717095eecf554a9ae5b Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 3 Dec 2023 10:49:24 -0600 Subject: [PATCH] refactor: make stanzas private (#9362) This requires going through a functor to construct all stanzas. The capability will be useful to eliminate physical equality for stanzas. Signed-off-by: Rudi Grinberg --- bin/describe/describe_external_lib_deps.ml | 6 +- bin/describe/describe_pp.ml | 2 +- bin/describe/describe_workspace.ml | 2 +- bin/dune_init.ml | 6 +- src/dune_lang/stanza.ml | 7 + src/dune_lang/stanza.mli | 8 +- src/dune_rules/artifacts_db.ml | 4 +- src/dune_rules/cinaps.ml | 4 +- src/dune_rules/coq/coq_stanza.ml | 12 +- src/dune_rules/cram/cram_stanza.ml | 4 +- src/dune_rules/ctypes/ctypes_field.ml | 4 +- src/dune_rules/dir_contents.ml | 8 +- src/dune_rules/dir_status.ml | 10 +- src/dune_rules/dune_env.ml | 4 +- src/dune_rules/dune_file.ml | 134 ++++++++++++------ src/dune_rules/dune_file.mli | 41 +++--- src/dune_rules/foreign.ml | 4 + src/dune_rules/foreign.mli | 2 + src/dune_rules/foreign_sources.ml | 6 +- src/dune_rules/gen_rules.ml | 18 +-- .../generate_sites_module_stanza.ml | 4 +- src/dune_rules/install_rules.ml | 18 +-- src/dune_rules/mdx.ml | 4 +- src/dune_rules/melange/melange_stanzas.ml | 4 +- src/dune_rules/menhir/menhir_stanza.ml | 4 +- src/dune_rules/ml_sources.ml | 4 +- src/dune_rules/only_packages.ml | 4 +- src/dune_rules/packages.ml | 2 +- src/dune_rules/scope.ml | 14 +- src/dune_rules/super_context.ml | 4 +- src/dune_rules/utop.ml | 4 +- 31 files changed, 224 insertions(+), 128 deletions(-) diff --git a/bin/describe/describe_external_lib_deps.ml b/bin/describe/describe_external_lib_deps.ml index cefc714b276..182bb74c33e 100644 --- a/bin/describe/describe_external_lib_deps.ml +++ b/bin/describe/describe_external_lib_deps.ml @@ -149,7 +149,7 @@ let libs db (context : Context.t) (build_system : Dune_rules.Main.build_system) Memo.parallel_map dune_file.stanzas ~f:(fun stanza -> let dir = dune_file.dir in match stanza with - | Dune_rules.Dune_file.Executables exes -> + | Dune_rules.Dune_file.Executables.T exes -> let* ocaml = Context.ocaml context in resolve_libs db @@ -161,7 +161,7 @@ let libs db (context : Context.t) (build_system : Dune_rules.Main.build_system) Item.Kind.Executables (exes_extensions ocaml.lib_config exes.modes) >>| List.singleton - | Dune_rules.Dune_file.Library lib -> + | Dune_rules.Dune_file.Library.T lib -> resolve_libs db dir @@ -172,7 +172,7 @@ let libs db (context : Context.t) (build_system : Dune_rules.Main.build_system) Item.Kind.Library [] >>| List.singleton - | Dune_rules.Dune_file.Tests tests -> + | Dune_rules.Dune_file.Tests.T tests -> let* ocaml = Context.ocaml context in resolve_libs db diff --git a/bin/describe/describe_pp.ml b/bin/describe/describe_pp.ml index 1c4416a3d6f..498bd7c6abf 100644 --- a/bin/describe/describe_pp.ml +++ b/bin/describe/describe_pp.ml @@ -62,7 +62,7 @@ let get_pped_file super_context file = dune_file.stanzas |> List.fold_left ~init:None ~f:(fun acc stanza -> match stanza with - | Dune_rules.Dune_file.Library lib -> + | Dune_rules.Dune_file.Library.T lib -> let preprocess = Dune_rules.Preprocess.Per_module.( lib.buildable.preprocess |> single_preprocess) diff --git a/bin/describe/describe_workspace.ml b/bin/describe/describe_workspace.ml index 663ae9b05e5..2a2951e8bb7 100644 --- a/bin/describe/describe_workspace.ml +++ b/bin/describe/describe_workspace.ml @@ -537,7 +537,7 @@ module Crawl = struct Memo.parallel_map dune_file.stanzas ~f:(fun stanza -> let dir = Path.Build.append_source (Context.build_dir context) dune_file.dir in match stanza with - | Dune_file.Executables exes -> + | Dune_file.Executables.T exes -> executables sctx ~options ~project:dune_file.project ~dir exes | _ -> Memo.return None) >>| List.filter_opt) diff --git a/bin/dune_init.ml b/bin/dune_init.ml index 885d2e49f45..f13b1b1b6a9 100644 --- a/bin/dune_init.ml +++ b/bin/dune_init.ml @@ -54,9 +54,9 @@ module File = struct let stanzas_conflict (a : Stanza.t) (b : Stanza.t) = let open Dune_file in match a, b with - | Executables a, Executables b -> executables_conflict a b - | Library a, Library b -> libraries_conflict a b - | Tests a, Tests b -> tests_conflict a b + | Executables.T a, Executables.T b -> executables_conflict a b + | Library.T a, Library.T b -> libraries_conflict a b + | Tests.T a, Tests.T b -> tests_conflict a b (* NOTE No other stanza types currently supported *) | _ -> false ;; diff --git a/src/dune_lang/stanza.ml b/src/dune_lang/stanza.ml index 1bfdaf36d14..0a32f07b22c 100644 --- a/src/dune_lang/stanza.ml +++ b/src/dune_lang/stanza.ml @@ -3,6 +3,13 @@ open Dune_sexp type t = .. +module Make (S : sig + type t + end) = +struct + type t += T of S.t +end + module Parser = struct type nonrec t = string * t list Decoder.t end diff --git a/src/dune_lang/stanza.mli b/src/dune_lang/stanza.mli index 4c40e8dc495..e78c128f968 100644 --- a/src/dune_lang/stanza.mli +++ b/src/dune_lang/stanza.mli @@ -3,7 +3,13 @@ open! Stdune open Dune_sexp -type t = .. +type t = private .. + +module Make (S : sig + type t + end) : sig + type t += T of S.t +end val latest_version : Syntax.Version.t diff --git a/src/dune_rules/artifacts_db.ml b/src/dune_rules/artifacts_db.ml index bbda641613b..0e4cffdda89 100644 --- a/src/dune_rules/artifacts_db.ml +++ b/src/dune_rules/artifacts_db.ml @@ -67,9 +67,9 @@ let get_installed_binaries ~(context : Context.t) stanzas = in Memo.List.map d.stanzas ~f:(fun stanza -> match (stanza : Stanza.t) with - | Dune_file.Install { section = Section Bin; files; _ } -> + | Dune_file.Install_conf.T { section = Section Bin; files; _ } -> binaries_from_install files - | Dune_file.Executables + | Dune_file.Executables.T ({ install_conf = Some { section = Section Bin; files; _ }; _ } as exes) -> let* available = let* enabled_if = diff --git a/src/dune_rules/cinaps.ml b/src/dune_rules/cinaps.ml index 01e30ef38b2..8851297a5f2 100644 --- a/src/dune_rules/cinaps.ml +++ b/src/dune_rules/cinaps.ml @@ -15,7 +15,9 @@ type t = let name = "cinaps" let cinaps_alias = Alias.Name.of_string name -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) let syntax = Dune_lang.Syntax.create diff --git a/src/dune_rules/coq/coq_stanza.ml b/src/dune_rules/coq/coq_stanza.ml index 4111a69e92e..27a511ed65b 100644 --- a/src/dune_rules/coq/coq_stanza.ml +++ b/src/dune_rules/coq/coq_stanza.ml @@ -44,7 +44,9 @@ module Coqpp = struct { modules; loc }) ;; - type Stanza.t += T of t + include Stanza.Make (struct + type nonrec t = t + end) let p = "coq.pp", decode >>| fun x -> [ T x ] end @@ -129,7 +131,9 @@ module Extraction = struct { prelude; extracted_modules; buildable }) ;; - type Stanza.t += T of t + include Stanza.Make (struct + type nonrec t = t + end) let p = "coq.extraction", decode >>| fun x -> [ T x ] end @@ -230,7 +234,9 @@ module Theory = struct }) ;; - type Stanza.t += T of t + include Stanza.Make (struct + type nonrec t = t + end) let coqlib_warn x = User_warning.emit diff --git a/src/dune_rules/cram/cram_stanza.ml b/src/dune_rules/cram/cram_stanza.ml index 2d2c5819c16..cc615b99997 100644 --- a/src/dune_rules/cram/cram_stanza.ml +++ b/src/dune_rules/cram/cram_stanza.ml @@ -31,7 +31,9 @@ type t = ; runtest_alias : (Loc.t * bool) option } -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) let decode = fields diff --git a/src/dune_rules/ctypes/ctypes_field.ml b/src/dune_rules/ctypes/ctypes_field.ml index 9f54d36cad6..9eba5b46a29 100644 --- a/src/dune_rules/ctypes/ctypes_field.ml +++ b/src/dune_rules/ctypes/ctypes_field.ml @@ -149,7 +149,9 @@ type t = ; version : Syntax.Version.t } -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) let decode = let open Dune_lang.Decoder in diff --git a/src/dune_rules/dir_contents.ml b/src/dune_rules/dir_contents.ml index 7713498ea0d..8721f8346c2 100644 --- a/src/dune_rules/dir_contents.ml +++ b/src/dune_rules/dir_contents.ml @@ -129,7 +129,7 @@ let build_mlds_map stanzas ~dir ~files = |> Memo.return) in Memo.parallel_map stanzas ~f:(function - | Documentation doc -> + | Documentation.T doc -> let+ mlds = let+ mlds = Memo.Lazy.force mlds in Ordered_set_lang.Unordered_string.eval @@ -194,19 +194,19 @@ end = struct | Coq_stanza.Extraction.T s -> Memo.return (Coq_stanza.Extraction.ml_target_fnames s) | Menhir_stanza.T menhir -> Memo.return (Menhir_stanza.targets menhir) - | Rule rule -> + | Rule.T rule -> Simple_rules.user_rule sctx rule ~dir ~expander >>| (function | None -> [] | Some targets -> (* CR-someday amokhov: Do not ignore directory targets. *) Path.Build.Set.to_list_map targets.files ~f:Path.Build.basename) - | Copy_files def -> + | Copy_files.T def -> Simple_rules.copy_files sctx def ~src_dir ~dir ~expander >>| Path.Set.to_list_map ~f:Path.basename | Generate_sites_module_stanza.T def -> Generate_sites_module_rules.setup_rules sctx ~dir def >>| List.singleton - | Library { buildable; _ } | Executables { buildable; _ } -> + | Library.T { buildable; _ } | Executables.T { buildable; _ } -> let select_deps_files = select_deps_files buildable.libraries in let ctypes_files = (* Also manually add files generated by ctypes rules. *) diff --git a/src/dune_rules/dir_status.ml b/src/dune_rules/dir_status.ml index 164b2b7ca1a..59777df42ee 100644 --- a/src/dune_rules/dir_status.ml +++ b/src/dune_rules/dir_status.ml @@ -56,7 +56,7 @@ let current_group dir = function let get_include_subdirs stanzas = List.fold_left stanzas ~init:None ~f:(fun acc stanza -> match stanza with - | Include_subdirs (loc, x) -> + | Include_subdirs.T (loc, x) -> if Option.is_some acc then User_error.raise @@ -70,9 +70,9 @@ let find_module_stanza stanzas = List.find_map stanzas ~f:(fun stanza -> match stanza with | Melange_stanzas.Emit.T { loc; _ } - | Library { buildable = { loc; _ }; _ } - | Executables { buildable = { loc; _ }; _ } - | Tests { exes = { buildable = { loc; _ }; _ }; _ } -> Some loc + | Library.T { buildable = { loc; _ }; _ } + | Executables.T { buildable = { loc; _ }; _ } + | Tests.T { exes = { buildable = { loc; _ }; _ }; _ } -> Some loc | _ -> None) ;; @@ -92,7 +92,7 @@ let error_no_module_consumer ~loc (qualification : Include_subdirs.qualification let extract_directory_targets ~dir stanzas = Memo.List.fold_left stanzas ~init:Path.Build.Map.empty ~f:(fun acc stanza -> match stanza with - | Rule { targets = Static { targets = l; _ }; loc = rule_loc; _ } -> + | Rule.T { targets = Static { targets = l; _ }; loc = rule_loc; _ } -> List.fold_left l ~init:acc ~f:(fun acc (target, kind) -> let loc = String_with_vars.loc target in match (kind : Targets_spec.Kind.t) with diff --git a/src/dune_rules/dune_env.ml b/src/dune_rules/dune_env.ml index 0d4c77b2307..37bf7da15ce 100644 --- a/src/dune_rules/dune_env.ml +++ b/src/dune_rules/dune_env.ml @@ -322,4 +322,6 @@ let fire_hooks t ~profile = User_warning.emit ?loc paragraphs)) ;; -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) diff --git a/src/dune_rules/dune_file.ml b/src/dune_rules/dune_file.ml index ff3b8736dd9..52427cf2174 100644 --- a/src/dune_rules/dune_file.ml +++ b/src/dune_rules/dune_file.ml @@ -1090,9 +1090,14 @@ module Library = struct ~instrumentation_backend ~melange_runtime_deps ;; + + include Stanza.Make (struct + type nonrec t = t + end) end module Plugin = struct + (* CR-rgrinberg: this shouldn't live here *) type t = { package : Package.t ; name : Package.Name.t @@ -1110,6 +1115,10 @@ module Plugin = struct and+ optional = field_b "optional" in { name; libraries; site; package; optional }) ;; + + include Stanza.Make (struct + type nonrec t = t + end) end module Install_conf = struct @@ -1122,6 +1131,10 @@ module Install_conf = struct ; enabled_if : Blang.t } + include Stanza.Make (struct + type nonrec t = t + end) + let decode = fields (let+ loc = loc @@ -1552,6 +1565,10 @@ module Executables = struct ; dune_version : Dune_lang.Syntax.Version.t } + include Stanza.Make (struct + type nonrec t = t + end) + let bootstrap_info_extension = let syntax = Dune_lang.Syntax.create @@ -1713,6 +1730,10 @@ module Rule = struct ; package : Package.t option } + include Stanza.Make (struct + type nonrec t = t + end) + type action_or_field = | Action | Field @@ -1971,6 +1992,10 @@ module Alias_conf = struct ; loc : Loc.t } + include Stanza.Make (struct + type nonrec t = t + end) + let decode = fields (let* deps = @@ -2006,6 +2031,10 @@ module Tests = struct ; action : Dune_lang.Action.t option } + include Stanza.Make (struct + type nonrec t = t + end) + let gen_parse names = fields (let* deps = @@ -2080,6 +2109,10 @@ module Toplevel = struct ; pps : Preprocess.Without_instrumentation.t Preprocess.t } + include Stanza.Make (struct + type nonrec t = t + end) + let decode = let open Dune_lang.Decoder in fields @@ -2114,6 +2147,10 @@ module Copy_files = struct ; syntax_version : Dune_lang.Syntax.Version.t } + include Stanza.Make (struct + type nonrec t = t + end) + let long_form = let check = Dune_lang.Syntax.since Stanza.syntax (2, 7) in let+ alias = field_o "alias" (check >>> Dune_lang.Alias.decode) @@ -2148,6 +2185,10 @@ module Documentation = struct ; mld_files : Ordered_set_lang.t } + include Stanza.Make (struct + type nonrec t = t + end) + let decode = fields (let+ package = Stanza_common.Pkg.field ~stanza:"documentation" @@ -2166,6 +2207,12 @@ module Include_subdirs = struct | No | Include of qualification + type stanza = Loc.t * t + + include Stanza.Make (struct + type nonrec t = stanza + end) + let decode ~enable_qualified = sum [ "no", return No @@ -2190,6 +2237,10 @@ module Library_redirect = struct module Local = struct type nonrec t = (Loc.t * Lib_name.Local.t) t + include Stanza.Make (struct + type nonrec t = t + end) + let for_lib (lib : Library.t) ~new_public_name ~loc : t = { loc; new_public_name; old_name = lib.name; project = lib.project } ;; @@ -2243,6 +2294,10 @@ module Deprecated_library_name = struct type t = Old_name.t Library_redirect.t + include Stanza.Make (struct + type nonrec t = t + end) + let old_public_name (t : t) = Public_lib.name (fst t.old_name) let decode = @@ -2263,48 +2318,39 @@ module Deprecated_library_name = struct ;; end -type Stanza.t += - | Library of Library.t - | Foreign_library of Foreign.Library.t - | Executables of Executables.t - | Rule of Rule.t - | Install of Install_conf.t - | Alias of Alias_conf.t - | Copy_files of Copy_files.t - | Documentation of Documentation.t - | Tests of Tests.t - | Include_subdirs of Loc.t * Include_subdirs.t - | Toplevel of Toplevel.t - | Library_redirect of Library_redirect.Local.t - | Deprecated_library_name of Deprecated_library_name.t - | Plugin of Plugin.t +module Include = struct + type t = Loc.t * string + + include Stanza.Make (struct + type nonrec t = t + end) +end module Stanzas = struct type t = Stanza.t list - let rules l = List.map l ~f:(fun x -> Rule x) - let execs exe = [ Executables exe ] + let rules l = List.map l ~f:(fun x -> Rule.T x) + let execs exe = [ Executables.T exe ] - type Stanza.t += Include of Loc.t * string type constructors = Stanza.Parser.t list let stanzas : constructors = [ ( "library" , let+ x = Library.decode in - let base = [ Library x ] in + let base = [ Library.T x ] in match Library_redirect.Local.of_lib x with | None -> base - | Some r -> Library_redirect r :: base ) + | Some r -> Library_redirect.Local.T r :: base ) ; ( "foreign_library" , let+ () = Dune_lang.Syntax.since Stanza.syntax (2, 0) and+ x = Foreign.Library.decode in - [ Foreign_library x ] ) + [ Foreign.Library.T x ] ) ; "executable", Executables.single >>| execs ; "executables", Executables.multi >>| execs ; ( "rule" , let+ loc = loc and+ x = Rule.decode in - [ Rule { x with loc } ] ) + [ Rule.T { x with loc } ] ) ; ( "ocamllex" , let+ loc = loc and+ x = Rule.ocamllex in @@ -2315,23 +2361,23 @@ module Stanzas = struct rules (Rule.ocamlyacc_to_rule loc x) ) ; ( "install" , let+ x = Install_conf.decode in - [ Install x ] ) + [ Install_conf.T x ] ) ; ( "alias" , let+ x = Alias_conf.decode in - [ Alias x ] ) + [ Alias_conf.T x ] ) ; ( "copy_files" , let+ x = Copy_files.decode in - [ Copy_files x ] ) + [ Copy_files.T x ] ) ; ( "copy_files#" , let+ x = Copy_files.decode in - [ Copy_files { x with add_line_directive = true } ] ) + [ Copy_files.T { x with add_line_directive = true } ] ) ; ( "include" , let+ loc = loc and+ fn = relative_file in - [ Include (loc, fn) ] ) + [ Include.T (loc, fn) ] ) ; ( "documentation" , let+ d = Documentation.decode in - [ Documentation d ] ) + [ Documentation.T d ] ) ; ( "jbuild_version" , let+ () = Dune_lang.Syntax.deleted_in Stanza.syntax (1, 0) and+ _ = Jbuild_version.decode in @@ -2339,11 +2385,11 @@ module Stanzas = struct ; ( "tests" , let+ () = Dune_lang.Syntax.since Stanza.syntax (1, 0) and+ t = Tests.multi in - [ Tests t ] ) + [ Tests.T t ] ) ; ( "test" , let+ () = Dune_lang.Syntax.since Stanza.syntax (1, 0) and+ t = Tests.single in - [ Tests t ] ) + [ Tests.T t ] ) ; ( "external_variant" , let+ () = Dune_lang.Syntax.deleted_in Stanza.syntax (2, 6) in [] ) @@ -2357,15 +2403,15 @@ module Stanzas = struct let enable_qualified = Dune_project.is_extension_set project Coq_stanza.key in Include_subdirs.decode ~enable_qualified and+ loc = loc in - [ Include_subdirs (loc, t) ] ) + [ Include_subdirs.T (loc, t) ] ) ; ( "toplevel" , let+ () = Dune_lang.Syntax.since Stanza.syntax (1, 7) and+ t = Toplevel.decode in - [ Toplevel t ] ) + [ Toplevel.T t ] ) ; ( "deprecated_library_name" , let+ () = Dune_lang.Syntax.since Stanza.syntax (2, 0) and+ t = Deprecated_library_name.decode in - [ Deprecated_library_name t ] ) + [ Deprecated_library_name.T t ] ) ; ( "cram" , let+ () = Dune_lang.Syntax.since Stanza.syntax (2, 7) and+ t = Cram_stanza.decode @@ -2390,7 +2436,7 @@ module Stanzas = struct ; ( "plugin" , let+ () = Dune_lang.Syntax.since Site.dune_site_syntax (0, 1) and+ t = Plugin.decode in - [ Plugin t ] ) + [ Plugin.T t ] ) ] ;; @@ -2413,7 +2459,7 @@ module Stanzas = struct let rec parse_file_includes ~stanza_parser ~context sexps = List.concat_map sexps ~f:(parse stanza_parser) |> Memo.List.concat_map ~f:(function - | Include (loc, fn) -> + | Include.T (loc, fn) -> let open Memo.O in let* sexps, context = Include_stanza.load_sexps ~context (loc, fn) in parse_file_includes ~stanza_parser ~context sexps @@ -2455,14 +2501,14 @@ module Stanzas = struct end let stanza_package = function - | Library lib -> Library.package lib - | Alias { package = Some package; _ } - | Rule { package = Some package; _ } - | Install { package; _ } - | Plugin { package; _ } - | Executables { install_conf = Some { package; _ }; _ } - | Documentation { package; _ } - | Tests { package = Some package; _ } -> Some package + | Library.T lib -> Library.package lib + | Alias_conf.T { package = Some package; _ } + | Rule.T { package = Some package; _ } + | Install_conf.T { package; _ } + | Plugin.T { package; _ } + | Executables.T { install_conf = Some { package; _ }; _ } + | Documentation.T { package; _ } + | Tests.T { package = Some package; _ } -> Some package | Coq_stanza.Theory.T { package = Some package; _ } -> Some package | _ -> None ;; @@ -2486,7 +2532,7 @@ let is_promoted_rule = in fun version rule -> match rule with - | Rule { mode; _ } | Menhir_stanza.T { mode; _ } -> is_promoted_mode version mode + | Rule.T { mode; _ } | Menhir_stanza.T { mode; _ } -> is_promoted_mode version mode | _ -> false ;; diff --git a/src/dune_rules/dune_file.mli b/src/dune_rules/dune_file.mli index 11a27a76c72..870ac47cfcd 100644 --- a/src/dune_rules/dune_file.mli +++ b/src/dune_rules/dune_file.mli @@ -171,6 +171,8 @@ module Library : sig ; melange_runtime_deps : Loc.t * Dep_conf.t list } + type Stanza.t += T of t + val sub_dir : t -> string option val package : t -> Package.t option @@ -221,6 +223,8 @@ module Plugin : sig ; site : Loc.t * (Package.Name.t * Site.t) ; optional : bool } + + type Stanza.t += T of t end module Install_conf : sig @@ -232,6 +236,8 @@ module Install_conf : sig ; package : Package.t ; enabled_if : Blang.t } + + type Stanza.t += T of t end module Executables : sig @@ -281,6 +287,8 @@ module Executables : sig ; dune_version : Dune_lang.Syntax.Version.t } + type Stanza.t += T of t + (** Check if the executables have any foreign stubs or archives. *) val has_foreign : t -> bool @@ -299,6 +307,8 @@ module Copy_files : sig ; files : String_with_vars.t ; syntax_version : Dune_lang.Syntax.Version.t } + + type Stanza.t += T of t end module Rule : sig @@ -314,6 +324,8 @@ module Rule : sig ; aliases : Alias.Name.t list ; package : Package.t option } + + type Stanza.t += T of t end module Alias_conf : sig @@ -326,6 +338,8 @@ module Alias_conf : sig ; enabled_if : Blang.t ; loc : Loc.t } + + type Stanza.t += T of t end module Documentation : sig @@ -334,6 +348,8 @@ module Documentation : sig ; package : Package.t ; mld_files : Ordered_set_lang.t } + + type Stanza.t += T of t end module Tests : sig @@ -346,6 +362,8 @@ module Tests : sig ; build_if : Blang.t ; action : Dune_lang.Action.t option } + + type Stanza.t += T of t end module Toplevel : sig @@ -355,6 +373,8 @@ module Toplevel : sig ; loc : Loc.t ; pps : Preprocess.Without_instrumentation.t Preprocess.t } + + type Stanza.t += T of t end module Include_subdirs : sig @@ -365,6 +385,9 @@ module Include_subdirs : sig type t = | No | Include of qualification + + type stanza = Loc.t * t + type Stanza.t += T of stanza end (** The purpose of [Library_redirect] stanza is to create a redirection from an @@ -388,6 +411,7 @@ module Library_redirect : sig module Local : sig type nonrec t = (Loc.t * Lib_name.Local.t) t + type Stanza.t += T of t val of_private_lib : Library.t -> t option end @@ -403,26 +427,11 @@ module Deprecated_library_name : sig end type t = Old_name.t Library_redirect.t + type Stanza.t += T of t val old_public_name : t -> Lib_name.t end -type Stanza.t += - | Library of Library.t - | Foreign_library of Foreign.Library.t - | Executables of Executables.t - | Rule of Rule.t - | Install of Install_conf.t - | Alias of Alias_conf.t - | Copy_files of Copy_files.t - | Documentation of Documentation.t - | Tests of Tests.t - | Include_subdirs of Loc.t * Include_subdirs.t - | Toplevel of Toplevel.t - | Library_redirect of Library_redirect.Local.t - | Deprecated_library_name of Deprecated_library_name.t - | Plugin of Plugin.t - val stanza_package : Stanza.t -> Package.t option (** [of_ast project ast] is the list of [Stanza.t]s derived from decoding the diff --git a/src/dune_rules/foreign.ml b/src/dune_rules/foreign.ml index 25e91be5f7a..c7dc9a131e0 100644 --- a/src/dune_rules/foreign.ml +++ b/src/dune_rules/foreign.ml @@ -263,6 +263,10 @@ module Library = struct and+ stubs = Stubs.decode_stubs ~for_library:true in { archive_name; archive_name_loc; stubs }) ;; + + include Stanza.Make (struct + type nonrec t = t + end) end module Source = struct diff --git a/src/dune_rules/foreign.mli b/src/dune_rules/foreign.mli index e90a0d0de85..e256b2adc3a 100644 --- a/src/dune_rules/foreign.mli +++ b/src/dune_rules/foreign.mli @@ -160,6 +160,8 @@ module Library : sig } val decode : t Dune_lang.Decoder.t + + type Stanza.t += T of t end (** A foreign source file that has a [path] and all information of the diff --git a/src/dune_rules/foreign_sources.ml b/src/dune_rules/foreign_sources.ml index 5e45b89d1b1..b673b71ca13 100644 --- a/src/dune_rules/foreign_sources.ml +++ b/src/dune_rules/foreign_sources.ml @@ -175,7 +175,7 @@ let make stanzas ~(sources : Foreign.Sources.Unresolved.t) ~dune_version = ~init:([], [], []) ~f:(fun ((libs, foreign_libs, exes) as acc) stanza -> match (stanza : Stanza.t) with - | Library lib -> + | Library.T lib -> let all = eval_foreign_stubs ~dune_version @@ -184,12 +184,12 @@ let make stanzas ~(sources : Foreign.Sources.Unresolved.t) ~dune_version = ~sources in (lib, all) :: libs, foreign_libs, exes - | Foreign_library library -> + | Foreign.Library.T library -> let all = eval_foreign_stubs ~dune_version [ library.stubs ] ~sources None in ( libs , (library.archive_name, (library.archive_name_loc, all)) :: foreign_libs , exes ) - | Executables exe | Tests { exes = exe; _ } -> + | Executables.T exe | Tests.T { exes = exe; _ } -> let all = eval_foreign_stubs ~dune_version diff --git a/src/dune_rules/gen_rules.ml b/src/dune_rules/gen_rules.ml index 49f4e196456..f939b462449 100644 --- a/src/dune_rules/gen_rules.ml +++ b/src/dune_rules/gen_rules.ml @@ -109,10 +109,10 @@ end = struct let toplevel_setup = Toplevel.Stanza.setup in let open Dune_file in match stanza with - | Toplevel toplevel -> + | Toplevel.T toplevel -> let+ () = toplevel_setup ~sctx ~dir ~toplevel in empty_none - | Library lib -> + | Library.T lib -> (* XXX why are we setting up private doc rules for disabled libraries? *) let* () = Odoc.setup_private_library_doc_alias sctx ~scope ~dir:ctx_dir lib and+ enabled_if = @@ -122,10 +122,10 @@ end = struct ~loc:lib.buildable.loc (fun () -> Lib_rules.rules lib ~sctx ~dir ~scope ~dir_contents ~expander) enabled_if - | Foreign_library lib -> + | Foreign.Library.T lib -> let+ () = Lib_rules.foreign_rules lib ~sctx ~dir ~dir_contents ~expander in empty_none - | Executables exes -> + | Executables.T exes -> Expander.eval_blang expander exes.enabled_if >>= if_available (fun () -> let+ () = @@ -139,14 +139,14 @@ end = struct (List.map exes.names ~f:(fun (_, exe) -> Path.Build.relative dir (exe ^ Js_of_ocaml.Ext.exe))) }) - | Alias alias -> + | Alias_conf.T alias -> let+ () = Simple_rules.alias sctx alias ~dir ~expander in empty_none - | Tests tests -> + | Tests.T tests -> Expander.eval_blang expander tests.build_if >>= if_available_buildable ~loc:tests.exes.buildable.loc (fun () -> Test_rules.rules tests ~sctx ~dir ~scope ~expander ~dir_contents) - | Copy_files { files = glob; _ } -> + | Copy_files.T { files = glob; _ } -> let+ source_dirs = let+ src_glob = Expander.No_deps.expand_str expander glob in if Filename.is_relative src_glob @@ -160,10 +160,10 @@ end = struct else None in { empty_none with source_dirs } - | Install i -> + | Install_conf.T i -> let+ () = install_stanza_rules ~ctx_dir ~expander i in empty_none - | Plugin p -> + | Plugin.T p -> let+ () = Plugin_rules.setup_rules ~sctx ~dir p in empty_none | Cinaps.T cinaps -> diff --git a/src/dune_rules/generate_sites_module/generate_sites_module_stanza.ml b/src/dune_rules/generate_sites_module/generate_sites_module_stanza.ml index c32e1fb04e0..1eae1a26868 100644 --- a/src/dune_rules/generate_sites_module/generate_sites_module_stanza.ml +++ b/src/dune_rules/generate_sites_module/generate_sites_module_stanza.ml @@ -26,4 +26,6 @@ let decode = { loc; module_; sourceroot; relocatable; sites; plugins }) ;; -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) diff --git a/src/dune_rules/install_rules.ml b/src/dune_rules/install_rules.ml index d0e8a9f10ad..d8502b8f18c 100644 --- a/src/dune_rules/install_rules.ml +++ b/src/dune_rules/install_rules.ml @@ -329,7 +329,7 @@ end = struct let+ keep = let open Dune_file in match (stanza : Stanza.t) with - | Library lib -> + | Library.T lib -> let* enabled_if = Expander.eval_blang expander lib.enabled_if in if enabled_if then @@ -337,10 +337,10 @@ end = struct then Lib.DB.available (Scope.libs scope) (Dune_file.Library.best_name lib) else Memo.return true else Memo.return false - | Documentation _ -> Memo.return true - | Install { enabled_if; _ } -> Expander.eval_blang expander enabled_if - | Plugin _ -> Memo.return true - | Executables ({ install_conf = Some _; _ } as exes) -> + | Documentation.T _ -> Memo.return true + | Install_conf.T { enabled_if; _ } -> Expander.eval_blang expander enabled_if + | Plugin.T _ -> Memo.return true + | Executables.T ({ install_conf = Some _; _ } as exes) -> Expander.eval_blang expander exes.enabled_if >>= (function | false -> Memo.return false @@ -455,14 +455,14 @@ end = struct let new_entries = let open Dune_file in match (stanza : Stanza.t) with - | Install i | Executables { install_conf = Some i; _ } -> + | Install_conf.T i | Executables.T { install_conf = Some i; _ } -> entries_of_install_stanza ~dir ~expander ~package_db i - | Library lib -> + | Library.T lib -> let sub_dir = Dune_file.Library.sub_dir lib in let* dir_contents = Dir_contents.get sctx ~dir in lib_install_files sctx ~scope ~dir ~sub_dir lib ~dir_contents | Coq_stanza.Theory.T coqlib -> Coq_rules.install_rules ~sctx ~dir coqlib - | Documentation d -> + | Documentation.T d -> let* dc = Dir_contents.get sctx ~dir in let+ mlds = Dir_contents.mlds dc d in List.map mlds ~f:(fun mld -> @@ -474,7 +474,7 @@ end = struct mld in Install.Entry.Sourced.create ~loc:d.loc entry) - | Plugin t -> Plugin_rules.install_rules ~sctx ~package_db ~dir t + | Plugin.T t -> Plugin_rules.install_rules ~sctx ~package_db ~dir t | _ -> Memo.return [] in let name = Package.name package in diff --git a/src/dune_rules/mdx.ml b/src/dune_rules/mdx.ml index 63327a4cb7c..dffdd259acf 100644 --- a/src/dune_rules/mdx.ml +++ b/src/dune_rules/mdx.ml @@ -192,7 +192,9 @@ type t = let enabled_if t = t.enabled_if -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) let syntax = let name = "mdx" in diff --git a/src/dune_rules/melange/melange_stanzas.ml b/src/dune_rules/melange/melange_stanzas.ml index b1b217d90eb..bccdfd7993f 100644 --- a/src/dune_rules/melange/melange_stanzas.ml +++ b/src/dune_rules/melange/melange_stanzas.ml @@ -21,7 +21,9 @@ module Emit = struct ; dune_version : Dune_lang.Syntax.Version.t } - type Stanza.t += T of t + include Stanza.Make (struct + type nonrec t = t + end) let implicit_alias = Alias.Name.of_string "melange" diff --git a/src/dune_rules/menhir/menhir_stanza.ml b/src/dune_rules/menhir/menhir_stanza.ml index 9b14a6e2d32..0b241548d10 100644 --- a/src/dune_rules/menhir/menhir_stanza.ml +++ b/src/dune_rules/menhir/menhir_stanza.ml @@ -41,7 +41,9 @@ let decode = { merge_into; flags; modules; mode; loc; infer; enabled_if }) ;; -type Stanza.t += T of t +include Stanza.Make (struct + type nonrec t = t + end) let () = Dune_project.Extension.register_simple diff --git a/src/dune_rules/ml_sources.ml b/src/dune_rules/ml_sources.ml index 11845b2cf62..1dd04d18185 100644 --- a/src/dune_rules/ml_sources.ml +++ b/src/dune_rules/ml_sources.ml @@ -428,7 +428,7 @@ let modules_of_stanzas = fun stanzas ~project ~dir ~libs ~lookup_vlib ~modules ~include_subdirs -> Memo.parallel_map stanzas ~f:(fun stanza -> match (stanza : Stanza.t) with - | Library lib -> + | Library.T lib -> (* jeremiedimino: this [Resolve.get] means that if the user writes an invalid [implements] field, we will get an error immediately even if the library is not built. We should change this to carry the @@ -447,7 +447,7 @@ let modules_of_stanzas = in let obj_dir = Library.obj_dir lib ~dir in `Library (lib, sources, modules, obj_dir) - | Executables exes | Tests { exes; _ } -> + | Executables.T exes | Tests.T { exes; _ } -> let obj_dir = Dune_file.Executables.obj_dir ~dir exes in let+ sources, modules = let { Buildable.loc = stanza_loc; modules = modules_settings; _ } = diff --git a/src/dune_rules/only_packages.ml b/src/dune_rules/only_packages.ml index 44b7b19ed1f..2c94826123a 100644 --- a/src/dune_rules/only_packages.ml +++ b/src/dune_rules/only_packages.ml @@ -93,10 +93,10 @@ let filter_out_stanzas_from_hidden_packages ~visible_pkgs = then Some stanza else ( match stanza with - | Dune_file.Library l -> + | Dune_file.Library.T l -> let open Option.O in let+ redirect = Dune_file.Library_redirect.Local.of_private_lib l in - Dune_file.Library_redirect redirect + Dune_file.Library_redirect.Local.T redirect | _ -> None)) ;; diff --git a/src/dune_rules/packages.ml b/src/dune_rules/packages.ml index 2256bcaa803..8eefe5819a3 100644 --- a/src/dune_rules/packages.ml +++ b/src/dune_rules/packages.ml @@ -14,7 +14,7 @@ let mlds_by_package_def = let* dune_files = Context.name ctx |> Only_packages.filtered_stanzas in Memo.parallel_map dune_files ~f:(fun dune_file -> Memo.parallel_map dune_file.stanzas ~f:(function - | Documentation d -> + | Documentation.T d -> let dir = Path.Build.append_source (Context.build_dir ctx) dune_file.dir in let* dc = Dir_contents.get sctx ~dir in let+ mlds = Dir_contents.mlds dc d in diff --git a/src/dune_rules/scope.ml b/src/dune_rules/scope.ml index c307b7d474e..bee36d5ae33 100644 --- a/src/dune_rules/scope.ml +++ b/src/dune_rules/scope.ml @@ -344,12 +344,12 @@ module DB = struct ~f:(fun dune_file stanza (acc, coq_acc) -> let build_dir = Context.build_dir context in match stanza with - | Dune_file.Library lib -> + | Dune_file.Library.T lib -> let ctx_dir = Path.Build.append_source build_dir dune_file.dir in Library_related_stanza.Library (ctx_dir, lib) :: acc, coq_acc - | Dune_file.Deprecated_library_name d -> + | Dune_file.Deprecated_library_name.T d -> Deprecated_library_name d :: acc, coq_acc - | Dune_file.Library_redirect d -> Library_redirect d :: acc, coq_acc + | Dune_file.Library_redirect.Local.T d -> Library_redirect d :: acc, coq_acc | Coq_stanza.Theory.T coq_lib -> let ctx_dir = Path.Build.append_source build_dir dune_file.dir in acc, (ctx_dir, coq_lib) :: coq_acc @@ -417,7 +417,7 @@ module DB = struct let+ libs = Dune_file.Memo_fold.fold_stanzas stanzas ~init:[] ~f:(fun d stanza acc -> match stanza with - | Dune_file.Library ({ visibility = Private (Some pkg); _ } as lib) -> + | Dune_file.Library.T ({ visibility = Private (Some pkg); _ } as lib) -> let+ lib = let* scope = find_by_dir (Path.Build.append_source build_dir d.dir) in let db = libs scope in @@ -428,7 +428,7 @@ module DB = struct | Some lib -> let name = Package.name pkg in (name, Lib_entry.Library (Lib.Local.of_lib_exn lib)) :: acc) - | Dune_file.Library { visibility = Public pub; _ } -> + | Dune_file.Library.T { visibility = Public pub; _ } -> let+ lib = Lib.DB.find public_libs (Dune_file.Public_lib.name pub) in (match lib with | None -> @@ -439,8 +439,8 @@ module DB = struct let package = Dune_file.Public_lib.package pub in let name = Package.name package in (name, Lib_entry.Library (Lib.Local.of_lib_exn lib)) :: acc) - | Dune_file.Deprecated_library_name ({ old_name = old_public_name, _; _ } as d) - -> + | Dune_file.Deprecated_library_name.T + ({ old_name = old_public_name, _; _ } as d) -> let package = Dune_file.Public_lib.package old_public_name in let name = Package.name package in Memo.return ((name, Lib_entry.Deprecated_library_name d) :: acc) diff --git a/src/dune_rules/super_context.ml b/src/dune_rules/super_context.ml index 763de365132..c379a28a576 100644 --- a/src/dune_rules/super_context.ml +++ b/src/dune_rules/super_context.ml @@ -334,9 +334,9 @@ let add_packages_env context ~base stanzas packages = add_in_package_section acc pkg_name section in match stanza with - | Dune_file.Install { section = Site { pkg; site; loc }; _ } -> + | Dune_file.Install_conf.T { section = Site { pkg; site; loc }; _ } -> add_in_package_sites pkg site loc - | Dune_file.Plugin { site = loc, (pkg, site); _ } -> + | Dune_file.Plugin.T { site = loc, (pkg, site); _ } -> add_in_package_sites pkg site loc | _ -> Memo.return acc) in diff --git a/src/dune_rules/utop.ml b/src/dune_rules/utop.ml index 0359c815f70..b1f43a66804 100644 --- a/src/dune_rules/utop.ml +++ b/src/dune_rules/utop.ml @@ -54,7 +54,7 @@ let libs_and_ppx_under_dir sctx ~db ~dir = | Some (d : Dune_file.t) -> Memo.List.fold_left d.stanzas ~init:Libs_and_ppxs.empty ~f:(fun (acc, pps) -> function - | Dune_file.Library l -> + | Dune_file.Library.T l -> let+ lib = let open Memo.O in let+ resolve = @@ -88,7 +88,7 @@ let libs_and_ppx_under_dir sctx ~db ~dir = (Lib_info.loc info, Lib_info.name info) pps )) else acc, pps) - | Dune_file.Executables exes -> + | Dune_file.Executables.T exes -> let+ libs = let open Memo.O in let* compile_info =