Skip to content

[12.x] Keep tagged template calls whose result is unused - #9454

Open
illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-tagged-template-effect-12.x
Open

illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-tagged-template-effect-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9420 to 12.x.

Problem

Js_analyzer.no_side_effect_expression treated a tagged template as pure when its tag and interpolated values were pure, so a tagged template whose result is unused (let _ =, ->ignore, an unused binding, a discarded array) was removed and the tag function never ran.

Keeping those calls exposed two more problems on 12.x: the payload of a @string polymorphic variant passed to an external was printed, and so evaluated, twice; and a @taggedTemplate external called in function form, tag(strings, values), with strings that aren't literals made Js_dump fail an assertion.

@module("./lib.js") @taggedTemplate
external tag: (array<string>, array<int>) => int = "tag"

@module("./lib.js")
external on: @string [#click(int) | #close(string)] => string = "on"

let unusedResult = () => {
  let _ = tag`a ${1}`
}

let next = () => Math.Int.random(0, 10)
let payload = () => on(#click(next()))

let dynamicPart = s => tag([s, "!"], [1])

Before (the compiler stops at dynamicPart):

function tag(prim0, prim1) {
  return LibJs.tag(prim0, ...prim1);
}

function on(prim) {
  return LibJs.on(prim.NAME, prim.VAL);
}

function unusedResult() {
  
}

function next() {
  return Stdlib_Math.Int.random(0, 10);
}

function payload() {
  return LibJs.on(({
    NAME: "click",
    VAL: Stdlib_Math.Int.random(0, 10)
  }).NAME, ({
    NAME: "click",
    VAL: Stdlib_Math.Int.random(0, 10)
  }).VAL);
}

function dynamicPart(s) {
  return LibJs.tag`
Fatal error: exception File "compiler/core/js_dump.ml", line 711, characters 13-19: Assertion failed

After:

function tag(prim0, prim1) {
  return LibJs.tag(prim0, ...prim1);
}

function on(prim) {
  return LibJs.on(prim.NAME, prim.VAL);
}

function unusedResult() {
  LibJs.tag`a ${1}`;
}

function next() {
  return Stdlib_Math.Int.random(0, 10);
}

function payload() {
  return LibJs.on("click", Stdlib_Math.Int.random(0, 10));
}

function dynamicPart(s) {
  return LibJs.tag([
    s,
    "!"
  ], 1);
}

Fix

As on master:

  • A tagged template is treated as a call of its tag, so it is never pure.
  • Js_dump prints the tag as a callee is, with parentheses around a function, conditional or await, and the interpolated values, which ${} encloses, without parentheses.

Differences from master:

  • A @string variant literal passed to an external passes its tag and its payload, evaluated once, in place, whatever its effects. Master already does this since Evaluate a polymorphic variant argument of an external once, in place #9217.
  • A @taggedTemplate external called in function form is printed as a template only when every string is a literal and there is one more of them than values, with backticks and ${ in them escaped (Js_dump_string.escaped_to_template and string_to_template). Otherwise it is a plain call.
  • 12.x has no taggedTemplate type, so a tag is always a @taggedTemplate external, and master's tests of inlined function, conditional and awaited tags can't be written in ReScript. Their printing is tested in tests/ounit_tests/ounit_js_dump_tests.ml instead.
  • The CHANGELOG entry names the @string and function-form fixes, and not the parentheses, which 12.x code can't reach.

Tests

tests/tests/src/tagged_template_effect_test.res calls a counting tag from tagged_template_effect_lib.js with its result unused at the top level, in a let _, an unused binding, ignore, ->ignore, a discarded array and an if branch, and checks that the tag ran. It also covers @string payloads with effects, @ignore arguments, a tagged template as a for loop start and a boolean operand, an interpolated conditional, and function-form calls with literal, escaped and dynamic strings. Ounit_js_dump_tests covers the printing of tags and values and the template escaping. No other module in tests/tests changes output.

Investigated by Claude.

Js_analyzer.no_side_effect_expression treated a tagged template as pure
when its tag and interpolated values were pure, so one whose result is
unused (let _ =, ->ignore, an unused binding, a discarded array) was
removed and the tag function never ran. A tagged template is a call of
its tag, so treat it like a call.

Three changes keep the calls that are now printed correct:

- A @string variant literal passed to an external evaluates its payload
  once, whatever its effects. A payload with effects, such as a call, was
  printed twice.
- A @taggedTemplate external called in function form, tag(strings, values),
  is printed as a template only when the strings are literals, one more
  than the values. Otherwise it is a plain call, where Js_dump failed an
  assertion. Backticks and ${ in the literals are escaped.
- Js_dump prints the tag as a callee, and the interpolated values without
  needless parentheses.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Bring the backport in line with the version of rescript-lang#9420 merged on master
(1f729ec). Its changes to Js_analyzer and Js_dump are the same here.

12.x has no taggedTemplate type, so a tag is always a @taggedTemplate
external, and the inlined function, conditional and awaited tags of
master's tests can't be written in ReScript. Their printing is covered by
Ounit_js_dump_tests instead. The other changes on this branch keep the
calls that are no longer removed correct on 12.x: a @string variant
payload is evaluated once (rescript-lang#9217 on master), and a @taggedTemplate
external called in function form with strings that aren't literals is a
plain call instead of a failed assertion in Js_dump.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 6616e25

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.

1 participant