Skip to content

Commit c280154

Browse files
Force identifiers + resolved references to have a datatype as parent
Resolved references/ids already always had datatype as parent, but it is now enforced by the subtyping system. The reason we cannot symmetrically do the same for fields is the following: ```ocaml type t = .. type t += Inline of { a : int} ``` In this example, `a` cannot have a `datatype` as parent. Parent for fields should ideally be `[datatype | constructor | extension]`. Signed-off-by: Paul-Elliot <[email protected]> Co-authored-by: Emile Trotignon <[email protected]>
1 parent be87d3a commit c280154

File tree

10 files changed

+82
-84
lines changed

10 files changed

+82
-84
lines changed

src/loader/cmi.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ let read_type_declaration env parent id decl =
713713
let params = mark_type_declaration decl in
714714
let manifest = opt_map (read_type_expr env) decl.type_manifest in
715715
let constraints = read_type_constraints env params in
716-
let representation = read_type_kind env (id :> Identifier.Parent.t) decl.type_kind in
716+
let representation = read_type_kind env (id :> Identifier.DataType.t) decl.type_kind in
717717
let abstr =
718718
match decl.type_kind with
719719
Type_abstract ->

src/loader/cmti.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ let read_constructor_declaration_arguments env parent label_parent arg =
214214
let read_constructor_declaration env parent cd =
215215
let open TypeDecl.Constructor in
216216
let id = Ident_env.find_constructor_identifier env cd.cd_id in
217-
let container = parent in
217+
let container = (parent :> Identifier.Parent.t) in
218218
let label_container = (container :> Identifier.LabelParent.t) in
219219
let doc = Doc_attr.attached_no_tag label_container cd.cd_attributes in
220220
let args =
@@ -231,7 +231,7 @@ let read_type_kind env parent =
231231
let cstrs = List.map (read_constructor_declaration env parent) cstrs in
232232
Some (Variant cstrs)
233233
| Ttype_record lbls ->
234-
let parent = (parent ) in
234+
let parent = (parent :> Identifier.Parent.t) in
235235
let label_parent = (parent :> Identifier.LabelParent.t) in
236236
let lbls =
237237
List.map (read_label_declaration env parent label_parent) lbls in
@@ -260,7 +260,7 @@ let read_type_declaration env parent decl =
260260
let doc, canonical = Doc_attr.attached Odoc_model.Semantics.Expect_canonical container decl.typ_attributes in
261261
let canonical = (canonical :> Path.Type.t option) in
262262
let equation = read_type_equation env container decl in
263-
let representation = read_type_kind env (id :> Identifier.Parent.t) decl.typ_kind in
263+
let representation = read_type_kind env id decl.typ_kind in
264264
{id; locs; doc; canonical; equation; representation}
265265

266266
let read_type_declarations env parent rec_flag decls =

src/model/paths.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module Identifier = struct
165165
| { iv = `Label (p, _); _ } -> p
166166
| { iv = `Method (p, _); _ } | { iv = `InstanceVariable (p, _); _ } ->
167167
(p : class_signature :> label_parent)
168-
| { iv = `Constructor (p, _); _ } -> (p : parent :> label_parent)
168+
| { iv = `Constructor (p, _); _ } -> (p : datatype :> label_parent)
169169
| { iv = `Field (p, _); _ } -> (p : parent :> label_parent)
170170

171171
let label_parent n = label_parent_aux (n :> Id.non_src)
@@ -572,8 +572,8 @@ module Identifier = struct
572572
mk_fresh (fun s -> s) "coret" (fun s -> `CoreType (TypeName.make_std s))
573573

574574
let constructor :
575-
Parent.t * ConstructorName.t ->
576-
[> `Constructor of Parent.t * ConstructorName.t ] id =
575+
DataType.t * ConstructorName.t ->
576+
[> `Constructor of DataType.t * ConstructorName.t ] id =
577577
mk_parent ConstructorName.to_string "ctor" (fun (p, n) ->
578578
`Constructor (p, n))
579579

@@ -1013,7 +1013,9 @@ module Reference = struct
10131013
| `Class _ | `ClassType _ | `ModuleType _ ) as r ->
10141014
(label_parent_identifier r :> Identifier.t)
10151015
| `Field (p, n) -> Identifier.Mk.field (parent_identifier p, n)
1016-
| `Constructor (s, n) -> Identifier.Mk.constructor (parent_identifier s, n)
1016+
| `Constructor (s, n) ->
1017+
Identifier.Mk.constructor
1018+
((parent_type_identifier s :> Identifier.DataType.t), n)
10171019
| `Extension (p, q) ->
10181020
Identifier.Mk.extension (parent_signature_identifier p, q)
10191021
| `ExtensionDecl (p, q, r) ->

src/model/paths.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ module Identifier : sig
290290
val core_type : string -> [> `CoreType of TypeName.t ] id
291291

292292
val constructor :
293-
Parent.t * ConstructorName.t ->
294-
[> `Constructor of Parent.t * ConstructorName.t ] id
293+
DataType.t * ConstructorName.t ->
294+
[> `Constructor of DataType.t * ConstructorName.t ] id
295295

296296
val field :
297297
Parent.t * FieldName.t -> [> `Field of Parent.t * FieldName.t ] id

