Merge FreeFunctions trait into Server trait

And rename FreeFunctions struct to Methods.
This commit is contained in:
bjorn3 2026-01-22 19:00:17 +00:00
parent 9481890143
commit 8a119c3145
9 changed files with 112 additions and 137 deletions

View file

@ -431,8 +431,6 @@ impl ToInternal<rustc_errors::Level> for Level {
}
}
pub(crate) struct FreeFunctions;
pub(crate) struct Rustc<'a, 'b> {
ecx: &'a mut ExtCtxt<'b>,
def_site: Span,
@ -461,13 +459,28 @@ impl<'a, 'b> Rustc<'a, 'b> {
}
impl server::Types for Rustc<'_, '_> {
type FreeFunctions = FreeFunctions;
type TokenStream = TokenStream;
type Span = Span;
type Symbol = Symbol;
}
impl server::FreeFunctions for Rustc<'_, '_> {
impl server::Server for Rustc<'_, '_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
fn intern_symbol(string: &str) -> Self::Symbol {
Symbol::intern(string)
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
f(symbol.as_str())
}
fn injected_env_var(&mut self, var: &str) -> Option<String> {
self.ecx.sess.opts.logical_env.get(var).cloned()
}
@ -843,21 +856,3 @@ impl server::FreeFunctions for Rustc<'_, '_> {
if rustc_lexer::is_ident(sym.as_str()) { Ok(sym) } else { Err(()) }
}
}
impl server::Server for Rustc<'_, '_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
fn intern_symbol(string: &str) -> Self::Symbol {
Symbol::intern(string)
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
f(symbol.as_str())
}
}

View file

