Skip to content

Commit e107345

Browse files
authored
Rollup merge of #151505 - bjorn3:proc_macro_refactors, r=petrochenkov,Kobzol
Various refactors to the proc_macro bridge This reduces the amount of types, traits and other abstractions that are involved with the bridge, which should make it easier to understand and modify. This should also help a bit with getting rid of the type marking hack, which is complicating the code a fair bit. Fixes: rust-lang/rust#139810
2 parents ecb3a42 + 643303f commit e107345

7 files changed

Lines changed: 131 additions & 135 deletions

File tree

crates/proc-macro-srv/src/bridge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `proc_macro::bridge` newtypes.
22
3-
use proc_macro::bridge as pm_bridge;
3+
use rustc_proc_macro::bridge as pm_bridge;
44

55
pub use pm_bridge::{DelimSpan, Diagnostic, ExpnGlobals, LitKind};
66

crates/proc-macro-srv/src/dylib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod proc_macros;
44
mod version;
55

6-
use proc_macro::bridge;
6+
use rustc_proc_macro::bridge;
77
use std::{fmt, fs, io, time::SystemTime};
88
use temp_dir::TempDir;
99

crates/proc-macro-srv/src/dylib/proc_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Proc macro ABI
22
use crate::{ProcMacroClientHandle, ProcMacroKind, ProcMacroSrvSpan, token_stream::TokenStream};
3-
use proc_macro::bridge;
3+
use rustc_proc_macro::bridge;
44

55
#[repr(transparent)]
66
pub(crate) struct ProcMacros([bridge::client::ProcMacro]);

crates/proc-macro-srv/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
)]
2323
#![deny(deprecated_safe, clippy::undocumented_unsafe_blocks)]
2424

25-
extern crate proc_macro;
2625
#[cfg(feature = "in-rust-tree")]
2726
extern crate rustc_driver as _;
27+
extern crate rustc_proc_macro;
2828

2929
#[cfg(not(feature = "in-rust-tree"))]
3030
extern crate ra_ap_rustc_lexer as rustc_lexer;
@@ -52,7 +52,7 @@ use temp_dir::TempDir;
5252

5353
pub use crate::server_impl::token_id::SpanId;
5454

55-
pub use proc_macro::Delimiter;
55+
pub use rustc_proc_macro::Delimiter;
5656
pub use span;
5757

5858
pub use crate::bridge::*;
@@ -181,7 +181,9 @@ impl ProcMacroSrv<'_> {
181181
}
182182

183183
pub trait ProcMacroSrvSpan: Copy + Send + Sync {
184-
type Server<'a>: proc_macro::bridge::server::Server<TokenStream = crate::token_stream::TokenStream<Self>>;
184+
type Server<'a>: rustc_proc_macro::bridge::server::Server<
185+
TokenStream = crate::token_stream::TokenStream<Self>,
186+
>;
185187
fn make_server<'a>(
186188
call_site: Self,
187189
def_site: Self,

crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
};
1111

1212
use intern::Symbol;
13-
use proc_macro::bridge::server;
13+
use rustc_proc_macro::bridge::server;
1414
use span::{FIXUP_ERASED_FILE_AST_ID_MARKER, Span, TextRange, TextSize};
1515

1616
use crate::{
@@ -19,8 +19,6 @@ use crate::{
1919
server_impl::literal_from_str,
2020
};
2121

22-
pub struct FreeFunctions;
23-
2422
pub struct RaSpanServer<'a> {
2523
// FIXME: Report this back to the caller to track as dependencies
2624
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
@@ -33,13 +31,28 @@ pub struct RaSpanServer<'a> {
3331
}
3432

3533
impl server::Types for RaSpanServer<'_> {
36-
type FreeFunctions = FreeFunctions;
3734
type TokenStream = crate::token_stream::TokenStream<Span>;
3835
type Span = Span;
3936
type Symbol = Symbol;
4037
}
4138

