Skip to content

Commit 76ce1a3

Browse files
committed
Stabilize Span::file and Span::local_file
1 parent b5dd3c6 commit 76ce1a3

File tree

7 files changed

+54
-18
lines changed

7 files changed

+54
-18
lines changed

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn main() {
2020
println!("cargo:rustc-check-cfg=cfg(no_literal_c_string)");
2121
println!("cargo:rustc-check-cfg=cfg(no_source_text)");
2222
println!("cargo:rustc-check-cfg=cfg(proc_macro_span)");
23+
println!("cargo:rustc-check-cfg=cfg(proc_macro_span_file)");
2324
println!("cargo:rustc-check-cfg=cfg(proc_macro_span_location)");
2425
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
2526
println!("cargo:rustc-check-cfg=cfg(procmacro2_build_probe)");
@@ -128,6 +129,12 @@ fn main() {
128129
println!("cargo:rustc-cfg=proc_macro_span_location");
129130
}
130131

132+
if proc_macro_span || (rustc >= 88 && compile_probe_stable("proc_macro_span_file")) {
133+
// Enable non-dummy behavior of Span::file and Span::local_file methods
134+
// on Rust 1.88+.
135+
println!("cargo:rustc-cfg=proc_macro_span_file");
136+
}
137+
131138
if semver_exempt && proc_macro_span {
132139
// Implement the semver exempt API in terms of the nightly-only
133140
// proc_macro API.

src/fallback.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::str::FromStr;
2323
use std::ffi::CStr;
2424
#[cfg(wrap_proc_macro)]
2525
use std::panic;
26-
#[cfg(procmacro2_semver_exempt)]
26+
#[cfg(span_locations)]
2727
use std::path::PathBuf;
2828

2929
/// Force use of proc-macro2's fallback implementation of the API for now, even
@@ -455,7 +455,6 @@ impl SourceMap {
455455
span
456456
}
457457

458-
#[cfg(procmacro2_semver_exempt)]
459458
fn filepath(&self, span: Span) -> String {
460459
for (i, file) in self.files.iter().enumerate() {
461460
if file.span_within(span) {
@@ -568,7 +567,7 @@ impl Span {
568567
})
569568
}
570569

571-
#[cfg(procmacro2_semver_exempt)]
570+
#[cfg(span_locations)]
572571
pub(crate) fn file(&self) -> String {
573572
#[cfg(fuzzing)]
574573
return "<unspecified>".to_owned();
@@ -580,7 +579,7 @@ impl Span {
580579
})
581580
}
582581

583-
#[cfg(procmacro2_semver_exempt)]
582+
#[cfg(span_locations)]
584583
pub(crate) fn local_file(&self) -> Option<PathBuf> {
585584
None
586585
}

src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ use core::ops::RangeBounds;
174174
use core::str::FromStr;
175175
use std::error::Error;
176176
use std::ffi::CStr;
177-
#[cfg(procmacro2_semver_exempt)]
177+
#[cfg(span_locations)]
178178
use std::path::PathBuf;
179179

180180
#[cfg(span_locations)]
@@ -471,10 +471,8 @@ impl Span {
471471
///
472472
/// This might not correspond to a valid file system path. It might be
473473
/// remapped, or might be an artificial path such as `"<macro expansion>"`.
474-
///
475-
/// This method is semver exempt and not exposed by default.
476-
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
477-
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
474+
#[cfg(span_locations)]
475+
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
478476
pub fn file(&self) -> String {
479477
self.inner.file()
480478
}
@@ -485,10 +483,8 @@ impl Span {
485483
///
486484
/// This path should not be embedded in the output of the macro; prefer
487485
/// `file()` instead.
488-
///
489-
/// This method is semver exempt and not exposed by default.
490-
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
491-
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
486+
#[cfg(span_locations)]
487+
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
492488
pub fn local_file(&self) -> Option<PathBuf> {
493489
self.inner.local_file()
494490
}

src/probe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
#[cfg(proc_macro_span)]
44
pub(crate) mod proc_macro_span;
55

6+
#[cfg(proc_macro_span_file)]
7+
pub(crate) mod proc_macro_span_file;
8+
69
#[cfg(proc_macro_span_location)]
710
pub(crate) mod proc_macro_span_location;

src/probe/proc_macro_span.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate proc_macro;
88

99
use core::ops::{Range, RangeBounds};
1010
use proc_macro::{Literal, Span};
11+
use std::path::PathBuf;
1112

1213
pub fn byte_range(this: &Span) -> Range<usize> {
1314
this.byte_range()
@@ -29,6 +30,14 @@ pub fn column(this: &Span) -> usize {
2930
this.column()
3031
}
3132

33+
pub fn file(this: &Span) -> String {
34+
this.file()
35+
}
36+
37+
pub fn local_file(this: &Span) -> Option<PathBuf> {
38+
this.local_file()
39+
}
40+
3241
pub fn join(this: &Span, other: Span) -> Option<Span> {
3342
this.join(other)
3443
}

src/probe/proc_macro_span_file.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// The subset of Span's API stabilized in Rust 1.88.
2+
3+
extern crate proc_macro;
4+
5+
use proc_macro::Span;
6+
use std::path::PathBuf;
7+
8+
pub fn file(this: &Span) -> String {
9+
this.file()
10+
}
11+
12+
pub fn local_file(this: &Span) -> Option<PathBuf> {
13+
this.local_file()
14+
}

src/wrapper.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::fallback::{self, FromStr2 as _};
44
use crate::location::LineColumn;
55
#[cfg(proc_macro_span)]
66
use crate::probe::proc_macro_span;
7+
#[cfg(all(span_locations, proc_macro_span_file))]
8+
use crate::probe::proc_macro_span_file;
79
#[cfg(all(span_locations, proc_macro_span_location))]
810
use crate::probe::proc_macro_span_location;
911
use crate::{Delimiter, Punct, Spacing, TokenTree};
@@ -12,7 +14,7 @@ use core::fmt::{self, Debug, Display};
1214
use core::ops::Range;
1315
use core::ops::RangeBounds;
1416
use std::ffi::CStr;
15-
#[cfg(super_unstable)]
17+
#[cfg(span_locations)]
1618
use std::path::PathBuf;
1719

1820
#[derive(Clone)]
@@ -463,18 +465,24 @@ impl Span {
463465
}
464466
}
465467

466-
#[cfg(super_unstable)]
468+
#[cfg(span_locations)]
467469
pub(crate) fn file(&self) -> String {
468470
match self {
469-
Span::Compiler(s) => s.file(),
471+
#[cfg(proc_macro_span_file)]
472+
Span::Compiler(s) => proc_macro_span_file::file(s),
473+
#[cfg(not(proc_macro_span_file))]
474+
Span::Compiler(_) => "<token stream>".to_owned(),
470475
Span::Fallback(s) => s.file(),
471476
}
472477
}
473478

474-
#[cfg(super_unstable)]
479+
#[cfg(span_locations)]
475480
pub(crate) fn local_file(&self) -> Option<PathBuf> {
476481
match self {
477-
Span::Compiler(s) => s.local_file(),
482+
#[cfg(proc_macro_span_file)]
483+
Span::Compiler(s) => proc_macro_span_file::local_file(s),
484+
#[cfg(not(proc_macro_span_file))]
485+
Span::Compiler(_) => None,
478486
Span::Fallback(s) => s.local_file(),
479487
}
480488
}

0 commit comments

Comments
 (0)