Skip to content

Commit e32ee19

Browse files
committed
proc_macro: Rename ExpnContext to ExpnGlobals, and unify method on Server trait
1 parent 2456ff8 commit e32ee19

File tree

4 files changed

+28
-47
lines changed

4 files changed

+28
-47
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::def_id::CrateNum;
1414
use rustc_span::symbol::{self, kw, sym, Symbol};
1515
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
1616

17-
use pm::bridge::{server, TokenTree};
17+
use pm::bridge::{server, ExpnGlobals, TokenTree};
1818
use pm::{Delimiter, Level, LineColumn, Spacing};
1919
use std::ops::Bound;
2020
use std::{ascii, panic};
@@ -370,10 +370,7 @@ impl<'a, 'b> Rustc<'a, 'b> {
370370
}
371371

372372
fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Literal {
373-
Literal {
374-
lit: token::Lit::new(kind, symbol, suffix),
375-
span: server::Server::call_site(self),
376-
}
373+
Literal { lit: token::Lit::new(kind, symbol, suffix), span: self.call_site }
377374
}
378375
}
379376

@@ -550,7 +547,7 @@ impl server::Group for Rustc<'_, '_> {
550547
Group {
551548
delimiter,
552549
stream: stream.unwrap_or_default(),
553-
span: DelimSpan::from_single(server::Server::call_site(self)),
550+
span: DelimSpan::from_single(self.call_site),
554551
flatten: false,
555552
}
556553
}
@@ -582,7 +579,7 @@ impl server::Group for Rustc<'_, '_> {
582579

583580
impl server::Punct for Rustc<'_, '_> {
584581
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
585-
Punct::new(ch, spacing == Spacing::Joint, server::Server::call_site(self))
582+
Punct::new(ch, spacing == Spacing::Joint, self.call_site)
586583
}
587584

588585
fn as_char(&mut self, punct: Self::Punct) -> char {
@@ -919,15 +916,11 @@ impl server::Span for Rustc<'_, '_> {
919916
}
920917

921918
impl server::Server for Rustc<'_, '_> {
922-
fn def_site(&mut self) -> Self::Span {
923-
self.def_site
924-
}
925-
926-
fn call_site(&mut self) -> Self::Span {
927-
self.call_site
928-
}
929-
930-
fn mixed_site(&mut self) -> Self::Span {
931-
self.mixed_site
919+
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
920+
ExpnGlobals {
921+
def_site: self.def_site,
922+
call_site: self.call_site,
923+
mixed_site: self.mixed_site,
924+
}
932925
}
933926
}

library/proc_macro/src/bridge/client.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,15 @@ impl Clone for SourceFile {
232232

233233
impl Span {
234234
pub(crate) fn def_site() -> Span {
235-
Bridge::with(|bridge| bridge.context.def_site)
235+
Bridge::with(|bridge| bridge.globals.def_site)
236236
}
237237

238238
pub(crate) fn call_site() -> Span {
239-
Bridge::with(|bridge| bridge.context.call_site)
239+
Bridge::with(|bridge| bridge.globals.call_site)
240240
}
241241

242242
pub(crate) fn mixed_site() -> Span {
243-
Bridge::with(|bridge| bridge.context.mixed_site)
243+
Bridge::with(|bridge| bridge.globals.mixed_site)
244244
}
245245
}
246246

@@ -285,8 +285,8 @@ struct Bridge<'a> {
285285
/// Server-side function that the client uses to make requests.
286286
dispatch: closure::Closure<'a, Buffer, Buffer>,
287287

288-
/// Provided context for this macro expansion.
289-
context: ExpnContext<Span>,
288+
/// Provided globals for this macro expansion.
289+
globals: ExpnGlobals<Span>,
290290
}
291291

292292
impl<'a> !Send for Bridge<'a> {}
@@ -414,11 +414,11 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
414414
maybe_install_panic_hook(force_show_panics);
415415

416416
let reader = &mut &buf[..];
417-
let (input, context) = <(A, ExpnContext<Span>)>::decode(reader, &mut ());
417+
let (globals, input) = <(ExpnGlobals<Span>, A)>::decode(reader, &mut ());
418418

419419
// Put the buffer we used for input back in the `Bridge` for requests.
420420
let new_state =
421-
BridgeState::Connected(Bridge { cached_buffer: buf.take(), dispatch, context });
421+
BridgeState::Connected(Bridge { cached_buffer: buf.take(), dispatch, globals });
422422

423423
BRIDGE_STATE.with(|state| {
424424
state.set(new_state, || {

library/proc_macro/src/bridge/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,15 @@ compound_traits!(
465465
}
466466
);
467467

468-
/// Context provided alongside the initial inputs for a macro expansion.
468+
/// Globals provided alongside the initial inputs for a macro expansion.
469469
/// Provides values such as spans which are used frequently to avoid RPC.
470470
#[derive(Clone)]
471-
struct ExpnContext<S> {
472-
def_site: S,
473-
call_site: S,
474-
mixed_site: S,
471+
pub struct ExpnGlobals<S> {
472+
pub def_site: S,
473+
pub call_site: S,
474+
pub mixed_site: S,
475475
}
476476

477477
compound_traits!(
478-
struct ExpnContext<Sp> { def_site, call_site, mixed_site }
478+
struct ExpnGlobals<Sp> { def_site, call_site, mixed_site }
479479
);

library/proc_macro/src/bridge/server.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ macro_rules! declare_server_traits {
3939
})*
4040

4141
pub trait Server: Types $(+ $name)* {
42-
fn def_site(&mut self) -> Self::Span;
43-
fn call_site(&mut self) -> Self::Span;
44-
fn mixed_site(&mut self) -> Self::Span;
42+
fn globals(&mut self) -> ExpnGlobals<Self::Span>;
4543
}
4644
}
4745
}
@@ -50,14 +48,8 @@ with_api!(Self, self_, declare_server_traits);
5048
pub(super) struct MarkedTypes<S: Types>(S);
5149

5250
impl<S: Server> Server for MarkedTypes<S> {
53-
fn def_site(&mut self) -> Self::Span {
54-
<_>::mark(Server::def_site(&mut self.0))
55-
}
56-
fn call_site(&mut self) -> Self::Span {
57-
<_>::mark(Server::call_site(&mut self.0))
58-
}
59-
fn mixed_site(&mut self) -> Self::Span {
60-
<_>::mark(Server::mixed_site(&mut self.0))
51+
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
52+
<_>::mark(Server::globals(&mut self.0))
6153
}
6254
}
6355

@@ -279,14 +271,10 @@ fn run_server<
279271
let mut dispatcher =
280272
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
281273

282-
let expn_context = ExpnContext {
283-
def_site: dispatcher.server.def_site(),
284-
call_site: dispatcher.server.call_site(),
285-
mixed_site: dispatcher.server.mixed_site(),
286-
};
274+
let globals = dispatcher.server.globals();
287275

288276
let mut buf = Buffer::new();
289-
(input, expn_context).encode(&mut buf, &mut dispatcher.handle_store);
277+
(globals, input).encode(&mut buf, &mut dispatcher.handle_store);
290278

291279
buf = strategy.run_bridge_and_client(&mut dispatcher, buf, run_client, force_show_panics);
292280

0 commit comments

Comments
 (0)