Skip to content

Commit b5ecda0

Browse files
committed
Further fixes for duplicate identifiers
As well as during loading, we also have to handle `includes` with potentially duplicate identifiers during `lang_of.ml`. This commit moves the 'synthetic parent' functions into path.ml and uses them both in cmti.ml and lang_of.ml
1 parent 252b33a commit b5ecda0

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

src/loader/cmti.ml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ type env = Cmi.env = {
3333

3434
let read_module_expr : (env -> Identifier.Signature.t -> Identifier.LabelParent.t -> Typedtree.module_expr -> ModuleType.expr) ref = ref (fun _ _ _ _ -> failwith "unset")
3535

36-
(* Counter for generating unique synthetic parents for include expressions.
37-
Items inside an include's module type expression need a different parent
38-
to avoid identifier conflicts with items in the enclosing signature. *)
39-
let include_parent_counter = ref 0
40-
41-
(* Create a synthetic parent identifier for items inside an include's module
42-
type expression. Uses a lowercase module name (illegal in normal OCaml)
43-
to ensure no clashes with real identifiers. *)
44-
let make_include_parent (parent : Identifier.Signature.t) : Identifier.Signature.t =
45-
incr include_parent_counter;
46-
let name = Printf.sprintf "include%d_" !include_parent_counter in
47-
(Identifier.Mk.module_ (parent, Odoc_model.Names.ModuleName.make_std name) :> Identifier.Signature.t)
48-
4936
let opt_map f = function
5037
| None -> None
5138
| Some x -> Some (f x)
@@ -794,7 +781,7 @@ and read_include env parent incl =
794781
identifier conflicts with items in the enclosing signature. Items inside
795782
the include expression (like TypeSubstitutions) will get identifiers under
796783
this synthetic parent, which won't clash with the real parent's items. *)
797-
let include_parent = make_include_parent parent in
784+
let include_parent = Identifier.fresh_include_parent parent in
798785
let include_container = (include_parent :> Identifier.LabelParent.t) in
799786
let expr = read_module_type env include_parent include_container incl.incl_mod in
800787
let umty = Odoc_model.Lang.umty_of_mty expr in

src/model/paths.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,19 @@ module Identifier = struct
624624
`SourceLocationInternal (p, n))
625625
end
626626

627+
(* Counter for generating unique synthetic parents for include expressions.
628+
Items inside an include's module type expression need a different parent
629+
to avoid identifier conflicts with items in the enclosing signature. *)
630+
let include_parent_counter = ref 0
631+
632+
(* Create a synthetic parent identifier for items inside an include's module
633+
type expression. Uses a lowercase module name (illegal in normal OCaml)
634+
to ensure no clashes with real identifiers. *)
635+
let fresh_include_parent (parent : Signature.t) : Signature.t =
636+
incr include_parent_counter;
637+
let name = Printf.sprintf "include%d_" !include_parent_counter in
638+
(Mk.module_ (parent, ModuleName.make_std name) :> Signature.t)
639+
627640
module Hashtbl = struct
628641
module Any = Hashtbl.Make (Any)
629642
module ContainerPage = Hashtbl.Make (ContainerPage)

src/model/paths.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ module Identifier : sig
345345
SourcePage.t * LocalName.t ->
346346
[> `SourceLocationInternal of SourcePage.t * LocalName.t ] id
347347
end
348+
349+
(** Create a synthetic parent identifier for items inside an include's
350+
module type expression. Uses a lowercase module name (illegal in normal
351+
OCaml) to ensure no clashes with real identifiers. Each call returns a
352+
fresh identifier. *)
353+
val fresh_include_parent : Signature.t -> Signature.t
348354
end
349355

350356
(** Normal OCaml paths (i.e. the ones present in types) *)

src/xref2/lang_of.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@ and include_decl :
649649
(* Don't start shadowing within any signatures *)
650650
match d with
651651
| Alias p -> Alias (Path.module_ map p)
652-
| ModuleType mty -> ModuleType (u_module_type_expr map identifier mty)
652+
| ModuleType mty ->
653+
let include_parent = Identifier.fresh_include_parent identifier in
654+
ModuleType (u_module_type_expr map include_parent mty)
653655

654656
and include_ parent map i =
655657
let open Component.Include in

0 commit comments

Comments
 (0)