diff --git a/CHANGES.md b/CHANGES.md index f8952ea1..6f62f15f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ #### Added - Handle the error-blocks syntax (#439, @jonludlam, @gpetiot) +- Fix directives not allowed in the `ocaml` mode. ### 2.3.1 diff --git a/lib/block.ml b/lib/block.ml index aa972fe4..239ef4f0 100644 --- a/lib/block.ml +++ b/lib/block.ml @@ -300,7 +300,7 @@ let guess_ocaml_kind contents = | h :: t -> let h = String.trim h in if h = "" then aux t - else if String.length h > 1 && h.[0] = '#' then `Toplevel + else if String.length h > 2 && h.[0] = '#' && h.[1] = ' ' then `Toplevel else `Code in aux contents @@ -383,7 +383,10 @@ let mk_ocaml ~loc ~config ~header ~contents ~errors = let kind = "OCaml" in match config with | { file_inc = None; part = None; env; non_det; _ } -> ( - (* TODO: why does this call guess_ocaml_kind when infer_block already did? *) + (* mk_ocaml can be invoked because an explicit "$MDX ocaml" is given. + In such case, we still want to make it an error if + a toplevel interaction is given (see the test "invalid ocaml"), + so we use guess_ocaml_kind here to do that. *) match guess_ocaml_kind contents with | `Code -> Ok (OCaml { env = Ocaml_env.mk env; non_det; errors; header }) | `Toplevel -> diff --git a/test/bin/mdx-test/expect/simple-mld/test-case.mld b/test/bin/mdx-test/expect/simple-mld/test-case.mld index e3355704..cc4b5806 100644 --- a/test/bin/mdx-test/expect/simple-mld/test-case.mld +++ b/test/bin/mdx-test/expect/simple-mld/test-case.mld @@ -67,3 +67,15 @@ Line 1, characters 15-18: Error: This expression has type string but an expression was expected of type int ]err}]} + + +{@ocaml ocaml[ + #require "astring";; + let x = Astring.strf;; +]} + + +{[ + # x;; + - : ('a, Format.formatter, unit, string) format4 -> 'a = +]}