Skip to content

Commit 6dcf79a

Browse files
authored
Fix crash with @get on external of type unit => 'a (#7866)
* Fix crash with `@get` on external of type `unit => 'a` * fix format * CHANGELOG
1 parent e1e3cf8 commit 6dcf79a

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#### :bug: Bug fix
2424

2525
- Fix code generation for emojis in polyvars and labels. https://github.com/rescript-lang/rescript/pull/7853
26+
- Fix crash with `@get` on external of type `unit => 'a`. https://github.com/rescript-lang/rescript/pull/7866
2627

2728
#### :memo: Documentation
2829

compiler/core/lam_compile_external_call.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ let translate_ffi ?(transformed_jsx = false) (cxt : Lam_compile_context.t)
421421
| [obj] ->
422422
let obj = translate_scoped_access scopes obj in
423423
E.dot obj name
424-
| _ -> assert false
424+
| _ ->
425+
(* This should have been caught in the frontend validation. *)
426+
invalid_arg
427+
"Internal compiler error: @get external called with wrong number of \
428+
arguments. Expected exactly one object argument."
425429
(* Note these assertion happens in call site *))
426430
| Js_set {js_set_name = name; js_set_scopes = scopes} -> (
427431
(* assert (js_splice = false) ; *)

compiler/frontend/ast_external_process.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,12 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
899899
tagged_template = _;
900900
} ->
901901
if arg_type_specs_length = 1 then
902-
Js_get {js_get_name = name; js_get_scopes = scopes}
902+
(* Check if the first argument is unit, which is invalid for @get *)
903+
match arg_type_specs with
904+
| [{arg_type = Extern_unit}] ->
905+
Location.raise_errorf ~loc
906+
"Ill defined attribute %@get (unit argument is not allowed)"
907+
| _ -> Js_get {js_get_name = name; js_get_scopes = scopes}
903908
else
904909
Location.raise_errorf ~loc
905910
"Ill defined attribute %@get (only one argument)"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/get_unit_arg.res:2:1-3:36
4+
5+
1 │ // Test case for issue #7676 - @get external with unit => 'a should give
6+
│ proper error
7+
2 │ @get
8+
3 │ external foo: unit => string = "foo"
9+
4 │
10+
5 │ let x = foo()
11+
12+
Ill defined attribute @get (unit argument is not allowed)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Test case for issue #7676 - @get external with unit => 'a should give proper error
2+
@get
3+
external foo: unit => string = "foo"
4+
5+
let x = foo()

0 commit comments

Comments
 (0)