www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3854] New: Error on static initialization of arrays with trailing comma.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854

           Summary: Error on static initialization of arrays with trailing
                    comma.
           Product: D
           Version: 1.056
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: sky q1cc.net


--- Comment #0 from Markus Dangl <sky q1cc.net> 2010-02-25 12:16:37 PST ---
The following code worked in DMD 1.045, but not with 1.056 and i think it
should:
uint[][] b = [[ 1, 2, ]];
The following code always works, which is odd:
uint[] a = [ 1, 2, ];
I assume it is a minor parser bug.

Workaround:
Omit the last comma.

Here is the full code i used:
module main;
int main(char[][] args) {
    uint[][] b = [[ 1, 2, ]];
    return 0;
}

And the output of "dmd main.d":
main.d(3): expression expected, not ']'
main.d(3): comma expected separating array initializers, not ;
main.d(4): semicolon expected, not 'return'

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 25 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #1 from Don <clugdbug yahoo.com.au> 2010-02-25 12:39:25 PST ---
I think this is a duplicate of bug 3716.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 25 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854



--- Comment #2 from Markus Dangl <sky q1cc.net> 2010-02-25 12:54:14 PST ---
(In reply to comment #1)
 I think this is a duplicate of bug 3716.
It was named D2 only and does not mention problems with trailing commas, thus i made it a seperate bug. I tested the code of #3716 with dmd 1.056 and it worked, so i suppose it really is a different bug. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 25 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854


Ellery Newcomer <ellery-newcomer utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ellery-newcomer utulsa.edu


--- Comment #3 from Ellery Newcomer <ellery-newcomer utulsa.edu> 2010-02-25
20:27:36 PST ---
(In reply to comment #2)
 (In reply to comment #1)
 I think this is a duplicate of bug 3716.
It was named D2 only and does not mention problems with trailing commas, thus i made it a seperate bug. I tested the code of #3716 with dmd 1.056 and it worked, so i suppose it really is a different bug.
The trailing commas are deliberate and part of the spec. If you want to complain about trailing commas, complain that array initializers allow them and array literal expressions don't. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 25 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854


Iain Buclaw <ibuclaw ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw ubuntu.com


--- Comment #4 from Iain Buclaw <ibuclaw ubuntu.com> 2010-03-21 12:04:34 PDT ---
Paradoxically, the absent of a trailing comma can trigger this error too.

int[][][] a = [[ [1,2],[3,4], ]]; // Error

Whereas:
int[][][] a = [[ [1,2],[3,4], ],]; // OK


Seems like a bug to me, and the patch in bug 3716 resolves this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854



--- Comment #5 from Iain Buclaw <ibuclaw ubuntu.com> 2010-04-01 17:43:06 PDT ---
Created an attachment (id=597)
keep track of static array initiliasiser depth

OK, just for clarification.

uint[][] b = [[ 1, 2 ]];   // OK
uint[][] b = [[ 1, 2 ],];  // OK
uint[][] b = [[ 1, 2, ],]; // OK
uint[][] b = [[ 1, 2, ]];  // Error

Why this happens?
The scan ahead loop in parse.c isn't aware of just how deep it has navigated
into the array it is scanning.

First loop, scans the string:
"[[ 1, 2, ]]"
Second loop, because of the trailing comma, starts another parseInitializer()
call, but this time scanning the string:
"[ 1, 2, ]]"

Because it reaches end of the scan ahead loop earlier this time, and checking
the next token fails, then treats it as an expression, not an array
initializer.

Attached is a patch that resolves the issue. Am not sure that the patch in the
other bug report is reasonably correct, but hey ho!

Regards
Iain

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854



--- Comment #6 from Ellery Newcomer <ellery-newcomer utulsa.edu> 2010-04-01
18:19:29 PDT ---
(In reply to comment #5)
 
 Attached is a patch that resolves the issue. Am not sure that the patch in the
 other bug report is reasonably correct, but hey ho!
 
 Regards
 Iain
Eh? What makes you think it isn't? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854



--- Comment #7 from Iain Buclaw <ibuclaw ubuntu.com> 2010-04-02 04:27:27 PDT ---
(In reply to comment #6)
 (In reply to comment #5)
 
 Attached is a patch that resolves the issue. Am not sure that the patch in the
 other bug report is reasonably correct, but hey ho!
 
 Regards
 Iain
Eh? What makes you think it isn't?
Well, from what I can tell, if I were to have the following code excerpt: int[] a = [1]]; The scan ahead routine will swallow the "[1]", and since (--brackets == 0), takes a peek at the last bracket, to which it matches in the if statement and says 'yep, that is OK'. In other words, it assumes that whether or not the above line is correct has already been checked, or will be checked later on during the compile (which I don't doubt it already has, but at least see it better to have some sort of sanitisation when checking things). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854



--- Comment #8 from Ellery Newcomer <ellery-newcomer utulsa.edu> 2010-04-02
09:50:23 PDT ---
(In reply to comment #7)
 
 Eh? What makes you think it isn't?
Well, from what I can tell, if I were to have the following code excerpt: int[] a = [1]]; The scan ahead routine will swallow the "[1]", and since (--brackets == 0), takes a peek at the last bracket, to which it matches in the if statement and says 'yep, that is OK'. In other words, it assumes that whether or not the above line is correct has already been checked, or will be checked later on during the compile (which I don't doubt it already has, but at least see it better to have some sort of sanitisation when checking things).
The scan ahead doesn't swallow anything. It does see only "[1]" and because (--brackets == 0) it says "I'm going to parse this as ArrayInitializer", which it does. The next loop (the ArrayInitializer rule) swallows "[1]", and no more. Then parseInitializer is done. The function that called parseInitializer (parseDeclarations or whatever), will expect a semicolon or comma to be the next token, and when it sees lbracket, it errors. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854


Ellery Newcomer <ellery-newcomer utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
 Attachment #597 is|0                           |1
           obsolete|                            |


--- Comment #9 from Ellery Newcomer <ellery-newcomer utulsa.edu> 2010-04-24
11:07:19 PDT ---
Created an attachment (id=614)
the patch which I mistakenly put in 3716

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 24 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3854


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #10 from Walter Bright <bugzilla digitalmars.com> 2010-06-01
23:52:32 PDT ---
http://www.dsource.org/projects/dmd/changeset/514

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