diff --git a/CHANGES.md b/CHANGES.md index 60712d0..d552d40 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,13 @@ +## 1.4.0 (2026-11-23) + +- An `.mlx` unit is parsed as mlx parsed it, from the AST its preprocessor + wrote, so attribute rules and `[@litany.allow]` work there. Any `.mlx` that + held an attribute was reported degraded ("editable source does not parse"), + since OCaml's own parser can't read JSX. A unit file names that AST in a + `parsed-source` field. +- An `.mlx` in a native library with no ppx is linted; it was skipped as + missing its artifact. + ## 1.3.0 (2026-11-22) - hypha-litany installs its documentation for agents: each page of the diff --git a/agents/pages/guide/build-integration.md b/agents/pages/guide/build-integration.md index d1a78bc..642a27a 100644 --- a/agents/pages/guide/build-integration.md +++ b/agents/pages/guide/build-integration.md @@ -84,9 +84,11 @@ The file is [canonical s-expressions](https://en.wikipedia.org/wiki/Canonical_S- 2. `(complete false)`, when it isn't your whole project. 3. `(cmi-dirs ...)`, where to find the `.cmi` files of dependencies. 4. One `(unit ...)` per unit, with fields in the order `source cmt cmti - pp-source library public kind`. `source` and one of `cmt` or `cmti` are - required; `pp-source` is the preprocessed file, where there is one; `public` - is `true` or `false`; `kind` is `lib`, `exe` or `test`. + pp-source parsed-source intf-source library public kind`. `source` and one + of `cmt` or `cmti` are required; `pp-source` is the preprocessed file, where + there is one; `parsed-source` is the AST a dialect's preprocessor wrote for + a source OCaml can't parse, such as an `.mlx`; `intf-source` is the unit's + `.mli`; `public` is `true` or `false`; `kind` is `lib`, `exe` or `test`. Anything else is refused. A wrong or stale entry costs a skip, never a finding. diff --git a/bin/cli_units.ml b/bin/cli_units.ml index 3196d11..db24f71 100644 --- a/bin/cli_units.ml +++ b/bin/cli_units.ml @@ -70,6 +70,7 @@ let dump roster = Option.iter (field "cmt") (E.cmt entry); Option.iter (field "cmti") (E.cmti entry); Option.iter (field "pp-source") (E.preprocessed_source entry); + Option.iter (field "parsed-source") (E.parsed_source entry); Option.iter (field "intf-source") (E.interface_source entry); Option.iter (field "library") (E.library entry); (match E.visibility entry with diff --git a/docs/src/content/docs/guide/build-integration.md b/docs/src/content/docs/guide/build-integration.md index 17e54bc..56560f5 100644 --- a/docs/src/content/docs/guide/build-integration.md +++ b/docs/src/content/docs/guide/build-integration.md @@ -85,9 +85,11 @@ The file is [canonical s-expressions](https://en.wikipedia.org/wiki/Canonical_S- 2. `(complete false)`, when it isn't your whole project. 3. `(cmi-dirs ...)`, where to find the `.cmi` files of dependencies. 4. One `(unit ...)` per unit, with fields in the order `source cmt cmti - pp-source library public kind`. `source` and one of `cmt` or `cmti` are - required; `pp-source` is the preprocessed file, where there is one; `public` - is `true` or `false`; `kind` is `lib`, `exe` or `test`. + pp-source parsed-source intf-source library public kind`. `source` and one + of `cmt` or `cmti` are required; `pp-source` is the preprocessed file, where + there is one; `parsed-source` is the AST a dialect's preprocessor wrote for + a source OCaml can't parse, such as an `.mlx`; `intf-source` is the unit's + `.mli`; `public` is `true` or `false`; `kind` is `lib`, `exe` or `test`. Anything else is refused. A wrong or stale entry costs a skip, never a finding. diff --git a/lib/adapter.ml b/lib/adapter.ml index d3b12cf..74d6e50 100644 --- a/lib/adapter.ml +++ b/lib/adapter.ml @@ -549,7 +549,8 @@ module Unit_file = struct (fun field -> let o, key, vo, value = unit_field field in match key with - | "source" | "cmt" | "cmti" | "pp-source" | "intf-source" | "library" -> + | "source" | "cmt" | "cmti" | "pp-source" | "parsed-source" + | "intf-source" | "library" -> store key o value | "public" -> ( match value with @@ -580,8 +581,8 @@ module Unit_file = struct in let kind = Option.bind (get "kind") Roster.kind_of_string in Roster.Entry.v ~source ?cmt ?cmti ?preprocessed_source:(get "pp-source") - ?interface_source:(get "intf-source") ?library:(get "library") ~visibility - ?kind () + ?parsed_source:(get "parsed-source") ?interface_source:(get "intf-source") + ?library:(get "library") ~visibility ?kind () let decode bytes = match @@ -676,6 +677,7 @@ module Unit_file = struct Option.iter (add_field b "cmt") (E.cmt e); Option.iter (add_field b "cmti") (E.cmti e); Option.iter (add_field b "pp-source") (E.preprocessed_source e); + Option.iter (add_field b "parsed-source") (E.parsed_source e); Option.iter (add_field b "intf-source") (E.interface_source e); Option.iter (add_field b "library") (E.library e); (match E.visibility e with @@ -1072,6 +1074,7 @@ module Dune = struct let candidates = (dir // (leaf ^ ".pp.ml")) :: (dir // (leaf ^ ".mlx.pp.ml")) + :: (dir // (leaf ^ ".mlx.ml")) :: (if compiled_by_melange then let copies = dir // ".melange_src" in @@ -1085,6 +1088,17 @@ module Dune = struct in List.find_opt exists candidates in + (* mlx-pp writes its parse of the .mlx beside the copy the + compiler's input was made from, whether or not a ppx ran on it + after. *) + let parsed_source = + match preprocessed_source with + | Some pp when Filename.check_suffix source ".mlx" -> + let leaf = Filename.basename source in + let parsed = Filename.dirname pp // (leaf ^ ".ml") in + if exists parsed then Some parsed else None + | Some _ | None -> None + in let library, visibility, kind = match stanza with | None -> (None, Roster.Unknown, None) @@ -1107,7 +1121,7 @@ module Dune = struct in Some (Roster.Entry.v ~source ?cmt ?cmti ?preprocessed_source - ?interface_source ?library ~visibility ?kind ())) + ?parsed_source ?interface_source ?library ~visibility ?kind ())) units in let local_cmi_dirs = diff --git a/lib/adapter.mli b/lib/adapter.mli index 2120c36..c7d159b 100644 --- a/lib/adapter.mli +++ b/lib/adapter.mli @@ -33,8 +33,8 @@ {v (unit (source lib/foo.ml) (cmt _build/default/lib/.foo.objs/byte/foo.cmt) - (cmti ...) (pp-source ...) (intf-source lib/foo.mli) (library foo) - (public true) (kind lib)) + (cmti ...) (pp-source ...) (parsed-source ...) + (intf-source lib/foo.mli) (library foo) (public true) (kind lib)) v} Only [source] and one of [cmt]/[cmti] are required in a [unit] form; unknown diff --git a/lib/roster.ml b/lib/roster.ml index b9bf44b..5d46c7b 100644 --- a/lib/roster.ml +++ b/lib/roster.ml @@ -37,19 +37,21 @@ module Entry = struct cmt : string option; cmti : string option; preprocessed_source : string option; + parsed_source : string option; interface_source : string option; library : string option; visibility : visibility; kind : kind option; } - let v ~source ?cmt ?cmti ?preprocessed_source ?interface_source ?library - ?(visibility = Unknown) ?kind () = + let v ~source ?cmt ?cmti ?preprocessed_source ?parsed_source ?interface_source + ?library ?(visibility = Unknown) ?kind () = { source; cmt; cmti; preprocessed_source; + parsed_source; interface_source; library; visibility; @@ -60,6 +62,7 @@ module Entry = struct let cmt e = e.cmt let cmti e = e.cmti let preprocessed_source e = e.preprocessed_source + let parsed_source e = e.parsed_source let interface_source e = e.interface_source let library e = e.library let visibility e = e.visibility @@ -72,11 +75,11 @@ module Entry = struct in let str = Format.pp_print_string in Format.fprintf ppf - "@[entry %s@ cmt %a@ cmti %a@ preprocessed %a@ interface %a@ \ - library %a@ visibility %a@ kind %a@]" + "@[entry %s@ cmt %a@ cmti %a@ preprocessed %a@ parsed %a@ \ + interface %a@ library %a@ visibility %a@ kind %a@]" e.source (opt str) e.cmt (opt str) e.cmti (opt str) e.preprocessed_source - (opt str) e.interface_source (opt str) e.library pp_visibility - e.visibility (opt pp_kind) e.kind + (opt str) e.parsed_source (opt str) e.interface_source (opt str) e.library + pp_visibility e.visibility (opt pp_kind) e.kind (* Both metadata fields project rules need; see [project_capable]. *) let has_project_metadata e = Option.is_some e.library && Option.is_some e.kind diff --git a/lib/roster.mli b/lib/roster.mli index 1112b89..9e2ad47 100644 --- a/lib/roster.mli +++ b/lib/roster.mli @@ -61,6 +61,7 @@ module Entry : sig ?cmt:string -> ?cmti:string -> ?preprocessed_source:string -> + ?parsed_source:string -> ?interface_source:string -> ?library:string -> ?visibility:visibility -> @@ -79,6 +80,10 @@ module Entry : sig - [preprocessed_source]: the built preprocessed source ([.pp.ml]) for units the compiler read from a built pp file — the anchor of the Derived witness. Defaults to none, meaning the unit is expected to join Direct. + - [parsed_source]: the editable source as its dialect's preprocessor + parsed it — the marshalled AST mlx-pp writes for an [.mlx], which the + OCaml parser cannot read. [Unit.parsetree] reads it in place of parsing + [source]. Defaults to none: [source] is OCaml. - [interface_source]: the unit's paired interface source ([.mli]) — the second editable file of a unit whose [source] is an implementation. Text rules ([Rule.source]) run over it too; nothing else reads it. Defaults @@ -104,6 +109,10 @@ module Entry : sig (** [preprocessed_source e] is the built preprocessed source path, if the compiler read one. *) + val parsed_source : t -> string option + (** [parsed_source e] is the path of the AST the source's dialect parsed it + into, if named. *) + val interface_source : t -> string option (** [interface_source e] is the paired interface source path ([.mli]), if the adapter named one. Read once at load ([Unit.load]) for the text lane; diff --git a/lib/unit.ml b/lib/unit.ml index 196dcaa..c045eee 100644 --- a/lib/unit.ml +++ b/lib/unit.ml @@ -368,6 +368,7 @@ type t = { kind : kind option; implementation : Typedtree.structure; cmti : string option; + parsed_source : string option; scope : Naming.Scope.t; intra : Shape.Uid.t -> Shape.Uid.t list; mutable interface : Typedtree.signature option demand; @@ -455,6 +456,8 @@ let load ~resolver ~build_current entry = kind = Roster.Entry.kind entry; implementation; cmti = Roster.Entry.cmti entry; + parsed_source = + Roster.Entry.parsed_source entry; scope = Naming.Scope.v ~resolver ~intra ~local:implementation.Typedtree.str_type; @@ -576,16 +579,35 @@ let interface u = u.interface <- Loaded v; v +(* A dialect's preprocessor writes its parse as [Pparse.read_ast] reads it: + the magic, the name of the file it read, then the structure. Read by + hand, because [Pparse.read_ast] also sets [Location.input_name]. *) +let read_parsed_source path = + let magic = Config.ast_impl_magic_number in + match + In_channel.with_open_bin path (fun ic -> + if String.equal (really_input_string ic (String.length magic)) magic + then + let (_ : string) = input_value ic in + Some (input_value ic : Parsetree.structure) + else None) + with + | tree -> tree + | exception (Sys_error _ | End_of_file | Failure _) -> None + let parsetree u = match u.parsetree with | Loaded v -> v | Not_loaded -> let v = - let lexbuf = Lexing.from_string (Source.contents u.source) in - Lexing.set_filename lexbuf u.path; - match Parse.implementation lexbuf with - | tree -> Some tree - | exception (Syntaxerr.Error _ | Lexer.Error _) -> None + match u.parsed_source with + | Some path -> read_parsed_source path + | None -> ( + let lexbuf = Lexing.from_string (Source.contents u.source) in + Lexing.set_filename lexbuf u.path; + match Parse.implementation lexbuf with + | tree -> Some tree + | exception (Syntaxerr.Error _ | Lexer.Error _) -> None) in u.parsetree <- Loaded v; v diff --git a/lib/unit.mli b/lib/unit.mli index 0120ebc..43b4fd1 100644 --- a/lib/unit.mli +++ b/lib/unit.mli @@ -306,10 +306,12 @@ val interface : t -> Typedtree.signature option witness). *) val parsetree : t -> Parsetree.structure option -(** [parsetree u] is the engine's own pre-PPX parse of {!source}, or [None] when - the editable source does not parse as OCaml (cppo sources and kin — such - units run typed rules only, with a summary note that attribute rules and - attribute suppression are unavailable). Demand-gated and cached; the same +(** [parsetree u] is the pre-PPX parse of {!source}, or [None] when there is + none (cppo sources and kin — such units run typed rules only, with a summary + note that attribute rules and attribute suppression are unavailable). It is + the engine's own parse of the source as OCaml, or, when the entry names a + {!Roster.Entry.parsed_source}, the AST read from that file: [None] when it + is unreadable or not this compiler's AST. Demand-gated and cached; the same parse serves attribute rules, suppression matching, and the emit contract's corroboration. *) diff --git a/test/cli/dune b/test/cli/dune index 14a9ab2..3e3d73f 100644 --- a/test/cli/dune +++ b/test/cli/dune @@ -143,3 +143,10 @@ (deps %{bin:litany} (source_tree %{project_root}/examples))) + +; An mlx unit read through its dialect's own parse: a nested project whose +; mlx dialect runs the stand-in preprocessor in mlx_pp/. + +(cram + (applies_to mlx) + (deps %{bin:litany} mlx_pp/mlx_pp.exe)) diff --git a/test/cli/mlx.t/run.t b/test/cli/mlx.t/run.t new file mode 100644 index 0000000..78922af --- /dev/null +++ b/test/cli/mlx.t/run.t @@ -0,0 +1,49 @@ +An implementation in mlx's dialect is its .mlx, which the OCaml parser +cannot read: litany reads it as the dialect's preprocessor parsed it, so +attribute suppression and the other parse-fed checks hold there as in a +.ml. The fixture is a real nested dune project whose mlx dialect runs a +stand-in for mlx-pp, which reads as an element. + + $ unset GITHUB_ACTIONS + $ MLX_PP=$PWD/../mlx_pp/mlx_pp.exe + $ mkdir proj && cd proj + $ cat > dune-project < (lang dune 3.20) + > (dialect + > (name mlx) + > (implementation + > (extension mlx) + > (preprocess (run $MLX_PP %{input-file})))) + > EOP + $ cat > dune <<'EOP' + > (library (name mlxlib)) + > EOP + $ cat > view.mlx <<'EOP' + > let element = + > let is_empty xs = List.length xs = 0 + > let is_blank xs = (List.length xs = 0) [@litany.allow "needless-list-length: shown as written"] + > EOP + $ env -u INSIDE_DUNE dune build --root . @check 2>&1 + +The OCaml parser refuses the source; the build does not. + + $ ocamlc -stop-after parsing -impl view.mlx 2>&1 | head -1 + File "view.mlx", line 1, characters 14-15: + +The unit is linted, not degraded: the unsuppressed finding is reported, +the suppressed one is not. + + $ env -u INSIDE_DUNE litany check + File "view.mlx", line 2, characters 18-36: + 2 | let is_empty xs = List.length xs = 0 + ^^^^^^^^^^^^^^^^^^ + Warning 0 [needless-list-length]: comparison through List.length is a needless emptiness test + + 30 rules selected · 2 units · 1 finding · 0 skipped · 1 facts-only · 1 suppressed + [1] + +The roster names the AST mlx-pp wrote beside the build's copy of the +source, and a unit file carries it. + + $ env -u INSIDE_DUNE litany units --dump | grep -o '(parsed-source [^)]*)' + (parsed-source _build/default/view.mlx.ml) diff --git a/test/cli/mlx_pp/dune b/test/cli/mlx_pp/dune new file mode 100644 index 0000000..62472aa --- /dev/null +++ b/test/cli/mlx_pp/dune @@ -0,0 +1,7 @@ +; A stand-in for mlx's preprocessor, so the mlx cram runs without mlx +; installed: OCaml with one form the OCaml parser refuses, written out as +; the marshalled AST mlx-pp hands dune. TEST-ONLY, never installed. + +(executable + (name mlx_pp) + (libraries compiler-libs.common)) diff --git a/test/cli/mlx_pp/mlx_pp.ml b/test/cli/mlx_pp/mlx_pp.ml new file mode 100644 index 0000000..ffd8c82 --- /dev/null +++ b/test/cli/mlx_pp/mlx_pp.ml @@ -0,0 +1,31 @@ +(* [] stands for an element: it is read as [()] padded to the same + six bytes, so every position in the AST is the .mlx's own, as mlx-pp's + are. The output is what [Pparse.read_ast] reads: the magic, the name of + the file read, then the structure. *) + +let element = "" +let unit_padded = "() " + +let read_elements source = + let b = Bytes.of_string source in + let n = String.length element in + for i = 0 to String.length source - n do + if String.equal (String.sub source i n) element then + Bytes.blit_string unit_padded 0 b i n + done; + Bytes.to_string b + +let () = + match Sys.argv with + | [| _; path |] -> + let source = In_channel.with_open_bin path In_channel.input_all in + let lexbuf = Lexing.from_string (read_elements source) in + Lexing.set_filename lexbuf path; + let ast = Parse.implementation lexbuf in + Out_channel.set_binary_mode stdout true; + output_string stdout Config.ast_impl_magic_number; + output_value stdout path; + output_value stdout (ast : Parsetree.structure) + | _ -> + prerr_endline "usage: mlx_pp FILE"; + exit 2 diff --git a/test/unit/test_adapter.ml b/test/unit/test_adapter.ml index 1dd243e..8816ac7 100644 --- a/test/unit/test_adapter.ml +++ b/test/unit/test_adapter.ml @@ -223,6 +223,39 @@ let dune_tests = equal (option string) (Some "_build/default/web/.melange_src/app.mlx.pp.ml") (Roster.Entry.preprocessed_source (entry_for r "web/app.mlx"))); + test "an mlx unit names mlx-pp's parse beside the copy" (fun () -> + let dir, write = craft () in + write "web/dune" "(library (name web) (modes melange))\n"; + write "web/app.mlx" ""; + write "_build/default/web/.web.objs/melange/web__App.cmt" ""; + write "_build/default/web/.melange_src/app.mlx" ""; + write "_build/default/web/.melange_src/app.mlx.ml" ""; + write "_build/default/web/.melange_src/app.mlx.pp.ml" ""; + let r = dune_roster dir in + equal (option string) + (Some "_build/default/web/.melange_src/app.mlx.ml") + (Roster.Entry.parsed_source (entry_for r "web/app.mlx"))); + test "a native mlx unit with no ppx rides mlx-pp's parse" (fun () -> + let dir, write = craft () in + write "lib/dune" "(library (name inv))\n"; + write "lib/view.mlx" ""; + write "_build/default/lib/.inv.objs/byte/inv__View.cmt" ""; + write "_build/default/lib/view.mlx.ml" ""; + let r = dune_roster dir in + let e = entry_for r "lib/view.mlx" in + equal (option string) (Some "_build/default/lib/view.mlx.ml") + (Roster.Entry.preprocessed_source e); + equal (option string) (Some "_build/default/lib/view.mlx.ml") + (Roster.Entry.parsed_source e)); + test "an OCaml unit names no parse" (fun () -> + let dir, write = craft () in + write "lib/dune" "(library (name inv))\n"; + write "lib/store.ml" ""; + write "_build/default/lib/.inv.objs/byte/inv__Store.cmt" ""; + write "_build/default/lib/store.pp.ml" ""; + let r = dune_roster dir in + equal (option string) None + (Roster.Entry.parsed_source (entry_for r "lib/store.ml"))); test "a native unit beside a melange copy names none" (fun () -> let dir, write = craft () in write "web/dune" "(library (name web) (modes native melange))\n"; diff --git a/test/unit/test_roster.ml b/test/unit/test_roster.ml index c4a1865..02686d6 100644 --- a/test/unit/test_roster.ml +++ b/test/unit/test_roster.ml @@ -32,11 +32,14 @@ let entries = test "paths are kept verbatim" (fun () -> let e = Roster.Entry.v ~source:"lib/a.ml" ~cmt:"_build/x/a.cmt" - ~preprocessed_source:"_build/x/a.pp.ml" () + ~preprocessed_source:"_build/x/a.pp.ml" + ~parsed_source:"_build/x/a.mlx.ml" () in equal string "lib/a.ml" (Roster.Entry.source e); equal (option string) (Some "_build/x/a.pp.ml") - (Roster.Entry.preprocessed_source e)); + (Roster.Entry.preprocessed_source e); + equal (option string) (Some "_build/x/a.mlx.ml") + (Roster.Entry.parsed_source e)); test "metadata defaults to absent, visibility to Unknown" (fun () -> let e = Roster.Entry.v ~source:"a.ml" ~cmt:"a.cmt" () in equal (option string) None (Roster.Entry.library e); diff --git a/test/unit/test_unit.ml b/test/unit/test_unit.ml index 094c03d..f362bdb 100644 --- a/test/unit/test_unit.ml +++ b/test/unit/test_unit.ml @@ -369,6 +369,41 @@ let substrate_tests = is_some (Unit.parsetree u); is_true ~msg:"same value on the second call" (Unit.parsetree u == Unit.parsetree u)); + with_bytes "parsetree is the parsed source's AST when one is named" + (fun () -> + let path = Filename.temp_file "litany-test" ".mlx.ml" in + let ast = + Parse.implementation (Lexing.from_string "let a = 1\nlet b = 2") + in + Out_channel.with_open_bin path (fun oc -> + output_string oc Config.ast_impl_magic_number; + output_value oc "view.mlx"; + output_value oc (ast : Parsetree.structure)); + (path, "")) + (fun (path, _) -> + let u = + require_load + (Entry.v ~source:direct_source ~cmt:direct_cmt ~parsed_source:path + ()) + in + match Unit.parsetree u with + | None -> fail "expected the parsed source's AST" + | Some tree -> equal int 2 (List.length tree)); + with_bytes "parsetree is None when the parsed source is no AST" + (crafted "let a = 1") (fun (path, _) -> + let u = + require_load + (Entry.v ~source:direct_source ~cmt:direct_cmt ~parsed_source:path + ()) + in + is_none (Unit.parsetree u)); + test "parsetree is None when the parsed source is missing" (fun () -> + let u = + require_load + (Entry.v ~source:direct_source ~cmt:direct_cmt + ~parsed_source:"fixtures/unit/plain/no-such.mlx.ml" ()) + in + is_none (Unit.parsetree u)); ] (* [bound_expr u name] is the expression bound to the top-level variable diff --git a/test/unit/test_unit_file.ml b/test/unit/test_unit_file.ml index 50fab71..4fea2e3 100644 --- a/test/unit/test_unit_file.ml +++ b/test/unit/test_unit_file.ml @@ -32,6 +32,7 @@ let entry_repr e = Entry.cmt e, Entry.cmti e, Entry.preprocessed_source e, + Entry.parsed_source e, Entry.interface_source e, Entry.library e, Entry.visibility e, @@ -356,6 +357,7 @@ let gen_entry = and+ a = gen_path and+ b = gen_path and+ preprocessed_source = option gen_path + and+ parsed_source = option gen_path and+ interface_source = option gen_path and+ library = option gen_path and+ visibility = gen_vis @@ -366,12 +368,13 @@ let gen_entry = | `Cmti -> (None, Some a) | `Both -> (Some a, Some b) in - Entry.v ~source ?cmt ?cmti ?preprocessed_source ?interface_source ?library - ~visibility ?kind () + Entry.v ~source ?cmt ?cmti ?preprocessed_source ?parsed_source + ?interface_source ?library ~visibility ?kind () let with_source source e = Entry.v ~source ?cmt:(Entry.cmt e) ?cmti:(Entry.cmti e) ?preprocessed_source:(Entry.preprocessed_source e) + ?parsed_source:(Entry.parsed_source e) ?interface_source:(Entry.interface_source e) ?library:(Entry.library e) ~visibility:(Entry.visibility e) ?kind:(Entry.kind e) ()