Skip to content

Commit 9e88874

Browse files
cknittclaude
andauthored
Print a type reached through a module alias once in type errors (#9054)
Why: The check for whether to print a type's expansion compared paths without resolving module aliases, so `Map.t` was printed again as its identical-looking expansion `Stdlib_Map.t`. Diagnostic modified (type mismatch): Example: let f = (x: Map.t<string, int>): Map.t<string, string> => x Before (diagnostic): This has type: Map.t<string, int> (defined as Map.t<string, int>) But it's expected to have type: Map.t<string, string> (defined as Map.t<string, string>) After (diagnostic): This has type: Map.t<string, int> But it's expected to have type: Map.t<string, string> Diagnostic modified (coercion): Example: module Inner = { type t<'a> } module A = Inner let m = (x: A.t<[#X]>) => (x :> A.t<[#X | #Y]>) Before (diagnostic): Type A.t<[#X]> = Inner.t<[#X]> is not a subtype of A.t<[#X | #Y]> = Inner.t<[#X | #Y]> After (diagnostic): Type A.t<[#X]> is not a subtype of A.t<[#X | #Y]> Fixes #8997 Claude-Session: https://claude.ai/code/session_01LZcKAmGvz6roKbTCq7or6f Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent faddac7 commit 9e88874

10 files changed

Lines changed: 49 additions & 8 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Print a type reached through a module alias once in type errors, without repeating it as its expansion.

‎compiler/ml/printtyp.ml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,12 @@ let same_path t t' =
11931193
let p1, s1 = best_type_path p and p2, s2 = best_type_path p' in
11941194
match (s1, s2) with
11951195
| Nth n1, Nth n2 when n1 = n2 -> true
1196-
| (Id | Map _), (Id | Map _) when Path.same p1 p2 ->
1196+
| (Id | Map _), (Id | Map _)
1197+
when let env = !(printing_env ()) in
1198+
(* A module alias names the same type as its target. *)
1199+
Path.same
1200+
(Env.normalize_path None env p1)
1201+
(Env.normalize_path None env p2) ->
11971202
let tl = apply_subst s1 tl and tl' = apply_subst s2 tl' in
11981203
List.length tl = List.length tl' && List.for_all2 same_type tl tl'
11991204
| _ -> false)

‎tests/build_tests/super_errors/expected/GeneratorResumeTypeWidening.res.expected‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
│ unit, [#X | #Y]>)
88
3 │
99

10-
Type Generator.t<int, unit, [#X]> = Generator.t<int, unit, [#X]>
11-
is not a subtype of
12-
Generator.t<int, unit, [#X | #Y]> = Generator.t<int, unit, [#X | #Y]>
10+
Type Generator.t<int, unit, [#X]> is not a subtype of
11+
Generator.t<int, unit, [#X | #Y]>
1312

1413
The second polymorphic variant is closed and doesn't include the constructor: #Y.
1514

‎tests/build_tests/super_errors/expected/coercion_hint_nested_mismatch.res.expected‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
But this function argument is expecting: unit => Iterable.t<int>
1313

1414
The incompatible parts:
15-
Generator.t<int, unit, unit> (defined as Generator.t<int, unit, unit>) vs
15+
Generator.t<int, unit, unit> vs
1616
Iterable.t<int> (defined as iterable<int>)
1717

1818
Possible solutions:

‎tests/build_tests/super_errors/expected/dict_show_no_coercion.res.expected‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
3 │
1010

1111
This has type: float
12-
But it's expected to have type: JSON.t (defined as JSON.t)
12+
But it's expected to have type: JSON.t
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/module_alias_coercion_printed_once.res:5:28-46
4+
5+
3 │ }
6+
4 │ module A = Inner
7+
5 │ let m = (x: A.t<[#X]>) => (x :> A.t<[#X | #Y]>)
8+
6 │
9+
10+
Type A.t<[#X]> is not a subtype of A.t<[#X | #Y]>
11+
12+
The first polymorphic variant is closed and doesn't include the constructor: #Y.
13+
14+
Possible solutions:
15+
- Either make the first variant open so it can accept additional constructors. To do this, make sure the type starts with [> instead of [
16+
- Or add the missing constructor to it.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/module_alias_type_printed_once.res:1:59
4+
5+
1 │ let f = (x: Map.t<string, int>): Map.t<string, string> => x
6+
2 │
7+
8+
This has type: Map.t<string, int>
9+
But it's expected to have type: Map.t<string, string>
10+
11+
The incompatible parts:
12+
int vs string
13+
14+
You can convert int to string with Int.toString.

‎tests/build_tests/super_errors/expected/typed_array_cases_distinct.res.expected‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
4 │ let x = I8(Uint8Array.fromArray([1]))
88
5 │
99

10-
This has type: Uint8Array.t (defined as Uint8Array.t)
11-
But it's expected to have type: Int8Array.t (defined as Int8Array.t)
10+
This has type: Uint8Array.t
11+
But it's expected to have type: Int8Array.t
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Inner = {
2+
type t<'a>
3+
}
4+
module A = Inner
5+
let m = (x: A.t<[#X]>) => (x :> A.t<[#X | #Y]>)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f = (x: Map.t<string, int>): Map.t<string, string> => x

0 commit comments

Comments
 (0)