Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

dirk
Copy link
Contributor

@dirk dirk commented Dec 28, 2014

Also contains small whitespace fix in same file (libsyntax/ext/tt/macro_rules.rs).

(related to #20192)

Also contains small whitespace fix in same file.
(related to rust-lang#20192)
@rust-highfive
Copy link
Contributor

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.

@alexcrichton
Copy link
Member

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?

@dirk
Copy link
Contributor Author

dirk commented Dec 29, 2014

@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?

@alexcrichton
Copy link
Member

Indeed! (in terms of testing)

@dirk
Copy link
Contributor Author

dirk commented Dec 29, 2014

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 macro_rules! ever make it from the syntax context to the linter context (or the type-context it points out to). Any ideas if they do or do not?

@alexcrichton
Copy link
Member

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 .add_lint on the Session so it can be processed at the end of compilation.

@dirk
Copy link
Contributor Author

dirk commented Dec 30, 2014

Gotcha, I was digging through the LintPass hook API yesterday and was struggling to figure out which hook function would be triggered by a macro_rules! in the AST. Do you know off the top of your head if that gets fully translated into an ast::Mac (and therefore handled by an fn check_mac(...)), or would it end up as an ast::ItemMac or ast::StmtMac? (Or could it in theory end up as any of those depending on where it occurs?)

@alexcrichton
Copy link
Member

I believe that the check_mac visitor function will hit all macros, and macro_rules! itself is a macro so it should be hit there. The ItemMac and StmtMac productions should also be covered by check_mac (they're the recursive call)

@dirk
Copy link
Contributor Author

dirk commented Jan 2, 2015

So I've added the following pass to the built-in set of lint passes (in librustc/lint/builtin.rs and in the body of register_builtin(...) in librustc/lint/context.rs).

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 check_mac(...) function seems to never be called. I did some digging and it looks like there are a few places where the compiler is skipping macro visiting; could those be possible culprits for why check_mac is being skipped over, or have I just done something dump somewhere?

@emberian
Copy link
Member

emberian commented Jan 3, 2015

Macros are gone after expansion. They're kept around in ast::Crate::exported_macros.

@dirk
Copy link
Contributor Author

dirk commented Jan 3, 2015

@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?

@emberian
Copy link
Member

emberian commented Jan 3, 2015

Yes, I think so. I think you'd just move the logic in that method into check_crate.

@alexcrichton
Copy link
Member

@dirk you probably want to do the analysis where you're currently detecting it, but you can call the add_lint method on the Session (I think it's available) for the actual lint and node id to be processed later.

dirk added 2 commits January 3, 2015 19:02
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).
@dirk dirk force-pushed the shadow-macro-warn branch from 672b601 to 8667465 Compare January 4, 2015 00:13
@dirk
Copy link
Contributor Author

dirk commented Jan 4, 2015

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,
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, fixed!

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with comments addressed!

@dirk
Copy link
Contributor Author

dirk commented Jan 20, 2015

Oh, sorry! Was on a business trip last week and let this lapse. Will get back to it shortly.

lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 28, 2025
…yarn/editors/code/form-data-4.0.4

Bump form-data from 4.0.2 to 4.0.4 in /editors/code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants