Skip to content

Commit

Permalink
Stabilize Span::resolved_at + Span::located_at with fallback
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kevinmehall committed May 19, 2020
1 parent 9e3896d commit b03702a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// procmacro2_semver_exempt surface area is implemented by using the
// nightly-only proc_macro API.
//
// "hygiene"
// Enable Span::mixed_site() and non-dummy behavior of Span::resolved_at
// and Span::located_at. Enabled on Rust 1.45+.
//
// "proc_macro_span"
// Enable non-dummy behavior of Span::start and Span::end methods which
// requires an unstable compiler feature. Enabled when building with
Expand Down
13 changes: 4 additions & 9 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,17 +384,12 @@ impl Span {
Span::call_site()
}

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

#[cfg(procmacro2_semver_exempt)]
pub fn located_at(&self, other: Span) -> Span {
other
pub fn located_at(&self, _other: Span) -> Span {
*self
}

#[cfg(procmacro2_semver_exempt)]
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,17 @@ impl Span {
/// Creates a new span with the same line/column information as `self` but
/// that resolves symbols as though it were at `other`.
///
/// This method is semver exempt and not exposed by default.
#[cfg(procmacro2_semver_exempt)]
/// On versions of Rust prior to 1.45, this returns `self` to preserve the
/// name resolution behavior while discarding location information.
pub fn resolved_at(&self, other: Span) -> Span {
Span::_new(self.inner.resolved_at(other.inner))
}

/// Creates a new span with the same name resolution behavior as `self` but
/// with the line/column information of `other`.
///
/// This method is semver exempt and not exposed by default.
#[cfg(procmacro2_semver_exempt)]
/// On versions of Rust prior to 1.45, this returns `self` to preserve the
/// name resolution behavior while discarding location information.
pub fn located_at(&self, other: Span) -> Span {
Span::_new(self.inner.located_at(other.inner))
}
Expand Down
14 changes: 12 additions & 2 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,19 +394,29 @@ impl Span {
}
}

#[cfg(super_unstable)]
pub fn resolved_at(&self, other: Span) -> Span {
match (self, other) {
#[cfg(hygiene)]
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),

// Name resolution affects semantics, but location is only cosmetic
#[cfg(not(hygiene))]
(Span::Compiler(_), Span::Compiler(_)) => other,

(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
_ => mismatch(),
}
}

#[cfg(super_unstable)]
pub fn located_at(&self, other: Span) -> Span {
match (self, other) {
#[cfg(hygiene)]
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),

// Name resolution affects semantics, but location is only cosmetic
#[cfg(not(hygiene))]
(Span::Compiler(_), Span::Compiler(_)) => *self,

(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
_ => mismatch(),
}
Expand Down

0 comments on commit b03702a

Please sign in to comment.