Skip to content

Commit f093323

Browse files
committed
Add Span::mixed_site() behind hygiene feature.
Fixes dtolnay#210 This was stabilized in Rust in rust-lang/rust#68716
1 parent 63635dc commit f093323

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
- run: cargo test --no-default-features
4141
- run: cargo test --no-default-features -- --ignored # run the ignored test to make sure the `proc-macro` feature is disabled
4242
- run: cargo test --features span-locations
43+
- run: cargo test --features hygiene
4344
- run: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test
4445
- run: RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test --no-default-features
4546
- run: RUSTFLAGS='-Z allow-features=' cargo test

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ default = ["proc-macro"]
3636
# of a token.
3737
span-locations = []
3838

39+
hygiene = []
40+
3941
# This feature no longer means anything.
4042
nightly = []
4143

build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ fn main() {
5757
println!("cargo:rustc-cfg=span_locations");
5858
}
5959

60+
if semver_exempt || cfg!(feature = "hygiene") {
61+
println!("cargo:rustc-cfg=hygiene");
62+
}
63+
6064
let target = env::var("TARGET").unwrap();
6165
if !enable_use_proc_macro(&target) {
6266
return;

src/fallback.rs

+5
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ impl Span {
374374
Span { lo: 0, hi: 0 }
375375
}
376376

377+
#[cfg(hygiene)]
378+
pub fn mixed_site() -> Span {
379+
Span::call_site()
380+
}
381+
377382
#[cfg(procmacro2_semver_exempt)]
378383
pub fn def_site() -> Span {
379384
Span::call_site()

src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ impl Span {
348348
Span::_new(imp::Span::call_site())
349349
}
350350

351+
/// The span located at the invocation of the procedural macro, but with
352+
/// local variables, labels, and `$crate` resolved at the definition site
353+
/// of the macro.
354+
///
355+
/// `macro_rules` behaves like this in terms of hygiene.
356+
///
357+
/// This method requires the `"hygiene"` feature to be enabled.
358+
#[cfg(hygiene)]
359+
pub fn mixed_site() -> Span {
360+
Span::_new(imp::Span::mixed_site())
361+
}
362+
351363
/// A span that resolves at the macro definition site.
352364
///
353365
/// This method is semver exempt and not exposed by default.

src/wrapper.rs

+9
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ impl Span {
376376
}
377377
}
378378

379+
#[cfg(hygiene)]
380+
pub fn mixed_site() -> Span {
381+
if inside_proc_macro() {
382+
Span::Compiler(proc_macro::Span::mixed_site())
383+
} else {
384+
Span::Fallback(fallback::Span::mixed_site())
385+
}
386+
}
387+
379388
#[cfg(super_unstable)]
380389
pub fn def_site() -> Span {
381390
if inside_proc_macro() {

0 commit comments

Comments
 (0)