mirror of
https://github.com/dlang/dmd.git
synced 2025-05-06 11:06:46 +03:00
Fix Issue 24078 - Fold constants on array concatenation only for strings
Without this limitation, the code could incorrectly concatenate `["c"] ~ "a" ~ "b"` as `["c"] ~ "ab"`, which was incorrect. Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
This commit is contained in:
parent
e3eccfe9a5
commit
b349b6d0de
2 changed files with 23 additions and 11 deletions
|
@ -1280,10 +1280,16 @@ Expression Expression_optimize(Expression e, int result, bool keepLvalue)
|
||||||
//printf("CatExp::optimize(%d) %s\n", result, e.toChars());
|
//printf("CatExp::optimize(%d) %s\n", result, e.toChars());
|
||||||
if (binOptimize(e, result))
|
if (binOptimize(e, result))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (e.type == Type.tstring)
|
||||||
if (auto ce1 = e.e1.isCatExp())
|
if (auto ce1 = e.e1.isCatExp())
|
||||||
{
|
{
|
||||||
// https://issues.dlang.org/show_bug.cgi?id=12798
|
// https://issues.dlang.org/show_bug.cgi?id=12798
|
||||||
// optimize ((expr ~ str1) ~ str2)
|
// optimize ((expr ~ str1) ~ str2)
|
||||||
|
// https://issues.dlang.org/show_bug.cgi?id=24078
|
||||||
|
// This optimization is only valid if `expr` is a string.
|
||||||
|
// Otherwise it leads to:
|
||||||
|
// `["c"] ~ "a" ~ "b"` becoming `["c"] ~ "ab"`
|
||||||
scope CatExp cex = new CatExp(e.loc, ce1.e2, e.e2);
|
scope CatExp cex = new CatExp(e.loc, ce1.e2, e.e2);
|
||||||
cex.type = e.type;
|
cex.type = e.type;
|
||||||
Expression ex = Expression_optimize(cex, result, false);
|
Expression ex = Expression_optimize(cex, result, false);
|
||||||
|
|
6
compiler/test/runnable/test24078.d
Normal file
6
compiler/test/runnable/test24078.d
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
//https://issues.dlang.org/show_bug.cgi?id=24078
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
assert(["c"] ~ "a" ~ "b" == ["c", "a", "b"]);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue