Skip to content

Commit 0283321

Browse files
committed
Merge pull request 228 from kevinmehall/hygiene
2 parents 63635dc + 9b4633b commit 0283321

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
// procmacro2_semver_exempt surface area is implemented by using the
1515
// nightly-only proc_macro API.
1616
//
17+
// "hygiene"
18+
// Enable Span::mixed_site() and non-dummy behavior of Span::resolved_at
19+
// and Span::located_at. Enabled on Rust 1.45+.
20+
//
1721
// "proc_macro_span"
1822
// Enable non-dummy behavior of Span::start and Span::end methods which
1923
// requires an unstable compiler feature. Enabled when building with
@@ -57,6 +61,10 @@ fn main() {
5761
println!("cargo:rustc-cfg=span_locations");
5862
}
5963

64+
if version.minor >= 45 {
65+
println!("cargo:rustc-cfg=hygiene");
66+
}
67+
6068
let target = env::var("TARGET").unwrap();
6169
if !enable_use_proc_macro(&target) {
6270
return;

src/fallback.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,20 +374,23 @@ 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()
380385
}
381386

382-
#[cfg(procmacro2_semver_exempt)]
383387
pub fn resolved_at(&self, _other: Span) -> Span {
384388
// Stable spans consist only of line/column information, so
385389
// `resolved_at` and `located_at` only select which span the
386390
// caller wants line/column information from.
387391
*self
388392
}
389393

390-
#[cfg(procmacro2_semver_exempt)]
391394
pub fn located_at(&self, other: Span) -> Span {
392395
other
393396
}

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ 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. This is the same hygiene behavior as `macro_rules`.
354+
///
355+
/// This function requires Rust 1.45 or later.
356+
#[cfg(hygiene)]
357+
pub fn mixed_site() -> Span {
358+
Span::_new(imp::Span::mixed_site())
359+
}
360+
351361
/// A span that resolves at the macro definition site.
352362
///
353363
/// This method is semver exempt and not exposed by default.
@@ -358,18 +368,12 @@ impl Span {
358368

359369
/// Creates a new span with the same line/column information as `self` but
360370
/// that resolves symbols as though it were at `other`.
361-
///
362-
/// This method is semver exempt and not exposed by default.
363-
#[cfg(procmacro2_semver_exempt)]
364371
pub fn resolved_at(&self, other: Span) -> Span {
365372
Span::_new(self.inner.resolved_at(other.inner))
366373
}
367374

368375
/// Creates a new span with the same name resolution behavior as `self` but
369376
/// with the line/column information of `other`.
370-
///
371-
/// This method is semver exempt and not exposed by default.
372-
#[cfg(procmacro2_semver_exempt)]
373377
pub fn located_at(&self, other: Span) -> Span {
374378
Span::_new(self.inner.located_at(other.inner))
375379
}

src/wrapper.rs

Lines changed: 21 additions & 2 deletions
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() {
@@ -385,19 +394,29 @@ impl Span {
385394
}
386395
}
387396

388-
#[cfg(super_unstable)]
389397
pub fn resolved_at(&self, other: Span) -> Span {
390398
match (self, other) {
399+
#[cfg(hygiene)]
391400
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
401+
402+
// Name resolution affects semantics, but location is only cosmetic
403+
#[cfg(not(hygiene))]
404+
(Span::Compiler(_), Span::Compiler(_)) => other,
405+
392406
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
393407
_ => mismatch(),
394408
}
395409
}
396410

397-
#[cfg(super_unstable)]
398411
pub fn located_at(&self, other: Span) -> Span {
399412
match (self, other) {
413+
#[cfg(hygiene)]
400414
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
415+
416+
// Name resolution affects semantics, but location is only cosmetic
417+
#[cfg(not(hygiene))]
418+
(Span::Compiler(_), Span::Compiler(_)) => *self,
419+
401420
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
402421
_ => mismatch(),
403422
}

0 commit comments

Comments
 (0)