Skip to content

Commit 3962069

Browse files
committed
Replace proc_macro::SourceFile by Span::{file, local_file}.
1 parent 6788ce7 commit 3962069

File tree

10 files changed

+61
-93
lines changed

10 files changed

+61
-93
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::ops::{Bound, Range};
2-
use std::sync::Arc;
32

43
use ast::token::IdentIsRaw;
54
use pm::bridge::{
@@ -18,7 +17,7 @@ use rustc_parse::parser::Parser;
1817
use rustc_parse::{exp, new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal};
1918
use rustc_session::parse::ParseSess;
2019
use rustc_span::def_id::CrateNum;
21-
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span, Symbol, sym};
20+
use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym};
2221
use smallvec::{SmallVec, smallvec};
2322

2423
use crate::base::ExtCtxt;
@@ -467,7 +466,6 @@ impl<'a, 'b> Rustc<'a, 'b> {
467466
impl server::Types for Rustc<'_, '_> {
468467
type FreeFunctions = FreeFunctions;
469468
type TokenStream = TokenStream;
470-
type SourceFile = Arc<SourceFile>;
471469
type Span = Span;
472470
type Symbol = Symbol;
473471
}
@@ -673,24 +671,6 @@ impl server::TokenStream for Rustc<'_, '_> {
673671
}
674672
}
675673

