Skip to content

Commit 672a159

Browse files
committed
Improve code generated for string templates.
Now that `j` interpolation is gone, string append can be optimized in the back-end. Fixes #5521
1 parent 40fc40b commit 672a159

9 files changed

+23
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ These are only breaking changes for unformatted code.
7878
- PPX V3: allow uncurried `make` function and treat it like a curried one https://github.com/rescript-lang/rescript-compiler/pull/6081
7979
- Add support for `|>` in uncurried mode by desugaring it https://github.com/rescript-lang/rescript-compiler/pull/6083
8080
- Change the compilation of pattern matching for variants so it does not depends on variats being integers https://github.com/rescript-lang/rescript-compiler/pull/6085
81+
- Improve code generated for string templates https://github.com/rescript-lang/rescript-compiler/pull/6090
8182

8283
# 10.1.4
8384

jscomp/core/js_exp_make.ml

+2
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ let rec string_append ?comment (e : t) (el : t) : t =
515515
{ e with expression_desc = Str { txt = a ^ b; delim } }
516516
in
517517
match (e.expression_desc, el.expression_desc) with
518+
| Str { txt = ""}, _ -> el
519+
| _, Str { txt = ""} -> e
518520
| ( Str { txt = a; delim },
519521
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
520522
when delim = delim_ ->

jscomp/test/caml_format_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function from_of_string(xs) {
8888
var b = param[1];
8989
var a = param[0];
9090
return [
91-
"of_string " + String(i) + "",
91+
"of_string " + String(i),
9292
(function (param) {
9393
return {
9494
TAG: /* Eq */0,
@@ -156,7 +156,7 @@ var suites = Pervasives.$at(from_of_string(of_string), Pervasives.$at({
156156
var b = param[1];
157157
var a = param[0];
158158
return [
159-
"infinity_of_string " + String(i) + "",
159+
"infinity_of_string " + String(i),
160160
(function (param) {
161161
return {
162162
TAG: /* Eq */0,
@@ -194,7 +194,7 @@ var suites = Pervasives.$at(from_of_string(of_string), Pervasives.$at({
194194
var b = param[1];
195195
var a = param[0];
196196
return [
197-
"normal_float_of_string " + String(i) + "",
197+
"normal_float_of_string " + String(i),
198198
(function (param) {
199199
return {
200200
TAG: /* Eq */0,
@@ -427,7 +427,7 @@ Mt.from_pair_suites("Caml_format_test", Pervasives.$at(suites, Pervasives.$at($$
427427
var f = param[1];
428428
var fmt = param[0];
429429
return [
430-
"loat_format " + String(i) + "",
430+
"loat_format " + String(i),
431431
(function (param) {
432432
return {
433433
TAG: /* Eq */0,

jscomp/test/ext_filename_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function find_root_filename(_cwd, filename) {
181181
_cwd = cwd$p;
182182
continue ;
183183
}
184-
var s = "" + filename + " not found from " + cwd;
184+
var s = filename + " not found from " + cwd;
185185
throw {
186186
RE_EXN_ID: "Failure",
187187
_1: s,

jscomp/test/int64_mul_div_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ function from_pairs(prefix, pairs) {
310310
var a = param[1];
311311
var result = param[0];
312312
return [
313-
"" + prefix + "_" + i,
313+
prefix + "_" + i,
314314
(function (param) {
315315
return commutative_mul(result, a, b);
316316
})

jscomp/test/mario_game.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3331,7 +3331,7 @@ function load(param) {
33313331
if (el !== null) {
33323332
canvas = el;
33333333
} else {
3334-
console.log("cant find canvas " + canvas_id + "");
3334+
console.log("cant find canvas " + canvas_id);
33353335
throw {
33363336
RE_EXN_ID: "Failure",
33373337
_1: "fail",

jscomp/test/template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
var bla2 = "";
55

66
function concat(param) {
7-
return "\n display:\r flex;\n " + bla2 + "";
7+
return "\n display:\r flex;\n " + bla2;
88
}
99

1010
exports.bla2 = bla2;

lib/es6/arg.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function add_help(speclist) {
120120
}
121121

122122
function usage_b(buf, speclist, errmsg) {
123-
$$Buffer.add_string(buf, "" + errmsg + "\n");
123+
$$Buffer.add_string(buf, errmsg + "\n");
124124
List.iter((function (param) {
125125
var doc = param[2];
126126
if (doc.length === 0) {
@@ -132,7 +132,7 @@ function usage_b(buf, speclist, errmsg) {
132132
return $$Buffer.add_string(buf, " " + key + " " + doc + "\n");
133133
}
134134
var sym = make_symlist("{", "|", "}", spec._0);
135-
return $$Buffer.add_string(buf, " " + key + " " + sym + "" + doc + "\n");
135+
return $$Buffer.add_string(buf, " " + key + " " + sym + doc + "\n");
136136
}), add_help(speclist));
137137
}
138138

@@ -202,17 +202,17 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist
202202
case "-help" :
203203
break;
204204
default:
205-
$$Buffer.add_string(b, "" + progname + ": unknown option '" + s + "'.\n");
205+
$$Buffer.add_string(b, progname + ": unknown option '" + s + "'.\n");
206206
}
207207
break;
208208
case /* Wrong */1 :
209-
$$Buffer.add_string(b, "" + progname + ": wrong argument '" + error._1 + "'; option '" + error._0 + "' expects " + error._2 + ".\n");
209+
$$Buffer.add_string(b, progname + ": wrong argument '" + error._1 + "'; option '" + error._0 + "' expects " + error._2 + ".\n");
210210
break;
211211
case /* Missing */2 :
212-
$$Buffer.add_string(b, "" + progname + ": option '" + error._0 + "' needs an argument.\n");
212+
$$Buffer.add_string(b, progname + ": option '" + error._0 + "' needs an argument.\n");
213213
break;
214214
case /* Message */3 :
215-
$$Buffer.add_string(b, "" + progname + ": " + error._0 + ".\n");
215+
$$Buffer.add_string(b, progname + ": " + error._0 + ".\n");
216216
break;
217217

218218
}

lib/js/arg.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function add_help(speclist) {
120120
}
121121

122122
function usage_b(buf, speclist, errmsg) {
123-
$$Buffer.add_string(buf, "" + errmsg + "\n");
123+
$$Buffer.add_string(buf, errmsg + "\n");
124124
List.iter((function (param) {
125125
var doc = param[2];
126126
if (doc.length === 0) {
@@ -132,7 +132,7 @@ function usage_b(buf, speclist, errmsg) {
132132
return $$Buffer.add_string(buf, " " + key + " " + doc + "\n");
133133
}
134134
var sym = make_symlist("{", "|", "}", spec._0);
135-
return $$Buffer.add_string(buf, " " + key + " " + sym + "" + doc + "\n");
135+
return $$Buffer.add_string(buf, " " + key + " " + sym + doc + "\n");
136136
}), add_help(speclist));
137137
}
138138

@@ -202,17 +202,17 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist
202202
case "-help" :
203203
break;
204204
default:
205-
$$Buffer.add_string(b, "" + progname + ": unknown option '" + s + "'.\n");
205+
$$Buffer.add_string(b, progname + ": unknown option '" + s + "'.\n");
206206
}
207207
break;
208208
case /* Wrong */1 :
209-
$$Buffer.add_string(b, "" + progname + ": wrong argument '" + error._1 + "'; option '" + error._0 + "' expects " + error._2 + ".\n");
209+
$$Buffer.add_string(b, progname + ": wrong argument '" + error._1 + "'; option '" + error._0 + "' expects " + error._2 + ".\n");
210210
break;
211211
case /* Missing */2 :
212-
$$Buffer.add_string(b, "" + progname + ": option '" + error._0 + "' needs an argument.\n");
212+
$$Buffer.add_string(b, progname + ": option '" + error._0 + "' needs an argument.\n");
213213
break;
214214
case /* Message */3 :
215-
$$Buffer.add_string(b, "" + progname + ": " + error._0 + ".\n");
215+
$$Buffer.add_string(b, progname + ": " + error._0 + ".\n");
216216
break;
217217

218218
}

0 commit comments

Comments
 (0)