From 56f7b8df579d09c5a3f5b96e1ae94a2a85a4ee2f Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Mon, 25 Aug 2025 18:38:38 +0200 Subject: [PATCH 1/2] try to improve error message about inline record escaping scope --- compiler/ml/typecore.ml | 10 ++++++++-- .../expected/inline_record_escape.res.expected | 14 ++++++++++++++ .../super_errors/fixtures/inline_record_escape.res | 6 ++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/build_tests/super_errors/expected/inline_record_escape.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/inline_record_escape.res diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index ebcd7bb210..90522f13d2 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -4598,8 +4598,14 @@ let report_error env loc ppf error = "@[Exception patterns must be at the top level of a match case.@]" | Inlined_record_escape -> fprintf ppf - "@[This form is not allowed as the type of the inlined record could \ - escape.@]" + "@[This use of an inlined record is not allowed: its anonymous type \ + would escape its constructor scope.@,\ + @,\ + Possible solutions: @,\ + - Destructure the fields you're interested in from the inline record@,\ + - Change the underlying type to use a defined record as payload instead \ + of an inline record. That will let you use the payload without \ + destructuring it first" | Inlined_record_expected -> fprintf ppf "@[This constructor expects an inlined record argument.@]" | Invalid_extension_constructor_payload -> diff --git a/tests/build_tests/super_errors/expected/inline_record_escape.res.expected b/tests/build_tests/super_errors/expected/inline_record_escape.res.expected new file mode 100644 index 0000000000..7425e757cd --- /dev/null +++ b/tests/build_tests/super_errors/expected/inline_record_escape.res.expected @@ -0,0 +1,14 @@ + + We've found a bug for you! + /.../fixtures/inline_record_escape.res:5:15 + + 3 │ let g = (v: x) => + 4 │ switch v { + 5 │ | One(r) => r + 6 │ } + 7 │ + + This use of an inlined record is not allowed: its anonymous type would escape its constructor scope. + Possible solutions: + - Destructure the fields you're interested in from the inline record + - Change the underlying type to use a defined record as payload instead of an inline record. That will let you use the payload without destructuring it first \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/inline_record_escape.res b/tests/build_tests/super_errors/fixtures/inline_record_escape.res new file mode 100644 index 0000000000..26c5e1db3e --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/inline_record_escape.res @@ -0,0 +1,6 @@ +type x = One({test: bool}) + +let g = (v: x) => + switch v { + | One(r) => r + } From dab55268f90a484379cd0aa8d1061305befc6931 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Thu, 28 Aug 2025 11:57:38 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce1bbb9338..2a3de8b852 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ - More autocomplete improvements involving modules and module types. https://github.com/rescript-lang/rescript/pull/7795 - Autocomplete `@react.componentWithProps` attribute. https://github.com/rescript-lang/rescript/pull/7812 - Add some missing iframe attributes to `domProps`. https://github.com/rescript-lang/rescript/pull/7813 +- Polish error message for inline record escaping scope. https://github.com/rescript-lang/rescript/pull/7808 #### :house: Internal