Skip to content

Commit 5a750ec

Browse files
committed
Auto merge of #116488 - petrochenkov:spacing, r=<try>
[perf] Do not reuse internal `TokenStream` in `proc_macro::TokenStream` Use a separate type instead, so that we need to convert from one `TokenStream` to another and back on proc macro boundaries. I want to check how much it affects performance. This PR also implements the suggestion to censor token jointness at proc macro boundary from #114571 (since we now have such a boundary). This PR is also semi-related to #101419. I don't like the result and will likely close this after the perf test.
2 parents 6683f13 + d6b704c commit 5a750ec

File tree

8 files changed

+235
-56
lines changed

8 files changed

+235
-56
lines changed

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub struct AttributesData {
301301
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for
302302
/// backwards compatibility.
303303
#[derive(Clone, Debug, Default, Encodable, Decodable)]
304-
pub struct TokenStream(pub(crate) Lrc<Vec<TokenTree>>);
304+
pub struct TokenStream(pub Lrc<Vec<TokenTree>>);
305305

306306
/// Similar to `proc_macro::Spacing`, but for tokens.
307307
///

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ impl base::BangProcMacro for BangProcMacro {
6060
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
6161
let strategy = exec_strategy(ecx);
6262
let server = proc_macro_server::Rustc::new(ecx);
63-
self.client.run(&strategy, server, input, proc_macro_backtrace).map_err(|e| {
64-
ecx.sess.emit_err(errors::ProcMacroPanicked {
65-
span,
66-
message: e
67-
.as_str()
68-
.map(|message| errors::ProcMacroPanickedHelp { message: message.into() }),
63+
let input = crate::proc_macro_server::TokenStream2::from_internal(input, true);
64+
self.client
65+
.run(&strategy, server, input, proc_macro_backtrace)
66+
.map_err(|e| {
67+
ecx.sess.emit_err(errors::ProcMacroPanicked {
68+
span,
69+
message: e
70+
.as_str()
71+
.map(|message| errors::ProcMacroPanickedHelp { message: message.into() }),
72+
})
6973
})
70-
})
74+
.map(|stream| stream.to_internal())
7175
}
7276
}
7377

@@ -91,15 +95,18 @@ impl base::AttrProcMacro for AttrProcMacro {
9195
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
9296
let strategy = exec_strategy(ecx);
9397
let server = proc_macro_server::Rustc::new(ecx);
94-
self.client.run(&strategy, server, annotation, annotated, proc_macro_backtrace).map_err(
95-
|e| {
98+
let annotation = crate::proc_macro_server::TokenStream2::from_internal(annotation, true);
99+
let annotated = crate::proc_macro_server::TokenStream2::from_internal(annotated, true);
100+
self.client
101+
.run(&strategy, server, annotation, annotated, proc_macro_backtrace)
102+
.map_err(|e| {
96103
let mut err = ecx.struct_span_err(span, "custom attribute panicked");
97104
if let Some(s) = e.as_str() {
98105
err.help(format!("message: {s}"));
99106
}
100107
err.emit()
101-
},
102-
)
108+
})
109+
.map(|stream| stream.to_internal())
103110
}
104111
}
105112

@@ -143,6 +150,7 @@ impl MultiItemModifier for DeriveProcMacro {
143150
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
144151
let strategy = exec_strategy(ecx);
145152
let server = proc_macro_server::Rustc::new(ecx);
153+
let input = crate::proc_macro_server::TokenStream2::from_internal(input, true);
146154
match self.client.run(&strategy, server, input, proc_macro_backtrace) {
147155
Ok(stream) => stream,
148156
Err(e) => {
@@ -157,6 +165,7 @@ impl MultiItemModifier for DeriveProcMacro {
157165
};
158166

159167
let error_count_before = ecx.sess.parse_sess.span_diagnostic.err_count();
168+
let stream = stream.to_internal();
160169
let mut parser =
161170
rustc_parse::stream_to_parser(&ecx.sess.parse_sess, stream, Some("proc-macro derive"));
162171
let mut items = vec![];

0 commit comments

Comments
 (0)