Skip to content

Commit

Permalink
Merge pull request #348 from dtolnay/beforeafter
Browse files Browse the repository at this point in the history
Expose proc_macro's before() and after() methods on Span
  • Loading branch information
dtolnay committed Sep 24, 2022
2 parents 648e2d8 + cc97264 commit c0592e3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion build.rs
Expand Up @@ -111,7 +111,10 @@ fn main() {
println!("cargo:rustc-cfg=wrap_proc_macro");
}

if version.nightly && feature_allowed("proc_macro_span") {
if version.nightly
&& feature_allowed("proc_macro_span")
&& feature_allowed("proc_macro_span_shrink")
{
println!("cargo:rustc-cfg=proc_macro_span");
}

Expand Down
20 changes: 20 additions & 0 deletions src/fallback.rs
Expand Up @@ -512,6 +512,26 @@ impl Span {
})
}

#[cfg(procmacro2_semver_exempt)]
pub fn before(&self) -> Span {
Span {
#[cfg(span_locations)]
lo: self.lo,
#[cfg(span_locations)]
hi: self.lo,
}
}

#[cfg(procmacro2_semver_exempt)]
pub fn after(&self) -> Span {
Span {
#[cfg(span_locations)]
lo: self.hi,
#[cfg(span_locations)]
hi: self.hi,
}
}

#[cfg(not(span_locations))]
pub fn join(&self, _other: Span) -> Option<Span> {
Some(Span {})
Expand Down
23 changes: 22 additions & 1 deletion src/lib.rs
Expand Up @@ -87,7 +87,10 @@

// Proc-macro2 types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.43")]
#![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]
#![cfg_attr(
any(proc_macro_span, super_unstable),
feature(proc_macro_span, proc_macro_span_shrink)
)]
#![cfg_attr(super_unstable, feature(proc_macro_def_site))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![allow(
Expand Down Expand Up @@ -509,6 +512,24 @@ impl Span {
LineColumn { line, column }
}

/// Creates an empty span pointing to directly before this span.
///
/// This method is semver exempt and not exposed by default.
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
#[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
pub fn before(&self) -> Span {
Span::_new(self.inner.before())
}

/// Creates an empty span pointing to directly after this span.
///
/// This method is semver exempt and not exposed by default.
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
#[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
pub fn after(&self) -> Span {
Span::_new(self.inner.after())
}

/// Create a new span encompassing `self` and `other`.
///
/// Returns `None` if `self` and `other` are from different files.
Expand Down
16 changes: 16 additions & 0 deletions src/wrapper.rs
Expand Up @@ -505,6 +505,22 @@ impl Span {
}
}

#[cfg(super_unstable)]
pub fn before(&self) -> Span {
match self {
Span::Compiler(s) => Span::Compiler(s.before()),
Span::Fallback(s) => Span::Fallback(s.before()),
}
}

#[cfg(super_unstable)]
pub fn after(&self) -> Span {
match self {
Span::Compiler(s) => Span::Compiler(s.after()),
Span::Fallback(s) => Span::Fallback(s.after()),
}
}

pub fn join(&self, other: Span) -> Option<Span> {
let ret = match (self, other) {
#[cfg(proc_macro_span)]
Expand Down

0 comments on commit c0592e3

Please sign in to comment.