Skip to content
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

Pass visitor to visit_number! macro #1

Merged
merged 1 commit into from
Jun 2, 2020

Conversation

Aaron1011
Copy link
Contributor

In Rust, variable identifiers in a macro_rules! body are resolved
in the scope of tht body - they cannot see through to the caller's body.
For example, the following code errors:

macro_rules! weird_ident {
    () => { my_ident; }
}

fn main() {
    let my_ident = 1;
    weird_ident!();
}

However, due to a compiler bug (rust-lang/rust#43081),
this kind of code current compiles when a procedural macro is invoked by
a macro_rules! macro. Eventually, this will cause a compilation error.

In the visit_number! macro, you're currently writing an expression
involving visitor, and passing it to a procedural macro (paste!).
However, visitor comes from the body of the caller of visit_number!,
so this code will stop compiling once the compiler bug is fixed.

Fortunately, the identifier of visitor can be passed into
visit_number!. This modified code will with the current version of
Rust, as well as future version that causes the old code into an error.

Feel free to ask me about any questions you may have. For more details,
see rust-lang/rust#72622

In Rust, variable identifiers in a `macro_rules!` body are resolved
in the scope of tht body - they cannot see through to the caller's body.
For example, the following code errors:

```rust
macro_rules! weird_ident {
    () => { my_ident; }
}

fn main() {
    let my_ident = 1;
    weird_ident!();
}
```

However, due to a compiler bug (rust-lang/rust#43081),
this kind of code current compiles when a procedural macro is invoked by
a `macro_rules!` macro. Eventually, this will cause a compilation error.

In the `visit_number!` macro, you're currently writing an expression
involving `visitor`, and passing it to a procedural macro (`paste!`).
However, `visitor` comes from the body of the caller of `visit_number!`,
so this code will stop compiling once the compiler bug is fixed.

Fortunately, the identifier of `visitor` can be passed into
`visit_number!`. This modified code will with the current version of
Rust, as well as future version that causes the old code into an error.

Feel free to ask me about any questions you may have. For more details,
see rust-lang/rust#72622
@ilyvion
Copy link
Owner

ilyvion commented Jun 2, 2020

Ah, you're the one who accidentally tagged all those people by accident. 😄 Thank you for finding and fixing this issue in my code. I honestly never expected a pull request to this repo, much less due to a change to the Rust compiler, but I appreciate just being handed the fix. 👍

@Aaron1011
Copy link
Contributor Author

@alexschrod: Could you make a new point release containing this fix? When rust-lang/rust#73084 is merged, this bug will be exposed.

ilyvion added a commit that referenced this pull request Jul 27, 2020
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.

2 participants