Semantic Diff

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

3 commits

83cf1de1984f1b00d45c63202a0fe032c079ce4f..pr-9375

fe36a705a · mununki · Nov 22, 2026, 04:48 UTC

Put typeof and functions in parentheses where their precedence needs 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.

modifiedJS typeof as an object

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

modifiedJS 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
Before
After
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; 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.
7ef1db234 · mununki · Nov 22, 2026, 04:57 UTC

Test a function as a branch of ?: in the parentheses test

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.

81503886f · mununki · Nov 28, 2026, 12:55 UTC

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

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.

modifiedJS 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
Before
After
function q(c, o) {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.