676-
impl server::SourceFile for Rustc<'_, '_> {
677-
fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool {
678-
Arc::ptr_eq(file1, file2)
679-
}
680-
681-
fn path(&mut self, file: &Self::SourceFile) -> String {
682-
match &file.name {
683-
FileName::Real(name) => name
684-
.local_path()
685-
.expect("attempting to get a file path in an imported file in `proc_macro::SourceFile::path`")
686-
.to_str()
687-
.expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
688-
.to_string(),
689-
_ => file.name.prefer_local().to_string(),
690-
}
691-
}
692-
}
693-
694674
impl server::Span for Rustc<'_, '_> {
695675
fn debug(&mut self, span: Self::Span) -> String {
696676
if self.ecx.ecfg.span_debug {
@@ -700,8 +680,29 @@ impl server::Span for Rustc<'_, '_> {
700680
}
701681
}
702682

703-
fn source_file(&mut self, span: Self::Span) -> Self::SourceFile {
704-
self.psess().source_map().lookup_char_pos(span.lo()).file
683+
fn file(&mut self, span: Self::Span) -> String {
684+
self.psess()
685+
.source_map()
686+
.lookup_char_pos(span.lo())
687+
.file
688+
.name
689+
.prefer_remapped_unconditionaly()
690+
.to_string()
691+
}
692+
693+
fn local_file(&mut self, span: Self::Span) -> Option<String> {
694+
self.psess()
695+
.source_map()
696+
.lookup_char_pos(span.lo())
697+
.file
698+
.name
699+
.clone()
700+
.into_local_path()
701+
.map(|p| {
702+
p.to_str()
703+
.expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
704+
.to_string()
705+
})
705706
}
706707

707708
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {

compiler/rustc_fluent_macro/src/fluent.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ fn invocation_relative_path_to_absolute(span: Span, path: &str) -> PathBuf {
2525
path.to_path_buf()
2626
} else {
2727
// `/a/b/c/foo/bar.rs` contains the current macro invocation
28+
#[cfg(bootstrap)]
2829
let mut source_file_path = span.source_file().path();
30+
#[cfg(not(bootstrap))]
31+
let mut source_file_path = span.local_file().unwrap();
2932
// `/a/b/c/foo/`
3033
source_file_path.pop();
3134
// `/a/b/c/foo/../locales/en-US/example.ftl`

library/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)

library/proc_macro/src/bridge/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -81,22 +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-
},
9084
Span {
9185
fn debug($self: $S::Span) -> String;
92-
fn source_file($self: $S::Span) -> $S::SourceFile;
9386
fn parent($self: $S::Span) -> Option<$S::Span>;
9487
fn source($self: $S::Span) -> $S::Span;
9588
fn byte_range($self: $S::Span) -> Range<usize>;
9689
fn start($self: $S::Span) -> $S::Span;
9790
fn end($self: $S::Span) -> $S::Span;
9891
fn line($self: $S::Span) -> usize;
9992
fn column($self: $S::Span) -> usize;
93+
fn file($self: $S::Span) -> String;
94+
fn local_file($self: $S::Span) -> Option<String>;
10095
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
10196
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
10297
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
@@ -119,7 +114,6 @@ macro_rules! with_api_handle_types {
119114
'owned:
120115
FreeFunctions,
121116
TokenStream,
122-
SourceFile,
123117

124118
'interned:
125119
Span,

library/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
}

library/proc_macro/src/lib.rs

+19-41
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,41 +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-
///
627-
/// If `--remap-path-prefix` was passed on
628-
/// the command line, the path as given might not actually be valid.
629-
#[unstable(feature = "proc_macro_span", issue = "54725")]
630-
pub fn path(&self) -> PathBuf {
631-
PathBuf::from(self.0.path())
632-
}
633-
}
634-
635-
#[unstable(feature = "proc_macro_span", issue = "54725")]
636-
impl fmt::Debug for SourceFile {
637-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
638-
f.debug_struct("SourceFile").field("path", &self.path()).finish()
639-
}
640-
}
641-
642-
#[unstable(feature = "proc_macro_span", issue = "54725")]
643-
impl PartialEq for SourceFile {
644-
fn eq(&self, other: &Self) -> bool {
645-
self.0.eq(&other.0)
646-
}
647-
}
648-
649-
#[unstable(feature = "proc_macro_span", issue = "54725")]
650-
impl Eq for SourceFile {}
651-
652630
/// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`).
653631
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
654632
#[derive(Clone)]

tests/ui/proc-macro/auxiliary/expand-expr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
extern crate proc_macro;
55

6-
use proc_macro::*;
76
use std::str::FromStr;
87

8+
use proc_macro::*;
9+
910
// Flatten the TokenStream, removing any toplevel `Delimiter::None`s for
1011
// comparison.
1112
fn flatten(ts: TokenStream) -> Vec<TokenTree> {
@@ -136,9 +137,8 @@ pub fn check_expand_expr_file(ts: TokenStream) -> TokenStream {
136137
.to_string();
137138
assert_eq!(input_t, parse_t);
138139

139-
// Check that the literal matches `Span::call_site().source_file().path()`
140-
let expect_t =
141-
Literal::string(&Span::call_site().source_file().path().to_string_lossy()).to_string();
140+
// Check that the literal matches `Span::call_site().file()`
141+
let expect_t = Literal::string(&Span::call_site().file()).to_string();
142142
assert_eq!(input_t, expect_t);
143143

144144
TokenStream::new()

tests/ui/proc-macro/auxiliary/macro-only-syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn check_useful_span(token: TokenTree, expected_filename: &str) {
7979
let span = token.span();
8080
assert!(span.column() < span.end().column());
8181

82-
let source_path = span.source_file().path();
82+
let source_path = span.local_file().unwrap();
8383
let filename = source_path.components().last().unwrap();
8484
assert_eq!(filename, Component::Normal(expected_filename.as_ref()));
8585
}

tests/ui/proc-macro/auxiliary/span-api-tests.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ pub fn reemit(input: TokenStream) -> TokenStream {
1111
}
1212

1313
#[proc_macro]
14-
pub fn assert_source_file(input: TokenStream) -> TokenStream {
14+
pub fn assert_local_file(input: TokenStream) -> TokenStream {
1515
for tk in input {
16-
let source_file = tk.span().source_file();
17-
assert!(!source_file.as_os_str().is_empty(), "No source file for span: {:?}", tk.span());
16+
assert!(tk.span().local_file().is_some(), "No local file for span: {:?}", tk.span());
1817
}
1918

2019
"".parse().unwrap()

tests/ui/proc-macro/span-api-tests.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ extern crate span_test_macros;
88

99
extern crate span_api_tests;
1010

11-
use span_api_tests::{reemit, assert_source_file, macro_stringify};
11+
use span_api_tests::{reemit, assert_local_file, macro_stringify};
1212

1313
macro_rules! say_hello {
1414
($macname:ident) => ( $macname! { "Hello, world!" })
1515
}
1616

17-
assert_source_file! { "Hello, world!" }
17+
assert_local_file! { "Hello, world!" }
1818

19-
say_hello! { assert_source_file }
19+
say_hello! { assert_local_file }
2020

2121
reemit_legacy! {
22-
assert_source_file! { "Hello, world!" }
22+
assert_local_file! { "Hello, world!" }
2323
}
2424

25-
say_hello_extern! { assert_source_file }
25+
say_hello_extern! { assert_local_file }
2626

2727
reemit! {
28-
assert_source_file! { "Hello, world!" }
28+
assert_local_file! { "Hello, world!" }
2929
}
3030

3131
fn main() {

0 commit comments

Comments
 (0)