2019-10-16 03:59:30 -05:00
|
|
|
use crate::base::{self, *};
|
|
|
|
use crate::proc_macro_server;
|
|
|
|
|
2020-11-14 05:47:14 -06:00
|
|
|
use rustc_ast as ast;
|
2020-11-24 13:47:49 -06:00
|
|
|
use rustc_ast::ptr::P;
|
2020-07-01 05:16:49 -05:00
|
|
|
use rustc_ast::token;
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-27 19:31:04 -05:00
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2021-01-18 15:47:37 -06:00
|
|
|
use rustc_parse::parser::ForceCollect;
|
2022-06-18 13:15:03 -05:00
|
|
|
use rustc_session::config::ProcMacroExecutionStrategy;
|
2022-04-01 14:00:51 -05:00
|
|
|
use rustc_span::profiling::SpannedEventArgRecorder;
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2019-07-18 13:02:34 -05:00
|
|
|
|
2022-06-18 13:15:03 -05:00
|
|
|
struct CrossbeamMessagePipe<T> {
|
|
|
|
tx: crossbeam_channel::Sender<T>,
|
|
|
|
rx: crossbeam_channel::Receiver<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> pm::bridge::server::MessagePipe<T> for CrossbeamMessagePipe<T> {
|
|
|
|
fn new() -> (Self, Self) {
|
|
|
|
let (tx1, rx1) = crossbeam_channel::bounded(1);
|
|
|
|
let (tx2, rx2) = crossbeam_channel::bounded(1);
|
|
|
|
(CrossbeamMessagePipe { tx: tx1, rx: rx2 }, CrossbeamMessagePipe { tx: tx2, rx: rx1 })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send(&mut self, value: T) {
|
|
|
|
self.tx.send(value).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recv(&mut self) -> Option<T> {
|
|
|
|
self.rx.recv().ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec_strategy(ecx: &ExtCtxt<'_>) -> impl pm::bridge::server::ExecutionStrategy {
|
|
|
|
pm::bridge::server::MaybeCrossThread::<CrossbeamMessagePipe<_>>::new(
|
|
|
|
ecx.sess.opts.unstable_opts.proc_macro_execution_strategy
|
|
|
|
== ProcMacroExecutionStrategy::CrossThread,
|
|
|
|
)
|
|
|
|
}
|
2019-07-18 13:02:34 -05:00
|
|
|
|
|
|
|
pub struct BangProcMacro {
|
2022-05-27 12:38:58 -05:00
|
|
|
pub client: pm::bridge::client::Client<pm::TokenStream, pm::TokenStream>,
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
|
2022-05-12 23:07:56 -05:00
|
|
|
impl base::BangProcMacro for BangProcMacro {
|
2019-07-18 13:02:34 -05:00
|
|
|
fn expand<'cx>(
|
|
|
|
&self,
|
|
|
|
ecx: &'cx mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
input: TokenStream,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<TokenStream, ErrorGuaranteed> {
|
2022-03-30 08:14:25 -05:00
|
|
|
let _timer =
|
2022-04-01 14:00:51 -05:00
|
|
|
ecx.sess.prof.generic_activity_with_arg_recorder("expand_proc_macro", |recorder| {
|
|
|
|
recorder.record_arg_with_span(ecx.expansion_descr(), span);
|
|
|
|
});
|
|
|
|
|
2021-07-18 14:53:06 -05:00
|
|
|
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
|
2022-06-18 13:15:03 -05:00
|
|
|
let strategy = exec_strategy(ecx);
|
2021-07-10 15:15:30 -05:00
|
|
|
let server = proc_macro_server::Rustc::new(ecx);
|
2022-06-18 13:15:03 -05:00
|
|
|
self.client.run(&strategy, server, input, proc_macro_backtrace).map_err(|e| {
|
2020-03-17 04:09:18 -05:00
|
|
|
let mut err = ecx.struct_span_err(span, "proc macro panicked");
|
|
|
|
if let Some(s) = e.as_str() {
|
|
|
|
err.help(&format!("message: {}", s));
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
2022-01-22 18:49:12 -06:00
|
|
|
err.emit()
|
2020-03-17 04:09:18 -05:00
|
|
|
})
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AttrProcMacro {
|
2022-05-27 12:38:58 -05:00
|
|
|
pub client: pm::bridge::client::Client<(pm::TokenStream, pm::TokenStream), pm::TokenStream>,
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl base::AttrProcMacro for AttrProcMacro {
|
|
|
|
fn expand<'cx>(
|
|
|
|
&self,
|
|
|
|
ecx: &'cx mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
annotation: TokenStream,
|
|
|
|
annotated: TokenStream,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<TokenStream, ErrorGuaranteed> {
|
2022-03-30 08:14:25 -05:00
|
|
|
let _timer =
|
2022-04-01 14:00:51 -05:00
|
|
|
ecx.sess.prof.generic_activity_with_arg_recorder("expand_proc_macro", |recorder| {
|
|
|
|
recorder.record_arg_with_span(ecx.expansion_descr(), span);
|
|
|
|
});
|
|
|
|
|
2021-07-18 14:53:06 -05:00
|
|
|
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
|
2022-06-18 13:15:03 -05:00
|
|
|
let strategy = exec_strategy(ecx);
|
2021-07-10 15:15:30 -05:00
|
|
|
let server = proc_macro_server::Rustc::new(ecx);
|
2022-06-18 13:15:03 -05:00
|
|
|
self.client.run(&strategy, server, annotation, annotated, proc_macro_backtrace).map_err(
|
|
|
|
|e| {
|
2020-08-30 21:17:24 -05:00
|
|
|
let mut err = ecx.struct_span_err(span, "custom attribute panicked");
|
|
|
|
if let Some(s) = e.as_str() {
|
|
|
|
err.help(&format!("message: {}", s));
|
|
|
|
}
|
2022-01-22 18:49:12 -06:00
|
|
|
err.emit()
|
2022-06-18 13:15:03 -05:00
|
|
|
},
|
|
|
|
)
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:56:36 -05:00
|
|
|
pub struct DeriveProcMacro {
|
2022-05-27 12:38:58 -05:00
|
|
|
pub client: pm::bridge::client::Client<pm::TokenStream, pm::TokenStream>,
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:56:36 -05:00
|
|
|
impl MultiItemModifier for DeriveProcMacro {
|
2019-07-18 13:02:34 -05:00
|
|
|
fn expand(
|
|
|
|
&self,
|
|
|
|
ecx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
_meta_item: &ast::MetaItem,
|
|
|
|
item: Annotatable,
|
2020-03-09 12:50:12 -05:00
|
|
|
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
|
2020-11-24 13:47:49 -06:00
|
|
|
// We need special handling for statement items
|
|
|
|
// (e.g. `fn foo() { #[derive(Debug)] struct Bar; }`)
|
2022-05-01 12:58:24 -05:00
|
|
|
let is_stmt = matches!(item, Annotatable::Stmt(..));
|
|
|
|
let hack = crate::base::ann_pretty_printing_compatibility_hack(&item, &ecx.sess.parse_sess);
|
|
|
|
let input = if hack {
|
|
|
|
let nt = match item {
|
|
|
|
Annotatable::Item(item) => token::NtItem(item),
|
|
|
|
Annotatable::Stmt(stmt) => token::NtStmt(stmt),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-27 19:31:04 -05:00
|
|
|
TokenStream::token_alone(token::Interpolated(Lrc::new(nt)), DUMMY_SP)
|
2020-06-14 06:30:42 -05:00
|
|
|
} else {
|
2022-05-21 07:50:39 -05:00
|
|
|
item.to_tokens()
|
2020-06-14 06:30:42 -05:00
|
|
|
};
|
2019-07-18 13:02:34 -05:00
|
|
|
|
2022-03-30 08:14:25 -05:00
|
|
|
let stream = {
|
|
|
|
let _timer =
|
2022-04-01 14:00:51 -05:00
|
|
|
ecx.sess.prof.generic_activity_with_arg_recorder("expand_proc_macro", |recorder| {
|
|
|
|
recorder.record_arg_with_span(ecx.expansion_descr(), span);
|
|
|
|
});
|
2022-03-30 08:14:25 -05:00
|
|
|
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
|
2022-06-18 13:15:03 -05:00
|
|
|
let strategy = exec_strategy(ecx);
|
2022-03-30 08:14:25 -05:00
|
|
|
let server = proc_macro_server::Rustc::new(ecx);
|
2022-06-18 13:15:03 -05:00
|
|
|
match self.client.run(&strategy, server, input, proc_macro_backtrace) {
|
2022-03-30 08:14:25 -05:00
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(e) => {
|
|
|
|
let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
|
|
|
|
if let Some(s) = e.as_str() {
|
|
|
|
err.help(&format!("message: {}", s));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
return ExpandResult::Ready(vec![]);
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
2021-07-18 14:53:06 -05:00
|
|
|
}
|
|
|
|
};
|
2019-07-18 13:02:34 -05:00
|
|
|
|
2020-07-29 20:27:50 -05:00
|
|
|
let error_count_before = ecx.sess.parse_sess.span_diagnostic.err_count();
|
2019-10-15 15:48:13 -05:00
|
|
|
let mut parser =
|
2020-07-29 20:27:50 -05:00
|
|
|
rustc_parse::stream_to_parser(&ecx.sess.parse_sess, stream, Some("proc-macro derive"));
|
2019-07-18 13:02:34 -05:00
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
loop {
|
2021-01-18 15:47:37 -06:00
|
|
|
match parser.parse_item(ForceCollect::No) {
|
2019-07-18 13:02:34 -05:00
|
|
|
Ok(None) => break,
|
2020-11-24 13:47:49 -06:00
|
|
|
Ok(Some(item)) => {
|
|
|
|
if is_stmt {
|
|
|
|
items.push(Annotatable::Stmt(P(ecx.stmt_item(span, item))));
|
|
|
|
} else {
|
|
|
|
items.push(Annotatable::Item(item));
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 13:02:34 -05:00
|
|
|
Err(mut err) => {
|
2020-03-17 05:30:53 -05:00
|
|
|
err.emit();
|
|
|
|
break;
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if there have been errors emitted
|
2020-07-29 20:27:50 -05:00
|
|
|
if ecx.sess.parse_sess.span_diagnostic.err_count() > error_count_before {
|
2020-03-17 05:30:53 -05:00
|
|
|
ecx.struct_span_err(span, "proc-macro derive produced unparseable tokens").emit();
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:50:12 -05:00
|
|
|
ExpandResult::Ready(items)
|
2019-07-18 13:02:34 -05:00
|
|
|
}
|
|
|
|
}
|