D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3066 - Array operation without a slice as the lvalue accepted, bad codegen
Summary: Array operation without a slice as the lvalue accepted, bad codegen
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86_64 Linux
: P3 major
Assignee: No Owner
URL: http://www.digitalmars.com/d/1.0/arra...
Keywords: accepts-invalid, diagnostic, patch, spec, wrong-code
: 3815 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-06-13 06:50 UTC by Matti Niemenmaa
Modified: 2014-04-18 09:12 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Matti Niemenmaa 2009-06-13 06:50:26 UTC
The below code compiles, but shouldn't: array operations can't be used as initializers according to the spec, they need a slice as the lvalue.

void main() {
	auto x = [2,3,4];
	auto y = [1,1,1];
	auto z = x[] - y[];
	assert (z == [1,2,3]);
}

The assertion fails: z becomes a dynamic array with length 0 and an invalid pointer.

A similarly incorrect array results with most other operators. Using +, however, gives the following strange diagnostic:

foo.d(5): Error: Array operation x[] + y[] not implemented

This makes no sense, since x[] + y[] itself is perfectly fine as well as implemented.

Replacing 'auto z' with 'int[3] z' makes all cases succeed, but that contradicts the spec. IMHO there's no reason why using array operations as initializers should be invalid but that's what the spec currently says; added the 'spec' keyword to this in the hopes that this is legalized.
Comment 1 Kyle Foley 2009-07-15 06:51:21 UTC
I have recently discovered some bugs that are related to this

---

import std.stdio;

int main()
{
	int[] a = [1, 2, 3][];
	
	int[] b = a[] + 4;
	writeln("-> ", b); // -> 1 2 3 0 1935766371 1768515939 1764585331
	
	int[] c;
	c[] = a[] + 4;
	writeln("-> ", c); // -> 
	
	c = [1, 1, 1][];
	c[] = a[] + 4;
	writeln("-> ", c); // -> 5 6 7
	
	c = [0][];
	c[] = a[] + 4;
	writeln("-> ", c); // -> 5
	
	c = a[] + 4;
	writeln("-> ", c); // -> 1 2 3 0 1935766371 1768515939 1764585331
	
	int[3] d = a[] + 4; // this is ok
	writeln("-> ", d); // -> 5 6 7
	
	int[3] e = [1, 2, 3];
	int[3] f = [5, 6, 7];
	//writeln( "-> ", e[] + f[] ); // Compile Error -- Error: Array operation e[] + f[] not implemented
	// yet, this works:
	writeln( "-> ", e.dup[] += f[] ); // -> 6 8 10
	
	return 0;
}
Comment 2 Trass3r 2010-01-17 17:32:31 UTC
Also see
http://d.puremagic.com/issues/show_bug.cgi?id=2549
Comment 3 Matti Niemenmaa 2010-02-18 11:20:29 UTC
*** Issue 3815 has been marked as a duplicate of this issue. ***
Comment 4 Don 2010-04-28 21:21:03 UTC
The patch for bug 3522 fixes this.
Comment 5 Walter Bright 2010-04-29 21:06:13 UTC
Changeset 460
Comment 6 Don 2010-05-05 19:06:00 UTC
Fixed DMD2.044