Skip to content

Commit 4e8e397

Browse files
committed
Span::resolved_at and Span::located_at to combine behavior of two spans
1 parent 71c4b88 commit 4e8e397

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ impl Span {
162162
Span(imp::Span::def_site())
163163
}
164164

165+
/// Creates a new span with the same line/column information as `self` but
166+
/// that resolves symbols as though it were at `other`.
167+
pub fn resolved_at(&self, other: Span) -> Span {
168+
Span(self.0.resolved_at(other.0))
169+
}
170+
171+
/// Creates a new span with the same name resolution behavior as `self` but
172+
/// with the line/column information of `other`.
173+
pub fn located_at(&self, other: Span) -> Span {
174+
Span(self.0.located_at(other.0))
175+
}
176+
165177
/// This method is only available when the `"nightly"` feature is enabled.
166178
#[cfg(feature = "nightly")]
167179
pub fn unstable(self) -> proc_macro::Span {

src/stable.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ impl Span {
334334
Span::call_site()
335335
}
336336

337+
pub fn resolved_at(&self, _other: Span) -> Span {
338+
// Stable spans consist only of line/column information, so
339+
// `resolved_at` and `located_at` only select which span the
340+
// caller wants line/column information from.
341+
*self
342+
}
343+
344+
pub fn located_at(&self, other: Span) -> Span {
345+
other
346+
}
347+
337348
#[cfg(procmacro2_semver_exempt)]
338349
pub fn source_file(&self) -> SourceFile {
339350
CODEMAP.with(|cm| {

src/unstable.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ impl Span {
225225
Span(proc_macro::Span::def_site())
226226
}
227227

228+
pub fn resolved_at(&self, other: Span) -> Span {
229+
Span(self.0.resolved_at(other.0))
230+
}
231+
232+
pub fn located_at(&self, other: Span) -> Span {
233+
Span(self.0.located_at(other.0))
234+
}
235+
228236
pub fn unstable(self) -> proc_macro::Span {
229237
self.0
230238
}

0 commit comments

Comments
 (0)