Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage for double trailing commas is poor #46238

Open
ExpHP opened this issue Nov 24, 2017 · 8 comments
Open

Test coverage for double trailing commas is poor #46238

ExpHP opened this issue Nov 24, 2017 · 8 comments
Labels
A-parser Area: The parsing of Rust source code to an AST C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@ExpHP
Copy link
Contributor

ExpHP commented Nov 24, 2017

Theory

Every place in the grammar which supports trailing commas should be tested that it fails for double commas, lest Rust be locked into supporting it forever. A particularly notable case of this is for macro_rules! macros, many of which must manually implement their own trailing comma support (leading to more chances for mistakes).

Reality

[ lampam @ 12:22:45 ] (master •2) ~/asd/clone/rust/src/test
$ ls **/*.rs -1 > files
$ python3
>>> import re
>>> contents = [open(x.strip()).read() for x in open('files')]
>>> r = re.compile(',\\s*,\\s*[\\])}]', re.MULTILINE)
>>> [x for x in map(r.findall, contents) if x]
[]
@ExpHP
Copy link
Contributor Author

ExpHP commented Nov 24, 2017

Agh, I should've waited a bit longer before posting.

My check did not take comments into account, so this may be a false alarm. I am hacking together a slightly more accurate test and will reopen if that test fails...

@ExpHP ExpHP closed this as completed Nov 24, 2017
@ExpHP ExpHP changed the title There is not a single file in test that includes double trailing commas Test coverage for double trailing commas is poor Nov 24, 2017
@ExpHP
Copy link
Contributor Author

ExpHP commented Nov 24, 2017

After more accurate testing, this still appears to be a problem.

#!/usr/bin/env python3
import re

# strip comments naively, under the assumption that tests which
# try to trip up comment parsers probably do not also test
# something such as double commas
BLOCK_COMMENT = re.compile('/\*(\*[^/]|[^*])*\*/', re.MULTILINE)
LINE_COMMENT = re.compile('//.*')
# Note: applied to a file's entire contents as a single string (with embedded newlines)
strip_comments = lambda s: BLOCK_COMMENT.sub('', LINE_COMMENT.sub('', s))

contents = [open(x.strip()).read() for x in open('files')]
contents = [strip_comments(s) for s in contents]

print("Test counts for trailing commas")
for end in ['\\]', '\\)', '\\}', '>']:
    r = re.compile(f',\\s*{end}', re.MULTILINE)
    print(f' ,{end[-1:]}:', len([x for x in map(r.findall, contents) if x]))

print("Test counts for double trailing commas")
for end in ['\\]', '\\)', '\\}', '>']:
    r = re.compile(f',\\s*,\\s*{end}', re.MULTILINE)
    print(f',,{end[-1:]}:', len([x for x in map(r.findall, contents) if x]))
Test counts for trailing commas
 ,]: 23
 ,): 118
 ,}: 1096
 ,>: 14
Test counts for double trailing commas
,,]: 0
,,): 0
,,}: 0
,,>: 6

@ExpHP ExpHP reopened this Nov 24, 2017
@kennytm kennytm added A-grammar Area: The grammar of Rust C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. A-parser Area: The parsing of Rust source code to an AST and removed A-grammar Area: The grammar of Rust labels Nov 24, 2017
@durka
Copy link
Contributor

durka commented Nov 27, 2017

IMO the theory applied to macro_rules! macros is a bit unfair as it's a huge chore for a macro to support single trailing commas without supporting infinite trailing commas, since the $(,)* trick is the only way to do it without duplicating rules. I've been meaning to submit an RFC for $()? which would improve the situation.

@ExpHP
Copy link
Contributor Author

ExpHP commented Nov 27, 2017

I think libcore and libstd are well in a position that they can at least afford to do it right, regardless of the ergonomics. The only disadvantage I can see is that it may make documentation look unruly for more complex macros; however, none of the macros currently existing in std are that complex, except maybe select!.


Put another way, I guess my feeling is that this should be applied to macro_rules! macros as long as it is within reason. (and that it currently is within reason for all stable macros)

@durka
Copy link
Contributor

durka commented Nov 27, 2017

The most complex is probably thread_local!. I looked back at my PR that made it parse multiple declarations, and it looks like my initial draft would have accepted multiple semicolons, but the accepted version does not. Its docs rendering is... not the best but not bad for a macro I guess.

@ExpHP
Copy link
Contributor Author

ExpHP commented Nov 27, 2017

One other thought: the possibility of a future $()? syntax imo makes it even more desirable that the stdlib macros today do not accept infinite delimiters, so that upgrading them to use $()? is a backwards compatible change. (this upgrade is definitely desirable, considering that people are likely to look to the macros in std for reference)

(looking forward to that RFC, by the way 😉 )

@steveklabnik
Copy link
Member

triage: @ExpHP how do you feel about this coverage today?

@ExpHP
Copy link
Contributor Author

ExpHP commented Dec 19, 2019

The coverage is still extremely poor.

Checking today, tests currently exist for double trailing commas in only two places in the grammar:

There's still a plethora of places in the grammar where accidental ,, support could slip in: Parameter lists, argument lists, enum/struct/union definitions, various kinds of types/patterns/literals, where bounds, and probably more.

One strategy may be to locate those test files which test interior ,,, and add cases to them with trailing ,,.

And of course, it still is the case that each individual std/core macro implements its own trailing comma support. $(,)? certainly has made life easier for macro authors, but it's still possible to make mistakes. I would like to update macro-comma-support-rpass.rs and friends eventually at some point to add trailing double comma tests. (perhaps I can do that today now that I'm thinking about it...)

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue May 12, 2022
… r=joshtriplett

Add test of matches macro for trailing commas

Almost all macros are tested for trailing commas.
The macro matches! was however not tested.
This PR adds that test case.
Related to rust-lang#46238
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 13, 2022
… r=joshtriplett

Add test of matches macro for trailing commas

Almost all macros are tested for trailing commas.
The macro matches! was however not tested.
This PR adds that test case.
Related to rust-lang#46238
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

4 participants