From 17b0e93acc16f797837452f07d0da718736afed9 Mon Sep 17 00:00:00 2001 From: BoredApe8416 Date: Tue, 19 May 2020 08:56:16 -0700 Subject: [PATCH] Add `Span::mixed_site()` for Rust 1.45+ Fixes #210 This was stabilized in Rust in https://github.com/rust-lang/rust/pull/68716 --- build.rs | 4 ++++ src/fallback.rs | 5 +++++ src/lib.rs | 10 ++++++++++ src/wrapper.rs | 9 +++++++++ 4 files changed, 28 insertions(+) diff --git a/build.rs b/build.rs index deb9b92..a06f009 100644 --- a/build.rs +++ b/build.rs @@ -57,6 +57,10 @@ fn main() { println!("cargo:rustc-cfg=span_locations"); } + if version.minor >= 45 { + println!("cargo:rustc-cfg=hygiene"); + } + let target = env::var("TARGET").unwrap(); if !enable_use_proc_macro(&target) { return; diff --git a/src/fallback.rs b/src/fallback.rs index fffea68..554c87e 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -374,6 +374,11 @@ impl Span { Span { lo: 0, hi: 0 } } + #[cfg(hygiene)] + pub fn mixed_site() -> Span { + Span::call_site() + } + #[cfg(procmacro2_semver_exempt)] pub fn def_site() -> Span { Span::call_site() diff --git a/src/lib.rs b/src/lib.rs index 23b2c26..725e121 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -348,6 +348,16 @@ impl Span { Span::_new(imp::Span::call_site()) } + /// The span located at the invocation of the procedural macro, but with + /// local variables, labels, and `$crate` resolved at the definition site + /// of the macro. This is the same hygiene behavior as `macro_rules`. + /// + /// This function requires Rust 1.45 or later. + #[cfg(hygiene)] + pub fn mixed_site() -> Span { + Span::_new(imp::Span::mixed_site()) + } + /// A span that resolves at the macro definition site. /// /// This method is semver exempt and not exposed by default. diff --git a/src/wrapper.rs b/src/wrapper.rs index 1887d8e..f9429b7 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -376,6 +376,15 @@ impl Span { } } + #[cfg(hygiene)] + pub fn mixed_site() -> Span { + if inside_proc_macro() { + Span::Compiler(proc_macro::Span::mixed_site()) + } else { + Span::Fallback(fallback::Span::mixed_site()) + } + } + #[cfg(super_unstable)] pub fn def_site() -> Span { if inside_proc_macro() {