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

Lint on incorrect unseparated repetition #55373

Closed
wants to merge 7 commits into from

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Oct 26, 2018

Macro pattern ($a:expr $b:expr) is invalid, for good reason.
Correctly reject pattern ($($a:expr)*), as it is functionally the
same.

This PR adds a warn by default lint that checks for any repeatable
pattern that would be rejected by the parser if it had been written
as independent arguments:

  • expr and stmt variables may only be followed by one of:
    => , ;
  • ty and path variables may only be followed by one of:
    => , = | ; : > [ { as where
  • pat variables may only be followed by one of:
    => , = | if in
  • Other variables may be followed by any token.

Fix #44975, fix #48220.

@rust-highfive
Copy link
Collaborator

r? @sfackler

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 26, 2018
@estebank
Copy link
Contributor Author

@bors try

@bors
Copy link
Contributor

bors commented Oct 26, 2018

⌛ Trying commit 5c5fca01e7afa4060e1d5910dc259db470afb4ed with merge a7b077cffd907eec053b7d79dbe723dd94548c08...

@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor Author

estebank commented Oct 26, 2018

cc @nikomatsakis @durka @joshtriplett


I'm afraid fixing this is going to be problematic, because even we are using this "feature" in rustc. I'll post a PR with the fix (including changing our mis-uses), but will expect there to be breakage. If that is the case after a crater run, should we still include the fix as a warning?

This will need a crater run to see how much this would break.

@estebank

This comment has been minimized.

@bors

This comment has been minimized.

bors added a commit that referenced this pull request Oct 26, 2018
Disallow incorrect unseparated repetition

Macro pattern `($a:expr, $b:expr)` is invalid, for good reason.
Correctly reject pattern `($($a:expr)*)`, as it is functionally the
same.

Fix #44975, fix #48220.
@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@bors

This comment has been minimized.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 26, 2018
@rust-highfive

This comment has been minimized.

@estebank estebank force-pushed the macro-rep branch 2 times, most recently from 7139c6b to 2915cc7 Compare October 26, 2018 15:05
@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor Author

@nikomatsakis I fully expect this change to wreak havoc. I wouldn't want to make it a lint because the logic to accomplish that would be much more involved than this change. Is it ok for us to start generating an unsilenceable warning all of a sudden?

@estebank estebank added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 26, 2018
@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor Author

cc @kennytm

@estebank
Copy link
Contributor Author

estebank commented Dec 6, 2018

@dtolnay added deprecation message, changed to allow by default and rebased.

@rust-highfive

This comment has been minimized.

@petrochenkov
Copy link
Contributor

Sigh, the following token restrictions exist to prevent breakage from extending Rust syntax, but this change alone breaks more code than syntax extensions ever did.

@petrochenkov
Copy link
Contributor

It looks like the warning may be reported both at definition crate (when the macro is defined) and at use crate when the macro is loaded?
Could you make sure this doesn't happen and add a test?
(The warning needs to be reported only when macro_rules::compile is called from define_macro.)

@petrochenkov
Copy link
Contributor

Also a test making sure that #[allow(incorrect_macro_fragment_repetition)] on the macro item silences the warning for both local crate and clients.

@petrochenkov
Copy link
Contributor

Otherwise LGTM.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 8, 2018
@withoutboats
Copy link
Contributor

We discussed this in the lang team meeting today. Overall, we're uncertain that this is worth the churn it will cause. The point of these restrictions is to avoid a lot of breakage when we do extend the expression syntax: are we just frontloading that breakage by adopting this change, defeating the point of the restriction in the first place?

Contrarily, one could argue that if we do ever extend expressions in the future, after Rust's adoption has continued to grow, we might end up breaking a lot more people than this lint will cause problems for.

But overall we were uncertain, so we're throwing it back to the people invested in the PR: this change is definitely correct, but is it worth the breakage to become more correct? Why or why not?

@estebank
Copy link
Contributor Author

Contrarily, one could argue that if we do ever extend expressions in the future, after Rust's adoption has continued to grow, we might end up breaking a lot more people than this lint will cause problems for.

This is exactly the camp where I would fall on. Getting a lint in at the earliest possible moment, even if it is disabled by default for several releases and warn only until the next edition makes it much more likely that breaking this in 2021 will be possible.

this change is definitely correct, but is it worth the breakage to become more correct? Why or why not?

I feel that merging as an allow by default lint is the clear right choice. That way projects that care about it can enable it selectively or opt out of it completely (indefinitely). After that we can work with crates to move away from exploiting this loophole. I don't think this lint should be deny-by-default unless it is part of a new edition.

Alternatively, we could explicitly allow some particular use cases, like repeated bare types and literals in expr arguments (enabling the common use in rustc), while disallowing/warning of plainly incorrect usage on use, not definition like we do here...

@bors
Copy link
Contributor

bors commented Dec 15, 2018

☔ The latest upstream changes (presumably #56203) made this pull request unmergeable. Please resolve the merge conflicts.

@XAMPPRocky
Copy link
Member

Triage; @estebank Hello, have you been able to get back to this PR?

@estebank
Copy link
Contributor Author

@Aaronepower I'll clean this up this weekend.

@TimNN
Copy link
Contributor

TimNN commented Jan 22, 2019

Ping from triage @estebank: What is the status of this PR?

@estebank
Copy link
Contributor Author

@TimNN I have bene quite busy and unable to get back to this, but will try to do so soonish. If you wish to close in the meantime, go ahead and I'll reopen once I'm ready.

Alternatively, we could explicitly allow some particular use cases, like repeated bare types and literals in expr arguments (enabling the common use in rustc), while disallowing/warning of plainly incorrect usage on use, not definition like we do here...

Does anyone have thoughts on this point?

@TimNN TimNN added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 29, 2019
@TimNN TimNN closed this Jan 29, 2019
@TimNN
Copy link
Contributor

TimNN commented Jan 29, 2019

If you wish to close in the meantime, go ahead and I'll reopen once I'm ready.

Thank you for your understanding and contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

macros sometimes allow expr followed by ident macro pattern ($($arg:expr)*) should be illegal