summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcuriousleo <curiousleo@users.noreply.github.com>2022-08-29 22:56:49 +0800
committercuriousleo <curiousleo@users.noreply.github.com>2022-09-02 00:16:17 +0800
commit22975f80800ee888ebf526561f8e44ff1cfaabd8 (patch)
tree4b4cd9760e0fa8c3366bbf6211d177d1e8fedc79
parent4d359e3e248ebe836e8f28193a2576152dde2fb7 (diff)
downloadocaml-22975f80800ee888ebf526561f8e44ff1cfaabd8.tar.gz
Include kinds in "Their kinds differ" error
Changes the error message for code like the following: module M : sig type t end = struct type t = { i : int } end type t = M.t = { i : int };; Before: 2 | type t = M.t = { i : int };; ^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: This variant or record definition does not match that of type M.t Their kinds differ. After: 2 | type t = M.t = { i : int };; ^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: This variant or record definition does not match that of type M.t The original is abstract, but this is a record.
-rw-r--r--Changes3
-rw-r--r--testsuite/tests/typing-extensions/open_types.ml4
-rw-r--r--testsuite/tests/typing-kind/kind_mismatch.ml27
-rw-r--r--testsuite/tests/typing-misc/enrich_typedecl.ml2
-rw-r--r--testsuite/tests/typing-misc/records.ml2
-rw-r--r--testsuite/tests/typing-misc/variant.ml2
-rw-r--r--testsuite/tests/typing-modules-bugs/pr6293_bad.compilers.reference2
-rw-r--r--testsuite/tests/typing-modules/inclusion_errors.ml4
-rw-r--r--typing/includecore.ml35
-rw-r--r--typing/includecore.mli10
10 files changed, 78 insertions, 13 deletions
diff --git a/Changes b/Changes
index 07c856400f..1a41046169 100644
--- a/Changes
+++ b/Changes
@@ -84,6 +84,9 @@ Working version
### Compiler user-interface and warnings:
+- #11530: Include kinds in kind mismatch error message.
+ (Leonhard Markert, review by Gabriel Scherer and Florian Angeletti)
+
- #10818: Preserve integer literal formatting in type hint.
(Leonhard Markert, review by Gabriel Scherer and Florian Angeletti)
diff --git a/testsuite/tests/typing-extensions/open_types.ml b/testsuite/tests/typing-extensions/open_types.ml
index eda342d2bf..e78ddef247 100644
--- a/testsuite/tests/typing-extensions/open_types.ml
+++ b/testsuite/tests/typing-extensions/open_types.ml
@@ -82,7 +82,7 @@ Line 1, characters 0-19:
1 | type baz = bar = ..
^^^^^^^^^^^^^^^^^^^
Error: This variant or record definition does not match that of type bar
- Their kinds differ.
+ The original is abstract, but this is an extensible variant.
|}]
(* Abbreviations need to match parameters *)
@@ -176,7 +176,7 @@ Error: Signature mismatch:
type foo = M.foo
is not included in
type foo = ..
- Their kinds differ.
+ The first is abstract, but the second is an extensible variant.
|}]
(* Check that signatures can make exstensibility private *)
diff --git a/testsuite/tests/typing-kind/kind_mismatch.ml b/testsuite/tests/typing-kind/kind_mismatch.ml
new file mode 100644
index 0000000000..93f5e54d97
--- /dev/null
+++ b/testsuite/tests/typing-kind/kind_mismatch.ml
@@ -0,0 +1,27 @@
+(* TEST
+ * expect
+*)
+
+(** Error messages for kind mismatches. *)
+
+module T0 : sig type t end = struct type t = unit end
+type t0 = T0.t = { a0 : int };;
+[%%expect {|
+module T0 : sig type t end
+Line 4, characters 0-29:
+4 | type t0 = T0.t = { a0 : int };;
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+Error: This variant or record definition does not match that of type T0.t
+ The original is abstract, but this is a record.
+|}]
+
+type t2a = ..
+type t2b = t2a = A2 | B2;;
+[%%expect {|
+type t2a = ..
+Line 2, characters 0-24:
+2 | type t2b = t2a = A2 | B2;;
+ ^^^^^^^^^^^^^^^^^^^^^^^^
+Error: This variant or record definition does not match that of type t2a
+ The original is an extensible variant, but this is a variant.
+|}]
diff --git a/testsuite/tests/typing-misc/enrich_typedecl.ml b/testsuite/tests/typing-misc/enrich_typedecl.ml
index c6087a9dc7..ac699720d9 100644
--- a/testsuite/tests/typing-misc/enrich_typedecl.ml
+++ b/testsuite/tests/typing-misc/enrich_typedecl.ml
@@ -95,7 +95,7 @@ Error: Signature mismatch:
type 'a t = 'a C.t = A of 'a | B
is not included in
type 'a t = { x : 'a; }
- Their kinds differ.
+ The first is a variant, but the second is a record.
|}];;
diff --git a/testsuite/tests/typing-misc/records.ml b/testsuite/tests/typing-misc/records.ml
index 5f0a486a4d..04cac3b381 100644
--- a/testsuite/tests/typing-misc/records.ml
+++ b/testsuite/tests/typing-misc/records.ml
@@ -208,7 +208,7 @@ Line 1, characters 0-59:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This variant or record definition does not match that of type
('a, [> `A ]) def
- Their kinds differ.
+ The original is a record, but this is a variant.
|}]
type d = { x:int; y : int }
diff --git a/testsuite/tests/typing-misc/variant.ml b/testsuite/tests/typing-misc/variant.ml
index b48142cd6d..b7d5ae8aee 100644
--- a/testsuite/tests/typing-misc/variant.ml
+++ b/testsuite/tests/typing-misc/variant.ml
@@ -77,7 +77,7 @@ Line 1, characters 0-65:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This variant or record definition does not match that of type
('a, [> `A ]) def
- Their kinds differ.
+ The original is a variant, but this is a record.
|}]
type d = X of int | Y of int
diff --git a/testsuite/tests/typing-modules-bugs/pr6293_bad.compilers.reference b/testsuite/tests/typing-modules-bugs/pr6293_bad.compilers.reference
index de6d90798f..58975e1620 100644
--- a/testsuite/tests/typing-modules-bugs/pr6293_bad.compilers.reference
+++ b/testsuite/tests/typing-modules-bugs/pr6293_bad.compilers.reference
@@ -7,6 +7,6 @@ Error: In this `with' constraint, the new definition of t
type t
is not included in
type t = { a : int; b : int; }
- Their kinds differ.
+ The first is abstract, but the second is a record.
File "pr6293_bad.ml", line 9, characters 20-50: Expected declaration
File "pr6293_bad.ml", line 10, characters 18-37: Actual declaration
diff --git a/testsuite/tests/typing-modules/inclusion_errors.ml b/testsuite/tests/typing-modules/inclusion_errors.ml
index e813587d98..36c1906042 100644
--- a/testsuite/tests/typing-modules/inclusion_errors.ml
+++ b/testsuite/tests/typing-modules/inclusion_errors.ml
@@ -1602,7 +1602,7 @@ Error: Signature mismatch:
type t = private { x : int; y : bool; }
is not included in
type t = A | B
- Their kinds differ.
+ The first is a record, but the second is a variant.
|}];;
module M : sig
@@ -1624,7 +1624,7 @@ Error: Signature mismatch:
type t = private A | B
is not included in
type t = { x : int; y : bool; }
- Their kinds differ.
+ The first is a variant, but the second is a record.
|}];;
module M : sig
diff --git a/typing/includecore.ml b/typing/includecore.ml
index bef4d335b3..a3cdd189c9 100644
--- a/typing/includecore.ml
+++ b/typing/includecore.ml
@@ -133,6 +133,20 @@ type privacy_mismatch =
| Private_extensible_variant
| Private_row_type
+type type_kind =
+ | Kind_abstract
+ | Kind_record
+ | Kind_variant
+ | Kind_open
+
+let of_kind = function
+ | Type_abstract -> Kind_abstract
+ | Type_record (_, _) -> Kind_record
+ | Type_variant (_, _) -> Kind_variant
+ | Type_open -> Kind_open
+
+type kind_mismatch = type_kind * type_kind
+
type label_mismatch =
| Type of Errortrace.equality_error
| Mutability of position
@@ -177,7 +191,7 @@ type variant_change =
type type_mismatch =
| Arity
| Privacy of privacy_mismatch
- | Kind
+ | Kind of kind_mismatch
| Constraint of Errortrace.equality_error
| Manifest of Errortrace.equality_error
| Private_variant of type_expr * type_expr * private_variant_mismatch
@@ -378,6 +392,19 @@ let report_private_object_mismatch env ppf err =
| Missing s -> pr "The implementation is missing the method %s" s
| Types err -> report_type_inequality env ppf err
+let report_kind_mismatch first second ppf (kind1, kind2) =
+ let pr fmt = Format.fprintf ppf fmt in
+ let kind_to_string = function
+ | Kind_abstract -> "abstract"
+ | Kind_record -> "a record"
+ | Kind_variant -> "a variant"
+ | Kind_open -> "an extensible variant" in
+ pr "%s is %s, but %s is %s."
+ (String.capitalize_ascii first)
+ (kind_to_string kind1)
+ second
+ (kind_to_string kind2)
+
let report_type_mismatch first second decl env ppf err =
let pr fmt = Format.fprintf ppf fmt in
pr "@ ";
@@ -386,8 +413,8 @@ let report_type_mismatch first second decl env ppf err =
pr "They have different arities."
| Privacy err ->
report_privacy_mismatch ppf err
- | Kind ->
- pr "Their kinds differ."
+ | Kind err ->
+ report_kind_mismatch first second ppf err
| Constraint err ->
(* This error can come from implicit parameter disagreement or from
explicit `constraint`s. Both affect the parameters, hence this choice
@@ -921,7 +948,7 @@ let type_declarations ?(equality = false) ~loc env ~mark name
labels1 labels2
rep1 rep2
| (Type_open, Type_open) -> None
- | (_, _) -> Some Kind
+ | (_, _) -> Some (Kind (of_kind decl1.type_kind, of_kind decl2.type_kind))
in
if err <> None then err else
let abstr = decl2.type_kind = Type_abstract && decl2.type_manifest = None in
diff --git a/typing/includecore.mli b/typing/includecore.mli
index be1687b620..50825976ce 100644
--- a/typing/includecore.mli
+++ b/typing/includecore.mli
@@ -43,6 +43,14 @@ type privacy_mismatch =
| Private_extensible_variant
| Private_row_type
+type type_kind =
+ | Kind_abstract
+ | Kind_record
+ | Kind_variant
+ | Kind_open
+
+type kind_mismatch = type_kind * type_kind
+
type label_mismatch =
| Type of Errortrace.equality_error
| Mutability of position
@@ -85,7 +93,7 @@ type private_object_mismatch =
type type_mismatch =
| Arity
| Privacy of privacy_mismatch
- | Kind
+ | Kind of kind_mismatch
| Constraint of Errortrace.equality_error
| Manifest of Errortrace.equality_error
| Private_variant of type_expr * type_expr * private_variant_mismatch