Span::resolved_at and Span::located_at to combine behavior of two spans

This commit is contained in:
David Tolnay 2018-01-05 18:10:22 -08:00
parent 71c4b88c5c
commit 4e8e3973f7
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 31 additions and 0 deletions

View File

@ -162,6 +162,18 @@ impl Span {
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 {

View File

@ -334,6 +334,17 @@ impl Span {
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| {

View File

@ -225,6 +225,14 @@ impl Span {
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
}