Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(melange): move stanza definition #6775

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/dune_engine/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,6 @@ end

module Melange_syntax = struct
let name = "melange"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to leave just name here? The only consumer is Melange_stanzas.syntax, so maybe it could be moved to that module. Or even inlined in syntax definition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I could remove it but it has to stay because it's used to define the dialect for rescript.


let t =
Dune_lang.Syntax.create ~name ~desc:"support for Melange compiler"
[ ((0, 1), `Since (3, 6)) ]

let () = Extension.register_simple t (return [])
end

let explicit_extensions_map explicit_extensions =
Expand Down
2 changes: 1 addition & 1 deletion src/dune_engine/dune_project.mli
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,5 @@ val encode : t -> Dune_lang.t list
val dune_site_extension : unit Extension.t

module Melange_syntax : sig
val t : Dune_lang.Syntax.t
val name : string
end
2 changes: 1 addition & 1 deletion src/dune_rules/dir_status.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let get_include_subdirs stanzas =
let find_module_stanza stanzas =
List.find_map stanzas ~f:(fun stanza ->
match stanza with
| Melange_emit { loc; _ }
| Melange_stanzas.Emit.T { loc; _ }
| Library { buildable = { loc; _ }; _ }
| Executables { buildable = { loc; _ }; _ }
| Tests { exes = { buildable = { loc; _ }; _ }; _ } -> Some loc
Expand Down
7 changes: 1 addition & 6 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ module Mode_conf = struct
; ("native", return @@ Ocaml Native)
; ("best", return @@ Ocaml Best)
; ( "melange"
, Dune_lang.Syntax.since Dune_project.Melange_syntax.t (0, 1)
, Dune_lang.Syntax.since Melange_stanzas.syntax (0, 1)
>>> return Melange )
]

Expand Down Expand Up @@ -2274,7 +2274,6 @@ type Stanza.t +=
| Cram of Cram_stanza.t
| Generate_sites_module of Generate_sites_module.t
| Plugin of Plugin.t
| Melange_emit of Melange_stanzas.Emit.t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this approach more exhaustive from a type checking perspective? What are the advantages of the new approach?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this approach more exhaustive from a type checking perspective

I think it's equivalent. There's no exhaustiveness checking on open types no matter how you define their constructors.

What are the advantages of the new approach?

That we isolate the melange rules from the rest as much as possible. This should make it easy for us to change things without impacting the rest of the code base.


module Stanzas = struct
type t = Stanza.t list
Expand Down Expand Up @@ -2391,10 +2390,6 @@ module Stanzas = struct
, let+ () = Dune_lang.Syntax.since Section.dune_site_syntax (0, 1)
and+ t = Plugin.decode in
[ Plugin t ] )
; ( "melange.emit"
, let+ () = Dune_lang.Syntax.since Dune_project.Melange_syntax.t (0, 1)
and+ t = Melange_stanzas.Emit.decode in
[ Melange_emit t ] )
]

let () = Dune_project.Lang.register Stanza.syntax stanzas
Expand Down
1 change: 0 additions & 1 deletion src/dune_rules/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ type Stanza.t +=
| Cram of Cram_stanza.t
| Generate_sites_module of Generate_sites_module.t
| Plugin of Plugin.t
| Melange_emit of Melange_stanzas.Emit.t

val stanza_package : Stanza.t -> Package.t option

Expand Down
6 changes: 3 additions & 3 deletions src/dune_rules/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ end = struct
| true ->
let+ () = Mdx.gen_rules ~sctx ~dir ~scope ~expander mdx in
empty_none)
| Melange_emit mel ->
| Melange_stanzas.Emit.T mel ->
let+ cctx, merlin =
Melange_rules.setup_emit_cmj_rules ~dir_contents ~dir ~scope ~sctx
~expander mel
Expand Down Expand Up @@ -379,7 +379,7 @@ let under_melange_emit_target ~dir =
| None -> None
| Some stanzas ->
List.find_map stanzas.stanzas ~f:(function
| Melange_emit mel ->
| Melange_stanzas.Emit.T mel ->
let target_dir = Path.Build.relative parent mel.target in
Option.some_if (Path.Build.equal target_dir dir) mel
| _ -> None)
Expand Down Expand Up @@ -505,7 +505,7 @@ let gen_rules ~sctx ~dir components : Build_config.gen_rules_result Memo.t =
| None -> subdirs
| Some stanzas ->
List.filter_map stanzas.stanzas ~f:(function
| Melange_emit mel -> Some mel.target
| Melange_stanzas.Emit.T mel -> Some mel.target
| _ -> None)
|> String.Set.of_list |> String.Set.union subdirs
in
Expand Down
11 changes: 11 additions & 0 deletions src/dune_rules/melange_stanzas.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Emit = struct
; javascript_extension : string
}

type Stanza.t += T of t

let decode_lib =
let+ loc = loc
and+ t =
Expand Down Expand Up @@ -125,3 +127,12 @@ module Emit = struct
; modules_without_implementation
})
end

let syntax =
Dune_lang.Syntax.create ~name:Dune_project.Melange_syntax.name
~desc:"support for Melange compiler"
[ ((0, 1), `Since (3, 6)) ]

let () =
Dune_project.Extension.register_simple syntax
(return [ ("melange.emit", Emit.decode >>| fun x -> [ Emit.T x ]) ])
4 changes: 4 additions & 0 deletions src/dune_rules/melange_stanzas.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ module Emit : sig
; javascript_extension : string
}

type Stanza.t += T of t

val decode : t Dune_lang.Decoder.t
end

val syntax : Syntax.t
2 changes: 1 addition & 1 deletion src/dune_rules/ml_sources.ml
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ let modules_of_stanzas dune_file ~dir ~scope ~lookup_vlib ~modules =
Modules_group.relocate_alias_module modules ~src_dir
in
Memo.return (`Executables (exes, modules, obj_dir))
| Melange_emit mel ->
| Melange_stanzas.Emit.T mel ->
let modules =
let modules =
Modules_field_evaluator.eval ~modules ~stanza_loc:mel.loc
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/ocaml_flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module Spec = struct
and+ native = field_oslu "ocamlopt_flags"
and+ melange =
field_oslu
~check:(Dune_lang.Syntax.since Dune_project.Melange_syntax.t (0, 1))
~check:(Dune_lang.Syntax.since Melange_stanzas.syntax (0, 1))
"melange.compile_flags"
in
let specific = Lib_mode.Map.make ~byte ~native ~melange in
Expand Down