42-
impl server::FreeFunctions for RaSpanServer<'_> {
39+
impl server::Server for RaSpanServer<'_> {
40+
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
41+
ExpnGlobals {
42+
def_site: self.def_site,
43+
call_site: self.call_site,
44+
mixed_site: self.mixed_site,
45+
}
46+
}
47+
48+
fn intern_symbol(ident: &str) -> Self::Symbol {
49+
Symbol::intern(ident)
50+
}
51+
52+
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
53+
f(symbol.as_str())
54+
}
55+
4356
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
4457
None
4558
}
@@ -58,13 +71,19 @@ impl server::FreeFunctions for RaSpanServer<'_> {
5871
fn emit_diagnostic(&mut self, _: Diagnostic<Self::Span>) {
5972
// FIXME handle diagnostic
6073
}
61-
}
6274

63-
impl server::TokenStream for RaSpanServer<'_> {
64-
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
75+
fn ts_drop(&mut self, stream: Self::TokenStream) {
76+
drop(stream);
77+
}
78+
79+
fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
80+
stream.clone()
81+
}
82+
83+
fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
6584
stream.is_empty()
6685
}
67-
fn from_str(&mut self, src: &str) -> Self::TokenStream {
86+
fn ts_from_str(&mut self, src: &str) -> Self::TokenStream {
6887
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
6988
Self::TokenStream::from_str(
7089
&format!("compile_error!(\"failed to parse str to token stream: {e}\")"),
@@ -73,15 +92,15 @@ impl server::TokenStream for RaSpanServer<'_> {
7392
.unwrap()
7493
})
7594
}
76-
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
95+
fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String {
7796
stream.to_string()
7897
}
7998

80-
fn from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
99+
fn ts_from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
81100
Self::TokenStream::new(vec![tree])
82101
}
83102

84-
fn expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
103+
fn ts_expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
85104
// FIXME: requires db, more importantly this requires name resolution so we would need to
86105
// eagerly expand this proc-macro, but we can't know that this proc-macro is eager until we
87106
// expand it ...
@@ -90,7 +109,7 @@ impl server::TokenStream for RaSpanServer<'_> {
90109
Ok(self_.clone())
91110
}
92111

93-
fn concat_trees(
112+
fn ts_concat_trees(
94113
&mut self,
95114
base: Option<Self::TokenStream>,
96115
trees: Vec<TokenTree<Self::Span>>,
@@ -106,7 +125,7 @@ impl server::TokenStream for RaSpanServer<'_> {
106125
}
107126
}
108127

109-
fn concat_streams(
128+
fn ts_concat_streams(
110129
&mut self,
111130
base: Option<Self::TokenStream>,
112131
streams: Vec<Self::TokenStream>,
@@ -118,28 +137,26 @@ impl server::TokenStream for RaSpanServer<'_> {
118137
stream
119138
}
120139

121-
fn into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
140+
fn ts_into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
122141
(*stream.0).clone()
123142
}
124-
}
125143

