Skip to content

Put typeof and functions in parentheses where their precedence needs them - #9375

Open
mununki wants to merge 3 commits into
masterfrom
parenthesize-typeof-and-arrow-functions
Open

mununki wants to merge 3 commits into
masterfrom
parenthesize-typeof-and-arrow-functions

Conversation

@mununki

@mununki mununki commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Fixes: #9353

Js_dump printed typeof e without parentheses wherever it stood, and a function without them except as a callee or the operand of typeof. As the object of a member access or a method call, typeof x.length read the length of x, so String.length((typeof(x) :> string)) returned "number" instead of 6. An arrow function took what followed it into its body, a syntax error: x => x + 1 | 0.length as the object of a member access, and x => x + 1 | 0 === y => y + 1 | 0 as an operand of ===. A function expression, as a @this function is printed, read as a declaration as the object of a method call at the start of a statement, also a syntax error.

typeof now takes parentheses above level 13, as ! does, and a function above level 2, as a conditional does: as an operand, the test of ?:, the object of a call or member access, and the operand of typeof, ! or await. Arguments, initializers, the right side of = and returned values take none.

  • The branches of ?: take a function at level 1, also one in Some, as in use(c ? Some(n => n + 1) : o), which is printed as the function itself. Cond prints them at level 3 only to put a nested conditional in parentheses, so c ? x => x + 1 | 0 : x => x + 2 | 0 stays as it was.
  • pp_function adds the parentheses, since it prints a function that only calls a name as that name, which needs none: arity(x => u(x)) stays u.length. The Call, Tagged_template and Typeof cases no longer put a function in parentheses themselves, so a function as the tag of a tagged template still takes one pair.
  • No checked-in output changes.

Tests: typeof_and_function_paren_test.res checks typeof as the object of a member access and of a method call, an arrow function and an async one as the object of a member access, an arrow function as the object of a method call, a @this function as the object of a member access and of a method call in a statement, an arrow function as an operand of ===, and arrow functions as the branches of ?:, also in Some, which take no parentheses. Compiled with master, the module fails to load with a SyntaxError, and without the cases that give one, the typeof checks return "number" and "string".

Checklist

  • Added a changelog fragment (see changes/README.md), or no release notes are needed and a maintainer should apply changelog:skip.
  • Added tests for the fix or feature.
  • Gave every commit a description below its subject, saying why it is made (see "Commit messages" in CONTRIBUTING.md).
  • Signed off every commit for the DCO (git commit -s).

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix missing parentheses around typeof and functions used as the object of a member access or a call, or as an operand.

Semantic Diff

3 commits · Full page

✏️ Put typeof and functions in parentheses where their precedence needs them

fe36a70 · mununki · Nov 22, 2026, 04:48 UTC · edited

Why

Js_dump printed typeof e without parentheses wherever it stood, and a function without them except as a callee or the operand of typeof. As the object of a member access or a method call, typeof x.length read the length of x, an arrow function took the access into its body, as in x => x + 1 | 0.length, a syntax error, and a function expression at the start of a statement read as a declaration. An arrow function as an operand of === gave a syntax error as well. typeof now takes parentheses above level 13, as ! does, and a function above level 2, as a conditional does.

JS modified (typeof as an object)

let x: unknown = %raw(`"abc"`)
let len = String.length(typeof(x) :> string)
let up = (typeof(x) :> string)->String.toUpperCase

js

-let len = typeof x.length;
+let len = (typeof x).length;
 
-let up = typeof x.toUpperCase();
+let up = (typeof x).toUpperCase();

JS modified (function as an object or an operand)

