Skip to content

Ignore inferred arity in case of %raw. #7542

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 1 commit into from
Jun 18, 2025

Conversation

cristianoc
Copy link
Collaborator

The inferred arity of raw JS code is actively used with %ffi, but it can produce unexpected results with %raw:

let foo: int => int = %raw(`function add(x, y=5){ return x + y }`)

Console.log(foo(2))

This PR tests turning off the use of inferred arity so that int => int above is just taken at face value.

tsnobip added a commit to tsnobip/rescript-relay-router that referenced this pull request Jun 12, 2025
avoids a type error with rescript v12
cf rescript-lang/rescript#7542
@cristianoc cristianoc requested review from tsnobip and Copilot June 12, 2025 14:40
Copilot

This comment was marked as outdated.

Copy link
Member

@tsnobip tsnobip left a comment

Choose a reason for hiding this comment

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

@cristianoc this means inferred arity will be ignored even when there's no type signature, right? It's dangerous anyway since it doesn't reflect it in the types.
I guess you can still use %ffi if you want to type your js code. We should likely document %ffi by the way.

The inferred arity of raw JS code is actively used with `%ffi`, but it can produce unexpected results with `%raw`:

```
let foo: int => int = %raw(`function add(x, y=5){ return x + y }`)

Console.log(foo(2))
```
@cristianoc cristianoc force-pushed the ignore-inferred-arity-with-raw branch from b21c81d to 327280d Compare June 18, 2025 08:45
@cristianoc cristianoc changed the title Test: ignore inferred arity in case of %raw. Ignore inferred arity in case of %raw. Jun 18, 2025
@cristianoc cristianoc requested a review from Copilot June 18, 2025 08:45
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR disables the inferred arity for raw JavaScript code passed to %raw, ensuring that the provided type annotations (e.g. int => int) are respected rather than inferring parameters from the raw JS function.

  • Updated tests to validate the behavior with raw JS code.
  • Removed logic handling arity inference for %raw code in the compiler passes.
  • Updated the CHANGELOG to document this bug fix.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/tests/src/raw_arity.res Adds a test using %raw to confirm that arity inference is disabled.
tests/tests/src/raw_arity.mjs Mirrors the Rescript test for raw JS code in JavaScript.
compiler/core/lam_pass_collect.ml Removes the arity inference logic for %raw JS code in the lam pass collection.
compiler/core/lam_arity_analysis.ml Removes the arity inference handling for %raw JS functions in arity analysis.
CHANGELOG.md Documents the bug fix regarding raw JS arity inference.
Comments suppressed due to low confidence (2)

compiler/core/lam_pass_collect.ml:64

  • Ensure that the removal of arity inference is paired with proper fallback handling for %raw usage, and consider adding a clarifying comment to explain why this case is now ignored.
    | Lprim {primitive = Psome | Psome_not_nest; args = [v]} ->

compiler/core/lam_arity_analysis.ml:69

  • Confirm that dropping this case does not affect other related arity analyses, and add inline documentation linking this change to the PR rationale for future maintainability.
    with

Copy link

pkg-pr-new bot commented Jun 18, 2025

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/win32-x64

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

commit: 327280d

@cristianoc cristianoc merged commit 15ebdcb into master Jun 18, 2025
17 checks passed
@cristianoc cristianoc deleted the ignore-inferred-arity-with-raw branch June 18, 2025 09:25
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.

2 participants