Skip to content

Commit

Permalink
Add Span::mixed_site() behind hygiene feature.
Browse files Browse the repository at this point in the history
Fixes dtolnay#210

This was stabilized in Rust in
rust-lang/rust#68716
  • Loading branch information
kevinmehall committed May 18, 2020
1 parent 63635dc commit f093323
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- run: cargo test --no-default-features
- run: cargo test --no-default-features -- --ignored # run the ignored test to make sure the `proc-macro` feature is disabled
- run: cargo test --features span-locations
- run: cargo test --features hygiene
- run: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test
- run: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test --no-default-features
- run: RUSTFLAGS='-Z allow-features=' cargo test
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ default = ["proc-macro"]
# of a token.
span-locations = []

hygiene = []

# This feature no longer means anything.
nightly = []

Expand Down
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ fn main() {
println!("cargo:rustc-cfg=span_locations");
}

if semver_exempt || cfg!(feature = "hygiene") {
println!("cargo:rustc-cfg=hygiene");
}

let target = env::var("TARGET").unwrap();
if !enable_use_proc_macro(&target) {
return;
Expand Down
5 changes: 5 additions & 0 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ 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.
///
/// `macro_rules` behaves like this in terms of hygiene.
///
/// This method requires the `"hygiene"` feature to be enabled.
#[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.
Expand Down
9 changes: 9 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit f093323

Please sign in to comment.