Skip to content

Commit 96c772e

Browse files
jonludlamclaude
andcommitted
Represent the fragment root as a fixed local module
The root of the signature that a with constraint changes is now a local module identifier with a fixed number, so the parent type no longer needs a FragmentRoot constructor that only component paths can hold. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e575287 commit 96c772e

8 files changed

Lines changed: 32 additions & 22 deletions

File tree

‎src/model/paths.ml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ module Path = struct
635635
(global_parent, na, canon) Paths_types.Resolved_path.module_ = function
636636
| `Module m -> m
637637
| `ModuleType (_, `Na _) -> .
638-
| `FragmentRoot (`Na _) -> .
639638

640639
type ra = (global_parent, na, canon) Paths_types.Resolved_path.any
641640

@@ -728,7 +727,6 @@ module Path = struct
728727
| `ModuleType (mt, _) ->
729728
is_resolved_hidden_gen ~weak_canonical_test
730729
(mt :> (local_parent, 'pty) gra)
731-
| `FragmentRoot _ -> false
732730

733731
and is_path_hidden_gen : 'pty. (local_parent, 'pty) gpa -> bool = function
734732
| `Resolved r -> is_resolved_hidden_gen ~weak_canonical_test:false r

‎src/model/paths_types.ml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ end =
325325
and Resolved_path : sig
326326
type ('p, 'pty, 'canon) parent =
327327
[ `Module of ('p, 'pty, 'canon) module_
328-
| `ModuleType of ('p, 'pty, 'canon) module_type * 'pty
329-
| `FragmentRoot of 'pty ]
328+
| `ModuleType of ('p, 'pty, 'canon) module_type * 'pty ]
330329
constraint 'canon = < m : 'cm ; mt : 'cmt ; t : 'ct >
331330

332331
and ('p, 'pty, 'canon) module_ =

‎src/xref2/component.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,6 @@ module Fmt = struct
13701370
| `ModuleType (m, _) ->
13711371
if c.short_paths then resolved_module_type_path c ppf m
13721372
else Format.fprintf ppf ">>%a<<" (resolved_module_type_path c) m
1373-
| `FragmentRoot _ -> Format.fprintf ppf "FragmentRoot"
13741373

13751374
and type_path : config -> Format.formatter -> Cpath.type_ -> unit =
13761375
fun c ppf p ->

‎src/xref2/cpath.ml‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ end
8585

8686
include Cpath
8787

88+
(* The parent that stands for the root of the signature a [with] constraint
89+
changes, while its fragments are resolved. *)
90+
let fragment_root : Resolved.parent =
91+
`Module (`Identifier (Ident.fragment_root :> Id.module_))
92+
93+
let is_fragment_root : Resolved.parent -> bool = function
94+
| `Module (`Identifier (`Module (`Local i, _))) ->
95+
let (`Module (`Local j, _)) = Ident.fragment_root in
96+
i = j
97+
| _ -> false
98+
8899
let rec is_resolved_module_substituted : Resolved.module_ -> bool = function
89100
| `Substituted _ -> true
90101
| `Identifier _ -> false
@@ -97,7 +108,6 @@ let rec is_resolved_module_substituted : Resolved.module_ -> bool = function
97108
and is_resolved_parent_substituted = function
98109
| `Module m -> is_resolved_module_substituted m
99110
| `ModuleType (m, _) -> is_resolved_module_type_substituted m
100-
| `FragmentRoot _ -> false
101111

102112
and is_resolved_module_type_substituted : Resolved.module_type -> bool =
103113
function
@@ -265,8 +275,9 @@ and unresolve_resolved_module_type_path : Resolved.module_type -> module_type =
265275
| `OpaqueModuleType m -> unresolve_resolved_module_type_path m
266276

267277
and unresolve_resolved_parent_path : Resolved.parent -> module_ = function
278+
| p when is_fragment_root p -> assert false
268279
| `Module m -> unresolve_resolved_module_path m
269-
| `FragmentRoot _ | `ModuleType _ -> assert false
280+
| `ModuleType _ -> assert false
270281

271282
and unresolve_resolved_type_path : Resolved.type_ -> type_ = function
272283
| (`Identifier _ | `CoreType _) as p -> `Resolved p

‎src/xref2/ident.ml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ let class_of_type : type_ -> [ `Class of [ `Local of int ] * TypeName.t ] =
5454
function
5555
| `Type (l, n) -> `Class (l, n)
5656

57+
(* The root of the signature that a [with] constraint changes. It is a fixed
58+
local, as fresh locals start from 0. *)
59+
let fragment_root : module_ =
60+
`Module (`Local (-1), ModuleName.make_std "FragmentRoot")
61+
5762
let fresh_int () =
5863
let n = !counter in
5964
incr counter;

‎src/xref2/lang_of.ml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ module Path = struct
151151
and resolved_module_inner map (p : Cpath.Resolved.module_) :
152152
Odoc_model.Paths.Path.Resolved.Module.t =
153153
match p with
154+
| `Identifier _ when Cpath.is_fragment_root (`Module p) -> (
155+
(* A module type root fails as a module type parent did before. *)
156+
match map.fragment_root with
157+
| Some (`Module m) -> resolved_module map m
158+
| Some (`ModuleType _) | None -> failwith "Invalid")
154159
| `Identifier id -> (
155160
match Cpath.Id.module_ id with
156161
| Global g -> `Identifier g
@@ -173,11 +178,6 @@ module Path = struct
173178
match p with
174179
| `Module m -> resolved_module map m
175180
| `ModuleType _ -> failwith "Invalid"
176-
| `FragmentRoot _ -> (
177-
match map.fragment_root with
178-
| Some (`Module m) -> resolved_parent map (`Module m)
179-
| Some (`ModuleType mt) -> resolved_parent map (`ModuleType (mt, `U))
180-
| None -> failwith "Invalid")
181181

182182
and resolved_module_type map (p : Cpath.Resolved.module_type) :
183183
Odoc_model.Paths.Path.Resolved.ModuleType.t =

‎src/xref2/subst.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ and resolved_parent_path s = function
263263
| Not_replaced p1 -> p1
264264
in
265265
`ModuleType (p, pty)
266-
| `FragmentRoot _ as x -> x
267266

268267
and module_path : t -> Cpath.module_ -> Cpath.module_ =
269268
fun s p ->

‎src/xref2/tools.ml‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ and lookup_parent :
634634
fun env' parent' ->
635635
let lookup env parent =
636636
match parent with
637+
| p when Cpath.is_fragment_root p ->
638+
Env.lookup_fragment_root env
639+
|> of_option ~error:(`Parent `Fragment_root)
640+
>>= fun (_, sg) -> Ok (sg, prefix_substitution parent sg)
637641
| `Module p ->
638642
lookup_module env p |> map_error (fun e -> `Parent (`Parent_module e))
639643
>>= fun m ->
@@ -650,10 +654,6 @@ and lookup_parent :
650654
|> map_error (fun e -> `Parent (`Parent_sig e))
651655
>>= assert_not_functor
652656
>>= fun sg -> Ok (sg, prefix_substitution parent sg)
653-
| `FragmentRoot _ ->
654-
Env.lookup_fragment_root env
655-
|> of_option ~error:(`Parent `Fragment_root)
656-
>>= fun (_, sg) -> Ok (sg, prefix_substitution parent sg)
657657
in
658658
LookupParentMemo.memoize lookup env' parent'
659659

@@ -1292,7 +1292,6 @@ and reresolve_parent : Env.t -> Cpath.Resolved.parent -> Cpath.Resolved.parent =
12921292
match path with
12931293
| `Module m -> `Module (reresolve_module env m)
12941294
| `ModuleType (mty, x) -> `ModuleType (reresolve_module_type env mty, x)
1295-
| `FragmentRoot _ -> path
12961295

12971296
(* *)
12981297
and module_type_expr_of_module_decl :
@@ -1854,10 +1853,10 @@ and find_external_parent_path :
18541853
fun p ->
18551854
let open Odoc_utils.OptionMonad in
18561855
match p with
1856+
| p when Cpath.is_fragment_root p -> None
18571857
| `Module m -> find_external_module_path m >>= fun m -> Some (`Module m)
18581858
| `ModuleType (m, x) ->
18591859
find_external_module_type_path m >>= fun m -> Some (`ModuleType (m, x))
1860-
| `FragmentRoot _ -> None
18611860

18621861
and fixup_module_cfrag (f : Cfrag.resolved_module) : Cfrag.resolved_module =
18631862
match f with
@@ -1925,8 +1924,8 @@ and resolve_signature_fragment :
19251924
fun env (p, sg) frag ->
19261925
match frag with
19271926
| `Root ->
1928-
let sg = prefix_signature (`FragmentRoot `U, sg) in
1929-
Some (`Root p, `FragmentRoot `U, sg)
1927+
let sg = prefix_signature (Cpath.fragment_root, sg) in
1928+
Some (`Root p, Cpath.fragment_root, sg)
19301929
| `Resolved _r -> None
19311930
| `Dot (parent, name) ->
19321931
let open Odoc_utils.OptionMonad in
@@ -1946,7 +1945,7 @@ and resolve_signature_fragment :
19461945
(`Alias (p', `Resolved new_path, None), `Alias (p', new_frag))
19471946
| Some (`SubstMT p') -> (`Subst (p', new_path), `Subst (p', new_frag))
19481947
in
1949-
(* Don't use the cached one - `FragmentRoot` is not unique *)
1948+
(* Don't use the cached one - the fragment root is not unique *)
19501949
of_result
19511950
Odoc_utils.ResultMonad.(
19521951
expansion_of_module env m' >>= assert_not_functor)

0 commit comments

Comments
 (0)