126-
impl server::Span for RaSpanServer<'_> {
127-
fn debug(&mut self, span: Self::Span) -> String {
144+
fn span_debug(&mut self, span: Self::Span) -> String {
128145
format!("{:?}", span)
129146
}
130-
fn file(&mut self, span: Self::Span) -> String {
147+
fn span_file(&mut self, span: Self::Span) -> String {
131148
self.callback.as_mut().map(|cb| cb.file(span.anchor.file_id.file_id())).unwrap_or_default()
132149
}
133-
fn local_file(&mut self, span: Self::Span) -> Option<String> {
150+
fn span_local_file(&mut self, span: Self::Span) -> Option<String> {
134151
self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id()))
135152
}
136-
fn save_span(&mut self, _span: Self::Span) -> usize {
153+
fn span_save_span(&mut self, _span: Self::Span) -> usize {
137154
// FIXME, quote is incompatible with third-party tools
138155
// This is called by the quote proc-macro which is expanded when the proc-macro is compiled
139156
// As such, r-a will never observe this
140157
0
141158
}
142-
fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
159+
fn span_recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
143160
// FIXME, quote is incompatible with third-party tools
144161
// This is called by the expansion of quote!, r-a will observe this, but we don't have
145162
// access to the spans that were encoded
@@ -149,23 +166,23 @@ impl server::Span for RaSpanServer<'_> {
149166
///
150167
/// See PR:
151168
/// https://github.com/rust-lang/rust/pull/55780
152-
fn source_text(&mut self, span: Self::Span) -> Option<String> {
169+
fn span_source_text(&mut self, span: Self::Span) -> Option<String> {
153170
self.callback.as_mut()?.source_text(span)
154171
}
155172

156-
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
173+
fn span_parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
157174
// FIXME requires db, looks up the parent call site
158175
None
159176
}
160-
fn source(&mut self, span: Self::Span) -> Self::Span {
177+
fn span_source(&mut self, span: Self::Span) -> Self::Span {
161178
// FIXME requires db, returns the top level call site
162179
span
163180
}
164-
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
181+
fn span_byte_range(&mut self, span: Self::Span) -> Range<usize> {
165182
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
166183
Range { start: span.range.start().into(), end: span.range.end().into() }
167184
}
168-
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
185+
fn span_join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
169186
// We can't modify the span range for fixup spans, those are meaningful to fixup, so just
170187
// prefer the non-fixup span.
171188
if first.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
@@ -193,7 +210,7 @@ impl server::Span for RaSpanServer<'_> {
193210
ctx: second.ctx,
194211
})
195212
}
196-
fn subspan(
213+
fn span_subspan(
197214
&mut self,
198215
span: Self::Span,
199216
start: Bound<usize>,
@@ -237,58 +254,38 @@ impl server::Span for RaSpanServer<'_> {
237254
})
238255
}
239256

240-
fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
257+
fn span_resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
241258
Span { ctx: at.ctx, ..span }
242259
}
243260

244-
fn end(&mut self, span: Self::Span) -> Self::Span {
261+
fn span_end(&mut self, span: Self::Span) -> Self::Span {
245262
// We can't modify the span range for fixup spans, those are meaningful to fixup.
246263
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
247264
return span;
248265
}
249266
Span { range: TextRange::empty(span.range.end()), ..span }
250267
}
251268

252-
fn start(&mut self, span: Self::Span) -> Self::Span {
269+
fn span_start(&mut self, span: Self::Span) -> Self::Span {
253270
// We can't modify the span range for fixup spans, those are meaningful to fixup.
254271
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
255272
return span;
256273
}
257274
Span { range: TextRange::empty(span.range.start()), ..span }
258275
}
259276

260-
fn line(&mut self, _span: Self::Span) -> usize {
277+
fn span_line(&mut self, _span: Self::Span) -> usize {
261278
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
262279
1
263280
}
264281

265-
fn column(&mut self, _span: Self::Span) -> usize {
282+
fn span_column(&mut self, _span: Self::Span) -> usize {
266283
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
267284
1
268285
}
269-
}
270286

271-
impl server::Symbol for RaSpanServer<'_> {
272-
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
287+
fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
273288
// FIXME: nfc-normalize and validate idents
274289
Ok(<Self as server::Server>::intern_symbol(string))
275290
}
276291
}
277-
278-
impl server::Server for RaSpanServer<'_> {
279-
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
280-
ExpnGlobals {
281-
def_site: self.def_site,
282-
call_site: self.call_site,
283-
mixed_site: self.mixed_site,
284-
}
285-
}
286-
287-
fn intern_symbol(ident: &str) -> Self::Symbol {
288-
Symbol::intern(ident)
289-
}
290-
291-
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
292-
f(symbol.as_str())
293-
}
294-
}

0 commit comments

Comments
 (0)