Skip to content

Commit cc97264

Browse files
committed
Expose proc_macro's before() and after() methods on Span
1 parent 648e2d8 commit cc97264

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

build.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ fn main() {
111111
println!("cargo:rustc-cfg=wrap_proc_macro");
112112
}
113113

114-
if version.nightly && feature_allowed("proc_macro_span") {
114+
if version.nightly
115+
&& feature_allowed("proc_macro_span")
116+
&& feature_allowed("proc_macro_span_shrink")
117+
{
115118
println!("cargo:rustc-cfg=proc_macro_span");
116119
}
117120

src/fallback.rs

+20
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,26 @@ impl Span {
512512
})
513513
}
514514

515+
#[cfg(procmacro2_semver_exempt)]
516+
pub fn before(&self) -> Span {
517+
Span {
518+
#[cfg(span_locations)]
519+
lo: self.lo,
520+
#[cfg(span_locations)]
521+
hi: self.lo,
522+
}
523+
}
524+
525+
#[cfg(procmacro2_semver_exempt)]
526+
pub fn after(&self) -> Span {
527+
Span {
528+
#[cfg(span_locations)]
529+
lo: self.hi,
530+
#[cfg(span_locations)]
531+
hi: self.hi,
532+
}
533+
}
534+
515535
#[cfg(not(span_locations))]
516536
pub fn join(&self, _other: Span) -> Option<Span> {
517537
Some(Span {})

src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@
8787
8888
// Proc-macro2 types in rustdoc of other crates get linked to here.
8989
#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.43")]
90-
#![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]
90+
#![cfg_attr(
91+
any(proc_macro_span, super_unstable),
92+
feature(proc_macro_span, proc_macro_span_shrink)
93+
)]
9194
#![cfg_attr(super_unstable, feature(proc_macro_def_site))]
9295
#![cfg_attr(doc_cfg, feature(doc_cfg))]
9396
#![allow(
@@ -509,6 +512,24 @@ impl Span {
509512
LineColumn { line, column }
510513
}
511514

515+
/// Creates an empty span pointing to directly before this span.
516+
///
517+
/// This method is semver exempt and not exposed by default.
518+
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
519+
#[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
520+
pub fn before(&self) -> Span {
521+
Span::_new(self.inner.before())
522+
}
523+
524+
/// Creates an empty span pointing to directly after this span.
525+
///
526+
/// This method is semver exempt and not exposed by default.
527+
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
528+
#[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
529+
pub fn after(&self) -> Span {
530+
Span::_new(self.inner.after())
531+
}
532+
512533
/// Create a new span encompassing `self` and `other`.
513534
///
514535
/// Returns `None` if `self` and `other` are from different files.

src/wrapper.rs

+16
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,22 @@ impl Span {
505505
}
506506
}
507507

508+
#[cfg(super_unstable)]
509+
pub fn before(&self) -> Span {
510+
match self {
511+
Span::Compiler(s) => Span::Compiler(s.before()),
512+
Span::Fallback(s) => Span::Fallback(s.before()),
513+
}
514+
}
515+
516+
#[cfg(super_unstable)]
517+
pub fn after(&self) -> Span {
518+
match self {
519+
Span::Compiler(s) => Span::Compiler(s.after()),
520+
Span::Fallback(s) => Span::Fallback(s.after()),
521+
}
522+
}
523+
508524
pub fn join(&self, other: Span) -> Option<Span> {
509525
let ret = match (self, other) {
510526
#[cfg(proc_macro_span)]

0 commit comments

Comments
 (0)