Skip to content

Commit 835c5bd

Browse files
authored
Merge pull request #497 from dtolnay/nosourcefile
Update semver-exempt API to nightly-2025-04-16
2 parents b867aa7 + 7bc363c commit 835c5bd

File tree

5 files changed

+71
-171
lines changed

5 files changed

+71
-171
lines changed

src/fallback.rs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -311,34 +311,6 @@ impl IntoIterator for TokenStream {
311311
}
312312
}
313313

314-
#[cfg(procmacro2_semver_exempt)]
315-
#[derive(Clone, PartialEq, Eq)]
316-
pub(crate) struct SourceFile {
317-
path: PathBuf,
318-
}
319-
320-
#[cfg(procmacro2_semver_exempt)]
321-
impl SourceFile {
322-
/// Get the path to this source file as a string.
323-
pub(crate) fn path(&self) -> PathBuf {
324-
self.path.clone()
325-
}
326-
327-
pub(crate) fn is_real(&self) -> bool {
328-
false
329-
}
330-
}
331-
332-
#[cfg(procmacro2_semver_exempt)]
333-
impl Debug for SourceFile {
334-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
335-
f.debug_struct("SourceFile")
336-
.field("path", &self.path())
337-
.field("is_real", &self.is_real())
338-
.finish()
339-
}
340-
}
341-
342314
#[cfg(all(span_locations, not(fuzzing)))]
343315
thread_local! {
344316
static SOURCE_MAP: RefCell<SourceMap> = RefCell::new(SourceMap {
@@ -484,14 +456,14 @@ impl SourceMap {
484456
}
485457

486458
#[cfg(procmacro2_semver_exempt)]
487-
fn filepath(&self, span: Span) -> PathBuf {
459+
fn filepath(&self, span: Span) -> String {
488460
for (i, file) in self.files.iter().enumerate() {
489461
if file.span_within(span) {
490-
return PathBuf::from(if i == 0 {
462+
return if i == 0 {
491463
"<unspecified>".to_owned()
492464
} else {
493465
format!("<parsed string {}>", i)
494-
});
466+
};
495467
}
496468
}
497469
unreachable!("Invalid span with no related FileInfo!");
@@ -555,21 +527,6 @@ impl Span {
555527
other
556528
}
557529

558-
#[cfg(procmacro2_semver_exempt)]
559-
pub(crate) fn source_file(&self) -> SourceFile {
560-
#[cfg(fuzzing)]
561-
return SourceFile {
562-
path: PathBuf::from("<unspecified>"),
563-
};
564-
565-
#[cfg(not(fuzzing))]
566-
SOURCE_MAP.with(|sm| {
567-
let sm = sm.borrow();
568-
let path = sm.filepath(*self);
569-
SourceFile { path }
570-
})
571-
}
572-
573530
#[cfg(span_locations)]
574531
pub(crate) fn byte_range(&self) -> Range<usize> {
575532
#[cfg(fuzzing)]
@@ -611,6 +568,23 @@ impl Span {
611568
})
612569
}
613570

571+
#[cfg(procmacro2_semver_exempt)]
572+
pub(crate) fn file(&self) -> String {
573+
#[cfg(fuzzing)]
574+
return "<unspecified>".to_owned();
575+
576+
#[cfg(not(fuzzing))]
577+
SOURCE_MAP.with(|sm| {
578+
let sm = sm.borrow();
579+
sm.filepath(*self)
580+
})
581+
}
582+
583+
#[cfg(procmacro2_semver_exempt)]
584+
pub(crate) fn local_file(&self) -> Option<PathBuf> {
585+
None
586+
}
587+
614588
#[cfg(not(span_locations))]
615589
pub(crate) fn join(&self, _other: Span) -> Option<Span> {
616590
Some(Span {})

src/lib.rs

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -338,57 +338,6 @@ impl Display for LexError {
338338

339339
impl Error for LexError {}
340340

341-
/// The source file of a given `Span`.
342-
///
343-
/// This type is semver exempt and not exposed by default.
344-
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
345-
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
346-
#[derive(Clone, PartialEq, Eq)]
347-
pub struct SourceFile {
348-
inner: imp::SourceFile,
349-
_marker: ProcMacroAutoTraits,
350-
}
351-
352-
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
353-
impl SourceFile {
354-
fn _new(inner: imp::SourceFile) -> Self {
355-
SourceFile {
356-
inner,
357-
_marker: MARKER,
358-
}
359-
}
360-
361-
/// Get the path to this source file.
362-
///
363-
/// ### Note
364-
///
365-
/// If the code span associated with this `SourceFile` was generated by an
366-
/// external macro, this may not be an actual path on the filesystem. Use
367-
/// [`is_real`] to check.
368-
///
369-
/// Also note that even if `is_real` returns `true`, if
370-
/// `--remap-path-prefix` was passed on the command line, the path as given
371-
/// may not actually be valid.
372-
///
373-
/// [`is_real`]: #method.is_real
374-
pub fn path(&self) -> PathBuf {
375-
self.inner.path()
376-
}
377-
378-
/// Returns `true` if this source file is a real source file, and not
379-
/// generated by an external macro's expansion.
380-
pub fn is_real(&self) -> bool {
381-
self.inner.is_real()
382-
}
383-
}
384-
385-
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
386-
impl Debug for SourceFile {
387-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
388-
Debug::fmt(&self.inner, f)
389-
}
390-
}
391-
392341
/// A region of source code, along with macro expansion information.
393342
#[derive(Copy, Clone)]
394343
pub struct Span {
@@ -470,15 +419,6 @@ impl Span {
470419
self.unwrap()
471420
}
472421

473-
/// The original source file into which this span points.
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)))]
478-
pub fn source_file(&self) -> SourceFile {
479-
SourceFile::_new(self.inner.source_file())
480-
}
481-
482422
/// Returns the span's byte position range in the source file.
483423
///
484424
/// This method requires the `"span-locations"` feature to be enabled.
@@ -524,6 +464,33 @@ impl Span {
524464
self.inner.end()
525465
}
526466

467+
/// The path to the source file in which this span occurs, for display
468+
/// purposes.
469+
///
470+
/// This might not correspond to a valid file system path. It might be
471+
/// remapped, or might be an artificial path such as `"<macro expansion>"`.
472+
///
473+
/// This method is semver exempt and not exposed by default.
474+
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
475+
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
476+
pub fn file(&self) -> String {
477+
self.inner.file()
478+
}
479+
480+
/// The path to the source file in which this span occurs on disk.
481+
///
482+
/// This is the actual path on disk. It is unaffected by path remapping.
483+
///
484+
/// This path should not be embedded in the output of the macro; prefer
485+
/// `file()` instead.
486+
///
487+
/// This method is semver exempt and not exposed by default.
488+
#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
489+
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
490+
pub fn local_file(&self) -> Option<PathBuf> {
491+
self.inner.local_file()
492+
}
493+
527494
/// Create a new span encompassing `self` and `other`.
528495
///
529496
/// Returns `None` if `self` and `other` are from different files.

src/wrapper.rs

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -360,45 +360,6 @@ impl Iterator for TokenTreeIter {
360360
}
361361
}
362362

363-
#[derive(Clone, PartialEq, Eq)]
364-
#[cfg(super_unstable)]
365-
pub(crate) enum SourceFile {
366-
Compiler(proc_macro::SourceFile),
367-
Fallback(fallback::SourceFile),
368-
}
369-
370-
#[cfg(super_unstable)]
371-
impl SourceFile {
372-
fn nightly(sf: proc_macro::SourceFile) -> Self {
373-
SourceFile::Compiler(sf)
374-
}
375-
376-
/// Get the path to this source file as a string.
377-
pub(crate) fn path(&self) -> PathBuf {
378-
match self {
379-
SourceFile::Compiler(a) => a.path(),
380-
SourceFile::Fallback(a) => a.path(),
381-
}
382-
}
383-
384-
pub(crate) fn is_real(&self) -> bool {
385-
match self {
386-
SourceFile::Compiler(a) => a.is_real(),
387-
SourceFile::Fallback(a) => a.is_real(),
388-
}
389-
}
390-
}
391-
392-
#[cfg(super_unstable)]
393-
impl Debug for SourceFile {
394-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
395-
match self {
396-
SourceFile::Compiler(a) => Debug::fmt(a, f),
397-
SourceFile::Fallback(a) => Debug::fmt(a, f),
398-
}
399-
}
400-
}
401-
402363
#[derive(Copy, Clone)]
403364
pub(crate) enum Span {
404365
Compiler(proc_macro::Span),
@@ -456,14 +417,6 @@ impl Span {
456417
}
457418
}
458419