@ -25,7 +25,7 @@ impl !Sync for TokenStream {}
// Forward `Drop::drop` to the inherent `drop` method.
impl Drop for TokenStream {
fn drop(&mut self) {
FreeFunctions::tt_drop(TokenStream { handle: self.handle });
Methods::tt_drop(TokenStream { handle: self.handle });
}
}
@ -75,7 +75,7 @@ impl<S> Decode<'_, '_, S> for Span {
impl Clone for TokenStream {
fn clone(&self) -> Self {
FreeFunctions::tt_clone(self)
Methods::tt_clone(self)
}
}
@ -95,21 +95,21 @@ impl Span {
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&FreeFunctions::span_debug(*self))
f.write_str(&Methods::span_debug(*self))
}
}
pub(crate) use super::FreeFunctions;
pub(crate) use super::Methods;
pub(crate) use super::symbol::Symbol;
macro_rules! define_client_side {
(
FreeFunctions {
Methods {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident),* $(,)?
) => {
impl FreeFunctions {
impl Methods {
$(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)? {
Bridge::with(|bridge| {
let mut buf = bridge.cached_buffer.take();

View file

@ -21,7 +21,7 @@ use crate::{Delimiter, Level, Spacing};
/// `with_api!(MySelf, my_self, my_macro)` expands to:
/// ```rust,ignore (pseudo-code)
/// my_macro! {
/// FreeFunctions {
/// Methods {
/// // ...
/// fn lit_character(ch: char) -> MySelf::Literal;
/// // ...
@ -49,7 +49,7 @@ use crate::{Delimiter, Level, Spacing};
macro_rules! with_api {
($S:ident, $self:ident, $m:ident) => {
$m! {
FreeFunctions {
Methods {
fn injected_env_var(var: &str) -> Option<String>;
fn track_env_var(var: &str, value: Option<&str>);
fn track_path(path: &str);
@ -103,7 +103,7 @@ macro_rules! with_api {
};
}
pub(crate) struct FreeFunctions;
pub(crate) struct Methods;
#[allow(unsafe_code)]
mod arena;
@ -158,7 +158,7 @@ mod api_tags {
macro_rules! declare_tags {
(
FreeFunctions {
Methods {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident),* $(,)?

View file

@ -54,7 +54,6 @@ impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>> for Marked<S::Span, c
}
pub trait Types {
type FreeFunctions: 'static;
type TokenStream: 'static + Clone;
type Span: 'static + Copy + Eq + Hash;
type Symbol: 'static;
@ -62,16 +61,12 @@ pub trait Types {
macro_rules! declare_server_traits {
(
FreeFunctions {
Methods {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident),* $(,)?
) => {
pub trait FreeFunctions: Types {
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)*
}
pub trait Server: Types + FreeFunctions {
pub trait Server: Types {
fn globals(&mut self) -> ExpnGlobals<Self::Span>;
/// Intern a symbol received from RPC
@ -79,6 +74,8 @@ macro_rules! declare_server_traits {
/// Recover the string value of a symbol, and invoke a callback with it.
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str));
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)*
}
}
}
@ -86,33 +83,30 @@ with_api!(Self, self_, declare_server_traits);
pub(super) struct MarkedTypes<S: Types>(S);
impl<S: Server> Server for MarkedTypes<S> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
<_>::mark(Server::globals(&mut self.0))
}
fn intern_symbol(ident: &str) -> Self::Symbol {
<_>::mark(S::intern_symbol(ident))
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
S::with_symbol_string(symbol.unmark(), f)
}
}
macro_rules! define_mark_types_impls {
(
FreeFunctions {
Methods {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident),* $(,)?
) => {
impl<S: Types> Types for MarkedTypes<S> {
type FreeFunctions = Marked<S::FreeFunctions, client::FreeFunctions>;
$(type $name = Marked<S::$name, client::$name>;)*
}
impl<S: FreeFunctions> FreeFunctions for MarkedTypes<S> {
impl<S: Server> Server for MarkedTypes<S> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
<_>::mark(Server::globals(&mut self.0))
}
fn intern_symbol(ident: &str) -> Self::Symbol {
<_>::mark(S::intern_symbol(ident))
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
S::with_symbol_string(symbol.unmark(), f)
}
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)? {
<_>::mark(FreeFunctions::$method(&mut self.0, $($arg.unmark()),*))
<_>::mark(S::$method(&mut self.0, $($arg.unmark()),*))
})*
}
}
@ -126,7 +120,7 @@ struct Dispatcher<S: Types> {
macro_rules! define_dispatcher_impl {
(
FreeFunctions {
Methods {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident),* $(,)?
@ -134,14 +128,12 @@ macro_rules! define_dispatcher_impl {
// FIXME(eddyb) `pub` only for `ExecutionStrategy` below.
pub trait DispatcherTrait {
// HACK(eddyb) these are here to allow `Self::$name` to work below.
type FreeFunctions;
$(type $name;)*
fn dispatch(&mut self, buf: Buffer) -> Buffer;
}
impl<S: Server> DispatcherTrait for Dispatcher<MarkedTypes<S>> {
type FreeFunctions = <MarkedTypes<S> as Types>::FreeFunctions;
$(type $name = <MarkedTypes<S> as Types>::$name;)*
fn dispatch(&mut self, mut buf: Buffer) -> Buffer {
@ -152,7 +144,7 @@ macro_rules! define_dispatcher_impl {
$(api_tags::Method::$method => {
let mut call_method = || {
$(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)*
FreeFunctions::$method(server, $($arg),*)
server.$method($($arg),*)
};
// HACK(eddyb) don't use `panic::catch_unwind` in a panic.
// If client and server happen to use the same `std`,

View file

@ -46,7 +46,7 @@ impl Symbol {
if string.is_ascii() {
Err(())
} else {
client::FreeFunctions::symbol_normalize_and_validate_ident(string)
client::Methods::symbol_normalize_and_validate_ident(string)
}
.unwrap_or_else(|_| panic!("`{:?}` is not a valid identifier", string))
}

View file

@ -170,6 +170,6 @@ impl Diagnostic {
}
}
crate::bridge::client::FreeFunctions::emit_diagnostic(to_internal(self));
crate::bridge::client::Methods::emit_diagnostic(to_internal(self));
}
}

View file

@ -158,7 +158,7 @@ impl TokenStream {
/// Checks if this `TokenStream` is empty.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn is_empty(&self) -> bool {
self.0.as_ref().map(|h| bridge::client::FreeFunctions::tt_is_empty(h)).unwrap_or(true)
self.0.as_ref().map(|h| bridge::client::Methods::tt_is_empty(h)).unwrap_or(true)
}
/// Parses this `TokenStream` as an expression and attempts to expand any
@ -174,7 +174,7 @@ impl TokenStream {
#[unstable(feature = "proc_macro_expand", issue = "90765")]
pub fn expand_expr(&self) -> Result<TokenStream, ExpandError> {
let stream = self.0.as_ref().ok_or(ExpandError)?;
match bridge::client::FreeFunctions::tt_expand_expr(stream) {
match bridge::client::Methods::tt_expand_expr(stream) {
Ok(stream) => Ok(TokenStream(Some(stream))),
Err(_) => Err(ExpandError),
}
@ -193,7 +193,7 @@ impl FromStr for TokenStream {
type Err = LexError;
fn from_str(src: &str) -> Result<TokenStream, LexError> {
Ok(TokenStream(Some(bridge::client::FreeFunctions::tt_from_str(src))))
Ok(TokenStream(Some(bridge::client::Methods::tt_from_str(src))))
}
}
@ -212,7 +212,7 @@ impl FromStr for TokenStream {
impl fmt::Display for TokenStream {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Some(ts) => write!(f, "{}", bridge::client::FreeFunctions::tt_to_string(ts)),
Some(ts) => write!(f, "{}", bridge::client::Methods::tt_to_string(ts)),
None => Ok(()),
}
}
@ -252,9 +252,7 @@ fn tree_to_bridge_tree(
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
impl From<TokenTree> for TokenStream {
fn from(tree: TokenTree) -> TokenStream {
TokenStream(Some(bridge::client::FreeFunctions::tt_from_token_tree(tree_to_bridge_tree(
tree,
))))
TokenStream(Some(bridge::client::Methods::tt_from_token_tree(tree_to_bridge_tree(tree))))
}
}
@ -283,7 +281,7 @@ impl ConcatTreesHelper {
if self.trees.is_empty() {
TokenStream(None)
} else {
TokenStream(Some(bridge::client::FreeFunctions::tt_concat_trees(None, self.trees)))
TokenStream(Some(bridge::client::Methods::tt_concat_trees(None, self.trees)))
}
}
@ -291,7 +289,7 @@ impl ConcatTreesHelper {
if self.trees.is_empty() {
return;
}
stream.0 = Some(bridge::client::FreeFunctions::tt_concat_trees(stream.0.take(), self.trees))
stream.0 = Some(bridge::client::Methods::tt_concat_trees(stream.0.take(), self.trees))
}
}
@ -316,7 +314,7 @@ impl ConcatStreamsHelper {
if self.streams.len() <= 1 {
TokenStream(self.streams.pop())
} else {
TokenStream(Some(bridge::client::FreeFunctions::tt_concat_streams(None, self.streams)))
TokenStream(Some(bridge::client::Methods::tt_concat_streams(None, self.streams)))
}
}
@ -328,7 +326,7 @@ impl ConcatStreamsHelper {
if base.is_none() && self.streams.len() == 1 {
stream.0 = self.streams.pop();
} else {
stream.0 = Some(bridge::client::FreeFunctions::tt_concat_streams(base, self.streams));
stream.0 = Some(bridge::client::Methods::tt_concat_streams(base, self.streams));
}
}
}
@ -441,7 +439,7 @@ pub mod token_stream {
fn into_iter(self) -> IntoIter {
IntoIter(
self.0
.map(|v| bridge::client::FreeFunctions::tt_into_trees(v))
.map(|v| bridge::client::Methods::tt_into_trees(v))
.unwrap_or_default()
.into_iter(),
)
@ -516,7 +514,7 @@ impl Span {
/// `self` was generated from, if any.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn parent(&self) -> Option<Span> {
bridge::client::FreeFunctions::span_parent(self.0).map(Span)
bridge::client::Methods::span_parent(self.0).map(Span)
}
/// The span for the origin source code that `self` was generated from. If
@ -524,25 +522,25 @@ impl Span {
/// value is the same as `*self`.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn source(&self) -> Span {
Span(bridge::client::FreeFunctions::span_source(self.0))
Span(bridge::client::Methods::span_source(self.0))
}
/// Returns the span's byte position range in the source file.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn byte_range(&self) -> Range<usize> {
bridge::client::FreeFunctions::span_byte_range(self.0)
bridge::client::Methods::span_byte_range(self.0)
}
/// Creates an empty span pointing to directly before this span.
#[stable(feature = "proc_macro_span_location", since = "1.88.0")]
pub fn start(&self) -> Span {
Span(bridge::client::FreeFunctions::span_start(self.0))
Span(bridge::client::Methods::span_start(self.0))
}
/// Creates an empty span pointing to directly after this span.
#[stable(feature = "proc_macro_span_location", since = "1.88.0")]
pub fn end(&self) -> Span {
Span(bridge::client::FreeFunctions::span_end(self.0))
Span(bridge::client::Methods::span_end(self.0))
}
/// The one-indexed line of the source file where the span starts.
@ -550,7 +548,7 @@ impl Span {
/// To obtain the line of the span's end, use `span.end().line()`.
#[stable(feature = "proc_macro_span_location", since = "1.88.0")]
pub fn line(&self) -> usize {
bridge::client::FreeFunctions::span_line(self.0)
bridge::client::Methods::span_line(self.0)
}
/// The one-indexed column of the source file where the span starts.
@ -558,7 +556,7 @@ impl Span {
/// To obtain the column of the span's end, use `span.end().column()`.
#[stable(feature = "proc_macro_span_location", since = "1.88.0")]
pub fn column(&self) -> usize {
bridge::client::FreeFunctions::span_column(self.0)
bridge::client::Methods::span_column(self.0)
}
/// The path to the source file in which this span occurs, for display purposes.
@ -567,7 +565,7 @@ impl Span {
/// It might be remapped (e.g. `"/src/lib.rs"`) or an artificial path (e.g. `"<command line>"`).
#[stable(feature = "proc_macro_span_file", since = "1.88.0")]
pub fn file(&self) -> String {
bridge::client::FreeFunctions::span_file(self.0)
bridge::client::Methods::span_file(self.0)
}
/// The path to the source file in which this span occurs on the local file system.
@ -577,7 +575,7 @@ impl Span {
/// This path should not be embedded in the output of the macro; prefer `file()` instead.
#[stable(feature = "proc_macro_span_file", since = "1.88.0")]
pub fn local_file(&self) -> Option<PathBuf> {
bridge::client::FreeFunctions::span_local_file(self.0).map(PathBuf::from)
bridge::client::Methods::span_local_file(self.0).map(PathBuf::from)
}
/// Creates a new span encompassing `self` and `other`.
@ -585,14 +583,14 @@ impl Span {
/// Returns `None` if `self` and `other` are from different files.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn join(&self, other: Span) -> Option<Span> {
bridge::client::FreeFunctions::span_join(self.0, other.0).map(Span)
bridge::client::Methods::span_join(self.0, other.0).map(Span)
}
/// Creates a new span with the same line/column information as `self` but
/// that resolves symbols as though it were at `other`.
#[stable(feature = "proc_macro_span_resolved_at", since = "1.45.0")]
pub fn resolved_at(&self, other: Span) -> Span {
Span(bridge::client::FreeFunctions::span_resolved_at(self.0, other.0))
Span(bridge::client::Methods::span_resolved_at(self.0, other.0))
}
/// Creates a new span with the same name resolution behavior as `self` but
@ -617,21 +615,21 @@ impl Span {
/// be used for diagnostics only.
#[stable(feature = "proc_macro_source_text", since = "1.66.0")]
pub fn source_text(&self) -> Option<String> {
bridge::client::FreeFunctions::span_source_text(self.0)
bridge::client::Methods::span_source_text(self.0)
}
// Used by the implementation of `Span::quote`
#[doc(hidden)]
#[unstable(feature = "proc_macro_internals", issue = "27812")]
pub fn save_span(&self) -> usize {
bridge::client::FreeFunctions::span_save_span(self.0)
bridge::client::Methods::span_save_span(self.0)
}
// Used by the implementation of `Span::quote`
#[doc(hidden)]
#[unstable(feature = "proc_macro_internals", issue = "27812")]
pub fn recover_proc_macro_span(id: usize) -> Span {
Span(bridge::client::FreeFunctions::span_recover_proc_macro_span(id))
Span(bridge::client::Methods::span_recover_proc_macro_span(id))
}
diagnostic_method!(error, Level::Error);
@ -1396,7 +1394,7 @@ impl Literal {
// was 'c' or whether it was '\u{63}'.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
bridge::client::FreeFunctions::span_subspan(
bridge::client::Methods::span_subspan(
self.0.span,
range.start_bound().cloned(),
range.end_bound().cloned(),
@ -1571,7 +1569,7 @@ impl FromStr for Literal {
type Err = LexError;
fn from_str(src: &str) -> Result<Self, LexError> {
match bridge::client::FreeFunctions::literal_from_str(src) {
match bridge::client::Methods::literal_from_str(src) {
Ok(literal) => Ok(Literal(literal)),
Err(()) => Err(LexError),
}
@ -1626,9 +1624,9 @@ pub mod tracked {
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
pub fn env_var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
let key: &str = key.as_ref();
let value = crate::bridge::client::FreeFunctions::injected_env_var(key)
.map_or_else(|| env::var(key), Ok);
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
let value =
crate::bridge::client::Methods::injected_env_var(key).map_or_else(|| env::var(key), Ok);
crate::bridge::client::Methods::track_env_var(key, value.as_deref().ok());
value
}
@ -1638,6 +1636,6 @@ pub mod tracked {
#[unstable(feature = "proc_macro_tracked_path", issue = "99515")]
pub fn path<P: AsRef<Path>>(path: P) {
let path: &str = path.as_ref().to_str().unwrap();
crate::bridge::client::FreeFunctions::track_path(path);
crate::bridge::client::Methods::track_path(path);
}
}

View file

@ -19,8 +19,6 @@ use crate::{
server_impl::literal_from_str,
};
pub struct FreeFunctions;
pub struct RaSpanServer<'a> {
// FIXME: Report this back to the caller to track as dependencies
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
@ -33,13 +31,28 @@ pub struct RaSpanServer<'a> {
}
impl server::Types for RaSpanServer<'_> {
type FreeFunctions = FreeFunctions;
type TokenStream = crate::token_stream::TokenStream<Span>;
type Span = Span;
type Symbol = Symbol;
}
impl server::FreeFunctions for RaSpanServer<'_> {
impl server::Server for RaSpanServer<'_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
fn intern_symbol(ident: &str) -> Self::Symbol {
Symbol::intern(ident)
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
f(symbol.as_str())
}
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
None
}
@ -276,21 +289,3 @@ impl server::FreeFunctions for RaSpanServer<'_> {
Ok(<Self as server::Server>::intern_symbol(string))
}
}
impl server::Server for RaSpanServer<'_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
fn intern_symbol(ident: &str) -> Self::Symbol {
Symbol::intern(ident)
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
f(symbol.as_str())
}
}

View file

@ -25,8 +25,6 @@ impl std::fmt::Debug for SpanId {
type Span = SpanId;
pub struct FreeFunctions;
pub struct SpanIdServer<'a> {
// FIXME: Report this back to the caller to track as dependencies
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
@ -39,13 +37,28 @@ pub struct SpanIdServer<'a> {
}
impl server::Types for SpanIdServer<'_> {
type FreeFunctions = FreeFunctions;
type TokenStream = crate::token_stream::TokenStream<Span>;
type Span = Span;
type Symbol = Symbol;
}
impl server::FreeFunctions for SpanIdServer<'_> {
impl server::Server for SpanIdServer<'_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
fn intern_symbol(ident: &str) -> Self::Symbol {
Symbol::intern(ident)
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
f(symbol.as_str())
}
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
None
}
@ -195,21 +208,3 @@ impl server::FreeFunctions for SpanIdServer<'_> {
Ok(<Self as server::Server>::intern_symbol(string))
}
}
impl server::Server for SpanIdServer<'_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {
def_site: self.def_site,
call_site: self.call_site,
mixed_site: self.mixed_site,
}
}
fn intern_symbol(ident: &str) -> Self::Symbol {
Symbol::intern(ident)
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
f(symbol.as_str())
}
}