src/model/paths_types.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ module Identifier = struct
132132
and type_ = type_pv id
133133
(** @canonical Odoc_model.Paths.Identifier.Type.t *)
134134

135-
type constructor_pv = [ `Constructor of parent * ConstructorName.t ]
135+
type constructor_pv = [ `Constructor of datatype * ConstructorName.t ]
136136
(** @canonical Odoc_model.Paths.Identifier.Constructor.t_pv *)
137137

138138
and constructor = constructor_pv id
@@ -843,7 +843,7 @@ and Resolved_reference : sig
843843

844844
type constructor =
845845
[ `Identifier of Identifier.reference_constructor
846-
| `Constructor of parent * ConstructorName.t
846+
| `Constructor of datatype * ConstructorName.t
847847
| `Extension of signature * ExtensionName.t
848848
| `Exception of signature * ExceptionName.t ]
849849
(** @canonical Odoc_model.Paths.Reference.Resolved.Constructor.t *)
@@ -916,7 +916,7 @@ and Resolved_reference : sig
916916
| `Hidden of module_
917917
| `ModuleType of signature * ModuleTypeName.t
918918
| `Type of signature * TypeName.t
919-
| `Constructor of parent * ConstructorName.t
919+
| `Constructor of datatype * ConstructorName.t
920920
| `Field of parent * FieldName.t
921921
| `Extension of signature * ExtensionName.t
922922
| `ExtensionDecl of signature * ExtensionName.t * ExtensionName.t

src/xref2/env.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ let add_type (identifier : Identifier.Type.t) t env =
294294
let add_cons env (cons : TypeDecl.Constructor.t) =
295295
let ident =
296296
Paths.Identifier.Mk.constructor
297-
( (identifier :> Identifier.Parent.t),
297+
( (identifier :> Identifier.DataType.t),
298298
ConstructorName.make_std cons.name )
299299
in
300300
add_to_elts Kind_Constructor ident (`Constructor (ident, cons)) env

src/xref2/lang_of.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,8 @@ and type_decl_constructor_argument :
895895
match a with
896896
| Tuple ls ->
897897
Tuple (List.map (type_expr map (parent :> Identifier.LabelParent.t)) ls)
898-
| Record fs -> Record (List.map (type_decl_field map parent) fs)
898+
| Record fs ->
899+
Record (List.map (type_decl_field map (parent :> Identifier.Parent.t)) fs)
899900

900901
and type_decl_field :
901902
maps ->
@@ -942,9 +943,7 @@ and type_decl_representation map id (t : Component.TypeDecl.Representation.t) :
942943
Odoc_model.Lang.TypeDecl.Representation.t =
943944
match t with
944945
| Extensible -> Extensible
945-
| Variant cs ->
946-
Variant
947-
(List.map (type_decl_constructor map (id :> Identifier.Parent.t)) cs)
946+
| Variant cs -> Variant (List.map (type_decl_constructor map id) cs)
948947
| Record fs ->
949948
Record
950949
(List.map
@@ -953,7 +952,7 @@ and type_decl_representation map id (t : Component.TypeDecl.Representation.t) :
953952

954953
and type_decl_constructor :
955954
maps ->
956-
Odoc_model.Paths.Identifier.Parent.t ->
955+
Odoc_model.Paths.Identifier.DataType.t ->
957956
Component.TypeDecl.Constructor.t ->
958957
Odoc_model.Lang.TypeDecl.Constructor.t =
959958
fun map id t ->
@@ -963,8 +962,8 @@ and type_decl_constructor :
963962
let parent = (id :> Identifier.LabelParent.t) in
964963
{
965964
id = identifier;
966-
doc = docs (id :> Identifier.LabelParent.t) t.doc;
967-
args = type_decl_constructor_argument map id t.args;
965+
doc = docs parent t.doc;
966+
args = type_decl_constructor_argument map (id :> Identifier.Parent.t) t.args;
968967
res = Opt.map (type_expr map parent) t.res;
969968
}
970969

src/xref2/lang_of.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ val type_decl_representation :
217217

218218
val type_decl_constructor :
219219
maps ->
220-
Identifier.Parent.t ->
220+
Identifier.DataType.t ->
221221
Component.TypeDecl.Constructor.t ->
222222
Odoc_model.Lang.TypeDecl.Constructor.t
223223

src/xref2/ref_tools.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,13 @@ module CS = struct
456456
find Find.any_in_type t name_s >>= function
457457
| `FField _ -> got_a_field name_s
458458
| `FConstructor _ ->
459-
Ok
460-
(`Constructor
461-
((parent' : Resolved.DataType.t :> Resolved.Parent.t), name)))
459+
Ok (`Constructor ((parent' : Resolved.DataType.t), name)))
462460
| (`C _ | `CT _ | `P _) as r -> wrong_kind_error [ `S; `T ] r
463461

464462
let of_component _env parent name =
465463
Ok
466464
(`Constructor
467-
( (parent : Resolved.DataType.t :> Resolved.Parent.t),
468-
ConstructorName.make_std name ))
465+
((parent : Resolved.DataType.t), ConstructorName.make_std name))
469466
end
470467

471468
module F = struct

0 commit comments

Comments
 (0)