Skip to content

Workaround for @asin labels in uncurried externals. #6527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- GenType: only export types (not values) from module types. https://github.com/rescript-lang/rescript-compiler/pull/6516
- Fix compiler crash with unboxed variant definition with only 1 constructor. https://github.com/rescript-lang/rescript-compiler/pull/6523
- GenType: support mutual recursive types inside modules. https://github.com/rescript-lang/rescript-compiler/pull/6528
- Workaround for `@as`in labels in uncurried externals, which was broken. https://github.com/rescript-lang/rescript-compiler/pull/6527

# 11.0.0-rc.7

Expand Down
23 changes: 23 additions & 0 deletions jscomp/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ module ErrorMessages = struct
mean `#" ^ number ^ "`?"
end

module InExternal = struct
let status = ref false
end

let jsxAttr = (Location.mknoloc "JSX", Parsetree.PStr [])
let uncurriedAppAttr = (Location.mknoloc "res.uapp", Parsetree.PStr [])
let ternaryAttr = (Location.mknoloc "res.ternary", Parsetree.PStr [])
Expand Down Expand Up @@ -4276,6 +4280,22 @@ and parseEs6ArrowType ~attrs p =
p.uncurried_config |> Res_uncurried.fromDotted ~dotted
in
let loc = mkLoc startPos endPos in
let arity =
(* Workaround for ~lbl: @as(json`false`) _, which changes the arity *)
match argLbl with
| Labelled _s ->
let typ_is_any =
match typ.ptyp_desc with
| Ptyp_any -> true
| _ -> false
in
let has_as =
Ext_list.exists typ.ptyp_attributes (fun (x, _) -> x.txt = "as")
in
if !InExternal.status && typ_is_any && has_as then arity - 1
else arity
| _ -> arity
in
let tArg = Ast_helper.Typ.arrow ~loc ~attrs argLbl typ t in
if uncurried && (paramNum = 1 || p.uncurried_config = Legacy) then
(paramNum - 1, Ast_uncurried.uncurriedType ~loc ~arity tArg, 1)
Expand Down Expand Up @@ -5441,6 +5461,8 @@ and parseTypeDefinitionOrExtension ~attrs p =

(* external value-name : typexp = external-declaration *)
and parseExternalDef ~attrs ~startPos p =
let inExternal = !InExternal.status in
InExternal.status := true;
Parser.leaveBreadcrumb p Grammar.External;
Parser.expect Token.External p;
let name, loc = parseLident p in
Expand All @@ -5465,6 +5487,7 @@ and parseExternalDef ~attrs ~startPos p =
let loc = mkLoc startPos p.prevEndPos in
let vb = Ast_helper.Val.mk ~loc ~attrs ~prim name typExpr in
Parser.eatBreadcrumb p;
InExternal.status := inExternal;
vb

(* constr-def ::=
Expand Down
26 changes: 26 additions & 0 deletions jscomp/test/AsInUncurriedExternals.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions jscomp/test/AsInUncurriedExternals.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@@uncurried

@obj
external makeOptions: (
~objectMode: @as(json`false`) _,
~name: string,
~someOther: @as(json`true`) _,
unit,
) => int = ""

let mo = makeOptions

let options = mo(~name="foo", ())

let shouldNotFail: (~objectMode: @as(json`false`) _, ~name: string) => int = (~objectMode, ~name) =>
3
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.