-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Give a better error when using an undeclared macro variable #95943
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
Comments
This scenario is caught by
It would be beneficial if this lint was documented somewhere a little less obscure than the rustc book, but I'm not sure what a good place would be. Maybe the Reference or the (unofficial) Little Book of Rust Macros? cc #61053 |
It seems strange that's a lint and not just a built-in diagnostic? I'd at least expect it to warn by default ... |
@jyn514 It's not an error or a warn-by-default because MBE macros are unbelievably permissive, which makes it impossible to detect things like this without false positives. For example, it is hard to determine such a snippet is occurring inside a nested macro definition, in which case the outer macro may be defining the meta-variable. We spent a lot of time trying to figure out what we can check and how: #61053 IMHO, we should remove the MBE system altogether in a future edition and create something more constrained/well-defined that is designed to be (1) easy to document (current macros have terrible documentation), (2) statically checkable, and (3) easy to use. Right now, MBEs satisfy none of these conditions and are not fixable as far as I can tell. |
I thought the issues you ran into were because you were trying to check it statically at definition time, right? I think giving an error about undefined metavariables should be easier at expansion time. |
Possibly, but is that useful? As a macro user, what would you do if you found that the macro author made a mistake? I suppose you would just report the bug and hope they fix it. I suppose that's marginally better than getting the error above... |
@mark-i-m not all macros are defined cross-crate; IME most of them are local to the current crate because people don't want to expose macros in the API. Having the better error is very useful for local macros, or just when testing your public macro. |
That's fair enough. |
It looks like this is the code you would want to change: rust/compiler/rustc_expand/src/mbe/transcribe.rs Lines 249 to 254 in 3bebee7
I'm not sure how you could tell if you were in a nested invocation or not, though. One option is to just make it an error and do a crater run to see how wide the impact is. Maybe the AST Validation might be another place to check for this? I don't fully understand what happens in that pass, but it happens after all macros are fully expanded. |
@rustbot claim |
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a17456ca7139db13e945cbde87ae6fbd
The current output is:
Ideally the output should look like:
@rustbot label +A-macros
The text was updated successfully, but these errors were encountered: