Bug 12153 – Ternary operator on static array lvalues creates copy

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-13T11:39:00Z
Last change time
2015-02-18T03:38:29Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
dlang-bugzilla

Comments

Comment #0 by dlang-bugzilla — 2014-02-13T11:39:54Z
void main() { int[1] i, j; bool b = true; (b ? i : j) = [4]; assert(i == [4]); }
Comment #1 by yebblies — 2014-02-15T07:21:21Z
Sigh, the backend sees int[1] i = 0; int[1] j = 0; bool b = true; (b ? i : j)[] = [4]; assert(i == [4]); return 0; Another bug caused by lowering static array ops to slice ops.
Comment #2 by k.hara.pg — 2014-02-19T01:47:31Z
(In reply to comment #1) > Sigh, the backend sees > > int[1] i = 0; > int[1] j = 0; > bool b = true; > (b ? i : j)[] = [4]; > assert(i == [4]); > return 0; > > Another bug caused by lowering static array ops to slice ops. Translating to the slice ops is not a problem. This is a glue-layer bug for cond expression. https://github.com/D-Programming-Language/dmd/pull/3285
Comment #3 by yebblies — 2014-02-19T02:07:20Z
(In reply to comment #2) > (In reply to comment #1) > > Sigh, the backend sees > > > > int[1] i = 0; > > int[1] j = 0; > > bool b = true; > > (b ? i : j)[] = [4]; > > assert(i == [4]); > > return 0; > > > > Another bug caused by lowering static array ops to slice ops. > > Translating to the slice ops is not a problem. This is a glue-layer bug for > cond expression. > Sure it is. If instead of converting `a = b` (where lhs is a static array) into `a[] = b[]` the compiler left the lhs intact as a static array, then converting the condexp to an lvalue would have resulted in `*(b ? &i : &j) = [4]` and this bug would not have occurred. > https://github.com/D-Programming-Language/dmd/pull/3285
Comment #4 by k.hara.pg — 2014-02-19T02:31:26Z
(In reply to comment #3) > Sure it is. If instead of converting `a = b` (where lhs is a static array) > into `a[] = b[]` the compiler left the lhs intact as a static array, then > converting the condexp to an lvalue would have resulted in `*(b ? &i : &j) = > [4]` and this bug would not have occurred. But in the cond exp, both i and j are already lvalue. So translating lvalue (b ? i : j) to lvalue *(b ? &i : &j) is redundant in front-end layer and will never occur.
Comment #5 by yebblies — 2014-02-19T02:38:16Z
(In reply to comment #4) > (In reply to comment #3) > > Sure it is. If instead of converting `a = b` (where lhs is a static array) > > into `a[] = b[]` the compiler left the lhs intact as a static array, then > > converting the condexp to an lvalue would have resulted in `*(b ? &i : &j) = > > [4]` and this bug would not have occurred. > > But in the cond exp, both i and j are already lvalue. So translating lvalue (b > ? i : j) to lvalue *(b ? &i : &j) is redundant in front-end layer and will > never occur. Sure it will. This: int a, b, c; void main() { c ? a : b = c; } results in this getting sent to codegen: *(c ? & a : & b) = c;
Comment #6 by github-bugzilla — 2014-02-23T14:19:12Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7438d1227a17436077e3df68ba40ef1ff8e0788c fix Issue 12153 - Ternary operator on static array lvalues creates copy https://github.com/D-Programming-Language/dmd/commit/ec7cf0a99f791495d142ed8a8799a022c0be84ef Merge pull request #3285 from 9rnsr/fix12153 Issue 12153 - Ternary operator on static array lvalues creates copy
Comment #7 by github-bugzilla — 2015-02-06T07:29:59Z
Comment #8 by github-bugzilla — 2015-02-07T20:38:21Z
Comment #9 by github-bugzilla — 2015-02-18T03:38:29Z