Skip to content

Commit 58f8800

Browse files
cristianocclaude
andcommitted
Collapse constructor identity into a kind
The Ordinary_constructor payload duplicated information already on the description: its name field mirrored cstr_name (read only by same_constructor, which has the whole description in hand), and its type_path had no remaining reader once the stored layout replaced the declaration lookups. constructor_identity becomes constructor_kind — Ordinary_constructor | Extension_constructor of Path.t — and identity is the pair of cstr_kind and cstr_name, or the extension's path. This also removes the unenforced invariant that the identity's name matched cstr_name, and makes the wrong comparison (ordinary constructors by path, which re-exports would break) inexpressible. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YWw5GW8t4UDEWAzoqcDMkE
1 parent 6ffd2bb commit 58f8800

11 files changed

Lines changed: 54 additions & 64 deletions

File tree

analysis/reanalyze/src/dead_value.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ let rec collect_expr ~config ~refs ~file_deps ~cross_file ~direct_callees
230230
( _,
231231
{
232232
cstr_loc = {Location.loc_start = pos_to; loc_ghost} as loc_to;
233-
cstr_identity;
233+
cstr_kind;
234234
},
235235
_ ) ->
236-
(match cstr_identity with
236+
(match cstr_kind with
237237
| Extension_constructor path ->
238238
path
239239
|> Dead_exception.mark_as_used ~config ~refs ~file_deps ~cross_file

compiler/ml/datarepr.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ let constructor_descrs ty_path decl cstrs =
144144
cstr_existentials = existentials;
145145
cstr_args;
146146
cstr_arity = List.length cstr_args;
147-
cstr_identity =
148-
Ordinary_constructor {type_path = ty_path; name = cstr_name};
147+
cstr_kind = Ordinary_constructor;
149148
cstr_layout = Some layout;
150149
cstr_transparent =
151150
!num_consts = 0 && !num_nonconsts = 1 && cstr_args <> []
@@ -201,7 +200,7 @@ let extension_descr path_ext ext =
201200
cstr_existentials = existentials;
202201
cstr_args;
203202
cstr_arity = List.length cstr_args;
204-
cstr_identity = Extension_constructor path_ext;
203+
cstr_kind = Extension_constructor path_ext;
205204
cstr_layout = None;
206205
cstr_transparent = false;
207206
cstr_private = ext.ext_private;

compiler/ml/env.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ let is_ident = function
510510
| Pdot _ | Papply _ -> false
511511

512512
let is_local_ext = function
513-
| {cstr_identity = Extension_constructor p} -> is_ident p
513+
| {cstr_kind = Extension_constructor p} -> is_ident p
514514
| _ -> false
515515

516516
let diff env1 env2 =
@@ -851,7 +851,7 @@ let find_type_full path env =
851851
Ext_list.filter
852852
(try Tbl.find_str s comps.comp_constrs with Not_found -> assert false)
853853
(function
854-
| {cstr_identity = Extension_constructor _} -> true
854+
| {cstr_kind = Extension_constructor _} -> true
855855
| _ -> false)
856856
in
857857

@@ -1092,7 +1092,7 @@ let lookup_all_simple proj1 proj2 shadow ?loc lid env =
10921092
let has_local_constraints env = not (Path_map.is_empty env.local_constraints)
10931093

10941094
let cstr_shadow cstr1 cstr2 =
1095-
match (cstr1.cstr_identity, cstr2.cstr_identity) with
1095+
match (cstr1.cstr_kind, cstr2.cstr_kind) with
10961096
| Extension_constructor _, Extension_constructor _ -> true
10971097
| _ -> false
10981098

@@ -1211,7 +1211,7 @@ let lookup_all_constructors ?loc lid env =
12111211

12121212
let mark_constructor usage env name desc =
12131213
if not (is_implicit_coercion env) then
1214-
match desc.cstr_identity with
1214+
match desc.cstr_kind with
12151215
| Extension_constructor _ -> (
12161216
let ty_path = ty_path desc.cstr_res in
12171217
let ty_name = Path.last ty_path in

compiler/ml/matching.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ and split_constr cls args def k =
927927
let ex_pat = what_is_cases cls in
928928
match ex_pat.pat_desc with
929929
| Tpat_any -> precompile_var args cls def k
930-
| Tpat_construct (_, {cstr_identity = Extension_constructor _}, _) ->
930+
| Tpat_construct (_, {cstr_kind = Extension_constructor _}, _) ->
931931
split_naive cls args def k
932932
| _ -> (
933933
let group = get_group ex_pat in
@@ -1271,8 +1271,8 @@ let make_constr_matching p def ctx = function
12711271
if cstr.cstr_inlined <> None || (untagged && cstr.cstr_args <> []) then
12721272
(arg, Alias) :: argl
12731273
else
1274-
match cstr.cstr_identity with
1275-
| Ordinary_constructor _
1274+
match cstr.cstr_kind with
1275+
| Ordinary_constructor
12761276
when cstr.cstr_args <> []
12771277
&& Datarepr.constructor_has_optional_shape cstr ->
12781278
let from_option =
@@ -1283,7 +1283,7 @@ let make_constr_matching p def ctx = function
12831283
| _ -> Pval_from_option
12841284
in
12851285
(Lprim (from_option, [arg], p.pat_loc), Alias) :: argl
1286-
| Ordinary_constructor _ ->
1286+
| Ordinary_constructor ->
12871287
make_field_args p.pat_loc Alias arg 0 (cstr.cstr_arity - 1) argl
12881288
~fld_info:(if cstr.cstr_name = "::" then Fld_cons else Fld_variant)
12891289
| Extension_constructor _ ->
@@ -2005,8 +2005,8 @@ let split_cases tag_lambda_list =
20052005
| [] -> ([], [])
20062006
| (cstr, act) :: rem -> (
20072007
let consts, nonconsts = split_rec rem in
2008-
match cstr.cstr_identity with
2009-
| Ordinary_constructor _ ->
2008+
match cstr.cstr_kind with
2009+
| Ordinary_constructor ->
20102010
if cstr.cstr_args = [] then ((cstr, act) :: consts, nonconsts)
20112011
else (consts, (cstr, act) :: nonconsts)
20122012
| Extension_constructor _ -> assert false)
@@ -2031,9 +2031,9 @@ let get_extension_cases tag_lambda_list =
20312031
| [] -> []
20322032
| (cstr, act) :: rem -> (
20332033
let nonconsts = split_rec rem in
2034-
match cstr.cstr_identity with
2034+
match cstr.cstr_kind with
20352035
| Extension_constructor path -> (path, act) :: nonconsts
2036-
| Ordinary_constructor _ -> assert false)
2036+
| Ordinary_constructor -> assert false)
20372037
in
20382038
split_rec tag_lambda_list
20392039

@@ -2053,9 +2053,9 @@ let constructor_switch_key layout (cstr : Types.constructor_description) =
20532053
let combine_constructor loc arg ex_pat cstr partial ctx def
20542054
(tag_lambda_list, total1, pats) =
20552055
let is_extension =
2056-
match cstr.cstr_identity with
2056+
match cstr.cstr_kind with
20572057
| Extension_constructor _ -> true
2058-
| Ordinary_constructor _ -> false
2058+
| Ordinary_constructor -> false
20592059
in
20602060
if is_extension then
20612061
(* Special cases for extensions *)

compiler/ml/parmatch.ml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,9 @@ let row_of_pat pat =
862862
let full_match closing env =
863863
match env with
864864
| (({pat_desc = Tpat_construct (_, c, _)} as p), _) :: _ -> (
865-
match c.cstr_identity with
865+
match c.cstr_kind with
866866
| Extension_constructor _ -> false
867-
| Ordinary_constructor _ ->
867+
| Ordinary_constructor ->
868868
List.length env
869869
= List.length (get_variant_constructors p.pat_env c.cstr_res))
870870
| (({pat_desc = Tpat_variant _} as p), _) :: _ ->
@@ -910,11 +910,10 @@ let should_extend ext env =
910910
| [] -> assert false
911911
| (p, _) :: _ -> (
912912
match p.pat_desc with
913-
| Tpat_construct (_, {cstr_identity = Ordinary_constructor _}, _) ->
913+
| Tpat_construct (_, {cstr_kind = Ordinary_constructor}, _) ->
914914
let path = get_type_path p.pat_type p.pat_env in
915915
Path.same path ext
916-
| Tpat_construct (_, {cstr_identity = Extension_constructor _}, _) ->
917-
false
916+
| Tpat_construct (_, {cstr_kind = Extension_constructor _}, _) -> false
918917
| Tpat_constant _ | Tpat_tuple _ | Tpat_variant _ | Tpat_record _
919918
| Tpat_array _ ->
920919
false
@@ -987,7 +986,7 @@ let complete_constrs p seen_constrs =
987986

988987
let build_other_constrs env p =
989988
match p.pat_desc with
990-
| Tpat_construct (_, {cstr_identity = Ordinary_constructor _}, _) ->
989+
| Tpat_construct (_, {cstr_kind = Ordinary_constructor}, _) ->
991990
let get_constr = function
992991
| {pat_desc = Tpat_construct (_, c, _)} -> c
993992
| _ -> fatal_error "Parmatch.get_constr"
@@ -1015,10 +1014,7 @@ let some_other_tag = "<some other tag>"
10151014

10161015
let build_other ext env : Typedtree.pattern =
10171016
match env with
1018-
| ( {
1019-
pat_desc =
1020-
Tpat_construct (lid, {cstr_identity = Extension_constructor _}, _);
1021-
},
1017+
| ( {pat_desc = Tpat_construct (lid, {cstr_kind = Extension_constructor _}, _)},
10221018
_ )
10231019
:: _ ->
10241020
(* let c = {c with cstr_name = "*extension*"} in *)
@@ -2141,15 +2137,15 @@ let extendable_path path =
21412137

21422138
let rec collect_paths_from_pat r p =
21432139
match p.pat_desc with
2144-
| Tpat_construct (_, {cstr_identity = Ordinary_constructor _}, ps) ->
2140+
| Tpat_construct (_, {cstr_kind = Ordinary_constructor}, ps) ->
21452141
let path = get_type_path p.pat_type p.pat_env in
21462142
List.fold_left collect_paths_from_pat
21472143
(if extendable_path path then add_path path r else r)
21482144
ps
21492145
| Tpat_any | Tpat_var _ | Tpat_constant _ | Tpat_variant (_, None, _) -> r
21502146
| Tpat_tuple ps
21512147
| Tpat_array ps
2152-
| Tpat_construct (_, {cstr_identity = Extension_constructor _}, ps) ->
2148+
| Tpat_construct (_, {cstr_kind = Extension_constructor _}, ps) ->
21532149
List.fold_left collect_paths_from_pat r ps
21542150
| Tpat_record (lps, _, _rest) ->
21552151
List.fold_left (fun r (_, _, p, _) -> collect_paths_from_pat r p) r lps

compiler/ml/rec_check.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ let rec expression : Env.env -> Typedtree.expression -> Use.t =
264264
| Texp_array exprs -> Use.guard (list expression env exprs)
265265
| Texp_construct (_, desc, exprs) ->
266266
let access_constructor =
267-
match desc.cstr_identity with
267+
match desc.cstr_kind with
268268
| Extension_constructor pth -> Use.inspect (path env pth)
269269
| _ -> Use.empty
270270
in

compiler/ml/translcore.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,18 +537,14 @@ let transl_primitive_application loc prim env ty args =
537537
{
538538
exp_desc =
539539
Texp_construct
540-
( _,
541-
{cstr_identity = Ordinary_constructor _; cstr_args = []},
542-
_ );
540+
(_, {cstr_kind = Ordinary_constructor; cstr_args = []}, _);
543541
};
544542
]
545543
| [
546544
{
547545
exp_desc =
548546
Texp_construct
549-
( _,
550-
{cstr_identity = Ordinary_constructor _; cstr_args = []},
551-
_ );
547+
(_, {cstr_kind = Ordinary_constructor; cstr_args = []}, _);
552548
};
553549
_;
554550
]
@@ -778,16 +774,16 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
778774
| [x] -> x
779775
| _ -> assert false
780776
else
781-
match cstr.cstr_identity with
782-
| Ordinary_constructor _ when cstr.cstr_args = [] ->
777+
match cstr.cstr_kind with
778+
| Ordinary_constructor when cstr.cstr_args = [] ->
783779
Lconst
784780
(Const_pointer
785781
(if Datarepr.constructor_has_optional_shape cstr then Pt_shape_none
786782
else
787783
Pt_constructor
788784
(Ast_untagged_variants.constructor_tag ~name:cstr.cstr_name
789785
cstr.cstr_attributes)))
790-
| Ordinary_constructor _ -> (
786+
| Ordinary_constructor -> (
791787
let runtime =
792788
Ast_untagged_variants.block_runtime ~name:cstr.cstr_name
793789
cstr.cstr_attributes

compiler/ml/typecore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3450,7 +3450,7 @@ and type_expect_ ?deprecated_context ~context ?(recarg = Rejected) env sexp
34503450
};
34513451
] ->
34523452
let path =
3453-
match (Typetexp.find_constructor env lid.loc lid.txt).cstr_identity with
3453+
match (Typetexp.find_constructor env lid.loc lid.txt).cstr_kind with
34543454
| Extension_constructor path -> path
34553455
| _ -> raise (Error (lid.loc, env, Not_an_extension_constructor))
34563456
in

compiler/ml/typedecl.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ let transl_extension_constructor env type_path type_params typext_params priv
16901690
| Private, Public -> raise (Error (lid.loc, Rebind_private lid.txt))
16911691
| _ -> ());
16921692
let path =
1693-
match cdescr.cstr_identity with
1693+
match cdescr.cstr_kind with
16941694
| Extension_constructor path -> path
16951695
| _ -> assert false
16961696
in

compiler/ml/types.ml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ type constructor_description = {
250250
cstr_existentials: type_expr list; (* list of existentials *)
251251
cstr_args: type_expr list; (* Type of the arguments *)
252252
cstr_arity: int; (* Number of arguments *)
253-
cstr_identity: constructor_identity; (* Semantic identity *)
253+
cstr_kind: constructor_kind;
254254
cstr_layout: Variant_runtime.variant_layout option;
255255
(* Runtime layout of the declaring variant; None for extension
256256
constructors *)
@@ -264,28 +264,27 @@ type constructor_description = {
264264
cstr_inlined: type_declaration option;
265265
}
266266

267-
and constructor_identity =
268-
| Ordinary_constructor of {type_path: Path.t; name: string}
269-
(* Constructor introduced by a variant type declaration. The path is
270-
the path of the declaring type as written, so a re-exported variant
271-
(type u = M.t = A | B) yields descriptions carrying the
272-
re-exporting type's path. *)
273-
| Extension_constructor of Path.t (* Extension constructor *)
267+
and constructor_kind =
268+
| Ordinary_constructor
269+
(* Constructor introduced by a variant type declaration; identified
270+
within its variant by [cstr_name] *)
271+
| Extension_constructor of Path.t
272+
(* Extension constructor, identified by its own path since extension
273+
constructors can be rebound *)
274274

275275
(* Whether two constructor descriptions denote the same constructor of a
276276
common scrutinee type. Because a re-exported variant mints descriptions
277277
with a different type path, ordinary constructors are compared by name
278278
only; the shared scrutinee type makes the name unambiguous. Not a
279279
general-purpose identity test across unrelated types. *)
280280
let same_constructor c1 c2 =
281-
match (c1.cstr_identity, c2.cstr_identity) with
282-
| Ordinary_constructor {name = n1}, Ordinary_constructor {name = n2} ->
283-
n1 = n2
281+
match (c1.cstr_kind, c2.cstr_kind) with
282+
| Ordinary_constructor, Ordinary_constructor -> c1.cstr_name = c2.cstr_name
284283
| Extension_constructor p1, Extension_constructor p2 -> Path.same p1 p2
285-
| (Ordinary_constructor _ | Extension_constructor _), _ -> false
284+
| (Ordinary_constructor | Extension_constructor _), _ -> false
286285

287286
let may_equal_constr c1 c2 =
288-
match (c1.cstr_identity, c2.cstr_identity) with
287+
match (c1.cstr_kind, c2.cstr_kind) with
289288
| Extension_constructor _, Extension_constructor _ ->
290289
(* extension constructors may be rebound, so paths cannot disprove
291290
equality; arity can *)

0 commit comments

Comments
 (0)