Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf] Do not reuse internal TokenStream in proc_macro::TokenStream #116488

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub struct AttributesData {
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for
/// backwards compatibility.
#[derive(Clone, Debug, Default, Encodable, Decodable)]
pub struct TokenStream(pub(crate) Lrc<Vec<TokenTree>>);
pub struct TokenStream(pub Lrc<Vec<TokenTree>>);

/// Similar to `proc_macro::Spacing`, but for tokens.
///
Expand Down
31 changes: 20 additions & 11 deletions compiler/rustc_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ impl base::BangProcMacro for BangProcMacro {
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
let strategy = exec_strategy(ecx);
let server = proc_macro_server::Rustc::new(ecx);
self.client.run(&strategy, server, input, proc_macro_backtrace).map_err(|e| {
ecx.sess.emit_err(errors::ProcMacroPanicked {
span,
message: e
.as_str()
.map(|message| errors::ProcMacroPanickedHelp { message: message.into() }),
let input = crate::proc_macro_server::TokenStream2::from_internal(input, true);
self.client
.run(&strategy, server, input, proc_macro_backtrace)
.map_err(|e| {
ecx.sess.emit_err(errors::ProcMacroPanicked {
span,
message: e
.as_str()
.map(|message| errors::ProcMacroPanickedHelp { message: message.into() }),
})
})
})
.map(|stream| stream.to_internal())
}
}

Expand All @@ -91,15 +95,18 @@ impl base::AttrProcMacro for AttrProcMacro {
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
let strategy = exec_strategy(ecx);
let server = proc_macro_server::Rustc::new(ecx);
self.client.run(&strategy, server, annotation, annotated, proc_macro_backtrace).map_err(
|e| {
let annotation = crate::proc_macro_server::TokenStream2::from_internal(annotation, true);
let annotated = crate::proc_macro_server::TokenStream2::from_internal(annotated, true);
self.client
.run(&strategy, server, annotation, annotated, proc_macro_backtrace)
.map_err(|e| {
let mut err = ecx.struct_span_err(span, "custom attribute panicked");
if let Some(s) = e.as_str() {
err.help(format!("message: {s}"));
}
err.emit()
},
)
})
.map(|stream| stream.to_internal())
}
}

Expand Down Expand Up @@ -143,6 +150,7 @@ impl MultiItemModifier for DeriveProcMacro {
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
let strategy = exec_strategy(ecx);
let server = proc_macro_server::Rustc::new(ecx);
let input = crate::proc_macro_server::TokenStream2::from_internal(input, true);
match self.client.run(&strategy, server, input, proc_macro_backtrace) {
Ok(stream) => stream,
Err(e) => {
Expand All @@ -157,6 +165,7 @@ impl MultiItemModifier for DeriveProcMacro {
};

let error_count_before = ecx.sess.parse_sess.span_diagnostic.err_count();
let stream = stream.to_internal();
let mut parser =
rustc_parse::stream_to_parser(&ecx.sess.parse_sess, stream, Some("proc-macro derive"));
let mut items = vec![];
Expand Down
Loading