rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

`^10.0.0-alpha.1` template literals output "regression"

Open DZakh opened this issue 3 years ago • 6 comments

The compiler started adding empty strings around values in template literals. Technically doesn't break anything, but adds a redundant operation.

Example:

Js.log(`${1->Js.Int.toString}${2->Js.Int.toString}`)

Output:

console.log("" + (1).toString() + "" + (2).toString() + "");

Before:

console.log((1).toString() + (2).toString());

Context: https://github.com/DZakh/rescript-struct/pull/1#discussion_r914182947, https://github.com/rescript-lang/rescript-compiler/issues/5514#issuecomment-1176231169

DZakh avatar Jul 06 '22 13:07 DZakh

@DZakh see here: https://github.com/rescript-lang/syntax/pull/602

cristianoc avatar Jul 06 '22 18:07 cristianoc

That attempt at the syntax level is no good. Some cleanup is needed first. Then it should be doable in the compiler back-end.

cristianoc avatar Jul 07 '22 02:07 cristianoc

It's actually not clear how to do it in the compiler back-end without invasive changes. The code generated for normal (default "js") and "j" interpolation is the same: appending strings together. In the back-end, one cannot distinguish them. And only for "js" it is safe to remove "" + . So we won't try to do this optimisation for this version of the compiler.

cristianoc avatar Jul 08 '22 02:07 cristianoc

What about the trailing empty string + ""?

DZakh avatar Jul 08 '22 07:07 DZakh

Same

cristianoc avatar Jul 08 '22 07:07 cristianoc

Putting this under milestone 10.1.

If anyone is interested in contributing a PR meanwhile, that's great. That PR should be considered an optimisation, which should not fire for j`stuff` template expressions.

cristianoc avatar Jul 09 '22 07:07 cristianoc