Skip to content

Keep tagged template calls whose result is unused - #9420

Merged
cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-tagged-template-effect
Nov 27, 2026
Merged

cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-tagged-template-effect

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Part of #9398 (item 3).

Problem

Js_analyzer treated a tagged template as free of side effects when its tag and values were, so a tagged template whose result is unused (let _ =, ->ignore) was removed and the tag function never ran. A tagged template is a call of its tag.

let tag: taggedTemplate<int, int> = Obj.magic(() => 1)

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

Before:

function unusedResult() {

}

After:

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

Reviewing this showed that the calls it keeps could print wrongly. A tag that inlining replaced with a function, a conditional or an await had no parentheses (this already affected templates whose result is used):

let use = (tag: taggedTemplate<int, int>) => tag`x${1}`
let useFunction = () => use(Obj.magic((_s, _v) => 1))
let useConditional = (c, a, b) => use(c ? a : b)
let useAwaited = async p => use(await p)

Before:

function useFunction() {
  return (_s, _v) => 1`x${1}`;
}

function useConditional(c, a, b) {
  return c ? a : b`x${1}`;
}

async function useAwaited(p) {
  return await p`x${1}`;
}

After:

function useFunction() {
  return ((_s, _v) => 1)`x${1}`;
}

function useConditional(c, a, b) {
  return (
    c ? a : b
  )`x${1}`;
}

async function useAwaited(p) {
  return (await p)`x${1}`;
}

Fix

no_side_effect_expression_desc treats Tagged_template like Call. The tag is printed as a callee is, and the interpolated values without needless parentheses.

Tests

tagged_template_effect_test.res covers discarded results and inlined tags. No other checked-in output changes.

12.x

12.x has the same bug, and I have a backport ready on fix-tagged-template-effect-12.x. I'll open it against maintenance/12.x once this one is reviewed.

The fix was written by Claude. A second Claude agent reviewed the first version; this version addresses that review and has not been reviewed again.

Why:
    `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. A tagged template is a call of its tag, so it is now treated
    like a call.

    `Js_dump` printed the tag at the level of the surrounding expression.
    A tag that inlining replaced with a function, a conditional or an
    `await` had no parentheses: the template bound to the last branch of
    the conditional and to the awaited promise, and a function was a syntax
    error. The tag is now printed as a callee is. The interpolated values,
    which `${}` encloses, are printed without parentheses.

JS modified (unused tagged templates):
Example:
    let tag: taggedTemplate<int, int> = Obj.magic(() => 1)

    let unusedResult = () => {
      let _ = tag`a ${1}`
    }
Before (js):
    function unusedResult() {

    }
After (js):
    function unusedResult() {
      tag`a ${1}`;
    }

JS modified (tags of tagged templates):
Example:
    let use = (tag: taggedTemplate<int, int>) => tag`x${1}`
    let useFunction = () => use(Obj.magic((_s, _v) => 1))
    let useConditional = (c, a, b) => use(c ? a : b)
    let useAwaited = async p => use(await p)
Before (js):
    function useFunction() {
      return (_s, _v) => 1`x${1}`;
    }

    function useConditional(c, a, b) {
      return c ? a : b`x${1}`;
    }

    async function useAwaited(p) {
      return await p`x${1}`;
    }
After (js):
    function useFunction() {
      return ((_s, _v) => 1)`x${1}`;
    }

    function useConditional(c, a, b) {
      return (
        c ? a : b
      )`x${1}`;
    }

    async function useAwaited(p) {
      return (await p)`x${1}`;
    }

JS modified (interpolated values of tagged templates):
Example:
    let f = (tag: taggedTemplate<int, int>, c) => tag`x${c ? 1 : 2}` + 1
Before (js):
    function f(tag, c) {
      return tag`x${(
        c ? 1 : 2
      )}` + 1 | 0;
    }
After (js):
    function f(tag, c) {
      return tag`x${c ? 1 : 2}` + 1 | 0;
    }

Review: Are tagged templates whose result is used affected? Only in how
    their tag and interpolated values are parenthesised.
Review: Which checked-in outputs change? Only the new test's snapshot.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a tagged template call whose result is unused being removed, so its tag function never ran, and the missing parentheses around a function, conditional or awaited tag.

Semantic Diff

1 commit · Full page

Keep tagged template calls whose result is unused

da4bea8 · illusionalsagacity · Nov 23, 2026, 20:40 UTC

Why

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. A tagged template is a call of its tag, so it is now treated like a call.

Js_dump printed the tag at the level of the surrounding expression. A tag that inlining replaced with a function, a conditional or an await had no parentheses: the template bound to the last branch of the conditional and to the awaited promise, and a function was a syntax error. The tag is now printed as a callee is. The interpolated values, which ${} encloses, are printed without parentheses.

JS modified (unused tagged templates)

let tag: taggedTemplate<int, int> = Obj.magic(() => 1)

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

js

 function unusedResult() {
-
+  tag`a ${1}`;
 }

JS modified (tags of tagged templates)

let use = (tag: taggedTemplate<int, int>) => tag`x${1}`
let useFunction = () => use(Obj.magic((_s, _v) => 1))
let useConditional = (c, a, b) => use(c ? a : b)
let useAwaited = async p => use(await p)

js

 function useFunction() {
-  return (_s, _v) => 1`x${1}`;
+  return ((_s, _v) => 1)`x${1}`;
 }
 
 function useConditional(c, a, b) {
-  return c ? a : b`x${1}`;
+  return (
+    c ? a : b
+  )`x${1}`;
 }
 
 async function useAwaited(p) {
-  return await p`x${1}`;
+  return (await p)`x${1}`;
 }

JS modified (interpolated values of tagged templates)

let f = (tag: taggedTemplate<int, int>, c) => tag`x${c ? 1 : 2}` + 1

js

 function f(tag, c) {
-  return tag`x${(
-    c ? 1 : 2
-  )}` + 1 | 0;
+  return tag`x${c ? 1 : 2}` + 1 | 0;
 }

Review

  • Are tagged templates whose result is used affected? Only in how their tag and interpolated values are parenthesised.
  • Which checked-in outputs change? Only the new test's snapshot.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.23%. Comparing base (45dba2c) to head (da4bea8).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9420   +/-   ##
=======================================
  Coverage   82.22%   82.23%           
=======================================
  Files         461      462    +1     
  Lines       65573    65580    +7     
=======================================
+ Hits        53916    53927   +11     
+ Misses      11657    11653    -4     
Files with missing lines Coverage Δ
compiler/backend/js_analyzer.ml 80.76% <100.00%> (-0.11%) ⬇️
compiler/backend/js_dump.ml 94.21% <100.00%> (+0.13%) ⬆️

... and 5 files with indirect coverage changes

🚀 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 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: da4bea8

@cknitt

cknitt commented Nov 26, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 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-26T20:24:09.802976Z da4bea8 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. Another round soon, please!

Reviewed commit: da4bea8162

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

@illusionalsagacity
illusionalsagacity marked this pull request as ready for review November 27, 2026 20:00

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. I built it, checked the fix against master with extra cases, and found no problems.

This review was done by Claude.

@cknitt
cknitt merged commit 1f729ec into rescript-lang:master Nov 27, 2026
23 checks passed
@illusionalsagacity
illusionalsagacity deleted the fix-tagged-template-effect branch November 27, 2026 21:50
illusionalsagacity added a commit to illusionalsagacity/rescript-compiler that referenced this pull request Nov 27, 2026
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>
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