D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14040 - Doesn't use assignment in slice
Summary: Doesn't use assignment in slice
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 blocker
Assignee: No Owner
URL:
Keywords: pull, wrong-code
Depends on:
Blocks:
 
Reported: 2015-01-24 23:57 UTC by Richard (Rikki) Andrew Cattermole
Modified: 2016-01-03 14:02 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Richard (Rikki) Andrew Cattermole 2015-01-24 23:57:58 UTC
Example code:
import std.stdio;

void main() {
	uint[] values = [0, 1, 2, 3, 4, 5, 6, 7];
	uint offset = 2;

	writeln(values[offset .. offset += 2]);
	writeln(offset);
}

Outputs:
[]
4

Expected output:
[2, 3]
4

Works correctly in LDC 2.063.2 (thanks DPaste).
Doesn't work in DMD 2.065 or 2.066.1
Comment 1 bearophile_hugs 2015-01-25 00:05:34 UTC
Code like this is ugly, if I see code like this I refactor it immediately:

values[offset .. offset += 2]


Into two lines like:

values[offset .. offset + 2]
offset += 2;
Comment 2 Kenji Hara 2015-01-26 14:19:46 UTC
(In reply to Richard Cattermole from comment #0)
> Works correctly in LDC 2.063.2 (thanks DPaste).
> Doesn't work in DMD 2.065 or 2.066.1

dmd 2.063 prints same output with 2.066. I guess that the behavior difference comes from the the difference of glue layer implementations of LDC and DMD (currently DMD, LDC, and GCC have their own glue layers to fit to each backends).
Comment 3 Kenji Hara 2015-01-26 15:18:20 UTC
https://github.com/D-Programming-Language/dmd/pull/4345

As far as I see, old versions of dmd have same behavior with git-head, so it's not a dmd regression.
But, the current behavior is contrary to the deterministic evaluation order, so I think it should be fixed.

Downgrade importance to 'blocker'.
Comment 4 github-bugzilla 2015-10-14 01:33:54 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/4fe170a3a3f2d5c3a4751f2c3e90ac6380583aab
fix Issue 14040 - Doesn't use assignment in slice

If upr has any side effects, lwr should be copied to temporary always.

https://github.com/D-Programming-Language/dmd/commit/13f41930d43825f5fa88dbfb994946b67160656e
Merge pull request #4345 from 9rnsr/fix14040

Issue 14040 - Doesn't use assignment in slice