-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Warn when macro shadows previous definition #20277
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
Conversation
Also contains small whitespace fix in same file. (related to rust-lang#20192)
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pcwalton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
Thanks for the PR! I think that unconditionally warning about using a feature of the macro system may not be a great idea, however. I've used this time to time in other projects and I think the error should at least be configurable via a lint (and I would personally prefer allow-by-default). Could you also add some tests for this change if you'd like to pursue it? |
@alexcrichton Sure thing! Was planning on adding configurability if this feature PR was welcome. Test-wise were you looking for both a) testing that it issues the warning by default and b) testing that the configuration flag/lint properly silences the warning? |
Indeed! (in terms of testing) |
So I'm working on implementing this as feature as a lint in the librustc built-in linters, however I'm struggling to figure out if macros defined by |
The definitions themselves live in the AST, and I believe macro definitions will live all the way through until translation. As a lint, you'll probably want to modify the list of lints we have and then call |
Gotcha, I was digging through the LintPass hook API yesterday and was struggling to figure out which hook function would be triggered by a |
I believe that the |
So I've added the following pass to the built-in set of lint passes (in impl LintPass for ShadowingMacrosPass {
fn get_lints(&self) -> LintArray {
lint_array!(SHADOWING_MACROS)
}
#[allow(unused_must_use)]
fn check_mac(&mut self, cx: &Context, mac: &ast::Mac) {
// Pull out the macro identifier out of the definition
let ast::MacInvocTT(ref path, _, _) = mac.node;
let id: ast::Ident = path.segments.last().unwrap().identifier;
let id_str = id.name.as_str();
io::stderr().write(format!("ShadowingMacrosPass#check_mac: {}\n", id_str).as_bytes());
}
} However, the |
Macros are gone after expansion. They're kept around in |
@cmr Gotcha, thanks! So would the appropriate approach (for the time being) be to continue with the current strategy of having the shadow-check in macro-rule expansion code (what this PR is currently doing) and control whether or not that check happens via a crate-level attribute? |
Yes, I think so. I think you'd just move the logic in that method into |
This moves the shadowing check from an early check that happens during macro expansion to a late lint-pass check (which inspects the crate rather than the syntax expansion context).
672b601
to
8667465
Compare
Woops, looks like it failed the tidy check. Will fix that with the next commit. |
@@ -1628,6 +1628,52 @@ impl LintPass for MissingCopyImplementations { | |||
} | |||
|
|||
declare_lint! { | |||
pub SHADOWING_MACROS, | |||
Warn, |
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.
Due to this being a known feature of the macro system, this should be Allow
by default.
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.
Gotcha, fixed!
Closing due to inactivity, but feel free to reopen with comments addressed! |
Oh, sorry! Was on a business trip last week and let this lapse. Will get back to it shortly. |
…yarn/editors/code/form-data-4.0.4 Bump form-data from 4.0.2 to 4.0.4 in /editors/code
Also contains small whitespace fix in same file (
libsyntax/ext/tt/macro_rules.rs
).(related to #20192)