459-
#[cfg(super_unstable)]
460-
pub(crate) fn source_file(&self) -> SourceFile {
461-
match self {
462-
Span::Compiler(s) => SourceFile::nightly(s.source_file()),
463-
Span::Fallback(s) => SourceFile::Fallback(s.source_file()),
464-
}
465-
}
466-
467420
#[cfg(span_locations)]
468421
pub(crate) fn byte_range(&self) -> Range<usize> {
469422
match self {
@@ -506,6 +459,22 @@ impl Span {
506459
}
507460
}
508461

462+
#[cfg(super_unstable)]
463+
pub(crate) fn file(&self) -> String {
464+
match self {
465+
Span::Compiler(s) => s.file(),
466+
Span::Fallback(s) => s.file(),
467+
}
468+
}
469+
470+
#[cfg(super_unstable)]
471+
pub(crate) fn local_file(&self) -> Option<PathBuf> {
472+
match self {
473+
Span::Compiler(s) => s.local_file(),
474+
Span::Fallback(s) => s.local_file(),
475+
}
476+
}
477+
509478
pub(crate) fn join(&self, other: Span) -> Option<Span> {
510479
let ret = match (self, other) {
511480
#[cfg(proc_macro_span)]

tests/marker.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,17 @@ assert_impl!(TokenTree is not Send or Sync);
5656

5757
#[cfg(procmacro2_semver_exempt)]
5858
mod semver_exempt {
59-
use proc_macro2::{LineColumn, SourceFile};
59+
use proc_macro2::LineColumn;
6060

6161
assert_impl!(LineColumn is Send and Sync);
62-
63-
assert_impl!(SourceFile is not Send or Sync);
6462
}
6563

6664
mod unwind_safe {
65+
#[cfg(procmacro2_semver_exempt)]
66+
use proc_macro2::LineColumn;
6767
use proc_macro2::{
6868
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
6969
};
70-
#[cfg(procmacro2_semver_exempt)]
71-
use proc_macro2::{LineColumn, SourceFile};
7270
use std::panic::{RefUnwindSafe, UnwindSafe};
7371

7472
macro_rules! assert_unwind_safe {
@@ -95,6 +93,5 @@ mod unwind_safe {
9593
#[cfg(procmacro2_semver_exempt)]
9694
assert_unwind_safe! {
9795
LineColumn
98-
SourceFile
9996
}
10097
}

tests/test.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,8 @@ fn default_span() {
549549
let end = Span::call_site().end();
550550
assert_eq!(end.line, 1);
551551
assert_eq!(end.column, 0);
552-
let source_file = Span::call_site().source_file();
553-
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
554-
assert!(!source_file.is_real());
552+
assert_eq!(Span::call_site().file(), "<unspecified>");
553+
assert!(Span::call_site().local_file().is_none());
555554
}
556555

557556
#[cfg(procmacro2_semver_exempt)]
@@ -568,11 +567,8 @@ fn span_join() {
568567
.into_iter()
569568
.collect::<Vec<_>>();
570569

571-
assert!(source1[0].span().source_file() != source2[0].span().source_file());
572-
assert_eq!(
573-
source1[0].span().source_file(),
574-
source1[1].span().source_file()
575-
);
570+
assert!(source1[0].span().file() != source2[0].span().file());
571+
assert_eq!(source1[0].span().file(), source1[1].span().file());
576572

577573
let joined1 = source1[0].span().join(source1[1].span());
578574
let joined2 = source1[0].span().join(source2[0].span());
@@ -586,10 +582,7 @@ fn span_join() {
586582
assert_eq!(end.line, 2);
587583
assert_eq!(end.column, 3);
588584

589-
assert_eq!(
590-
joined1.unwrap().source_file(),
591-
source1[0].span().source_file()
592-
);
585+
assert_eq!(joined1.unwrap().file(), source1[0].span().file());
593586
}
594587

595588
#[test]

0 commit comments

Comments
 (0)