From e32ee19b3a03acd2dbd427c12cb4bcfd0c5d9881 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Sun, 26 Jun 2022 12:48:33 -0400 Subject: [PATCH] proc_macro: Rename ExpnContext to ExpnGlobals, and unify method on Server trait --- .../rustc_expand/src/proc_macro_server.rs | 27 +++++++------------ library/proc_macro/src/bridge/client.rs | 14 +++++----- library/proc_macro/src/bridge/mod.rs | 12 ++++----- library/proc_macro/src/bridge/server.rs | 22 ++++----------- 4 files changed, 28 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index dddcff4c6dd1c..6b9bc0ab54bf3 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -14,7 +14,7 @@ use rustc_span::def_id::CrateNum; 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 @@ impl<'a, 'b> Rustc<'a, 'b> { } fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option) -> 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 @@ impl server::Group for Rustc<'_, '_> { 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 @@ impl server::Group for Rustc<'_, '_> { 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 @@ impl server::Span for Rustc<'_, '_> { } 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 { + ExpnGlobals { + def_site: self.def_site, + call_site: self.call_site, + mixed_site: self.mixed_site, + } } } diff --git a/library/proc_macro/src/bridge/client.rs b/library/proc_macro/src/bridge/client.rs index f1f8ae3389857..74b4a91662bb5 100644 --- a/library/proc_macro/src/bridge/client.rs +++ b/library/proc_macro/src/bridge/client.rs @@ -232,15 +232,15 @@ impl Clone for SourceFile { 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, + /// Provided globals for this macro expansion. + globals: ExpnGlobals, } impl<'a> !Send for Bridge<'a> {} @@ -414,11 +414,11 @@ fn run_client DecodeMut<'a, 's, ()>, R: Encode<()>>( maybe_install_panic_hook(force_show_panics); let reader = &mut &buf[..]; - let (input, context) = <(A, ExpnContext)>::decode(reader, &mut ()); + let (globals, input) = <(ExpnGlobals, 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, || { diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 91d2d0f3ccc9f..3bdc9007cd2e9 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -465,15 +465,15 @@ compound_traits!( } ); -/// 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 { - def_site: S, - call_site: S, - mixed_site: S, +pub struct ExpnGlobals { + pub def_site: S, + pub call_site: S, + pub mixed_site: S, } compound_traits!( - struct ExpnContext { def_site, call_site, mixed_site } + struct ExpnGlobals { def_site, call_site, mixed_site } ); diff --git a/library/proc_macro/src/bridge/server.rs b/library/proc_macro/src/bridge/server.rs index debcffc6f79a0..1b7657eab7034 100644 --- a/library/proc_macro/src/bridge/server.rs +++ b/library/proc_macro/src/bridge/server.rs @@ -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; } } } @@ -50,14 +48,8 @@ with_api!(Self, self_, declare_server_traits); pub(super) struct MarkedTypes(S); impl Server for MarkedTypes { - 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 { + <_>::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);