-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add meta-variable checks in macro definitions #62008
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
r? @mark-i-m (not sure it's ready for final review, but the functionality is there) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Thanks @ia0! I will take a look soon. For the sake of documentation: Next steps:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ia0 This is excellent! Overall I think it looks good. I left a bunch of comments, but none of them is a major issue. My comments fall into 3 categories:
- Blocking
bors try
TODO
will causetidy
to fail. UseFIXME
instead (more below).- Change to allow-by-default lint.
- Blocking
r=me
- Using
SmallVec
instead ofVec
. This will avoid heap allocations in a few places in the most common case. - It would be good to have more comments in some places. The handling of nested macros is a bit subtle and non-obvious. I've left notes where I think the comments could be augmented. (Overall, though, your comments were very helpful ❤️).
- Would be good to use an enum instead of an integer for the state machine in
check_nested_occurrences
- Using
DUMMY_SP
is more conventional thanSpan::default
, I think. Perhaps @petrochenkov can correct me if I am wrong. - In the
ui
tests,//~ERROR
should be followed by a prefix of the error message
- Using
- Could be done in a followup PR (perhaps just leave FIXMEs for them)
- Warning where we believe we are in a nested macro (false positives)
- Reducing the amount of
clone
andto_owned
. - Move
is_delimited
andis_token
to inherent methods onTokenTree
.
@petrochenkov It might also be good to get a perf run after crater.
@mark-i-m Thanks a lot for your comments! I applied the simple ones and realized a bit more how approximative the current check is. This is due to the fact that meta-variables in nested macros can be hidden by using a meta-variable for the dollar token: I'll focus on making this a lint and adding more comments explaining nested macros. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@ia0 Thanks :) I think the check in its current form probably covers the vast majority of use cases. If somebody is doing Regarding the |
I turned the errors into a lint (and removed the overridden dependencies). |
Might be easiest if you just submit that commit as another PR too. |
Fix meta-variable binding errors in macros The errors are either: - The meta-variable used in the right-hand side is not bound (or defined) in the left-hand side. - The meta-variable used in the right-hand side does not repeat with the same kleene operator as its binder in the left-hand side. Either it does not repeat enough, or it uses a different operator somewhere. This change should have no semantic impact. Found by rust-lang#62008
Indeed, created #62070. And actually even the second commit (storing kleene operator span) can be submitted in a separate PR. All commits in this PR are orthogonal. |
a148f69
to
7d06bd0
Compare
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🎉 Experiment
|
@ia0 |
The content of this post has been moved to the issue: #61053 (comment). |
Yeah, you should only look at the regressed section. |
@ia0 Is there a way to filter out the transitive failures? I would hate for you to have to look through thousands of error message. |
What I'm doing is Also, note that the issue of transitive failures is probably because I use |
@ia0 The node ID determines the scope in which the lint can be |
"Non-root" regressions are probably from macros generating macro definitions. Issues like this are usually fixed by disabling lints for items (macro items in this case) produced by macros from other crates. |
This is needed for having complete error messages where reporting macro variable errors. Here is what they would look like: error: meta-variable repeats with different kleene operator --> $DIR/issue-61053-different-kleene.rs:3:57 | LL | ( $( $i:ident = $($j:ident),+ );* ) => { $( $( $i = $j; )* )* }; | - expected repetition ^^ - conflicting repetition
@bors r+ |
📌 Commit 6ec4584 has been approved by |
Add meta-variable checks in macro definitions This is an implementation of #61053. It is not sound (some errors are not reported) and not complete (reports may not be actual errors). This is due to the possibility to define macros in macros in indirect ways. See module documentation of `macro_check` for more details. What remains to be done: - [x] Migrate from an error to an allow-by-default lint. - [x] Add more comments in particular for the handling of nested macros. - [x] Add more tests if needed. - [x] Try to avoid cloning too much (one idea is to use lists on the stack). - [ ] Run crater with deny-by-default lint (measure rate of false positives). - [ ] Remove extra commit for deny-by-default lint - [x] Create a PR to remove the old `question_mark_macro_sep` lint #62160
☀️ Test successful - checks-azure |
This is an implementation of #61053. It is not sound (some errors are not reported) and not complete (reports may not be actual errors). This is due to the possibility to define macros in macros in indirect ways. See module documentation of
macro_check
for more details.What remains to be done:
question_mark_macro_sep
lint Remove outdated question_mark_macro_sep lint #62160