www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2477] New: Trailing comma in array literal sometimes accepted, sometimes not

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

           Summary: Trailing comma in array literal sometimes accepted,
                    sometimes not
           Product: D
           Version: 1.037
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: wbaxter gmail.com


void main() {
    int[] data = [1,2,3,];  // OK

    data = [ 1,2,3, ];  // fails
/+
// error messages
trailcomma.d(20): found ';' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
trailcomma.d(29): found 'EOF' when expecting ','
+/
}

The repetition of the EOF error message 20 times is also rather odd.

I don't know if trailing comma is spec'ed as being allowed or not (I much
prefer allowing it), but either way it should be consistent.


-- 
Nov 28 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2477


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
           Keywords|                            |diagnostic, spec





The problem is that the spec is inconsistent.  In the first case, it's not an
array literal, but an array initializer.

ArrayInitializer:
        [ ]
        [ ArrayMemberInitializations ]

ArrayMemberInitializations:
        ArrayMemberInitialization
        ArrayMemberInitialization ,
        ArrayMemberInitialization , ArrayMemberInitializations

versus

ArrayLiteral:
        [ ArgumentList ]

ArgumentList:
        AssignExpression
        AssignExpression , ArgumentList


Making these consistent is technically an enhancement request rather than a
bug.  I nearly set this to enhancement, but the second issue (repetition of the
error message) qualifies as a bug IMO.


-- 
Nov 30 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2477






Thanks for the spec digging, Stewart.  So I see three options:

1) change the ArrayMemberInitializations production to:

ArrayMemberInitializations:
        ArrayMemberInitialization
        ArrayMemberInitialization , ArrayMemberInitializations

(get rid of the trailing comma case for initializations)

2) change the ArrayLiteral productions to 

ArrayLiteral:
        [ ArrayLiteralMemberList ]

ArrayLiteralMemberList:
        AssignExpression
        AssignExpression ,
        AssignExpression , ArrayLiteralMemberList

3) Just make all argument lists have an optional trailing comma.  This is based
on the thinking that if allowing a trailing comma is a nice feature for arrays,
then why not general argument lists (which can be used as general array
constructors with typesafe variadic A[]... style parameters.)?

ArgumentList:
        AssignExpression
        AssignExpression ,
        AssignExpression , ArgumentList




-- 
Nov 30 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2477


Brian Kropf <brian.kropf gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |brian.kropf gmail.com





PDT ---
I'd originally submitted this on the ldc bugtracker and got forwarded here
(since it is using the dmd front-end).  Here's a bit more info on the issue:

When using sabledd generated code, I'm seeing an error with ldc that was not
present when I used gdc. It boils down to ldc not accepting only part of the
endings of array initializers having commas. The following minimal test case
does not build successfully on ldc x64 (but it will if you add a comma to the
end of the second array initializer (like '[ 4, 5, 6, ],'). Curiously enough it
also builds cleanly if I remove the commas after the 3 and the 6 in the
example.

test.d: int arr[][] = [
    [ 1, 2, 3, ], [ 4, 5, 6, ]
];

int main(char[][] args) {
    return 0;
}

Error output:

test.d(7): expression expected, not ']' test.d(7): comma expected separating
array initializers, not ; test.d(9): semicolon expected, not 'int' Error:
Command failed, aborting.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 17 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2477


Aziz Köksal <aziz.koeksal gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aziz.koeksal gmail.com



PDT ---
I agree, ArgumentLists, ArrayLiterals etc. should allow a trailing comma, just
like ParameterLists or enums.

Fixing this should be possible in 6 to 10 lines of code. Why is this issue
still open?

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


Don <clugdbug yahoo.com.au> changed:

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




 I agree, ArgumentLists, ArrayLiterals etc. should allow a trailing comma, just
 like ParameterLists or enums.
 
 Fixing this should be possible in 6 to 10 lines of code. Why is this issue
 still open?
Because there are over 1000 open bugs, and nearly all of them are more important than this one. Make a patch for it, if you care about it. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 30 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2477




08:49:05 PDT ---
Created an attachment (id=615)
allow trailing commas in various places

modifications for ArrayLiteral, Arguments, TemplateParameterList, and
TemplateArguments.

I haven't tested it too heavily, but the simple stuff works. There might be
latent trouble in the ambiguity between ArrayInitializer and ArrayLiteral
(aside from what's already present), as I'm not well versed in what happens
past the parser.

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




PDT ---
Don, I know it's not extremely important. I care about it, but I can't make
patches for the DMD front- or back-end. Not because I don't want to, but
because I'm working on a similar project; my own D compiler, remember? :-)

Ellery, thanks for taking care of this.

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




10:02:04 PDT ---

 Don, I know it's not extremely important. I care about it, but I can't make
 patches for the DMD front- or back-end. Not because I don't want to, but
 because I'm working on a similar project; my own D compiler, remember? :-)
 
Come to think of it, so am I, or I was. I don't care much about the licensing, though. Or releasing it, if I ever finish it.
 Ellery, thanks for taking care of this.
Only needed to change 4 lines :) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 30 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2477


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com
            Version|1.037                       |D2
         OS/Version|Windows                     |All
           Severity|normal                      |enhancement



13:19:43 PDT ---
Since this changes the language, I'm going to do it for D2 only and mark it as
an enhancement request.

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




13:20:58 PDT ---
A more comprehensive test case:

void foo(T,)(T t) { T x; }

void main() {
    int[] data = [1,2,3,];
    data = [ 1,2,3, ];
    auto i = data[1,];
    foo!(int)(3);
    foo!(int,)(3);
    foo!(int,)(3,);
}

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



13:41:10 PDT ---
For D2:

http://www.dsource.org/projects/dmd/changeset/644

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




13:51:24 PDT ---
Phobos changeset 1939

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