From 1701f12b54cef648a2d7b97ac72b3d066364701b Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Tue, 2 Sep 2025 10:08:53 +0200 Subject: [PATCH 1/2] Better error messages for GADTs and int overflows --- compiler/frontend/bs_ast_invariant.ml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/frontend/bs_ast_invariant.ml b/compiler/frontend/bs_ast_invariant.ml index fb4bfa370b..5ae58f8d84 100644 --- a/compiler/frontend/bs_ast_invariant.ml +++ b/compiler/frontend/bs_ast_invariant.ml @@ -85,7 +85,9 @@ let emit_external_warnings : iterator = ( Nonrecursive, [{ptype_kind = Ptype_variant ({pcd_res = Some _} :: _)}] ) -> Location.raise_errorf ~loc:str_item.pstr_loc - "GADT has to be recursive types, please try `type rec'" + "GADTs require recursive type syntax.\n\ + Please define your type using `type rec` instead of `type`.\n\ + Example: type rec t = ..." | _ -> super.structure_item self str_item); expr = (fun self ({pexp_loc = loc} as a) -> @@ -95,7 +97,8 @@ let emit_external_warnings : iterator = try ignore (Ext_string.hash_number_as_i32_exn s : int32) with _ -> Location.raise_errorf ~loc - "This number is too large to cause int overlow") + "Integer literal exceeds int32 range. Use float or BigInt if \ + larger values are required.") | _ -> super.expr self a); label_declaration = (fun self lbl -> From 40f90a7cd81961c46b1523520e16aed3ef3223b8 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Tue, 2 Sep 2025 10:51:36 +0200 Subject: [PATCH 2/2] Add test fixtures --- .../expected/gadt_rec_missing.res.expected | 16 ++++++++++++++++ .../polyvariant_int_too_large.res.expected | 8 ++++++++ .../super_errors/fixtures/gadt_rec_missing.res | 8 ++++++++ .../fixtures/polyvariant_int_too_large.res | 1 + 4 files changed, 33 insertions(+) create mode 100644 tests/build_tests/super_errors/expected/gadt_rec_missing.res.expected create mode 100644 tests/build_tests/super_errors/expected/polyvariant_int_too_large.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/gadt_rec_missing.res create mode 100644 tests/build_tests/super_errors/fixtures/polyvariant_int_too_large.res diff --git a/tests/build_tests/super_errors/expected/gadt_rec_missing.res.expected b/tests/build_tests/super_errors/expected/gadt_rec_missing.res.expected new file mode 100644 index 0000000000..69f6b0b227 --- /dev/null +++ b/tests/build_tests/super_errors/expected/gadt_rec_missing.res.expected @@ -0,0 +1,16 @@ + + We've found a bug for you! + /.../fixtures/gadt_rec_missing.res:4:1-8:27 + + 2 │ type daylight + 3 │ + 4 │ type timezone<_> = + 5 │  | EST: timezone + 6 │  | EDT: timezone + 7 │  | CST: timezone + 8 │  | CDT: timezone + 9 │ + + GADTs require recursive type syntax. +Please define your type using `type rec` instead of `type`. +Example: type rec t = ... \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/polyvariant_int_too_large.res.expected b/tests/build_tests/super_errors/expected/polyvariant_int_too_large.res.expected new file mode 100644 index 0000000000..2eb60a209e --- /dev/null +++ b/tests/build_tests/super_errors/expected/polyvariant_int_too_large.res.expected @@ -0,0 +1,8 @@ + + We've found a bug for you! + /.../fixtures/polyvariant_int_too_large.res:1:9-29 + + 1 │ let x = #12345678901234567890 + 2 │ + + Integer literal exceeds int32 range. Use float or BigInt if larger values are required. \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/gadt_rec_missing.res b/tests/build_tests/super_errors/fixtures/gadt_rec_missing.res new file mode 100644 index 0000000000..20a92ec824 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/gadt_rec_missing.res @@ -0,0 +1,8 @@ +type standard +type daylight + +type timezone<_> = + | EST: timezone + | EDT: timezone + | CST: timezone + | CDT: timezone diff --git a/tests/build_tests/super_errors/fixtures/polyvariant_int_too_large.res b/tests/build_tests/super_errors/fixtures/polyvariant_int_too_large.res new file mode 100644 index 0000000000..c15559a057 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/polyvariant_int_too_large.res @@ -0,0 +1 @@ +let x = #12345678901234567890