Bug 21881 – Array operation fails to compile even though it really should

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-04-30T00:42:47Z
Last change time
2024-12-13T19:16:01Z
Assigned to
No Owner
Creator
Blatnik
Moved to GitHub: dmd#19914 →

Comments

Comment #0 by blatblatnik — 2021-04-30T00:42:47Z
Comment #1 by blatblatnik — 2021-04-30T00:45:40Z
float[3] add42(float[3] a) { return a[] + 42; } Fails to compile with "Error: array operation `a[] + cast(float)3` without destination memory not allowed." But float[3] add42(float[3] a) { float[3] result = a[] + 42; return result; } compiles just fine. I'm not sure if this is a bug, but if it was intended, I don't see a reason why we couldn't do that.
Comment #2 by blatblatnik — 2021-04-30T00:53:50Z
I also don't understand why the error message I get from that is different from other useless statements. For example: 1 + 1; // Error: `1 + 1` has no effect int[1] i = [1]; i[] + i[]; // Error: array operation `i[] + i[]` without destination memory not allowed. I would have expected the same error message for both of these. It just seems like an inconsistency though, and not an actual problem.
Comment #3 by blatblatnik — 2021-04-30T10:59:15Z
I just realized that this is especially annoying when dealing with multi-dimensional arrays. float[2] row0 = [1, 2]; float[2] row1 = [3, 4]; float[2][2] matrix = [ row0[] + row0[], // Error: array operation without destination memory not allowed. row1[] + row1[] // Error: array operation without destination memory not allowed. ]; The error message says there is no destination memory even though there clearly is.
Comment #4 by johan_forsberg_86 — 2021-04-30T11:17:45Z
This one is annoying. Also, imo we should allow these kinds of operations using temporaries.
Comment #5 by robert.schadek — 2024-12-13T19:16:01Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19914 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB