From 610cec1843fa827f04ea29906bb3a59a1ef46ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Fri, 9 May 2025 13:11:59 +0100 Subject: [PATCH 1/2] Recommend stdlib functions over Belt for converting between float/int/string --- compiler/ml/typecore.ml | 12 ++++++------ .../expected/array_item_type_mismatch.res.expected | 2 +- .../super_errors/expected/collections.res.expected | 2 +- .../expected/dict_inference.res.expected | 2 +- .../dict_pattern_inference_constrained.res.expected | 2 +- .../expected/function_argument_mismatch.res.expected | 2 +- .../super_errors/expected/highlighting1.res.expected | 2 +- .../super_errors/expected/highlighting2.res.expected | 2 +- .../super_errors/expected/highlighting3.res.expected | 2 +- .../super_errors/expected/highlighting5.res.expected | 2 +- .../expected/if_branch_mismatch.res.expected | 2 +- .../inline_types_record_type_params.res.expected | 2 +- .../expected/math_operator_constant.res.expected | 2 +- .../expected/math_operator_float.res.expected | 2 +- .../super_errors/expected/primitives1.res.expected | 2 +- .../super_errors/expected/primitives11.res.expected | 2 +- .../super_errors/expected/primitives2.res.expected | 2 +- .../super_errors/expected/primitives6.res.expected | 2 +- .../super_errors/expected/primitives7.res.expected | 2 +- .../super_errors/expected/primitives9.res.expected | 2 +- .../record_type_spreads_deep_sub.res.expected | 2 +- .../set_record_field_type_match.res.expected | 2 +- .../expected/stdlib_removed_in_error.res.expected | 2 +- .../super_errors/expected/syntaxErrors4.res.expected | 2 +- .../super_errors/expected/type1.res.expected | 2 +- .../super_errors/expected/type2.res.expected | 2 +- .../expected/unicode_location.res.expected | 2 +- 27 files changed, 32 insertions(+), 32 deletions(-) diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index a7ac76a341..dd69ad1ad2 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -676,12 +676,12 @@ let rec bottom_aliases = function let simple_conversions = [ - (("float", "int"), "Belt.Float.toInt"); - (("float", "string"), "Belt.Float.toString"); - (("int", "float"), "Belt.Int.toFloat"); - (("int", "string"), "Belt.Int.toString"); - (("string", "float"), "Belt.Float.fromString"); - (("string", "int"), "Belt.Int.fromString"); + (("float", "int"), "Float.toInt"); + (("float", "string"), "Float.toString"); + (("int", "float"), "Int.toFloat"); + (("int", "string"), "Int.toString"); + (("string", "float"), "Float.fromString"); + (("string", "int"), "Int.fromString"); ] let print_simple_conversion ppf (actual, expected) = diff --git a/tests/build_tests/super_errors/expected/array_item_type_mismatch.res.expected b/tests/build_tests/super_errors/expected/array_item_type_mismatch.res.expected index 54c99ad256..af3c2e059d 100644 --- a/tests/build_tests/super_errors/expected/array_item_type_mismatch.res.expected +++ b/tests/build_tests/super_errors/expected/array_item_type_mismatch.res.expected @@ -14,4 +14,4 @@ - Convert all values in the array to the same type. - Use a tuple, if your array is of fixed length. Tuples can mix types freely, and compiles to a JavaScript array. Example of a tuple: `let myTuple = (10, "hello", 15.5, true) - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/collections.res.expected b/tests/build_tests/super_errors/expected/collections.res.expected index 41b190b6f8..2e83a91635 100644 --- a/tests/build_tests/super_errors/expected/collections.res.expected +++ b/tests/build_tests/super_errors/expected/collections.res.expected @@ -9,4 +9,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/dict_inference.res.expected b/tests/build_tests/super_errors/expected/dict_inference.res.expected index 42f32b879e..d7a4a84f49 100644 --- a/tests/build_tests/super_errors/expected/dict_inference.res.expected +++ b/tests/build_tests/super_errors/expected/dict_inference.res.expected @@ -10,4 +10,4 @@ This has type: string But this function argument is expecting: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/dict_pattern_inference_constrained.res.expected b/tests/build_tests/super_errors/expected/dict_pattern_inference_constrained.res.expected index ce340c0ae9..7263287dfb 100644 --- a/tests/build_tests/super_errors/expected/dict_pattern_inference_constrained.res.expected +++ b/tests/build_tests/super_errors/expected/dict_pattern_inference_constrained.res.expected @@ -14,4 +14,4 @@ The incompatible parts: int vs string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/function_argument_mismatch.res.expected b/tests/build_tests/super_errors/expected/function_argument_mismatch.res.expected index 3905f123f6..50caaf7ca6 100644 --- a/tests/build_tests/super_errors/expected/function_argument_mismatch.res.expected +++ b/tests/build_tests/super_errors/expected/function_argument_mismatch.res.expected @@ -10,4 +10,4 @@ This has type: int But this function argument is expecting: string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/highlighting1.res.expected b/tests/build_tests/super_errors/expected/highlighting1.res.expected index 3e79ccf230..4fd22bb259 100644 --- a/tests/build_tests/super_errors/expected/highlighting1.res.expected +++ b/tests/build_tests/super_errors/expected/highlighting1.res.expected @@ -10,4 +10,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/highlighting2.res.expected b/tests/build_tests/super_errors/expected/highlighting2.res.expected index 3701c234bb..4c7cb77157 100644 --- a/tests/build_tests/super_errors/expected/highlighting2.res.expected +++ b/tests/build_tests/super_errors/expected/highlighting2.res.expected @@ -10,4 +10,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/highlighting3.res.expected b/tests/build_tests/super_errors/expected/highlighting3.res.expected index 7845aa238d..292a9c4902 100644 --- a/tests/build_tests/super_errors/expected/highlighting3.res.expected +++ b/tests/build_tests/super_errors/expected/highlighting3.res.expected @@ -10,4 +10,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/highlighting5.res.expected b/tests/build_tests/super_errors/expected/highlighting5.res.expected index 6e6ab03a30..6062a37b7d 100644 --- a/tests/build_tests/super_errors/expected/highlighting5.res.expected +++ b/tests/build_tests/super_errors/expected/highlighting5.res.expected @@ -10,4 +10,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/if_branch_mismatch.res.expected b/tests/build_tests/super_errors/expected/if_branch_mismatch.res.expected index 714a6f074f..ac887753f2 100644 --- a/tests/build_tests/super_errors/expected/if_branch_mismatch.res.expected +++ b/tests/build_tests/super_errors/expected/if_branch_mismatch.res.expected @@ -13,4 +13,4 @@ if expressions must return the same type in all branches (if, else if, else). - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/inline_types_record_type_params.res.expected b/tests/build_tests/super_errors/expected/inline_types_record_type_params.res.expected index be9fed0456..8a69447541 100644 --- a/tests/build_tests/super_errors/expected/inline_types_record_type_params.res.expected +++ b/tests/build_tests/super_errors/expected/inline_types_record_type_params.res.expected @@ -11,4 +11,4 @@ This has type: int But it's expected to have type: string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/math_operator_constant.res.expected b/tests/build_tests/super_errors/expected/math_operator_constant.res.expected index 741b07af7a..d500a914ab 100644 --- a/tests/build_tests/super_errors/expected/math_operator_constant.res.expected +++ b/tests/build_tests/super_errors/expected/math_operator_constant.res.expected @@ -10,5 +10,5 @@ This has type: float But it's expected to have type: int - You can convert float to int with Belt.Float.toInt. + You can convert float to int with Float.toInt. If this is a literal, try a number without a trailing dot (e.g. 20). \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/math_operator_float.res.expected b/tests/build_tests/super_errors/expected/math_operator_float.res.expected index cd0fffa1c1..640e57a500 100644 --- a/tests/build_tests/super_errors/expected/math_operator_float.res.expected +++ b/tests/build_tests/super_errors/expected/math_operator_float.res.expected @@ -16,5 +16,5 @@ - Ensure all values in this calculation has the type float. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat. - Change the operator to +, which works on int - You can convert int to float with Belt.Int.toFloat. + You can convert int to float with Int.toFloat. If this is a literal, try a number with a trailing dot (e.g. 20.). \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/primitives1.res.expected b/tests/build_tests/super_errors/expected/primitives1.res.expected index cc2946f160..761f341aba 100644 --- a/tests/build_tests/super_errors/expected/primitives1.res.expected +++ b/tests/build_tests/super_errors/expected/primitives1.res.expected @@ -9,5 +9,5 @@ This has type: int But it's expected to have type: float - You can convert int to float with Belt.Int.toFloat. + You can convert int to float with Int.toFloat. If this is a literal, try a number with a trailing dot (e.g. 20.). \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/primitives11.res.expected b/tests/build_tests/super_errors/expected/primitives11.res.expected index 6123d58b2c..be16c35ee0 100644 --- a/tests/build_tests/super_errors/expected/primitives11.res.expected +++ b/tests/build_tests/super_errors/expected/primitives11.res.expected @@ -16,4 +16,4 @@ Further expanded: int vs string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/primitives2.res.expected b/tests/build_tests/super_errors/expected/primitives2.res.expected index 29b554f51e..a967be243a 100644 --- a/tests/build_tests/super_errors/expected/primitives2.res.expected +++ b/tests/build_tests/super_errors/expected/primitives2.res.expected @@ -9,4 +9,4 @@ This has type: int But string concatenation is expecting: string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/primitives6.res.expected b/tests/build_tests/super_errors/expected/primitives6.res.expected index 6e46bb560a..cc665fac7b 100644 --- a/tests/build_tests/super_errors/expected/primitives6.res.expected +++ b/tests/build_tests/super_errors/expected/primitives6.res.expected @@ -10,5 +10,5 @@ This has type: int But it's expected to have type: float - You can convert int to float with Belt.Int.toFloat. + You can convert int to float with Int.toFloat. If this is a literal, try a number with a trailing dot (e.g. 20.). \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/primitives7.res.expected b/tests/build_tests/super_errors/expected/primitives7.res.expected index 95aa2472c5..a6fe42b223 100644 --- a/tests/build_tests/super_errors/expected/primitives7.res.expected +++ b/tests/build_tests/super_errors/expected/primitives7.res.expected @@ -16,5 +16,5 @@ - Ensure all values in this calculation has the type float. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat. - Change the operator to +, which works on int - You can convert int to float with Belt.Int.toFloat. + You can convert int to float with Int.toFloat. If this is a literal, try a number with a trailing dot (e.g. 20.). \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/primitives9.res.expected b/tests/build_tests/super_errors/expected/primitives9.res.expected index b97f1b023d..c9068c29fb 100644 --- a/tests/build_tests/super_errors/expected/primitives9.res.expected +++ b/tests/build_tests/super_errors/expected/primitives9.res.expected @@ -8,4 +8,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/record_type_spreads_deep_sub.res.expected b/tests/build_tests/super_errors/expected/record_type_spreads_deep_sub.res.expected index cdbe9281a9..aa0178907f 100644 --- a/tests/build_tests/super_errors/expected/record_type_spreads_deep_sub.res.expected +++ b/tests/build_tests/super_errors/expected/record_type_spreads_deep_sub.res.expected @@ -11,4 +11,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/set_record_field_type_match.res.expected b/tests/build_tests/super_errors/expected/set_record_field_type_match.res.expected index d45880d7d2..fc634f630c 100644 --- a/tests/build_tests/super_errors/expected/set_record_field_type_match.res.expected +++ b/tests/build_tests/super_errors/expected/set_record_field_type_match.res.expected @@ -10,4 +10,4 @@ You're assigning something to this field that has type: int But this record field is of type: string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/stdlib_removed_in_error.res.expected b/tests/build_tests/super_errors/expected/stdlib_removed_in_error.res.expected index b9dbe7d8cd..5de1d8a86e 100644 --- a/tests/build_tests/super_errors/expected/stdlib_removed_in_error.res.expected +++ b/tests/build_tests/super_errors/expected/stdlib_removed_in_error.res.expected @@ -13,4 +13,4 @@ The incompatible parts: string vs int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/syntaxErrors4.res.expected b/tests/build_tests/super_errors/expected/syntaxErrors4.res.expected index 5cf63f8107..942d5cce09 100644 --- a/tests/build_tests/super_errors/expected/syntaxErrors4.res.expected +++ b/tests/build_tests/super_errors/expected/syntaxErrors4.res.expected @@ -15,4 +15,4 @@ This has type: string But it's expected to have type: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/type1.res.expected b/tests/build_tests/super_errors/expected/type1.res.expected index 6bc3692c57..4ca40ba83b 100644 --- a/tests/build_tests/super_errors/expected/type1.res.expected +++ b/tests/build_tests/super_errors/expected/type1.res.expected @@ -8,5 +8,5 @@ This has type: int But it's expected to have type: float - You can convert int to float with Belt.Int.toFloat. + You can convert int to float with Int.toFloat. If this is a literal, try a number with a trailing dot (e.g. 20.). \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/type2.res.expected b/tests/build_tests/super_errors/expected/type2.res.expected index 1b189ab2be..59a05b86eb 100644 --- a/tests/build_tests/super_errors/expected/type2.res.expected +++ b/tests/build_tests/super_errors/expected/type2.res.expected @@ -11,4 +11,4 @@ This has type: string But this function argument is expecting: int - You can convert string to int with Belt.Int.fromString. \ No newline at end of file + You can convert string to int with Int.fromString. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/unicode_location.res.expected b/tests/build_tests/super_errors/expected/unicode_location.res.expected index e14d1c9d76..c7ac9eb8e5 100644 --- a/tests/build_tests/super_errors/expected/unicode_location.res.expected +++ b/tests/build_tests/super_errors/expected/unicode_location.res.expected @@ -10,4 +10,4 @@ This has type: int But string concatenation is expecting: string - You can convert int to string with Belt.Int.toString. \ No newline at end of file + You can convert int to string with Int.toString. \ No newline at end of file From 9a0c53990298346980872dfef1f68139c2d1b1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Fri, 9 May 2025 13:15:33 +0100 Subject: [PATCH 2/2] Add CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34079500d0..9b356217bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,10 @@ - AST: Add bar location to `case`. https://github.com/rescript-lang/rescript/pull/7407 +#### :nail_care: Polish + +- In type errors, recommend stdlib over Belt functions for converting between float/int/string. https://github.com/rescript-lang/rescript/pull/7453 + # 12.0.0-alpha.12 #### :bug: Bug fix