Skip to content

Conversation

@cknitt
Copy link
Member

@cknitt cknitt commented Oct 22, 2025

Fixes #7972

When the external function has a single arg that gets a fixed value via @as(json, all that's left is a actually constant, see test.

@cknitt cknitt requested a review from cristianoc October 22, 2025 15:46
in
let fn_type = Ast_helper.Typ.arrows ~loc args result_type in
( build_uncurried_type ~arity:(List.length args) fn_type,
( fn_type,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_uncurried_type was trying to set a function type even if no args were left.
But the function type with the correct arity is already set by Ast_helper.Typ.arrows anyway when required.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 22, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7978

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7978

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7978

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7978

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7978

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@7978

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7978

commit: 1badbec

@cknitt
Copy link
Member Author

cknitt commented Oct 22, 2025

When the external function has a single arg that gets a fixed value via @as(json, all that's left is a actually constant, see test.

Or do we want/need to add an "artificial" unit argument there to keep it a function?

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the function without any args gets called automatically seems pretty weird to me.
I would have expected this would not be allowed.
But if that's what one wants, OK.

Notice the risk is this change affects other cases unrelated to this. But I assume some though went into removing build_uncurried_type as mentioned in the comment.

@cknitt
Copy link
Member Author

cknitt commented Oct 23, 2025

The fact that the function without any args gets called automatically seems pretty weird to me.
I would have expected this would not be allowed.
But if that's what one wants, OK.

Not sure if that's what one wants. 🙂 I can look into introducing an "artificial" unit arg.

Notice the risk is this change affects other cases unrelated to this. But I assume some though went into removing build_uncurried_type as mentioned in the comment.

build_uncurried_type only sets the same arity that arrows already set before.
(Unless fn_type is not actually a function, in which case it crashes.)

@cknitt cknitt requested a review from cristianoc October 23, 2025 08:41
@cknitt
Copy link
Member Author

cknitt commented Oct 23, 2025

@cristianoc now adding a synthetic unit argument, please have another look.

@cknitt
Copy link
Member Author

cknitt commented Oct 23, 2025

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1029 to 1031
( Ast_helper.Typ.arrows ~loc args result_type,
External_ffi_types.ffi_bs arg_type_specs return_wrapper ffi,
unused_attrs,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve uncurried arity when rebuilding external type

Previously this code rebuilt the return type with build_uncurried_type ~arity:(List.length args) so externals declared with an uncurried arrow retained their arity metadata. After the change it returns Ast_helper.Typ.arrows directly and the build_uncurried_type wrapper is never applied in the non-object path, so externals such as external add: (int, int) => int are now typed as int -> int -> int. Calls written with uncurried syntax (add(1, 2)) will start raising Apply_non_function/arity mismatches during type checking because the arity flag is gone. The new code should still apply build_uncurried_type to the reconstructed arrow to keep uncurried externals working.

Useful? React with 👍 / 👎.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex But Ast_helper.Typ.arrows does already set the arity for the outer function to List.length args.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not true, external add: (int, int) => int still works correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How comes codex runs now? That changed?
It used to get stuck trying to install ocaml.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it didn't actually run anything, it just reviews the code.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.
Btw conceptually this just builds a function of arity 0 (the same way as arity 2 with unit type). I thought we already had helpers that take arity as arg and support 0.
But not 100% sure.

@cknitt cknitt merged commit 3565257 into master Oct 23, 2025
25 checks passed
@cknitt cknitt deleted the fix-external-as branch October 23, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@as in extern can crash compiler

2 participants