proc_macro: Rename ExpnContext to ExpnGlobals, and unify method on Server trait

This commit is contained in:
Nika Layzell 2022-06-26 12:48:33 -04:00
parent 2456ff8928
commit e32ee19b3a
4 changed files with 28 additions and 47 deletions

View File

@ -14,7 +14,7 @@
use rustc_span::symbol::{self, kw, sym, Symbol};
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
use pm::bridge::{server, TokenTree};
use pm::bridge::{server, ExpnGlobals, TokenTree};
use pm::{Delimiter, Level, LineColumn, Spacing};
use std::ops::Bound;
use std::{ascii, panic};
@ -370,10 +370,7 @@ fn sess(&self) -> &ParseSess {
}
fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Literal {
Literal {
lit: token::Lit::new(kind, symbol, suffix),
span: server::Server::call_site(self),
}
Literal { lit: token::Lit::new(kind, symbol, suffix), span: self.call_site }
}
}
@ -550,7 +547,7 @@ fn new(&mut self, delimiter: Delimiter, stream: Option<Self::TokenStream>) -> Se
Group {
delimiter,
stream: stream.unwrap_or_default(),
span: DelimSpan::from_single(server::Server::call_site(self)),
span: DelimSpan::from_single(self.call_site),
flatten: false,
}
}
@ -582,7 +579,7 @@ fn set_span(&mut self, group: &mut Self::Group, span: Self::Span) {
impl server::Punct for Rustc<'_, '_> {
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
Punct::new(ch, spacing == Spacing::Joint, server::Server::call_site(self))
Punct::new(ch, spacing == Spacing::Joint, self.call_site)
}
fn as_char(&mut self, punct: Self::Punct) -> char {
@ -919,15 +916,11 @@ fn recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
}
impl server::Server for Rustc<'_, '_> {
fn def_site(&mut self) -> Self::Span {
self.def_site
}
fn call_site(&mut self) -> Self::Span {
self.call_site
}
fn mixed_site(&mut self) -> Self::Span {
self.mixed_site
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
}

View File

@ -232,15 +232,15 @@ fn clone(&self) -> Self {
impl Span {
pub(crate) fn def_site() -> Span {
Bridge::with(|bridge| bridge.context.def_site)
Bridge::with(|bridge| bridge.globals.def_site)
}
pub(crate) fn call_site() -> Span {
Bridge::with(|bridge| bridge.context.call_site)
Bridge::with(|bridge| bridge.globals.call_site)
}
pub(crate) fn mixed_site() -> Span {
Bridge::with(|bridge| bridge.context.mixed_site)
Bridge::with(|bridge| bridge.globals.mixed_site)
}
}
@ -285,8 +285,8 @@ struct Bridge<'a> {
/// Server-side function that the client uses to make requests.
dispatch: closure::Closure<'a, Buffer, Buffer>,
/// Provided context for this macro expansion.
context: ExpnContext<Span>,
/// Provided globals for this macro expansion.
globals: ExpnGlobals<Span>,
}
impl<'a> !Send for Bridge<'a> {}
@ -414,11 +414,11 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
maybe_install_panic_hook(force_show_panics);
let reader = &mut &buf[..];
let (input, context) = <(A, ExpnContext<Span>)>::decode(reader, &mut ());
let (globals, input) = <(ExpnGlobals<Span>, A)>::decode(reader, &mut ());
// Put the buffer we used for input back in the `Bridge` for requests.
let new_state =
BridgeState::Connected(Bridge { cached_buffer: buf.take(), dispatch, context });
BridgeState::Connected(Bridge { cached_buffer: buf.take(), dispatch, globals });
BRIDGE_STATE.with(|state| {
state.set(new_state, || {

View File

@ -465,15 +465,15 @@ enum TokenTree<G, P, I, L> {
}
);
/// Context provided alongside the initial inputs for a macro expansion.
/// Globals provided alongside the initial inputs for a macro expansion.
/// Provides values such as spans which are used frequently to avoid RPC.
#[derive(Clone)]
struct ExpnContext<S> {
def_site: S,
call_site: S,
mixed_site: S,
pub struct ExpnGlobals<S> {
pub def_site: S,
pub call_site: S,
pub mixed_site: S,
}
compound_traits!(
struct ExpnContext<Sp> { def_site, call_site, mixed_site }
struct ExpnGlobals<Sp> { def_site, call_site, mixed_site }
);

View File

@ -39,9 +39,7 @@ macro_rules! declare_server_traits {
})*
pub trait Server: Types $(+ $name)* {
fn def_site(&mut self) -> Self::Span;
fn call_site(&mut self) -> Self::Span;
fn mixed_site(&mut self) -> Self::Span;
fn globals(&mut self) -> ExpnGlobals<Self::Span>;
}
}
}
@ -50,14 +48,8 @@ pub trait Server: Types $(+ $name)* {
pub(super) struct MarkedTypes<S: Types>(S);
impl<S: Server> Server for MarkedTypes<S> {
fn def_site(&mut self) -> Self::Span {
<_>::mark(Server::def_site(&mut self.0))
}
fn call_site(&mut self) -> Self::Span {
<_>::mark(Server::call_site(&mut self.0))
}
fn mixed_site(&mut self) -> Self::Span {
<_>::mark(Server::mixed_site(&mut self.0))
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
<_>::mark(Server::globals(&mut self.0))
}
}
@ -279,14 +271,10 @@ fn run_server<
let mut dispatcher =
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
let expn_context = ExpnContext {
def_site: dispatcher.server.def_site(),
call_site: dispatcher.server.call_site(),
mixed_site: dispatcher.server.mixed_site(),
};
let globals = dispatcher.server.globals();
let mut buf = Buffer::new();
(input, expn_context).encode(&mut buf, &mut dispatcher.handle_store);
(globals, input).encode(&mut buf, &mut dispatcher.handle_store);
buf = strategy.run_bridge_and_client(&mut dispatcher, buf, run_client, force_show_panics);