Skip to content

Commit d7ffff5

Browse files
cknittclaude
andauthored
Fix the sign of @inline bigint constants (#8732)
* Fix the sign of @inline bigint constants Bigint_utils.parse_bigint returns whether the literal is positive, but Ast_external_mk.inline_bigint bound that flag as `negative`, so the stored inline constant had its sign inverted. Translation passed the flag straight through as Lambda's positive `sign`, inverting it back, so generated JavaScript was correct; signatures and error messages printed `@inline(-12n)` for `@inline(12n)`. Store the real `negative` flag and convert it to Lambda's sign in translcore. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Store @inline bigint signs as positive, like every other layer Bigint_utils.parse_bigint, Asttypes.Const_bigint, Lambda.Const_bigint and Js_op.bigint_lit all record whether a bigint is positive. The inline constant was the only one recording `negative`, which is how its sign got inverted. Use `positive` there too, so the value passes through the frontend and translation unchanged. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b15ccd1 commit d7ffff5

12 files changed

Lines changed: 75 additions & 8 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Keep comments on labelled parameters with default values, and before `=?` in optional arrow-type arguments, when formatting. https://github.com/rescript-lang/rescript/pull/8690
2929
- Fix invalid JavaScript that exported names left unbound when a module's toplevel always throws. https://github.com/rescript-lang/rescript/pull/8692
3030
- Make module inclusion error messages independent of the length of the source file path. https://github.com/rescript-lang/rescript/pull/8691
31+
- Fix the sign of `@inline` bigint constants printed in signatures and error messages, which showed `@inline(-12n)` for `@inline(12n)`. https://github.com/rescript-lang/rescript/pull/8732
3132

3233
#### :memo: Documentation
3334

‎compiler/frontend/ast_external_mk.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let inline_bool b = inline_const (Const_bool b)
5555
let inline_int i = inline_const (Const_int i)
5656

5757
let inline_bigint s =
58-
let negative, digits = Bigint_utils.parse_bigint s in
59-
inline_const (Const_bigint {negative; digits})
58+
let positive, digits = Bigint_utils.parse_bigint s in
59+
inline_const (Const_bigint {positive; digits})
6060

6161
let inline_float s = inline_const (Const_float s)

‎compiler/ml/external_ffi_types.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type inline_const =
8282
source spelling is not needed after FFI processing. *)
8383
| Const_bool of bool
8484
| Const_int of int32
85-
| Const_bigint of {negative: bool; digits: string}
85+
| Const_bigint of {positive: bool; digits: string}
8686
| Const_float of string
8787

8888
type t =

‎compiler/ml/external_ffi_types.mli‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type inline_const =
8282
source spelling is not needed after FFI processing. *)
8383
| Const_bool of bool
8484
| Const_int of int32
85-
| Const_bigint of {negative: bool; digits: string}
85+
| Const_bigint of {positive: bool; digits: string}
8686
| Const_float of string
8787

8888
type t = private

‎compiler/ml/translcore.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ let lambda_of_inline_const (c : External_ffi_types.inline_const) :
456456
| Const_bool true -> Const_js_true
457457
| Const_bool false -> Const_js_false
458458
| Const_int i -> Const_int i
459-
| Const_bigint {negative; digits} -> Const_bigint (negative, digits)
459+
| Const_bigint {positive; digits} -> Const_bigint (positive, digits)
460460
| Const_float f -> Const_float f
461461

