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::symbol::{self, kw, sym, Symbol};
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span}; 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 pm::{Delimiter, Level, LineColumn, Spacing};
use std::ops::Bound; use std::ops::Bound;
use std::{ascii, panic}; 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 { fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Literal {
Literal { Literal { lit: token::Lit::new(kind, symbol, suffix), span: self.call_site }
lit: token::Lit::new(kind, symbol, suffix),
span: server::Server::call_site(self),
}
} }
} }
@ -550,7 +547,7 @@ fn new(&mut self, delimiter: Delimiter, stream: Option<Self::TokenStream>) -> Se
Group { Group {
delimiter, delimiter,
stream: stream.unwrap_or_default(), stream: stream.unwrap_or_default(),
span: DelimSpan::from_single(server::Server::call_site(self)), span: DelimSpan::from_single(self.call_site),
flatten: false, flatten: false,
} }
} }
@ -582,7 +579,7 @@ fn set_span(&mut self, group: &mut Self::Group, span: Self::Span) {
impl server::Punct for Rustc<'_, '_> { impl server::Punct for Rustc<'_, '_> {
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct { 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 { 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<'_, '_> { impl server::Server for Rustc<'_, '_> {
fn def_site(&mut self) -> Self::Span { fn globals(&mut self) -> ExpnGlobals<Self::Span> {
self.def_site ExpnGlobals {
} def_site: self.def_site,
call_site: self.call_site,
fn call_site(&mut self) -> Self::Span { mixed_site: self.mixed_site,
self.call_site }
}
fn mixed_site(&mut self) -> Self::Span {
self.mixed_site
} }
} }

View File

@ -232,15 +232,15 @@ fn clone(&self) -> Self {
impl Span { impl Span {
pub(crate) fn def_site() -> 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 { 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 { 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. /// Server-side function that the client uses to make requests.
dispatch: closure::Closure<'a, Buffer, Buffer>, dispatch: closure::Closure<'a, Buffer, Buffer>,
/// Provided context for this macro expansion. /// Provided globals for this macro expansion.
context: ExpnContext<Span>, globals: ExpnGlobals<Span>,
} }
impl<'a> !Send for Bridge<'a> {} 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); maybe_install_panic_hook(force_show_panics);
let reader = &mut &buf[..]; 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. // Put the buffer we used for input back in the `Bridge` for requests.
let new_state = 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| { BRIDGE_STATE.with(|state| {
state.set(new_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. /// Provides values such as spans which are used frequently to avoid RPC.
#[derive(Clone)] #[derive(Clone)]
struct ExpnContext<S> { pub struct ExpnGlobals<S> {
def_site: S, pub def_site: S,
call_site: S, pub call_site: S,
mixed_site: S, pub mixed_site: S,
} }
compound_traits!( 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)* { pub trait Server: Types $(+ $name)* {
fn def_site(&mut self) -> Self::Span; fn globals(&mut self) -> ExpnGlobals<Self::Span>;
fn call_site(&mut self) -> Self::Span;
fn mixed_site(&mut self) -> Self::Span;
} }
} }
} }
@ -50,14 +48,8 @@ pub trait Server: Types $(+ $name)* {
pub(super) struct MarkedTypes<S: Types>(S); pub(super) struct MarkedTypes<S: Types>(S);
impl<S: Server> Server for MarkedTypes<S> { impl<S: Server> Server for MarkedTypes<S> {
fn def_site(&mut self) -> Self::Span { fn globals(&mut self) -> ExpnGlobals<Self::Span> {
<_>::mark(Server::def_site(&mut self.0)) <_>::mark(Server::globals(&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))
} }
} }
@ -279,14 +271,10 @@ fn run_server<
let mut dispatcher = let mut dispatcher =
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) }; Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
let expn_context = ExpnContext { let globals = dispatcher.server.globals();
def_site: dispatcher.server.def_site(),
call_site: dispatcher.server.call_site(),
mixed_site: dispatcher.server.mixed_site(),
};
let mut buf = Buffer::new(); 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); buf = strategy.run_bridge_and_client(&mut dispatcher, buf, run_client, force_show_panics);