Bug 23773 – array length assignment in assert condition should error
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-03-11T14:34:16Z
Last change time
2023-03-31T08:44:55Z
Assigned to
No Owner
Creator
Nick Treleaven
Comments
Comment #0 by nick — 2023-03-11T14:34:16Z
int[] a;
if (a.length = i) {} // error
assert(a.length = i); // not caught
A variable assignment is caught:
int i, j;
if (i = j) {} // error
assert(i = j); // error
Comment #2 by razvan.nitu1305 — 2023-03-13T16:30:11Z
But I do get an error:
test.d(5): Error: cannot modify constant `a.length`
And that happens because `a.length = i` gets analyzed before the compiler gets the chance to error about the assert. It makes sense that the assignment needs to be semantically correct before it's checked if it's in an assert.
I don't think this bug report is valid.
Comment #3 by razvan.nitu1305 — 2023-03-13T16:31:14Z
But I do get an error:
test.d(5): Error: cannot modify constant `a.length`
And that happens because `a.length = i` gets analyzed before the compiler gets the chance to error about the assert. It makes sense that the assignment needs to be semantically correct before it's checked if it's in an assert.
I don't think this bug report is valid.
Comment #4 by razvan.nitu1305 — 2023-03-13T16:31:54Z
Scratch that, when I tested locally I had a static array, instead of a dynamic one. The bug report is valid.
Comment #5 by razvan.nitu1305 — 2023-03-14T10:02:15Z
Well, the compiler rewrites `a.length = i` to `_d_arraysetlengthT(a, i)` so that's why the assert error is not caught.
Comment #6 by dlang-bot — 2023-03-14T13:06:03Z
@RazvanN7 created dlang/dmd pull request #14985 "Add a lowering field for AssignExp to store potential lowerings" mentioning this issue:
- Add a lowering field for AssignExp to store potential lowerings and use it for array length expressions + Fix Issue 23773
https://github.com/dlang/dmd/pull/14985
Comment #7 by dlang-bot — 2023-03-31T08:44:55Z
dlang/dmd pull request #14985 "Add a `lowering` field for AssignExp to store potential lowerings" was merged into master:
- e6df5b96bcf1c38f44d3a151fdaf6a2381e17564 by RazvanN7:
Fix Issue 23773 - Add a lowering field for AssignExp to store potential lowerings and use it for array length expressions
https://github.com/dlang/dmd/pull/14985