-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #46551 - jseyfried:improve_legacy_modern_macro_interact…
…ion, r=nrc macros: improve 1.0/2.0 interaction This PR supports using unhygienic macros from hygienic macros without breaking the latter's hygiene. ```rust // crate A: #[macro_export] macro_rules! m1 { () => { f(); // unhygienic: this macro needs `f` in its environment fn g() {} // (1) unhygienic: `g` is usable outside the macro definition } } // crate B: #![feature(decl_macro)] extern crate A; use A::m1; macro m2() { fn f() {} // (2) m1!(); // After this PR, `f()` in the expansion resolves to (2), not (3) g(); // After this PR, this resolves to `fn g() {}` from the above expansion. // Today, it is a resolution error. } fn test() { fn f() {} // (3) m2!(); // Today, `m2!()` can see (3) even though it should be hygienic. fn g() {} // Today, this conflicts with `fn g() {}` from the expansion, even though it should be hygienic. } ``` Once this PR lands, you can make an existing unhygienic macro hygienic by wrapping it in a hygienic macro. There is an [example](b766fa8) of this in the tests. r? @nrc
- Loading branch information
Showing
11 changed files
with
217 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// ignore-pretty pretty-printing is unhygienic | ||
|
||
#[macro_export] | ||
macro_rules! m { | ||
() => { | ||
fn f() {} // (2) | ||
g(); // (1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
pub fn f() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![crate_type = "lib"] | ||
|
||
extern crate my_crate; | ||
|
||
pub fn g() {} // (a) | ||
|
||
#[macro_export] | ||
macro_rules! unhygienic_macro { | ||
() => { | ||
// (1) unhygienic: depends on `my_crate` in the crate root at the invocation site. | ||
::my_crate::f(); | ||
|
||
// (2) unhygienic: defines `f` at the invocation site (in addition to the above point). | ||
use my_crate::f; | ||
f(); | ||
|
||
g(); // (3) unhygienic: `g` needs to be in scope at use site. | ||
|
||
$crate::g(); // (4) hygienic: this always resolves to (a) | ||
} | ||
} | ||
|
||
#[allow(unused)] | ||
fn test_unhygienic() { | ||
unhygienic_macro!(); | ||
f(); // `f` was defined at the use site | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// ignore-pretty pretty-printing is unhygienic | ||
|
||
// aux-build:legacy_interaction.rs | ||
|
||
#![feature(decl_macro)] | ||
#[allow(unused)] | ||
|
||
extern crate legacy_interaction; | ||
// ^ defines | ||
// ```rust | ||
// macro_rules! m { | ||
// () => { | ||
// fn f() // (1) | ||
// g() // (2) | ||
// } | ||
// } | ||
// ```rust | ||
|
||
mod def_site { | ||
// Unless this macro opts out of hygiene, it should resolve the same wherever it is invoked. | ||
pub macro m2() { | ||
::legacy_interaction::m!(); | ||
f(); // This should resolve to (1) | ||
fn g() {} // We want (2) resolve to this, not to (4) | ||
} | ||
} | ||
|
||
mod use_site { | ||
fn test() { | ||
fn f() -> bool { true } // (3) | ||
fn g() -> bool { true } // (4) | ||
|
||
::def_site::m2!(); | ||
|
||
let _: bool = f(); // This should resolve to (3) | ||
let _: bool = g(); // This should resolve to (4) | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// ignore-pretty pretty-printing is unhygienic | ||
|
||
// aux-build:my_crate.rs | ||
// aux-build:unhygienic_example.rs | ||
|
||
#![feature(decl_macro)] | ||
|
||
extern crate unhygienic_example; | ||
extern crate my_crate; // (b) | ||
|
||
// Hygienic version of `unhygienic_macro`. | ||
pub macro hygienic_macro() { | ||
fn g() {} // (c) | ||
::unhygienic_example::unhygienic_macro!(); | ||
// ^ Even though we invoke an unhygienic macro, `hygienic_macro` remains hygienic. | ||
// In the above expansion: | ||
// (1) `my_crate` always resolves to (b) regardless of invocation site. | ||
// (2) The defined function `f` is only usable inside this macro definition. | ||
// (3) `g` always resolves to (c) regardless of invocation site. | ||
// (4) `$crate::g` remains hygienic and continues to resolve to (a). | ||
|
||
f(); | ||
} | ||
|
||
#[allow(unused)] | ||
fn test_hygienic_macro() { | ||
hygienic_macro!(); | ||
|
||
fn f() {} // (d) no conflict | ||
f(); // resolves to (d) | ||
} | ||
|
||
fn main() {} |