Skip to content

Loosen shadowing check inside macro contexts. #99191

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 1 commit into from

Conversation

davidv1992
Copy link
Contributor

This allows macro code to use identifiers for local variables which
are also defined as statics/consts/ctors outside of the macro. This
improves robustness of macro hygiene, as identifiers used inside a
macro can no longer produce unexpected restrictions on names for
statics and consts defined outside of it. Concretely, constructions
such as

thread_local!(static FOO: usize = 0);
static init: usize = 0;

no longer unexpectedly fail to compile because thread_local! happens
to define a function with init as a parameter name.

This fixes #99018

This allows macro code to use identifiers for local variables which
are also defined as statics/consts/ctors outside of the macro. This
improves robustness of macro hygiene, as identifiers used inside a
macro can no longer produce unexpected restrictions on names for
statics and consts defined outside of it. Concretely, constructions
such as
```rust
thread_local!(static FOO: usize = 0);
static init: usize = 0;
```
no longer unexpectedly fail to compile because thread_local! happens
to define a function with init as a parameter name.
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 12, 2022
@rust-highfive
Copy link
Contributor

r? @jackh726

(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 Jul 12, 2022
@jackh726
Copy link
Member

Probably needs lang FCP, but this isn't my area of expertise

r? @petrochenkov

@davidv1992
Copy link
Contributor Author

Hmm, did a further check today that I forgot yesterday, turns out there is also still a bug in my fix, in that it now doesn't complain about shadowing in the following code:

macro_rules! h {
    () => {
        static x: usize = 2;
        fn a(x: usize) -> usize {
            x + 2
        }
    }
}

h!();

fn main() {}

I will dive deeper into this next weekend when I have some time.

@petrochenkov
Copy link
Contributor

thread_local is a macro_rules, and macro_rules do not provide hygiene for items.
Adjusting macro_rules hygiene to work for items in one specific case is certainly not a good idea.

@petrochenkov
Copy link
Contributor

Closing as motivated by #99191 (comment), macro_rules are supposed to be transparent for item names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

E0530 when using std::thread_local and a static named init
5 participants