Skip to content

Commit

Permalink
dialects: allow embedded dots in extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
  • Loading branch information
nojb committed May 22, 2023
1 parent 6c82fd1 commit c9b1c66
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 6 deletions.
5 changes: 3 additions & 2 deletions doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ dialect

Specify the file extension used for this dialect.

The extension string must not contain any dots and be unique in a given
The extension string must not start with a dot and be unique in a given
project (so that a given extension can be mapped back to a
corresponding dialect).
corresponding dialect). In Dune 3.8 and later, the extension string may
contain embedded dots (eg `cppo.ml`).

This field is required.

Expand Down
11 changes: 8 additions & 3 deletions src/dune_rules/dialect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ let decode =
(map
~f:(fun (loc, x) -> (loc, x, []))
(located Action.decode_dune_file))
in
and+ syntax_ver = Syntax.get_exn Stanza.syntax in
let extension =
if String.contains extension '.' then
User_error.raise ~loc [ Pp.textf "extension must not contain '.'" ];
if String.length extension > 0 && extension.[0] = '.' then
User_error.raise ~loc [ Pp.textf "extension must not start with a '.'" ];
let ver = (3, 8) in
if syntax_ver < ver && String.contains extension '.' then begin
let what = "the possibility of defining extensions with embedded dots" in
Syntax.Error.since loc Stanza.syntax ver ~what
end;
"." ^ extension
in
{ File_kind.kind; extension; preprocess; format }
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox-tests/test-cases/dialects/bad3.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
File "dune-project", line 5, characters 28-32:
5 | (implementation (extension .foo))
^^^^
Error: extension must not contain '.'
Error: extension must not start with a '.'
[1]
2 changes: 2 additions & 0 deletions test/blackbox-tests/test-cases/dialects/dots.t/cppo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let () =
print_endline {|print_endline "Hello, World"|}
11 changes: 11 additions & 0 deletions test/blackbox-tests/test-cases/dialects/dots.t/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(executable
(name main)
(modules main))

(executable
(name cppo)
(modules cppo))

(rule
(alias show)
(action (cat main.cppo.ml.ml)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(dialect
(name cppo)
(implementation
(extension cppo.ml)
(preprocess (run ./cppo.exe %{input-file})))
(interface
(extension cppo.mli)
(preprocess (run ./cppo.exe %{input-file}))))
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions test/blackbox-tests/test-cases/dialects/dots.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Test the (dialect ...) stanza inside the dune-project file.

$ { echo '(lang dune 3.7)'; cat dune-project.in; } >dune-project

$ dune build --display short
File "dune-project", line 5, characters 13-20:
5 | (extension cppo.ml)
^^^^^^^
Error: the possibility of defining extensions with embedded dots is only
available since version 3.8 of the dune language. Please update your
dune-project file to have (lang dune 3.8).
[1]

$ { echo '(lang dune 3.8)'; cat dune-project.in; } >dune-project

$ dune build --display short
ocamlc .cppo.eobjs/byte/dune__exe__Cppo.{cmi,cmti}
ocamlc .main.eobjs/byte/dune__exe__Main.{cmi,cmti}
ocamlopt .cppo.eobjs/native/dune__exe__Cppo.{cmx,o}
ocamlopt cppo.exe
cppo main.cppo.ml.ml
ocamlopt .main.eobjs/native/dune__exe__Main.{cmx,o}
ocamlopt main.exe

$ dune build @show
print_endline "Hello, World"

0 comments on commit c9b1c66

Please sign in to comment.