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

Fix macro hygiene bug #32923

Merged
merged 4 commits into from Apr 16, 2016
Merged

Fix macro hygiene bug #32923

merged 4 commits into from Apr 16, 2016

Conversation

@jseyfried
Copy link
Contributor

jseyfried commented Apr 13, 2016

This fixes #32922 (EDIT: also #31856, #10681, and #26223), macro hygiene bugs.
It is a [breaking-change]. For example, the following would break:

fn main() {
    let x = true;
    macro_rules! foo { () => {
        let x = 0;
        macro_rules! bar { () => {x} }
        let _: bool = bar!();
        //^ `bar!()` used to resolve the first `x` (a bool),
        //| but will now resolve to the second x (an i32).
    }}
    foo! {};
}

r? @nrc

@jseyfried jseyfried changed the title Fix hygiene Fix macro hygiene bug Apr 13, 2016
@jseyfried jseyfried force-pushed the jseyfried:fix_hygiene branch from 4ba7b9c to 974f1ef Apr 14, 2016
@jseyfried jseyfried mentioned this pull request Apr 14, 2016
@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Apr 14, 2016

This could use a crater run (cf. discussion in #32922).

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 14, 2016

Starting a crater run

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 15, 2016

Crater reports one regression, but it looks spurious, so essentially nothing from crater.

@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Apr 15, 2016

Great, thanks!

@nrc

This comment has been minimized.

Copy link
Member

nrc commented Apr 15, 2016

Could you add some of the tests from #31856 please? r+ with that

@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Apr 15, 2016

@nrc I added another test from #31856.

@nrc

This comment has been minimized.

Copy link
Member

nrc commented Apr 15, 2016

@bors: r+

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Apr 15, 2016

📌 Commit ca1d29c has been approved by nrc

Manishearth added a commit to Manishearth/rust that referenced this pull request Apr 15, 2016
Fix macro hygiene bug

This fixes rust-lang#32922 (EDIT: and fixes rust-lang#31856), macro hygiene bugs.
It is a [breaking-change]. For example, the following would break:
```rust
fn main() {
    let x = true;
    macro_rules! foo { () => {
        let x = 0;
        macro_rules! bar { () => {x} }
        let _: bool = bar!();
        //^ `bar!()` used to resolve the first `x` (a bool),
        //| but will now resolve to the second x (an i32).
    }}
    foo! {};
}
```

r? @nrc
bors added a commit that referenced this pull request Apr 15, 2016
Rollup of 11 pull requests

- Successful merges: #32923, #32926, #32929, #32931, #32935, #32945, #32946, #32964, #32970, #32973, #32997
- Failed merges:
@bors bors merged commit ca1d29c into rust-lang:master Apr 16, 2016
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Jun 2, 2016

Fixes #10681.

@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Jun 9, 2016

Fixes #26223.

bors added a commit that referenced this pull request Jun 23, 2016
Fix macro hygiene regression

The regression was caused by #32923, which is currently in beta.

The following is an example of regressed code:
```rust
fn main() {
    let x = 0;
    macro_rules! foo { () => {
        println!("{}", x); // prints `0` on stable and after this PR, prints `1` on beta and nightly
    } }

    let x = 1;
    foo!();
}
```

For code to regress, the following is necessary (but not sufficient):
 - There must be a local variable before a macro in a block, and the macro must use the variable.
 - There must be a second local variable with the same name after the macro.
 - The macro must be invoked in a statement position after the second local variable.

For example, if the `let x = 0;` from the breaking example were commented out, it would (correctly) not compile on beta/nightly. If the semicolon were removed from `foo!();`, it would (correctly) print `0` on beta and nightly.

r? @nrc
@jseyfried jseyfried deleted the jseyfried:fix_hygiene branch Feb 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.