Skip to content

Commit f162c7f

Browse files
authored
Rollup merge of rust-lang#139671 - m-ou-se:proc-macro-span, r=dtolnay
Proc macro span API redesign: Replace proc_macro::SourceFile by Span::{file, local_file} Simplification/redesign of the unstable proc macro span API, tracked in rust-lang#54725: Before: ```rust impl Span { pub fn line(&self) -> usize; pub fn column(&self) -> usize; pub fn source_file(&self) -> SourceFile; } #[derive(Clone, Debug, PartialEq, Eq)] pub struct SourceFile { .. } impl !Send for SourceFile {} impl !Sync for SourceFile {} impl SourceFile { pub fn path(&self) -> PathBuf; pub fn is_real(&self) -> bool; } ``` After: ```rust impl Span { pub fn line(&self) -> usize; pub fn column(&self) -> usize; pub fn file(&self) -> String; // Mapped file name, for display purposes. pub fn local_file(&self) -> Option<PathBuf>; // Real file name as it exists on disk. } ``` This resolves the last blocker for stabilizing these methods. (Stabilizing will be a separate PR with FCP.)
2 parents 39e05bf + 2c3a474 commit f162c7f

File tree

4 files changed

+21
-74
lines changed

4 files changed

+21
-74
lines changed

proc_macro/src/bridge/client.rs

-6
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ impl Clone for TokenStream {
111111
}
112112
}
113113

114-
impl Clone for SourceFile {
115-
fn clone(&self) -> Self {
116-
self.clone()
117-
}
118-
}
119-
120114
impl Span {
121115
pub(crate) fn def_site() -> Span {
122116
Bridge::with(|bridge| bridge.globals.def_site)

proc_macro/src/bridge/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,17 @@ macro_rules! with_api {
8181
$self: $S::TokenStream
8282
) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>;
8383
},
84-
SourceFile {
85-
fn drop($self: $S::SourceFile);
86-
fn clone($self: &$S::SourceFile) -> $S::SourceFile;
87-
fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool;
88-
fn path($self: &$S::SourceFile) -> String;
89-
fn is_real($self: &$S::SourceFile) -> bool;
90-
},
9184
Span {
9285
fn debug($self: $S::Span) -> String;
93-
fn source_file($self: $S::Span) -> $S::SourceFile;
9486
fn parent($self: $S::Span) -> Option<$S::Span>;
9587
fn source($self: $S::Span) -> $S::Span;
9688
fn byte_range($self: $S::Span) -> Range<usize>;
9789
fn start($self: $S::Span) -> $S::Span;
9890
fn end($self: $S::Span) -> $S::Span;
9991
fn line($self: $S::Span) -> usize;
10092
fn column($self: $S::Span) -> usize;
93+
fn file($self: $S::Span) -> String;
94+
fn local_file($self: $S::Span) -> Option<String>;
10195
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
10296
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
10397
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
@@ -120,7 +114,6 @@ macro_rules! with_api_handle_types {
120114
'owned:
121115
FreeFunctions,
122116
TokenStream,
123-
SourceFile,
124117

125118
'interned:
126119
Span,

proc_macro/src/bridge/server.rs

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ with_api_handle_types!(define_server_handles);
8282
pub trait Types {
8383
type FreeFunctions: 'static;
8484
type TokenStream: 'static + Clone;
85-
type SourceFile: 'static + Clone;
8685
type Span: 'static + Copy + Eq + Hash;
8786
type Symbol: 'static;
8887
}

proc_macro/src/lib.rs

+19-58
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,6 @@ impl Span {
491491
Span(bridge::client::Span::mixed_site())
492492
}
493493

494-
/// The original source file into which this span points.
495-
#[unstable(feature = "proc_macro_span", issue = "54725")]
496-
pub fn source_file(&self) -> SourceFile {
497-
SourceFile(self.0.source_file())
498-
}
499-
500494
/// The `Span` for the tokens in the previous macro expansion from which
501495
/// `self` was generated from, if any.
502496
#[unstable(feature = "proc_macro_span", issue = "54725")]
@@ -546,6 +540,25 @@ impl Span {
546540
self.0.column()
547541
}
548542

543+
/// The path to the source file in which this span occurs, for display purposes.
544+
///
545+
/// This might not correspond to a valid file system path.
546+
/// It might be remapped, or might be an artificial path such as `"<macro expansion>"`.
547+
#[unstable(feature = "proc_macro_span", issue = "54725")]
548+
pub fn file(&self) -> String {
549+
self.0.file()
550+
}
551+
552+
/// The path to the source file in which this span occurs on disk.
553+
///
554+
/// This is the actual path on disk. It is unaffected by path remapping.
555+
///
556+
/// This path should not be embedded in the output of the macro; prefer `file()` instead.
557+
#[unstable(feature = "proc_macro_span", issue = "54725")]
558+
pub fn local_file(&self) -> Option<PathBuf> {
559+
self.0.local_file().map(|s| PathBuf::from(s))
560+
}
561+
549562
/// Creates a new span encompassing `self` and `other`.
550563
///
551564
/// Returns `None` if `self` and `other` are from different files.
@@ -614,58 +627,6 @@ impl fmt::Debug for Span {
614627
}
615628
}
616629

617-
/// The source file of a given `Span`.
618-
#[unstable(feature = "proc_macro_span", issue = "54725")]
619-
#[derive(Clone)]
620-
pub struct SourceFile(bridge::client::SourceFile);
621-
622-
impl SourceFile {
623-
/// Gets the path to this source file.
624-
///
625-
/// ### Note
626-
/// If the code span associated with this `SourceFile` was generated by an external macro, this
627-
/// macro, this might not be an actual path on the filesystem. Use [`is_real`] to check.
628-
///
629-
/// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on
630-
/// the command line, the path as given might not actually be valid.
631-
///
632-
/// [`is_real`]: Self::is_real
633-
#[unstable(feature = "proc_macro_span", issue = "54725")]
634-
pub fn path(&self) -> PathBuf {
635-
PathBuf::from(self.0.path())
636-
}
637-
638-
/// Returns `true` if this source file is a real source file, and not generated by an external
639-
/// macro's expansion.
640-
#[unstable(feature = "proc_macro_span", issue = "54725")]
641-
pub fn is_real(&self) -> bool {
642-
// This is a hack until intercrate spans are implemented and we can have real source files
643-
// for spans generated in external macros.
644-
// https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368
645-
self.0.is_real()
646-
}
647-
}
648-
649-
#[unstable(feature = "proc_macro_span", issue = "54725")]
650-
impl fmt::Debug for SourceFile {
651-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
652-
f.debug_struct("SourceFile")
653-
.field("path", &self.path())
654-
.field("is_real", &self.is_real())
655-
.finish()
656-
}
657-
}
658-
659-
#[unstable(feature = "proc_macro_span", issue = "54725")]
660-
impl PartialEq for SourceFile {
661-
fn eq(&self, other: &Self) -> bool {
662-
self.0.eq(&other.0)
663-
}
664-
}
665-
666-
#[unstable(feature = "proc_macro_span", issue = "54725")]
667-
impl Eq for SourceFile {}
668-
669630
/// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`).
670631
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
671632
#[derive(Clone)]

0 commit comments

Comments
 (0)