Span::resolved_at and Span::located_at to combine behavior of two spans
diff --git a/src/lib.rs b/src/lib.rs
index 49e3d21..6556c97 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -162,6 +162,18 @@
Span(imp::Span::def_site())
}
+ /// Creates a new span with the same line/column information as `self` but
+ /// that resolves symbols as though it were at `other`.
+ pub fn resolved_at(&self, other: Span) -> Span {
+ Span(self.0.resolved_at(other.0))
+ }
+
+ /// Creates a new span with the same name resolution behavior as `self` but
+ /// with the line/column information of `other`.
+ pub fn located_at(&self, other: Span) -> Span {
+ Span(self.0.located_at(other.0))
+ }
+
/// This method is only available when the `"nightly"` feature is enabled.
#[cfg(feature = "nightly")]
pub fn unstable(self) -> proc_macro::Span {
diff --git a/src/stable.rs b/src/stable.rs
index b5820b1..0a439ed 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -334,6 +334,17 @@
Span::call_site()
}
+ 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 located_at(&self, other: Span) -> Span {
+ other
+ }
+
#[cfg(procmacro2_semver_exempt)]
pub fn source_file(&self) -> SourceFile {
CODEMAP.with(|cm| {
diff --git a/src/unstable.rs b/src/unstable.rs
index f8cd68c..1c4b834 100644
--- a/src/unstable.rs
+++ b/src/unstable.rs
@@ -225,6 +225,14 @@
Span(proc_macro::Span::def_site())
}
+ pub fn resolved_at(&self, other: Span) -> Span {
+ Span(self.0.resolved_at(other.0))
+ }
+
+ pub fn located_at(&self, other: Span) -> Span {
+ Span(self.0.located_at(other.0))
+ }
+
pub fn unstable(self) -> proc_macro::Span {
self.0
}