Skip to content

Commit 213399d

Browse files
authored
Unrolled build for rust-lang#139671
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 58c2dd9 + 1f3199c commit 213399d

File tree

13 files changed

+76
-165
lines changed

13 files changed

+76
-165
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+24-27
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;
@@ -458,7 +457,6 @@ impl<'a, 'b> Rustc<'a, 'b> {
458457
impl server::Types for Rustc<'_, '_> {
459458
type FreeFunctions = FreeFunctions;
460459
type TokenStream = TokenStream;
461-
type SourceFile = Arc<SourceFile>;
462460
type Span = Span;
463461
type Symbol = Symbol;
464462
}
@@ -664,28 +662,6 @@ impl server::TokenStream for Rustc<'_, '_> {
664662
}
665663
}
666664

667-
impl server::SourceFile for Rustc<'_, '_> {
668-
fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool {
669-
Arc::ptr_eq(file1, file2)
670-
}
671-
672-
fn path(&mut self, file: &Self::SourceFile) -> String {
673-
match &file.name {
674-
FileName::Real(name) => name
675-
.local_path()
676-
.expect("attempting to get a file path in an imported file in `proc_macro::SourceFile::path`")
677-
.to_str()
678-
.expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
679-
.to_string(),
680-
_ => file.name.prefer_local().to_string(),
681-
}
682-
}
683-
684-
fn is_real(&mut self, file: &Self::SourceFile) -> bool {
685-
file.is_real_file()
686-
}
687-
}
688-
689665
impl server::Span for Rustc<'_, '_> {
690666
fn debug(&mut self, span: Self::Span) -> String {
691667
if self.ecx.ecfg.span_debug {
@@ -695,8 +671,29 @@ impl server::Span for Rustc<'_, '_> {
695671
}
696672
}
697673

698-
fn source_file(&mut self, span: Self::Span) -> Self::SourceFile {
699-
self.psess().source_map().lookup_char_pos(span.lo()).file
674+
fn file(&mut self, span: Self::Span) -> String {
675+
self.psess()
676+
.source_map()
677+
.lookup_char_pos(span.lo())
678+
.file
679+
.name
680+
.prefer_remapped_unconditionaly()
681+
.to_string()
682+
}
683+
684+
fn local_file(&mut self, span: Self::Span) -> Option<String> {
685+
self.psess()
686+
.source_map()
687+
.lookup_char_pos(span.lo())
688+
.file
689+
.name
690+
.clone()
691+
.into_local_path()
692+
.map(|p| {
693+
p.to_str()
694+
.expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
695+
.to_string()
696+
})
700697
}
701698

702699
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-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,

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-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)]

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

+8-21
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111

1212
use intern::Symbol;
1313
use proc_macro::bridge::{self, server};
14-
use span::{FileId, Span, FIXUP_ERASED_FILE_AST_ID_MARKER};
14+
use span::{Span, FIXUP_ERASED_FILE_AST_ID_MARKER};
1515
use tt::{TextRange, TextSize};
1616

1717
use crate::server_impl::{literal_kind_to_internal, token_stream::TokenStreamBuilder, TopSubtree};
@@ -27,10 +27,6 @@ mod tt {
2727

2828
type TokenStream = crate::server_impl::TokenStream<Span>;
2929

30-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
31-
pub struct SourceFile {
32-
file_id: FileId,
33-
}
3430
pub struct FreeFunctions;
3531

3632
pub struct RaSpanServer {
@@ -46,7 +42,6 @@ pub struct RaSpanServer {
4642
impl server::Types for RaSpanServer {
4743
type FreeFunctions = FreeFunctions;
4844
type TokenStream = TokenStream;
49-
type SourceFile = SourceFile;
5045
type Span = Span;
5146
type Symbol = Symbol;
5247
}
@@ -245,25 +240,17 @@ impl server::TokenStream for RaSpanServer {
245240
}
246241
}
247242

248-
impl server::SourceFile for RaSpanServer {
249-
fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool {
250-
file1 == file2
251-
}
252-
fn path(&mut self, _file: &Self::SourceFile) -> String {
253-
// FIXME
254-
String::new()
255-
}
256-
fn is_real(&mut self, _file: &Self::SourceFile) -> bool {
257-
true
258-
}
259-
}
260-
261243
impl server::Span for RaSpanServer {
262244
fn debug(&mut self, span: Self::Span) -> String {
263245
format!("{:?}", span)
264246
}
265-
fn source_file(&mut self, span: Self::Span) -> Self::SourceFile {
266-
SourceFile { file_id: span.anchor.file_id.file_id() }
247+
fn file(&mut self, _: Self::Span) -> String {
248+
// FIXME
249+
String::new()
250+
}
251+
fn local_file(&mut self, _: Self::Span) -> Option<String> {
252+
// FIXME
253+
None
267254
}
268255
fn save_span(&mut self, _span: Self::Span) -> usize {
269256
// FIXME, quote is incompatible with third-party tools

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ type Literal = tt::Literal;
2424
type Span = tt::TokenId;
2525
type TokenStream = crate::server_impl::TokenStream<Span>;
2626

27-
#[derive(Clone)]
28-
pub struct SourceFile;
2927
pub struct FreeFunctions;
3028

3129
pub struct TokenIdServer {
@@ -37,7 +35,6 @@ pub struct TokenIdServer {
3735
impl server::Types for TokenIdServer {
3836
type FreeFunctions = FreeFunctions;
3937
type TokenStream = TokenStream;
40-
type SourceFile = SourceFile;
4138
type Span = Span;
4239
type Symbol = Symbol;
4340
}
@@ -223,24 +220,15 @@ impl server::TokenStream for TokenIdServer {
223220
}
224221
}
225222

226-
impl server::SourceFile for TokenIdServer {
227-
fn eq(&mut self, _file1: &Self::SourceFile, _file2: &Self::SourceFile) -> bool {
228-
true
229-
}
230-
fn path(&mut self, _file: &Self::SourceFile) -> String {
231-
String::new()
232-
}
233-
fn is_real(&mut self, _file: &Self::SourceFile) -> bool {
234-
true
235-
}
236-
}
237-
238223
impl server::Span for TokenIdServer {
239224
fn debug(&mut self, span: Self::Span) -> String {
240225
format!("{:?}", span.0)
241226
}
242-
fn source_file(&mut self, _span: Self::Span) -> Self::SourceFile {
243-
SourceFile {}
227+
fn file(&mut self, _span: Self::Span) -> String {
228+
String::new()
229+
}
230+
fn local_file(&mut self, _span: Self::Span) -> Option<String> {
231+
None
244232
}
245233
fn save_span(&mut self, _span: Self::Span) -> usize {
246234
0

src/tools/rust-analyzer/crates/proc-macro-srv/src/tests/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ fn test_fn_like_macro_clone_raw_ident() {
9797
}
9898

9999
#[test]
100+
#[cfg(not(bootstrap))]
100101
fn test_fn_like_fn_like_span_join() {
101102
assert_expand(
102103
"fn_like_span_join",
@@ -111,6 +112,7 @@ fn test_fn_like_fn_like_span_join() {
111112
}
112113

113114
#[test]
115+
#[cfg(not(bootstrap))]
114116
fn test_fn_like_fn_like_span_ops() {
115117
assert_expand(
116118
"fn_like_span_ops",

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()

0 commit comments

Comments
 (0)