3064 – Invalid array operation accepted, generates bad code

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3064 - Invalid array operation accepted, generates bad code
Summary: Invalid array operation accepted, generates bad code
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86_64 Linux
: P2 major
Assignee: No Owner
URL: http://www.digitalmars.com/d/1.0/arra...
Keywords: accepts-invalid, wrong-code
Depends on:
Blocks:
Reported: 2009年06月11日 09:23 UTC by Matti Niemenmaa
Modified: 2014年04月18日 09:12 UTC (History)
3 users (show)

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 Matti Niemenmaa 2009年06月11日 09:23:41 UTC
The following compiles in DMD 1.045, but shouldn't:
void main() {
	int[] a = [1,2];
	int[] b = [1,2];
	a[] += b;
	assert (b[0] == 1);
	assert (b[1] == 2);
	assert (a[0] == 2);
	assert (a[1] == 4);
}
Currently, the code compiles but the third assertion fails, since DMD generates code as though b were an int. Note that the following both compiles and doesn't assert:
void main() {
	int[] a = [1,2];
	int[] b = [1,2];
	a[] += b;
	assert (b[0] == 1);
	assert (b[1] == 2);
	assert (a[0] == 1+*cast(int*)&b);
	assert (a[1] == 2+*cast(int*)&b);
}
This is, of course, nonsense.
The error is in the line 'a[] += b': according to http://www.digitalmars.com/d/1.0/arrays.html "[t]he rvalue can be an expression consisting either of an array slice of the same length and type as the lvalue or an expression of the element type of the lvalue, in any combination." Thus the line is incorrect and should read 'a[] += b[]', and that indeed works.
Comment 1 Don 2010年05月03日 01:39:52 UTC
Fixed DMD1.059 and 2.044
Comment 2 bearophile_hugs 2010年05月03日 04:08:26 UTC
Reopened, because this wrong code compiles still with dmd v2.044, the bug persists:
void main() {
 int[] a = [1,2];
 int[] b = [1,2];
 a[] += b;
 assert (b[0] == 1);
 assert (b[1] == 2);
 assert (a[0] == 2);
 assert (a[1] == 4);
}
Comment 3 Don 2010年05月03日 04:34:59 UTC
(In reply to comment #2)
> Reopened, because this wrong code compiles still with dmd v2.044, the bug
> persists:
> 
> void main() {
> int[] a = [1,2];
> int[] b = [1,2];
> a[] += b;
> assert (b[0] == 1);
> assert (b[1] == 2);
> assert (a[0] == 2);
> assert (a[1] == 4);
> }
Oops, it's fixed only in my personal copy, not in the official DMD.
Comment 4 Walter Bright 2010年05月31日 19:03:32 UTC
http://www.dsource.org/projects/dmd/changeset/509 


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