Bug 5953 – Too many trailing commas are accepted

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-05-07T16:28:00Z
Last change time
2011-08-31T00:36:53Z
Keywords
accepts-invalid, patch
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-05-07T16:28:09Z
Accepting an extra trailing comma is acceptable, but this looks excessive (this compiles and runs with no errors, DMD 2.053 beta): void main() { auto a = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]; assert(a.length == 0); } As in Python I suggest to accept only one extra trailing comma: >>> a = [1] >>> a = [1,] >>> a = [1,,] File "<stdin>", line 1 a = [1,,] ^ SyntaxError: invalid syntax
Comment #1 by kennytm — 2011-05-07T23:51:21Z
Please don't use Python to say a D syntax is invalid. Use the D spec or TDPL. -------- In the D spec an array literal follows the rule: ArrayLiteral: '[' ArgumentList ']' ArgumentList: AssignExpression AssignExpression ',' AssignExpression ',' ArgumentList and an AssignExpression cannot be empty, so '[1,,]' should be rejected. (BTW this rule doesn't handle '[]' at all.)
Comment #2 by bearophile_hugs — 2011-05-08T03:39:56Z
(In reply to comment #1) > Please don't use Python to say a D syntax is invalid. Use the D spec or TDPL. I feel free to show what's the design of other languages, if they are a good example. > and an AssignExpression cannot be empty, so '[1,,]' should be rejected. OK. So it's not a design problem, just an implementation issue.
Comment #3 by kennytm — 2011-05-08T04:40:20Z
(In reply to comment #2) > (In reply to comment #1) > > Please don't use Python to say a D syntax is invalid. Use the D spec or TDPL. > > I feel free to show what's the design of other languages, if they are a good > example. > Yes, but with the 'accept-invalid' tag I think that you want to say [,,,,,,,,,,,,,,,,,,] is invalid because it is invalid in Python. Sorry for misinterpreting. > > > and an AssignExpression cannot be empty, so '[1,,]' should be rejected. > > OK. So it's not a design problem, just an implementation issue.
Comment #4 by k.hara.pg — 2011-08-27T22:45:11Z
Reduce test case. StructInitializer has same problem as ArrayInitializer. void main() { // parsing ArrayInitializer auto a1 = []; // valid auto a2 = [,]; // invalid, but compiles auto a3 = [,,,]; // invalid, but compiles // parsing StructInitializer struct S{} S s1 = {}; // valid S s2 = {,}; // invalid, but compiles S s3 = {,,,}; // invalid, but compiles } And, maybe, D1 also has same problem.
Comment #5 by k.hara.pg — 2011-08-27T23:54:14Z
Comment #6 by bugzilla — 2011-08-31T00:36:53Z