@get external arity: ('a => 'b) => int = "length"
@send external callThis: (@this ('self, int) => int, Nullable.t<'s>, int) => int = "call"
let n = arity(x => x + 1)
let same = (x => x + 1) === (y => y + 1)
callThis(@this (_, x) => x + 1, Nullable.null, 1)->ignore

js

-let n = x => x + 1 | 0.length;
+let n = (x => x + 1 | 0).length;
 
-let same = x => x + 1 | 0 === y => y + 1 | 0;
+let same = (x => x + 1 | 0) === (y => y + 1 | 0);
 
-function (x) {
+(function (x) {
   return x + 1 | 0;
-}.call(null, 1);
+}).call(null, 1);

Review

  • Where does a function take parentheses now? Where a conditional does: as an operand, the test of ?:, the object of a call or member access, and the operand of typeof, ! or await. Arguments, initializers, the right side of = and returned values take none.
  • Why not in the branches of ?:? Cond prints them at level 3 only to put a nested conditional in parentheses. A function needs none there and is printed at level 1, so c ? x => x + 1 | 0 : x => x + 2 | 0 stays as it was.
  • Why does pp_function add them? It prints a function that only calls a name as that name, which needs none: arity(x => u(x)) stays u.length. The Call, Tagged_template and Typeof cases no longer put a function in parentheses themselves.
  • Which checked-in outputs change? None.
Test a function as a branch of ?: in the parentheses test

7ef1db2 · mununki · Nov 22, 2026, 04:57 UTC

A branch of ?: prints a function at level 1, so it takes no parentheses there. Codecov found that no test had a function in a branch, so that case of Cond was not run.

✨ Print a function in Some as a branch of ?: without parentheses

8150388 · mununki · Nov 28, 2026, 12:55 UTC · new

Why

A branch of ?: is printed at level 3, so that a nested conditional takes parentheses, and at level 1 when it is a function, which needs none. A function in Some, as in use(c ? Some(n => n + 1) : o) with an option<int => int> argument, reaches Cond as Optional_block (Fun _, true), which is printed as the function itself. branch_level did not match it, so the previous commits gave it parentheses that master does not print. It now looks through such an option, as the review of #9375 suggested.

JS modified (function in Some as a branch of ?:)

@val external use: option<int => int> => unit = "use"
let q = (c, o) => use(c ? Some(n => n + 1) : o)

js

 function q(c, o) {
-  use(c ? (n => n + 1 | 0) : o);
+  use(c ? n => n + 1 | 0 : o);
 }

Tests: typeof_and_function_paren_test.res passes a function in Some as a branch of ?:. Compiled with the previous commit, its output has the parentheses. No other checked-in output changes.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 655,718,201 → 655,564,121 (-0.02%).

Generated projects: 629,292,681 → 629,285,284 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,224,546,672 → 9,224,341,578 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 26,662,166 → 26,662,309 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 8150388 compared with base 83cf1de in this run.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.34%. Comparing base (83cf1de) to head (8150388).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9375   +/-   ##
=======================================
  Coverage   82.34%   82.34%           
=======================================
  Files         462      462           
  Lines       65802    65802           
=======================================
  Hits        54183    54183           
  Misses      11619    11619           
Files with missing lines Coverage Δ
compiler/backend/js_dump.ml 92.59% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9375

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9375

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9375

@rescript/win32-x64

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

commit: 8150388

@github-actions

Copy link
Copy Markdown

@mununki
mununki requested review from cknitt and cristianoc November 22, 2026 14:17
@mununki
mununki force-pushed the parenthesize-typeof-and-arrow-functions branch from f6e1a86 to a270105 Compare November 23, 2026 08:31
@cknitt

cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thanks for the fix and the thorough tests!

From a review by Claude (Claude Code):

A function inside Some(...) in a branch of ?: now gets parentheses it didn't get before:

@val external use: option<int => int> => unit = "use"
let q = (c, o) => use(c ? Some(n => n + 1) : o)

Master prints use(c ? n => n + 1 | 0 : o);, and this PR prints use(c ? (n => n + 1 | 0) : o);.

The output is still valid JS. The branch reaches Cond as Optional_block (Fun _, true), which branch_level doesn't match, so it is printed at level 3. Looking through it would keep the output as it was:

let rec branch_level (e : J.expression) =
  match e.expression_desc with
  | Fun _ -> 1
  | Optional_block (e, true) -> branch_level e
  | _ -> 3

No checked-in test hits this case, which is why no output changed. A line in typeof_and_function_paren_test.res would cover it.

Apart from this, the change looks correct to me.

…them

Why:
    `Js_dump` printed `typeof e` without parentheses wherever it stood, and
    a function without them except as a callee or the operand of `typeof`.
    As the object of a member access or a method call, `typeof x.length`
    read the length of `x`, an arrow function took the access into its
    body, as in `x => x + 1 | 0.length`, a syntax error, and a `function`
    expression at the start of a statement read as a declaration. An arrow
    function as an operand of `===` gave a syntax error as well. `typeof`
    now takes parentheses above level 13, as `!` does, and a function above
    level 2, as a conditional does.

JS modified (typeof as an object):
Example:
    let x: unknown = %raw(`"abc"`)
    let len = String.length(typeof(x) :> string)
    let up = (typeof(x) :> string)->String.toUpperCase
Before (js):
    let len = typeof x.length;

    let up = typeof x.toUpperCase();
After (js):
    let len = (typeof x).length;

    let up = (typeof x).toUpperCase();

JS modified (function as an object or an operand):
Example:
    @get external arity: ('a => 'b) => int = "length"
    @send external callThis: (@this ('self, int) => int, Nullable.t<'s>, int) => int = "call"
    let n = arity(x => x + 1)
    let same = (x => x + 1) === (y => y + 1)
    callThis(@this (_, x) => x + 1, Nullable.null, 1)->ignore
Before (js):
    let n = x => x + 1 | 0.length;

    let same = x => x + 1 | 0 === y => y + 1 | 0;

    function (x) {
      return x + 1 | 0;
    }.call(null, 1);
After (js):
    let n = (x => x + 1 | 0).length;

    let same = (x => x + 1 | 0) === (y => y + 1 | 0);

    (function (x) {
      return x + 1 | 0;
    }).call(null, 1);

Review: Where does a function take parentheses now? Where a conditional
    does: as an operand, the test of `?:`, the object of a call or member
    access, and the operand of `typeof`, `!` or `await`. Arguments,
    initializers, the right side of `=` and returned values take none.
Review: Why not in the branches of `?:`? `Cond` prints them at level 3
    only to put a nested conditional in parentheses. A function needs none
    there and is printed at level 1, so `c ? x => x + 1 | 0 : x => x + 2 | 0`
    stays as it was.
Review: Why does `pp_function` add them? It prints a function that only
    calls a name as that name, which needs none: `arity(x => u(x))` stays
    `u.length`. The `Call`, `Tagged_template` and `Typeof` cases no longer
    put a function in parentheses themselves.
Review: Which checked-in outputs change? None.

Signed-off-by: mununki <woonki.moon@gmail.com>
A branch of `?:` prints a function at level 1, so it takes no
parentheses there. Codecov found that no test had a function in a
branch, so that case of `Cond` was not run.

Signed-off-by: mununki <woonki.moon@gmail.com>
Why:
    A branch of `?:` is printed at level 3, so that a nested conditional
    takes parentheses, and at level 1 when it is a function, which needs
    none. A function in `Some`, as in `use(c ? Some(n => n + 1) : o)` with
    an `option<int => int>` argument, reaches `Cond` as
    `Optional_block (Fun _, true)`, which is printed as the function itself.
    `branch_level` did not match it, so the previous commits gave it
    parentheses that master does not print. It now looks through such an
    option, as the review of #9375 suggested.

JS modified (function in Some as a branch of ?:):
Example:
    @Val external use: option<int => int> => unit = "use"
    let q = (c, o) => use(c ? Some(n => n + 1) : o)
Before (js):
    function q(c, o) {
      use(c ? (n => n + 1 | 0) : o);
    }
After (js):
    function q(c, o) {
      use(c ? n => n + 1 | 0 : o);
    }

Tests: `typeof_and_function_paren_test.res` passes a function in `Some` as a
branch of `?:`. Compiled with the previous commit, its output has the
parentheses. No other checked-in output changes.

Signed-off-by: mununki <woonki.moon@gmail.com>
@mununki
mununki force-pushed the parenthesize-typeof-and-arrow-functions branch from a270105 to 8150388 Compare November 28, 2026 13:05
@mununki

mununki commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review. Done in 8150388: branch_level looks through Optional_block (e, true), so use(c ? Some(n => n + 1) : o) compiles to use(c ? n => n + 1 | 0 : o) again, and typeof_and_function_paren_test.res passes a function in Some as a branch of ?:.

The branch also needed a rebase onto master. There, #9420 made the tag of a tagged template take parentheses itself when it is a function, as Call did. Since pp_function adds them in this PR, the rebased fe36a70 drops that check as well, so the function tag in tagged_template_effect_test.mjs keeps one pair and its output does not change.

@cknitt

cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T13:43:08.668691Z 8150388 Manual 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 81503886f1

ℹ️ 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".

This branch has not been deployed

No deployments
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.

Missing parentheses around typeof and an arrow function before a member access

2 participants