about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNika Layzell <nika@thelayzells.com>2022-06-26 12:48:33 -0400
committerNika Layzell <nika@thelayzells.com>2022-06-26 12:48:33 -0400
commite32ee19b3a03acd2dbd427c12cb4bcfd0c5d9881 (patch)
treec4af7cf8bdf1b448e367b559faec775c58c54381
parent2456ff892889fff5dea1ed0d234c22ab676d91da (diff)
downloadrust-e32ee19b3a03acd2dbd427c12cb4bcfd0c5d9881.tar.gz
rust-e32ee19b3a03acd2dbd427c12cb4bcfd0c5d9881.zip
proc_macro: Rename ExpnContext to ExpnGlobals, and unify method on Server trait
-rw-r--r--compiler/rustc_expand/src/proc_macro_server.rs27
-rw-r--r--library/proc_macro/src/bridge/client.rs14
-rw-r--r--library/proc_macro/src/bridge/mod.rs12
-rw-r--r--library/proc_macro/src/bridge/server.rs22
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 dddcff4c6dd..6b9bc0ab54b 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<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 @@ 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<Self::Span> {
+        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 f1f8ae33898..74b4a91662b 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<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, || {
diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs
index 91d2d0f3ccc..3bdc9007cd2 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<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 }
 );
diff --git a/library/proc_macro/src/bridge/server.rs b/library/proc_macro/src/bridge/server.rs
index debcffc6f79..1b7657eab70 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<Self::Span>;
         }
     }
 }
@@ -50,14 +48,8 @@ with_api!(Self, self_, declare_server_traits);
 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);