Skip to content

Commit b03702a

Browse files
committed
Stabilize Span::resolved_at + Span::located_at with fallback
On compilers prior to 1.45 where this is not stable, these fall back to returning the span associated with resolution behavior, since the source location is only cosmetic. This is a reversal of the previous fallback implementation, which preserved source location because it does not track resolution location. The differnce is only observable with `span_locations` enabled. These methods were stabilized in Rust in rust-lang/rust#69041
1 parent 9e3896d commit b03702a

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

build.rs

+4
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

src/fallback.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,12 @@ impl Span {
384384
Span::call_site()
385385
}
386386

387-
#[cfg(procmacro2_semver_exempt)]
388-
pub fn resolved_at(&self, _other: Span) -> Span {
389-
// Stable spans consist only of line/column information, so
390-
// `resolved_at` and `located_at` only select which span the
391-
// caller wants line/column information from.
392-
*self
387+
pub fn resolved_at(&self, other: Span) -> Span {
388+
other
393389
}
394390

395-
#[cfg(procmacro2_semver_exempt)]
396-
pub fn located_at(&self, other: Span) -> Span {
397-
other
391+
pub fn located_at(&self, _other: Span) -> Span {
392+
*self
398393
}
399394

400395
#[cfg(procmacro2_semver_exempt)]

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,17 @@ impl Span {
369369
/// Creates a new span with the same line/column information as `self` but
370370
/// that resolves symbols as though it were at `other`.
371371
///
372-
/// This method is semver exempt and not exposed by default.
373-
#[cfg(procmacro2_semver_exempt)]
372+
/// On versions of Rust prior to 1.45, this returns `self` to preserve the
373+
/// name resolution behavior while discarding location information.
374374
pub fn resolved_at(&self, other: Span) -> Span {
375375
Span::_new(self.inner.resolved_at(other.inner))
376376
}
377377

378378
/// Creates a new span with the same name resolution behavior as `self` but
379379
/// with the line/column information of `other`.
380380
///
381-
/// This method is semver exempt and not exposed by default.
382-
#[cfg(procmacro2_semver_exempt)]
381+
/// On versions of Rust prior to 1.45, this returns `self` to preserve the
382+
/// name resolution behavior while discarding location information.
383383
pub fn located_at(&self, other: Span) -> Span {
384384
Span::_new(self.inner.located_at(other.inner))
385385
}

src/wrapper.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -394,19 +394,29 @@ impl Span {
394394
}
395395
}
396396

397-
#[cfg(super_unstable)]
398397
pub fn resolved_at(&self, other: Span) -> Span {
399398
match (self, other) {
399+
#[cfg(hygiene)]
400400
(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+
401406
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
402407
_ => mismatch(),
403408
}
404409
}
405410

406-
#[cfg(super_unstable)]
407411
pub fn located_at(&self, other: Span) -> Span {
408412
match (self, other) {
413+
#[cfg(hygiene)]
409414
(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+
410420
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
411421
_ => mismatch(),
412422
}

0 commit comments

Comments
 (0)