Thread overview
[Issue 4565] New: In array literals single values can replace arrays of length 1
Oct 22, 2012
Andrej Mitrovic
Oct 27, 2012
yebblies
Nov 15, 2012
Kenji Hara
Nov 15, 2012
Andrej Mitrovic
May 31, 2013
Shriramana Sharma
August 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4565

           Summary: In array literals single values can replace arrays of
                    length 1
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-08-01 15:27:31 PDT ---
This program compiles with no errors with dmd 2.047:

int[1][3] a1 = [1, 2, 3];
void main() {
    int[1][3] a2 = [1, 2, 3];
}


But those array literals are wrong. This is the correct program:

int[1][3] a1 = [[1], [2], [3]];
void main() {
    int[1][3] a2 = [[1], [2], [3]];
}


A sloppy syntax is bad because it *always* offers space for bugs, like this one, dmd compiles this program with no errors (note the missing comma):


int[1][3] a = [[1] [0], [2]];
void main() {}


Now a contains [1, 2, 0], a silent bug.
This situation is partially caused by bug 3849

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4565


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com
         AssignedTo|nobody@puremagic.com        |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-10-22 16:00:17 PDT ---
Do you by any chance have a somewhat elaborate set of test-cases that cover many types of arrays which should and shouldn't compile? It would help to make solid test-cases.

Otherwise I'll write them myself, no problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4565



--- Comment #2 from bearophile_hugs@eml.cc 2012-10-22 16:26:08 PDT ---
(In reply to comment #1)
> Do you by any chance have a somewhat elaborate set of test-cases that cover many types of arrays which should and shouldn't compile?

I don't, sorry. Currently finding such test cases is a laborious manual work. There is no fuzzy testing framework for D, as ones used on the certified C compiler.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4565


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
                 CC|                            |yebblies@gmail.com
           Platform|x86                         |All
         OS/Version|Windows                     |All


--- Comment #3 from yebblies <yebblies@gmail.com> 2012-10-28 02:49:25 EST ---
https://github.com/D-Programming-Language/dmd/pull/1207

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4565



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2012-11-15 00:49:19 PST ---
(In reply to comment #0)
> This program compiles with no errors with dmd 2.047:
> 
> int[1][3] a1 = [1, 2, 3];
> void main() {
>     int[1][3] a2 = [1, 2, 3];
> }
>
> But those array literals are wrong. This is the correct program:
> 
> int[1][3] a1 = [[1], [2], [3]];
> void main() {
>     int[1][3] a2 = [[1], [2], [3]];
> }

I think this is not a bad program.
> int[1][3] a1 = [1, 2, 3];
is same as:

int[1][3] a1 = void;
a1[0][] = 1; // fill all elements by 1
a1[1][] = 2; // fill all elements by 2
a1[2][] = 3; // fill all elements by 3

Then a1 is initialized by [[1], [2], [3]].

And it is consistent with:
int[3] sa = 1;  // sa is initialized to [1, 1, 1]

----

> A sloppy syntax is bad because it *always* offers space for bugs, like this one, dmd compiles this program with no errors (note the missing comma):
> 
> int[1][3] a = [[1] [0], [2]];
> void main() {}

[1][0] is an expression which indexing array literal, and evaluated to 1. It's equivalent to: [1,2,3][0] == 1

> Now a contains [1, 2, 0], a silent bug.

Now a is initialized by [1, [2]], and is same as:
int[1][3] a = void;
a[0][] = 1;
a[1]   = [2];
a[2][] = 0;    // == int.init

After all, a == [1, 2, 0]. There is no bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4565



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-11-15 00:56:56 PST ---
> After all, a == [1, 2, 0]. There is no bug.

Ok, you can close https://github.com/D-Programming-Language/dmd/pull/1207 if bug is invalid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 12, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4565



--- Comment #6 from bearophile_hugs@eml.cc 2013-03-11 19:44:12 PDT ---
(In reply to comment #4)

Sorry for the very delayed answer.

> > A sloppy syntax is bad because it *always* offers space for bugs, like this one, dmd compiles this program with no errors (note the missing comma):
> > 
> > int[1][3] a = [[1] [0], [2]];
> > void main() {}
> 
> [1][0] is an expression which indexing array literal, and evaluated to 1. It's equivalent to: [1,2,3][0] == 1
> 
> > Now a contains [1, 2, 0], a silent bug.
> 
> Now a is initialized by [1, [2]], and is same as:
> int[1][3] a = void;
> a[0][] = 1;
> a[1]   = [2];
> a[2][] = 0;    // == int.init
> 
> After all, a == [1, 2, 0]. There is no bug.

In my opinion that's probably a bug in user code. I doubt the user meant to write that. Most probably the user meant to write this, but missed a comma after the first sub-array:

int[1][3] a = [[1], [0], [2]];
void main() {}


But this is an uncommon situation, so I think it's not worth thinking too much about it.

What follows is more important:


> I think this is not a bad program.
> > int[1][3] a1 = [1, 2, 3];
> is same as:
> 
> int[1][3] a1 = void;
> a1[0][] = 1; // fill all elements by 1
> a1[1][] = 2; // fill all elements by 2
> a1[2][] = 3; // fill all elements by 3
> 
> Then a1 is initialized by [[1], [2], [3]].
> 
> And it is consistent with:
> int[3] sa = 1;  // sa is initialized to [1, 1, 1]

Today this syntax compiles:

void main() {
    int[1][3] a2;
    a2[0][] = 1;
    a2[1][] = 2;
    a2[2][] = 3;
}


This used to compile fine, but today it gives warnings (and this is good):

void main() {
    int[1][3] a3;
    a3[0] = 1;
    a3[1] = 2;
    a3[2] = 3;
}


temp.d(3): Warning: explicit element-wise assignment (a3[cast(uint)0])[] = 1 is
better than a3[cast(uint)0] = 1
temp.d(4): Warning: explicit element-wise assignment (a3[cast(uint)1])[] = 2 is
better than a3[cast(uint)1] = 2
temp.d(5): Warning: explicit element-wise assignment (a3[cast(uint)2])[] = 3 is
better than a3[cast(uint)2] = 3


Currently both of the following forms are accepted:

void main() {
    int[1][3] a1 = [[1], [2], [3]];
    int[1][3] a2 = [1, 2, 3];
}


Generally I trust your good judgement Hara, but if we are going to deprecate the assignment of array slices without using [], then I don't know if the idea of allowing both of those syntaxes is a good idea...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4565


Shriramana Sharma <samjnaa@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samjnaa@gmail.com


--- Comment #7 from Shriramana Sharma <samjnaa@gmail.com> 2013-05-31 10:33:43 PDT ---
(In reply to comment #4)
> 
> I think this is not a bad program.
> > int[1][3] a1 = [1, 2, 3];
> is same as:
> 
> int[1][3] a1 = void;
> a1[0][] = 1; // fill all elements by 1
> a1[1][] = 2; // fill all elements by 2
> a1[2][] = 3; // fill all elements by 3
> 
> Then a1 is initialized by [[1], [2], [3]].
> 
> And it is consistent with:
> int[3] sa = 1;  // sa is initialized to [1, 1, 1]

FWIW I agree with bearophile here. In the case of having a single value on the RHS it is easy (?) to construe how int[3] a = 1 (or even maybe int[3][3] a = 1) would initialize all the elements, but when you specify an array literal on the RHS, I think it should have the same shape as the array specified in the type on the LHS to avoid hard-to-find bugs.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------