Skip to content

Commit 2294674

Browse files
cknittclaude
andcommitted
Give distinct existential types distinct names
Codex review on #9080: printing `$C_'a` as `$a` made existentials of different constructors that bind the same variable print alike, so a mismatch between them read `$a` vs `$a`. Existentials are now named `$a` before the compiler makes the name unique, as OCaml 5 does, so the second one is `$a1`; the constructor binding each name is recorded for the escape explanation instead of being decoded from the name. Diagnostic modified (existentials of two constructors): Example: type rec any = First('a): any | Second('a): any let same = (First(x), Second(y)) => x == y Before (diagnostic): This has type: $a But it's being compared to something of type: $a After (diagnostic): This has type: $a1 But it's being compared to something of type: $a Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gLMawVgtmiKy3TMBc3 Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent bff79a6 commit 2294674

5 files changed

Lines changed: 39 additions & 22 deletions

File tree

‎compiler/ml/ctype.ml‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,19 +932,32 @@ let new_declaration newtype manifest =
932932
type_inlined_types = [];
933933
}
934934

935+
(* The constructor binding each existential type named by
936+
[instance_constructor], for explaining an escaping one. *)
937+
let existential_constructors_key =
938+
Request_local.new_key (fun () -> Hashtbl.create 8)
939+
let existential_constructor name =
940+
Hashtbl.find_opt (Domain.DLS.get existential_constructors_key) name
941+
935942
let instance_constructor ?in_pattern cstr =
936943
with_copy_session (fun () ->
937944
(match in_pattern with
938945
| None -> ()
939946
| Some (env, newtype_lev) ->
940947
let process existential =
941948
let decl = new_declaration (Some (newtype_lev, newtype_lev)) None in
949+
(* [$a] for the variable ['a], numbered when the name is taken,
950+
so that distinct existentials print differently *)
942951
let name =
943952
match repr existential with
944-
| {desc = Tvar (Some name)} -> "$" ^ cstr.cstr_name ^ "_'" ^ name
953+
| {desc = Tvar (Some name)} -> "$" ^ name
945954
| _ -> "$" ^ cstr.cstr_name
946955
in
947-
let path = Path.Pident (Ident.create (get_new_abstract_name name)) in
956+
let name = get_new_abstract_name name in
957+
Hashtbl.replace
958+
(Domain.DLS.get existential_constructors_key)
959+
name cstr.cstr_name;
960+
let path = Path.Pident (Ident.create name) in
948961
let new_env = Env.add_local_type path decl !env in
949962
env := new_env;
950963
let to_unify = newty (Tconstr (path, [], ref Mnil)) in

‎compiler/ml/ctype.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ val instance_constructor :
172172
type_expr list * type_expr
173173
(* Same, for a constructor *)
174174

175+
val existential_constructor : string -> string option
176+
(* The constructor that binds the existential type of this name, given to it
177+
by [instance_constructor] in the current request *)
178+
175179
val instance_parameterized_type :
176180
?keep_names:bool -> type_expr list -> type_expr -> type_expr list * type_expr
177181
val instance_declaration : type_declaration -> type_declaration

‎compiler/ml/printtyp.ml‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,8 @@ let rec longident ppf = function
4141
let unique_names_key = Request_local.new_key (fun () -> ref Ident.empty)
4242
let unique_names () = Domain.DLS.get unique_names_key
4343

44-
(* [Ctype.instance_constructor] names the existential type of variable ['a]
45-
of constructor [C] [$C_'a], followed by a number when the name is taken.
46-
It is printed [$a], and [C] is named in the escape explanation. *)
47-
let existential_of_name name =
48-
match String.rindex_opt name '\'' with
49-
| Some i when i >= 3 && name.[0] = '$' && name.[i - 1] = '_' ->
50-
let constructor = String.sub name 1 (i - 2) in
51-
let var = String.sub name (i + 1) (String.length name - i - 1) in
52-
Some ("$" ^ var, constructor)
53-
| _ -> None
54-
5544
let ident_name id =
56-
try Ident.find_same id !(unique_names ())
57-
with Not_found -> (
58-
let name = Ident.name id in
59-
match existential_of_name name with
60-
| Some (display, _) -> display
61-
| None -> name)
45+
try Ident.find_same id !(unique_names ()) with Not_found -> Ident.name id
6246

6347
let add_unique id =
6448
try ignore (Ident.find_same id !(unique_names ()))
@@ -1318,13 +1302,13 @@ let rec mismatch = function
13181302
let escape_explanation ppf p =
13191303
match p with
13201304
| Pident id -> (
1321-
match existential_of_name (Ident.name id) with
1322-
| Some (display, constructor) ->
1305+
match Ctype.existential_constructor (Ident.name id) with
1306+
| Some constructor ->
13231307
fprintf ppf
13241308
"@,\
13251309
@[%s is an existential type bound by the constructor %s.@ It is only \
13261310
known in the scope of the pattern that matches %s.@]"
1327-
display constructor constructor
1311+
(Ident.name id) constructor constructor
13281312
| None ->
13291313
fprintf ppf "@,@[The type constructor@;<1 2>%a@ would escape its scope@]"
13301314
path p)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/gadt_existential_same_name.res:3:42
4+
5+
1 │ type rec any = First('a): any | Second('a): any
6+
2 │
7+
3 │ let same = (First(x), Second(y)) => x == y
8+
4 │
9+
10+
This has type: $a1
11+
But it's being compared to something of type: $a
12+
13+
You can only compare things of the same type.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type rec any = First('a): any | Second('a): any
2+
3+
let same = (First(x), Second(y)) => x == y

0 commit comments

Comments
 (0)