Skip to content

Commit 6192cf0

Browse files
committed
Warn on unhandled internal tags
It was possible to attach those tags to anything and would be ignored if unecessary. This emits a warning in this case.
1 parent 684b587 commit 6192cf0

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

src/model/error.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ let raised_warnings = ref []
7979
let raise_warnings' warnings =
8080
raised_warnings := List.rev_append warnings !raised_warnings
8181

82+
let raise_warning t = raised_warnings := t :: !raised_warnings
83+
8284
let raise_warnings with_warnings =
8385
raise_warnings' with_warnings.warnings;
8486
with_warnings.value

src/model/error.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ val accumulate_warnings : (warning_accumulator -> 'a) -> 'a with_warnings
3333

3434
val warning : warning_accumulator -> t -> unit
3535

36+
val raise_warning : t -> unit
37+
(** Raise a warning that need to be caught with [catch_warnings]. *)
38+
3639
val raise_warnings : 'a with_warnings -> 'a
3740
(** Accumulate warnings into a global variable. See [catch_warnings]. *)
3841

src/model/semantics.ml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,26 @@ type _ handle_internal_tags =
1515
: [ `Dot of Paths.Path.Module.t * string ] option handle_internal_tags
1616
| Expect_none : unit handle_internal_tags
1717

18+
let describe_internal_tag = function
19+
| `Canonical _ -> "@canonical"
20+
| `Inline -> "@inline"
21+
| `Open -> "@open"
22+
| `Closed -> "@closed"
23+
24+
let unexpected_tag { Location.value; location } =
25+
Error.raise_warning
26+
@@ Error.make "Unexpected tag '%s' at this location."
27+
(describe_internal_tag value)
28+
location
29+
1830
let rec find_tag f = function
1931
| [] -> None
2032
| hd :: tl -> (
21-
match f hd.Location.value with Some _ as x -> x | None -> find_tag f tl)
33+
match f hd.Location.value with
34+
| Some _ as x -> x
35+
| None ->
36+
unexpected_tag hd;
37+
find_tag f tl)
2238

2339
let handle_internal_tags (type a) tags : a handle_internal_tags -> a = function
2440
| Expect_status -> (
@@ -31,7 +47,10 @@ let handle_internal_tags (type a) tags : a handle_internal_tags -> a = function
3147
| None -> `Default)
3248
| Expect_canonical ->
3349
find_tag (function `Canonical (`Dot _ as p) -> Some p | _ -> None) tags
34-
| Expect_none -> ()
50+
| Expect_none ->
51+
(* Will raise warnings. *)
52+
ignore (find_tag (fun _ -> None) tags);
53+
()
3554

3655
(* Errors *)
3756
let invalid_raw_markup_target : string -> Location.span -> Error.t =

test/model/test.ml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6981,32 +6981,27 @@ let%expect_test _ =
69816981

69826982
let canonical_something =
69836983
test "@canonical Foo";
6984-
[%expect
6985-
{|
6984+
[%expect {|
69866985
{ "value": [], "warnings": [] } |}]
69876986

69886987
let canonical_module =
69896988
test "@canonical module-Foo";
6990-
[%expect
6991-
{|
6989+
[%expect {|
69926990
{ "value": [], "warnings": [] } |}]
69936991

69946992
let canonical_path =
69956993
test "@canonical Foo.Bar";
6996-
[%expect
6997-
{|
6994+
[%expect {|
69986995
{ "value": [], "warnings": [] } |}]
69996996

70006997
let canonical_val =
70016998
test "@canonical val-foo";
7002-
[%expect
7003-
{|
6999+
[%expect {|
70047000
{ "value": [], "warnings": [] } |}]
70057001

70067002
let canonical_bad_parent =
70077003
test "@canonical bar.page-foo";
7008-
[%expect
7009-
{|
7004+
[%expect {|
70107005
{ "value": [], "warnings": [] } |}]
70117006

70127007
let canonical_empty_component =

test/xref2/canonical_unit.t/run.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ top-comment.
44
The module Test__X is expected to be referenced through Test.X.
55

66
$ compile test__x.mli test.ml
7+
File "test.ml", line 15, characters 6-24:
8+
Unexpected tag '@canonical' at this location.
79

810
Test__x has a 'canonical' field:
911

test/xref2/module_preamble.t/run.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and that "hidden" modules (eg. `A__b`, rendered to `html/A__b`) are not rendered
1313

1414
$ odoc compile --pkg test -o a__b.odoc -I . a__b.cmti
1515
$ odoc compile --pkg test -o a.odoc -I . a.cmti
16+
File "a.mli", line 4, characters 4-17:
17+
Unexpected tag '@canonical' at this location.
1618

1719
$ odoc link -I . a__b.odoc
1820
$ odoc link -I . a.odoc

test/xref2/ocaml_stdlib.t/run.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Imitate the way the stdlib is built and how its documentation should be built.
99
[1]
1010

1111
$ odoc compile --pkg ocaml -o main.odoc main.cmti -I .
12+
File "main.mli", line 3, characters 4-17:
13+
Unexpected tag '@canonical' at this location.
1214
$ odoc compile --pkg ocaml -o main__x.odoc main__x.cmti -I .
1315

1416
$ odoc html --indent -o html main__x.odoc -I .

0 commit comments

Comments
 (0)