462462
(* The argument of the dynamic-import primitive is a module reference,

‎compiler/syntax/src/res_outcome_printer.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ let print_inline_const_doc (c : External_ffi_types.inline_const) =
483483
| Const_string s -> print_string_literal_doc s
484484
| Const_bool b -> Doc.text (if b then "true" else "false")
485485
| Const_int i -> Doc.text (Int32.to_string i)
486-
| Const_bigint {negative; digits} ->
487-
Doc.text ((if negative then "-" else "") ^ digits ^ "n")
486+
| Const_bigint {positive; digits} ->
487+
Doc.text ((if positive then "" else "-") ^ digits ^ "n")
488488
| Const_float f -> Doc.text f
489489

490490
let print_external_module_doc (emn : External_ffi_types.external_module_name) =

‎tests/ERROR_VARIANTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Wrapper symptoms attached to inclusion failures. Source: `type symptom` in [incl
366366
| Variant | Status | Fixture | Notes |
367367
|---|---|---|---|
368368
| `Missing_field` | ✓ | `super_errors_multi/Iface_missing_value` | |
369-
| `Value_descriptions` | ✓ | `super_errors_multi/Iface_value_descriptions`, `super_errors_multi/Iface_value_arity_mismatch`, `super_errors_multi/Smoke_interface_mismatch`, `super_errors_multi/Cross_external_spec_mismatch`, `super_errors_multi/Cross_external_payload_name`, `super_errors_multi/Cross_external_import_attrs`, `module_sig_value_arity_mismatch*.res`, `object_value_signature_set_mismatch.res` | Arity mismatches print a dedicated hint (implementation vs interface argument counts), including through aliases and nested function types. The object fixture pins that a value signature cannot drop `@set` from an object type (moregeneral requires equal field mutability). |
369+
| `Value_descriptions` | ✓ | `super_errors_multi/Iface_value_descriptions`, `super_errors_multi/Iface_value_arity_mismatch`, `super_errors_multi/Smoke_interface_mismatch`, `super_errors_multi/Cross_external_spec_mismatch`, `super_errors_multi/Cross_external_payload_name`, `super_errors_multi/Cross_external_import_attrs`, `module_sig_value_arity_mismatch*.res`, `object_value_signature_set_mismatch.res`, `inline_bigint_signature_mismatch.res` | Arity mismatches print a dedicated hint (implementation vs interface argument counts), including through aliases and nested function types. The object fixture pins that a value signature cannot drop `@set` from an object type (moregeneral requires equal field mutability). The inline-bigint fixture pins the sign printed for `@inline(<bigint>)` values. |
370370
| `Type_declarations` | ✓ | `super_errors_multi/Iface_type_decl_record`, `super_errors_multi/Iface_type_decl_variant`, `RecordInclusion.res`, `type_decl_function_arity_mismatch.res` | |
371371
| `Extension_constructors` | ✓ | `super_errors_multi/Iface_extension_constructors` | |
372372
| `Module_types` | ✓ | `super_errors_multi/Iface_module_types` | |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inline_bigint_signature_mismatch.res:4:5-7:1
4+
5+
2 │ @inline(12n) let pos: bigint
6+
3 │ @inline(-5n) let neg: bigint
7+
4 │ } = {
8+
5 │  let pos = 12n
9+
6 │  let neg = -5n
10+
7 │ }
11+
8 │
12+
13+
Signature mismatch:
14+
Modules do not match:
15+
{
16+
let pos: bigint
17+
let neg: bigint
18+
}
19+
is not included in
20+
{
21+
@inline(12n) let pos: bigint
22+
@inline(-5n) let neg: bigint
23+
}
24+
Values do not match:
25+
let pos: bigint
26+
is not included in
27+
@inline(12n) let pos: bigint
28+
/.../fixtures/inline_bigint_signature_mismatch.res:2:3-30:
29+
Expected declaration
30+
/.../fixtures/inline_bigint_signature_mismatch.res:5:7-9:
31+
Actual declaration
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module M: {
2+
@inline(12n) let pos: bigint
3+
@inline(-5n) let neg: bigint
4+
} = {
5+
let pos = 12n
6+
let neg = -5n
7+
}

‎tests/tests/src/inline_const.mjs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ console.log([
88
3e-6
99
]);
1010

11+
function BigintConsts(X) {
12+
return {
13+
both: [
14+
12n,
15+
-5n
16+
]
17+
};
18+
}
19+
1120
let x = true;
1221

1322
function N1(funarg) {
@@ -24,5 +33,6 @@ export {
2433
N1,
2534
h,
2635
hh,
36+
BigintConsts,
2737
}
2838
/* Not a pure module */

0 commit comments

Comments
 (0)