From 7bc363c50931c5d3e6de1f785b9b6bc0b53c8866 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 15 Apr 2025 19:27:34 -0700 Subject: [PATCH] Update semver-exempt API to nightly-2025-04-16 --- src/fallback.rs | 66 ++++++++++++------------------------- src/lib.rs | 87 +++++++++++++++---------------------------------- src/wrapper.rs | 63 +++++++++-------------------------- tests/marker.rs | 9 ++--- tests/test.rs | 17 +++------- 5 files changed, 71 insertions(+), 171 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index 1acdf06..fbce9c4 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -311,34 +311,6 @@ impl IntoIterator for TokenStream { } } -#[cfg(procmacro2_semver_exempt)] -#[derive(Clone, PartialEq, Eq)] -pub(crate) struct SourceFile { - path: PathBuf, -} - -#[cfg(procmacro2_semver_exempt)] -impl SourceFile { - /// Get the path to this source file as a string. - pub(crate) fn path(&self) -> PathBuf { - self.path.clone() - } - - pub(crate) fn is_real(&self) -> bool { - false - } -} - -#[cfg(procmacro2_semver_exempt)] -impl Debug for SourceFile { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("SourceFile") - .field("path", &self.path()) - .field("is_real", &self.is_real()) - .finish() - } -} - #[cfg(all(span_locations, not(fuzzing)))] thread_local! { static SOURCE_MAP: RefCell = RefCell::new(SourceMap { @@ -484,14 +456,14 @@ impl SourceMap { } #[cfg(procmacro2_semver_exempt)] - fn filepath(&self, span: Span) -> PathBuf { + fn filepath(&self, span: Span) -> String { for (i, file) in self.files.iter().enumerate() { if file.span_within(span) { - return PathBuf::from(if i == 0 { + return if i == 0 { "".to_owned() } else { format!("", i) - }); + }; } } unreachable!("Invalid span with no related FileInfo!"); @@ -555,21 +527,6 @@ impl Span { other } - #[cfg(procmacro2_semver_exempt)] - pub(crate) fn source_file(&self) -> SourceFile { - #[cfg(fuzzing)] - return SourceFile { - path: PathBuf::from(""), - }; - - #[cfg(not(fuzzing))] - SOURCE_MAP.with(|sm| { - let sm = sm.borrow(); - let path = sm.filepath(*self); - SourceFile { path } - }) - } - #[cfg(span_locations)] pub(crate) fn byte_range(&self) -> Range { #[cfg(fuzzing)] @@ -611,6 +568,23 @@ impl Span { }) } + #[cfg(procmacro2_semver_exempt)] + pub(crate) fn file(&self) -> String { + #[cfg(fuzzing)] + return "".to_owned(); + + #[cfg(not(fuzzing))] + SOURCE_MAP.with(|sm| { + let sm = sm.borrow(); + sm.filepath(*self) + }) + } + + #[cfg(procmacro2_semver_exempt)] + pub(crate) fn local_file(&self) -> Option { + None + } + #[cfg(not(span_locations))] pub(crate) fn join(&self, _other: Span) -> Option { Some(Span {}) diff --git a/src/lib.rs b/src/lib.rs index 2ed9b72..b02bec9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -338,57 +338,6 @@ impl Display for LexError { impl Error for LexError {} -/// The source file of a given `Span`. -/// -/// This type is semver exempt and not exposed by default. -#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))] -#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] -#[derive(Clone, PartialEq, Eq)] -pub struct SourceFile { - inner: imp::SourceFile, - _marker: ProcMacroAutoTraits, -} - -#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))] -impl SourceFile { - fn _new(inner: imp::SourceFile) -> Self { - SourceFile { - inner, - _marker: MARKER, - } - } - - /// Get the path to this source file. - /// - /// ### Note - /// - /// If the code span associated with this `SourceFile` was generated by an - /// external macro, this may not be an actual path on the filesystem. Use - /// [`is_real`] to check. - /// - /// Also note that even if `is_real` returns `true`, if - /// `--remap-path-prefix` was passed on the command line, the path as given - /// may not actually be valid. - /// - /// [`is_real`]: #method.is_real - pub fn path(&self) -> PathBuf { - self.inner.path() - } - - /// Returns `true` if this source file is a real source file, and not - /// generated by an external macro's expansion. - pub fn is_real(&self) -> bool { - self.inner.is_real() - } -} - -#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))] -impl Debug for SourceFile { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - Debug::fmt(&self.inner, f) - } -} - /// A region of source code, along with macro expansion information. #[derive(Copy, Clone)] pub struct Span { @@ -470,15 +419,6 @@ impl Span { self.unwrap() } - /// The original source file into which this span points. - /// - /// This method is semver exempt and not exposed by default. - #[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))] - #[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] - pub fn source_file(&self) -> SourceFile { - SourceFile::_new(self.inner.source_file()) - } - /// Returns the span's byte position range in the source file. /// /// This method requires the `"span-locations"` feature to be enabled. @@ -524,6 +464,33 @@ impl Span { self.inner.end() } + /// The path to the source file in which this span occurs, for display + /// purposes. + /// + /// This might not correspond to a valid file system path. It might be + /// remapped, or might be an artificial path such as `""`. + /// + /// This method is semver exempt and not exposed by default. + #[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))] + #[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] + pub fn file(&self) -> String { + self.inner.file() + } + + /// The path to the source file in which this span occurs on disk. + /// + /// This is the actual path on disk. It is unaffected by path remapping. + /// + /// This path should not be embedded in the output of the macro; prefer + /// `file()` instead. + /// + /// This method is semver exempt and not exposed by default. + #[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))] + #[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] + pub fn local_file(&self) -> Option { + self.inner.local_file() + } + /// Create a new span encompassing `self` and `other`. /// /// Returns `None` if `self` and `other` are from different files. diff --git a/src/wrapper.rs b/src/wrapper.rs index 17d7b41..ee31fa6 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -360,45 +360,6 @@ impl Iterator for TokenTreeIter { } } -#[derive(Clone, PartialEq, Eq)] -#[cfg(super_unstable)] -pub(crate) enum SourceFile { - Compiler(proc_macro::SourceFile), - Fallback(fallback::SourceFile), -} - -#[cfg(super_unstable)] -impl SourceFile { - fn nightly(sf: proc_macro::SourceFile) -> Self { - SourceFile::Compiler(sf) - } - - /// Get the path to this source file as a string. - pub(crate) fn path(&self) -> PathBuf { - match self { - SourceFile::Compiler(a) => a.path(), - SourceFile::Fallback(a) => a.path(), - } - } - - pub(crate) fn is_real(&self) -> bool { - match self { - SourceFile::Compiler(a) => a.is_real(), - SourceFile::Fallback(a) => a.is_real(), - } - } -} - -#[cfg(super_unstable)] -impl Debug for SourceFile { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - SourceFile::Compiler(a) => Debug::fmt(a, f), - SourceFile::Fallback(a) => Debug::fmt(a, f), - } - } -} - #[derive(Copy, Clone)] pub(crate) enum Span { Compiler(proc_macro::Span), @@ -456,14 +417,6 @@ impl Span { } } - #[cfg(super_unstable)] - pub(crate) fn source_file(&self) -> SourceFile { - match self { - Span::Compiler(s) => SourceFile::nightly(s.source_file()), - Span::Fallback(s) => SourceFile::Fallback(s.source_file()), - } - } - #[cfg(span_locations)] pub(crate) fn byte_range(&self) -> Range { match self { @@ -506,6 +459,22 @@ impl Span { } } + #[cfg(super_unstable)] + pub(crate) fn file(&self) -> String { + match self { + Span::Compiler(s) => s.file(), + Span::Fallback(s) => s.file(), + } + } + + #[cfg(super_unstable)] + pub(crate) fn local_file(&self) -> Option { + match self { + Span::Compiler(s) => s.local_file(), + Span::Fallback(s) => s.local_file(), + } + } + pub(crate) fn join(&self, other: Span) -> Option { let ret = match (self, other) { #[cfg(proc_macro_span)] diff --git a/tests/marker.rs b/tests/marker.rs index 99f64c0..af8932a 100644 --- a/tests/marker.rs +++ b/tests/marker.rs @@ -56,19 +56,17 @@ assert_impl!(TokenTree is not Send or Sync); #[cfg(procmacro2_semver_exempt)] mod semver_exempt { - use proc_macro2::{LineColumn, SourceFile}; + use proc_macro2::LineColumn; assert_impl!(LineColumn is Send and Sync); - - assert_impl!(SourceFile is not Send or Sync); } mod unwind_safe { + #[cfg(procmacro2_semver_exempt)] + use proc_macro2::LineColumn; use proc_macro2::{ Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree, }; - #[cfg(procmacro2_semver_exempt)] - use proc_macro2::{LineColumn, SourceFile}; use std::panic::{RefUnwindSafe, UnwindSafe}; macro_rules! assert_unwind_safe { @@ -95,6 +93,5 @@ mod unwind_safe { #[cfg(procmacro2_semver_exempt)] assert_unwind_safe! { LineColumn - SourceFile } } diff --git a/tests/test.rs b/tests/test.rs index 0d7c88d..aa7397b 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -549,9 +549,8 @@ fn default_span() { let end = Span::call_site().end(); assert_eq!(end.line, 1); assert_eq!(end.column, 0); - let source_file = Span::call_site().source_file(); - assert_eq!(source_file.path().to_string_lossy(), ""); - assert!(!source_file.is_real()); + assert_eq!(Span::call_site().file(), ""); + assert!(Span::call_site().local_file().is_none()); } #[cfg(procmacro2_semver_exempt)] @@ -568,11 +567,8 @@ fn span_join() { .into_iter() .collect::>(); - assert!(source1[0].span().source_file() != source2[0].span().source_file()); - assert_eq!( - source1[0].span().source_file(), - source1[1].span().source_file() - ); + assert!(source1[0].span().file() != source2[0].span().file()); + assert_eq!(source1[0].span().file(), source1[1].span().file()); let joined1 = source1[0].span().join(source1[1].span()); let joined2 = source1[0].span().join(source2[0].span()); @@ -586,10 +582,7 @@ fn span_join() { assert_eq!(end.line, 2); assert_eq!(end.column, 3); - assert_eq!( - joined1.unwrap().source_file(), - source1[0].span().source_file() - ); + assert_eq!(joined1.unwrap().file(), source1[0].span().file()); } #[test]