Bug 24371 – [REG 2.104] String array concatenation does not respect operator precedence
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2024-02-05T15:50:31Z
Last change time
2024-03-15T08:20:40Z
Keywords
pull, wrong-code
Assigned to
No Owner
Creator
Dechcaudron
Comments
Comment #0 by dechcaudron+issues.dlang — 2024-02-05T15:50:31Z
Hi,
something odd happens when you mix parenthesis and variables to concatenate strings and string arrays. See code below:
```
unittest
{
// All works well with literals
assert("b" ~ "c" == "bc");
assert(["a"] ~ "b" == ["a", "b"]);
assert(["a"] ~ ("b" ~ "c") == ["a", "bc"]);
// Things work well as long as you only reference one variable
auto strArr = ["a"];
assert(strArr ~ ("b" ~ "c") == ["a", "bc"]);
auto str = "c";
assert(["a"] ~ ("b" ~ str) == ["a", "bc"]);
// Things stop working when both operands are variables
// This should not pass
assert(strArr ~ ("b" ~ str) == ["a", "b", "c"]);
// This should not fail
assert(strArr ~ ("b" ~ str) == ["a", "bc"]);
}
```
When the issue is reproduced, the compiler behaves as if the parenthesis were not there and appends each individual string to the string array.
This code used to work years ago (unfortunately I cannot remember the DMD version I was using back then, but it was 2021). I don't know if it also happens in systems other than Linux.