66 – Bad length in value of T[a..b] = scalar

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 66 - Bad length in value of T[a..b] = scalar
Summary: Bad length in value of T[a..b] = scalar
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 critical
Assignee: Walter Bright
URL: http://www.digitalmars.com/drn-bin/ww...
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2006年03月22日 05:37 UTC by Stewart Gordon
Modified: 2014年02月15日 02:09 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Stewart Gordon 2006年03月22日 05:37:38 UTC
When a slice is assigned by a scalar, the value of the assignment expression has the length of the whole array, not that of the slice.
----------
import std.stdio;
void show(int[] s) {
 foreach (int i; s) {
 writef("%d ", i);
 }
 writefln();
}
void main() {
 int[] qwert = new int[6];
 int[] yuiop;
 yuiop = qwert[2..5] = 3;
 show(yuiop);
 show(qwert[2..5] = 4);
 show(qwert[2..5]);
 show(qwert);
 show(yuiop[2..5] = qwert[1..4]);
 yuiop = qwert[2..5];
 show(yuiop[1..3] = 6);
 writefln((yuiop[1..3] = 7).length);
}
----------
Output:
3 3 3 0 0 0
4 4 4 0 0 0
4 4 4
0 0 4 4 4 0
0 4 4
6 6 4
3
Expected output:
3 3 3
4 4 4
4 4 4
0 0 4 4 4 0
0 4 4
6 6
2
A testcase (array_chain.d) is also included in my DStress contribution apparently still waiting to be added (see bug 63).
Comment 1 Matti Niemenmaa 2006年04月03日 06:00:11 UTC
Fixed in 0.151.
Also, unless I'm mistaken, there is a bug in that test code: you have "show(yuiop[2..5] = qwert[1..4]);" at a point where yuiop's length is 3. Shouldn't that read "show(yuiop = qwert[1..4]);"? I get your expected output after that change.
Comment 2 Stewart Gordon 2006年04月04日 06:22:32 UTC
You're right that the testcase is buggy. However, the point was to compare the behaviour when copying a slice to a slice, whereas your proposed change turns it into a reference assignment.
A better corretion is simply to add
 yuiop.length = 6;
before that line.


AltStyle によって変換されたページ (->オリジナル) /