Repository navigation
[12.x] Keep tagged template calls whose result is unused - #9454
Open
illusionalsagacity wants to merge 2 commits into
Open
illusionalsagacity wants to merge 2 commits into
illusionalsagacity wants to merge 2 commits into
Conversation
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>
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #9420 to 12.x.
Problem
Js_analyzer.no_side_effect_expressiontreated 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
@stringpolymorphic variant passed to an external was printed, and so evaluated, twice; and a@taggedTemplateexternal called in function form,tag(strings, values), with strings that aren't literals madeJs_dumpfail an assertion.Before (the compiler stops at
dynamicPart):After:
Fix
As on master:
Js_dumpprints the tag as a callee is, with parentheses around a function, conditional orawait, and the interpolated values, which${}encloses, without parentheses.Differences from master:
@stringvariant 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.@taggedTemplateexternal 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_templateandstring_to_template). Otherwise it is a plain call.taggedTemplatetype, so a tag is always a@taggedTemplateexternal, and master's tests of inlined function, conditional and awaited tags can't be written in ReScript. Their printing is tested intests/ounit_tests/ounit_js_dump_tests.mlinstead.@stringand function-form fixes, and not the parentheses, which 12.x code can't reach.Tests
tests/tests/src/tagged_template_effect_test.rescalls a counting tag fromtagged_template_effect_lib.jswith its result unused at the top level, in alet _, an unused binding,ignore,->ignore, a discarded array and anifbranch, and checks that the tag ran. It also covers@stringpayloads with effects,@ignorearguments, a tagged template as aforloop start and a boolean operand, an interpolated conditional, and function-form calls with literal, escaped and dynamic strings.Ounit_js_dump_testscovers the printing of tags and values and the template escaping. No other module intests/testschanges output.Investigated by Claude.