Move all bridge methods into a single type

This commit is contained in:
bjorn3 2026-01-22 18:10:03 +00:00
parent 4dc28c59ab
commit 2f44019470
8 changed files with 218 additions and 187 deletions

View file

@ -552,14 +552,20 @@ impl server::FreeFunctions for Rustc<'_, '_> {
}
diag.emit();
}
}
impl server::TokenStream for Rustc<'_, '_> {
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
fn tt_drop(&mut self, stream: Self::TokenStream) {
drop(stream);
}
fn tt_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
stream.clone()
}
fn tt_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
fn tt_from_str(&mut self, src: &str) -> Self::TokenStream {
unwrap_or_emit_fatal(source_str_to_stream(
self.psess(),
FileName::proc_macro_source_code(src),
@ -568,11 +574,11 @@ impl server::TokenStream for Rustc<'_, '_> {
))
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
fn tt_to_string(&mut self, stream: &Self::TokenStream) -> String {
pprust::tts_to_string(stream)
}
fn expand_expr(&mut self, stream: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
fn tt_expand_expr(&mut self, stream: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
// Parse the expression from our tokenstream.
let expr: PResult<'_, _> = try {
let mut p = Parser::new(self.psess(), stream.clone(), Some("proc_macro expand expr"));
@ -633,14 +639,14 @@ impl server::TokenStream for Rustc<'_, '_> {
}
}
fn from_token_tree(
fn tt_from_token_tree(
&mut self,
tree: TokenTree<Self::TokenStream, Self::Span, Self::Symbol>,
) -> Self::TokenStream {
Self::TokenStream::new((tree, &mut *self).to_internal().into_iter().collect::<Vec<_>>())
}
fn concat_trees(
fn tt_concat_trees(
&mut self,
base: Option<Self::TokenStream>,
trees: Vec<TokenTree<Self::TokenStream, Self::Span, Self::Symbol>>,
@ -654,7 +660,7 @@ impl server::TokenStream for Rustc<'_, '_> {
stream
}
fn concat_streams(
fn tt_concat_streams(
&mut self,
base: Option<Self::TokenStream>,
streams: Vec<Self::TokenStream>,
@ -666,16 +672,14 @@ impl server::TokenStream for Rustc<'_, '_> {
stream
}
fn into_trees(
fn tt_into_trees(
&mut self,
stream: Self::TokenStream,
) -> Vec<TokenTree<Self::TokenStream, Self::Span, Self::Symbol>> {
FromInternal::from_internal((stream, self))
}
}
impl server::Span for Rustc<'_, '_> {
fn debug(&mut self, span: Self::Span) -> String {
fn span_debug(&mut self, span: Self::Span) -> String {
if self.ecx.ecfg.span_debug {
format!("{span:?}")
} else {
@ -683,7 +687,7 @@ impl server::Span for Rustc<'_, '_> {
}
}
fn file(&mut self, span: Self::Span) -> String {
fn span_file(&mut self, span: Self::Span) -> String {
self.psess()
.source_map()
.lookup_char_pos(span.lo())
@ -693,7 +697,7 @@ impl server::Span for Rustc<'_, '_> {
.to_string()
}
fn local_file(&mut self, span: Self::Span) -> Option<String> {
fn span_local_file(&mut self, span: Self::Span) -> Option<String> {
self.psess()
.source_map()
.lookup_char_pos(span.lo())
@ -708,15 +712,15 @@ impl server::Span for Rustc<'_, '_> {
})
}
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
fn span_parent(&mut self, span: Self::Span) -> Option<Self::Span> {
span.parent_callsite()
}
fn source(&mut self, span: Self::Span) -> Self::Span {
fn span_source(&mut self, span: Self::Span) -> Self::Span {
span.source_callsite()
}
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
fn span_byte_range(&mut self, span: Self::Span) -> Range<usize> {
let source_map = self.psess().source_map();
let relative_start_pos = source_map.lookup_byte_offset(span.lo()).pos;
@ -724,25 +728,25 @@ impl server::Span for Rustc<'_, '_> {
Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
}
fn start(&mut self, span: Self::Span) -> Self::Span {
fn span_start(&mut self, span: Self::Span) -> Self::Span {
span.shrink_to_lo()
}
fn end(&mut self, span: Self::Span) -> Self::Span {
fn span_end(&mut self, span: Self::Span) -> Self::Span {
span.shrink_to_hi()
}
fn line(&mut self, span: Self::Span) -> usize {
fn span_line(&mut self, span: Self::Span) -> usize {
let loc = self.psess().source_map().lookup_char_pos(span.lo());
loc.line
}
fn column(&mut self, span: Self::Span) -> usize {
fn span_column(&mut self, span: Self::Span) -> usize {
let loc = self.psess().source_map().lookup_char_pos(span.lo());
loc.col.to_usize() + 1
}
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
fn span_join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
let self_loc = self.psess().source_map().lookup_char_pos(first.lo());
let other_loc = self.psess().source_map().lookup_char_pos(second.lo());
@ -753,7 +757,7 @@ impl server::Span for Rustc<'_, '_> {
Some(first.to(second))
}
fn subspan(
fn span_subspan(
&mut self,
span: Self::Span,
start: Bound<usize>,
@ -789,11 +793,11 @@ impl server::Span for Rustc<'_, '_> {
Some(span.with_lo(new_lo).with_hi(new_hi))
}
fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
fn span_resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
span.with_ctxt(at.ctxt())
}
fn source_text(&mut self, span: Self::Span) -> Option<String> {
fn span_source_text(&mut self, span: Self::Span) -> Option<String> {
self.psess().source_map().span_to_snippet(span).ok()
}
@ -821,11 +825,11 @@ impl server::Span for Rustc<'_, '_> {
/// span from the metadata of `my_proc_macro` (which we have access to,
/// since we've loaded `my_proc_macro` from disk in order to execute it).
/// In this way, we have obtained a span pointing into `my_proc_macro`
fn save_span(&mut self, span: Self::Span) -> usize {
fn span_save_span(&mut self, span: Self::Span) -> usize {
self.psess().save_proc_macro_span(span)
}
fn recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
fn span_recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
let (resolver, krate, def_site) = (&*self.ecx.resolver, self.krate, self.def_site);
*self.rebased_spans.entry(id).or_insert_with(|| {
// FIXME: `SyntaxContext` for spans from proc macro crates is lost during encoding,
@ -833,15 +837,19 @@ impl server::Span for Rustc<'_, '_> {
resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt())
})
}
}
impl server::Symbol for Rustc<'_, '_> {
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
let sym = nfc_normalize(string);
if rustc_lexer::is_ident(sym.as_str()) { Ok(sym) } else { Err(()) }
}
}
impl server::TokenStream for Rustc<'_, '_> {}
impl server::Span for Rustc<'_, '_> {}
impl server::Symbol for Rustc<'_, '_> {}
impl server::Server for Rustc<'_, '_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {

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) {
TokenStream { handle: self.handle }.drop();
FreeFunctions::tt_drop(TokenStream { handle: self.handle });
}
}
@ -75,7 +75,7 @@ impl<S> Decode<'_, '_, S> for Span {
impl Clone for TokenStream {
fn clone(&self) -> Self {
self.clone()
FreeFunctions::tt_clone(self)
}
}
@ -95,7 +95,7 @@ impl Span {
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.debug())
f.write_str(&FreeFunctions::span_debug(*self))
}
}

View file

@ -54,50 +54,50 @@ macro_rules! with_api {
fn track_path(path: &str);
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
fn emit_diagnostic(diagnostic: Diagnostic<$S::Span>);
},
TokenStream {
fn drop($self: $S::TokenStream);
fn clone($self: &$S::TokenStream) -> $S::TokenStream;
fn is_empty($self: &$S::TokenStream) -> bool;
fn expand_expr($self: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
fn from_str(src: &str) -> $S::TokenStream;
fn to_string($self: &$S::TokenStream) -> String;
fn from_token_tree(
fn tt_drop(stream: $S::TokenStream);
fn tt_clone(stream: &$S::TokenStream) -> $S::TokenStream;
fn tt_is_empty(stream: &$S::TokenStream) -> bool;
fn tt_expand_expr(stream: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
fn tt_from_str(src: &str) -> $S::TokenStream;
fn tt_to_string(stream: &$S::TokenStream) -> String;
fn tt_from_token_tree(
tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol>,
) -> $S::TokenStream;
fn concat_trees(
fn tt_concat_trees(
base: Option<$S::TokenStream>,
trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>,
) -> $S::TokenStream;
fn concat_streams(
fn tt_concat_streams(
base: Option<$S::TokenStream>,
streams: Vec<$S::TokenStream>,
) -> $S::TokenStream;
fn into_trees(
$self: $S::TokenStream
fn tt_into_trees(
stream: $S::TokenStream
) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>;
fn span_debug(span: $S::Span) -> String;
fn span_parent(span: $S::Span) -> Option<$S::Span>;
fn span_source(span: $S::Span) -> $S::Span;
fn span_byte_range(span: $S::Span) -> Range<usize>;
fn span_start(span: $S::Span) -> $S::Span;
fn span_end(span: $S::Span) -> $S::Span;
fn span_line(span: $S::Span) -> usize;
fn span_column(span: $S::Span) -> usize;
fn span_file(span: $S::Span) -> String;
fn span_local_file(span: $S::Span) -> Option<String>;
fn span_join(span: $S::Span, other: $S::Span) -> Option<$S::Span>;
fn span_subspan(span: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
fn span_resolved_at(span: $S::Span, at: $S::Span) -> $S::Span;
fn span_source_text(span: $S::Span) -> Option<String>;
fn span_save_span(span: $S::Span) -> usize;
fn span_recover_proc_macro_span(id: usize) -> $S::Span;
fn symbol_normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>;
},
Span {
fn debug($self: $S::Span) -> String;
fn parent($self: $S::Span) -> Option<$S::Span>;
fn source($self: $S::Span) -> $S::Span;
fn byte_range($self: $S::Span) -> Range<usize>;
fn start($self: $S::Span) -> $S::Span;
fn end($self: $S::Span) -> $S::Span;
fn line($self: $S::Span) -> usize;
fn column($self: $S::Span) -> usize;
fn file($self: $S::Span) -> String;
fn local_file($self: $S::Span) -> Option<String>;
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
fn source_text($self: $S::Span) -> Option<String>;
fn save_span($self: $S::Span) -> usize;
fn recover_proc_macro_span(id: usize) -> $S::Span;
},
Symbol {
fn normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>;
},
TokenStream {},
Span {},
Symbol {},
}
};
}
@ -156,20 +156,22 @@ mod api_tags {
use super::rpc::{Decode, Encode, Reader, Writer};
macro_rules! declare_tags {
($($name:ident {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
}),* $(,)?) => {
$(
pub(super) enum $name {
$($method),*
}
rpc_encode_decode!(enum $name { $($method),* });
)*
(
FreeFunctions {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident { $($x:tt)* }),* $(,)?
) => {
pub(super) enum FreeFunctions {
$($method),*
}
rpc_encode_decode!(enum FreeFunctions { $($method),* });
pub(super) enum Method {
$($name($name)),*
FreeFunctions(FreeFunctions),
}
rpc_encode_decode!(enum Method { $($name(m)),* });
rpc_encode_decode!(enum Method { FreeFunctions(m) });
}
}
with_api!(self, self, declare_tags);

View file

@ -60,24 +60,12 @@ pub trait Types {
type Symbol: 'static;
}
/// Declare an associated fn of one of the traits below, adding necessary
/// default bodies.
macro_rules! associated_fn {
(fn drop(&mut self, $arg:ident: $arg_ty:ty)) =>
(fn drop(&mut self, $arg: $arg_ty) { mem::drop($arg) });
(fn clone(&mut self, $arg:ident: $arg_ty:ty) -> $ret_ty:ty) =>
(fn clone(&mut self, $arg: $arg_ty) -> $ret_ty { $arg.clone() });
($($item:tt)*) => ($($item)*;)
}
macro_rules! declare_server_traits {
($($name:ident {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
}),* $(,)?) => {
$(pub trait $name: Types {
$(associated_fn!(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?);)*
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)*
})*
pub trait Server: Types $(+ $name)* {
@ -130,18 +118,23 @@ struct Dispatcher<S: Types> {
}
macro_rules! define_dispatcher_impl {
($($name:ident {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
}),* $(,)?) => {
(
FreeFunctions {
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
},
$($name:ident { $($x:tt)* }),* $(,)?
) => {
// 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 {
@ -149,11 +142,11 @@ macro_rules! define_dispatcher_impl {
let mut reader = &buf[..];
match api_tags::Method::decode(&mut reader, &mut ()) {
$(api_tags::Method::$name(m) => match m {
$(api_tags::$name::$method => {
api_tags::Method::FreeFunctions(m) => match m {
$(api_tags::FreeFunctions::$method => {
let mut call_method = || {
$(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)*
$name::$method(server, $($arg),*)
FreeFunctions::$method(server, $($arg),*)
};
// HACK(eddyb) don't use `panic::catch_unwind` in a panic.
// If client and server happen to use the same `std`,
@ -169,7 +162,7 @@ macro_rules! define_dispatcher_impl {
buf.clear();
r.encode(&mut buf, handle_store);
})*
}),*
}
}
buf
}

View file

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

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| h.is_empty()).unwrap_or(true)
self.0.as_ref().map(|h| bridge::client::FreeFunctions::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::TokenStream::expand_expr(stream) {
match bridge::client::FreeFunctions::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::TokenStream::from_str(src))))
Ok(TokenStream(Some(bridge::client::FreeFunctions::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, "{}", ts.to_string()),
Some(ts) => write!(f, "{}", bridge::client::FreeFunctions::tt_to_string(ts)),
None => Ok(()),
}
}
@ -252,7 +252,9 @@ 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::TokenStream::from_token_tree(tree_to_bridge_tree(tree))))
TokenStream(Some(bridge::client::FreeFunctions::tt_from_token_tree(tree_to_bridge_tree(
tree,
))))
}
}
@ -281,7 +283,7 @@ impl ConcatTreesHelper {
if self.trees.is_empty() {
TokenStream(None)
} else {
TokenStream(Some(bridge::client::TokenStream::concat_trees(None, self.trees)))
TokenStream(Some(bridge::client::FreeFunctions::tt_concat_trees(None, self.trees)))
}
}
@ -289,7 +291,7 @@ impl ConcatTreesHelper {
if self.trees.is_empty() {
return;
}
stream.0 = Some(bridge::client::TokenStream::concat_trees(stream.0.take(), self.trees))
stream.0 = Some(bridge::client::FreeFunctions::tt_concat_trees(stream.0.take(), self.trees))
}
}
@ -314,7 +316,7 @@ impl ConcatStreamsHelper {
if self.streams.len() <= 1 {
TokenStream(self.streams.pop())
} else {
TokenStream(Some(bridge::client::TokenStream::concat_streams(None, self.streams)))
TokenStream(Some(bridge::client::FreeFunctions::tt_concat_streams(None, self.streams)))
}
}
@ -326,7 +328,7 @@ impl ConcatStreamsHelper {
if base.is_none() && self.streams.len() == 1 {
stream.0 = self.streams.pop();
} else {
stream.0 = Some(bridge::client::TokenStream::concat_streams(base, self.streams));
stream.0 = Some(bridge::client::FreeFunctions::tt_concat_streams(base, self.streams));
}
}
}
@ -437,7 +439,12 @@ pub mod token_stream {
type IntoIter = IntoIter;
fn into_iter(self) -> IntoIter {
IntoIter(self.0.map(|v| v.into_trees()).unwrap_or_default().into_iter())
IntoIter(
self.0
.map(|v| bridge::client::FreeFunctions::tt_into_trees(v))
.unwrap_or_default()
.into_iter(),
)
}
}
}
@ -509,7 +516,7 @@ impl Span {
/// `self` was generated from, if any.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn parent(&self) -> Option<Span> {
self.0.parent().map(Span)
bridge::client::FreeFunctions::span_parent(self.0).map(Span)
}
/// The span for the origin source code that `self` was generated from. If
@ -517,25 +524,25 @@ impl Span {
/// value is the same as `*self`.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn source(&self) -> Span {
Span(self.0.source())
Span(bridge::client::FreeFunctions::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> {
self.0.byte_range()
bridge::client::FreeFunctions::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(self.0.start())
Span(bridge::client::FreeFunctions::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(self.0.end())
Span(bridge::client::FreeFunctions::span_end(self.0))
}
/// The one-indexed line of the source file where the span starts.
@ -543,7 +550,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 {
self.0.line()
bridge::client::FreeFunctions::span_line(self.0)
}
/// The one-indexed column of the source file where the span starts.
@ -551,7 +558,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 {
self.0.column()
bridge::client::FreeFunctions::span_column(self.0)
}
/// The path to the source file in which this span occurs, for display purposes.
@ -560,7 +567,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 {
self.0.file()
bridge::client::FreeFunctions::span_file(self.0)
}
/// The path to the source file in which this span occurs on the local file system.
@ -570,7 +577,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> {
self.0.local_file().map(PathBuf::from)
bridge::client::FreeFunctions::span_local_file(self.0).map(PathBuf::from)
}
/// Creates a new span encompassing `self` and `other`.
@ -578,14 +585,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> {
self.0.join(other.0).map(Span)
bridge::client::FreeFunctions::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(self.0.resolved_at(other.0))
Span(bridge::client::FreeFunctions::span_resolved_at(self.0, other.0))
}
/// Creates a new span with the same name resolution behavior as `self` but
@ -610,21 +617,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> {
self.0.source_text()
bridge::client::FreeFunctions::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 {
self.0.save_span()
bridge::client::FreeFunctions::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::Span::recover_proc_macro_span(id))
Span(bridge::client::FreeFunctions::span_recover_proc_macro_span(id))
}
diagnostic_method!(error, Level::Error);
@ -1389,7 +1396,12 @@ 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> {
self.0.span.subspan(range.start_bound().cloned(), range.end_bound().cloned()).map(Span)
bridge::client::FreeFunctions::span_subspan(
self.0.span,
range.start_bound().cloned(),
range.end_bound().cloned(),
)
.map(Span)
}
fn with_symbol_and_suffix<R>(&self, f: impl FnOnce(&str, &str) -> R) -> R {

View file

@ -58,13 +58,19 @@ impl server::FreeFunctions for RaSpanServer<'_> {
fn emit_diagnostic(&mut self, _: Diagnostic<Self::Span>) {
// FIXME handle diagnostic
}
}
impl server::TokenStream for RaSpanServer<'_> {
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
fn tt_drop(&mut self, stream: Self::TokenStream) {
drop(stream);
}
fn tt_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
stream.clone()
}
fn tt_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
fn tt_from_str(&mut self, src: &str) -> Self::TokenStream {
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
Self::TokenStream::from_str(
&format!("compile_error!(\"failed to parse str to token stream: {e}\")"),
@ -73,15 +79,15 @@ impl server::TokenStream for RaSpanServer<'_> {
.unwrap()
})
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
fn tt_to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
}
fn from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
fn tt_from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
Self::TokenStream::new(vec![tree])
}
fn expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
fn tt_expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
// FIXME: requires db, more importantly this requires name resolution so we would need to
// eagerly expand this proc-macro, but we can't know that this proc-macro is eager until we
// expand it ...
@ -90,7 +96,7 @@ impl server::TokenStream for RaSpanServer<'_> {
Ok(self_.clone())
}
fn concat_trees(
fn tt_concat_trees(
&mut self,
base: Option<Self::TokenStream>,
trees: Vec<TokenTree<Self::Span>>,
@ -106,7 +112,7 @@ impl server::TokenStream for RaSpanServer<'_> {
}
}
fn concat_streams(
fn tt_concat_streams(
&mut self,
base: Option<Self::TokenStream>,
streams: Vec<Self::TokenStream>,
@ -118,28 +124,26 @@ impl server::TokenStream for RaSpanServer<'_> {
stream
}
fn into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
fn tt_into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
(*stream.0).clone()
}
}
impl server::Span for RaSpanServer<'_> {
fn debug(&mut self, span: Self::Span) -> String {
fn span_debug(&mut self, span: Self::Span) -> String {
format!("{:?}", span)
}
fn file(&mut self, span: Self::Span) -> String {
fn span_file(&mut self, span: Self::Span) -> String {
self.callback.as_mut().map(|cb| cb.file(span.anchor.file_id.file_id())).unwrap_or_default()
}
fn local_file(&mut self, span: Self::Span) -> Option<String> {
fn span_local_file(&mut self, span: Self::Span) -> Option<String> {
self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id()))
}
fn save_span(&mut self, _span: Self::Span) -> usize {
fn span_save_span(&mut self, _span: Self::Span) -> usize {
// FIXME, quote is incompatible with third-party tools
// This is called by the quote proc-macro which is expanded when the proc-macro is compiled
// As such, r-a will never observe this
0
}
fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
fn span_recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
// FIXME, quote is incompatible with third-party tools
// This is called by the expansion of quote!, r-a will observe this, but we don't have
// access to the spans that were encoded
@ -149,23 +153,23 @@ impl server::Span for RaSpanServer<'_> {
///
/// See PR:
/// https://github.com/rust-lang/rust/pull/55780
fn source_text(&mut self, span: Self::Span) -> Option<String> {
fn span_source_text(&mut self, span: Self::Span) -> Option<String> {
self.callback.as_mut()?.source_text(span)
}
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
fn span_parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
// FIXME requires db, looks up the parent call site
None
}
fn source(&mut self, span: Self::Span) -> Self::Span {
fn span_source(&mut self, span: Self::Span) -> Self::Span {
// FIXME requires db, returns the top level call site
span
}
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
fn span_byte_range(&mut self, span: Self::Span) -> Range<usize> {
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
Range { start: span.range.start().into(), end: span.range.end().into() }
}
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
fn span_join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
// We can't modify the span range for fixup spans, those are meaningful to fixup, so just
// prefer the non-fixup span.
if first.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
@ -193,7 +197,7 @@ impl server::Span for RaSpanServer<'_> {
ctx: second.ctx,
})
}
fn subspan(
fn span_subspan(
&mut self,
span: Self::Span,
start: Bound<usize>,
@ -237,11 +241,11 @@ impl server::Span for RaSpanServer<'_> {
})
}
fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
fn span_resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
Span { ctx: at.ctx, ..span }
}
fn end(&mut self, span: Self::Span) -> Self::Span {
fn span_end(&mut self, span: Self::Span) -> Self::Span {
// We can't modify the span range for fixup spans, those are meaningful to fixup.
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return span;
@ -249,7 +253,7 @@ impl server::Span for RaSpanServer<'_> {
Span { range: TextRange::empty(span.range.end()), ..span }
}
fn start(&mut self, span: Self::Span) -> Self::Span {
fn span_start(&mut self, span: Self::Span) -> Self::Span {
// We can't modify the span range for fixup spans, those are meaningful to fixup.
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return span;
@ -257,24 +261,28 @@ impl server::Span for RaSpanServer<'_> {
Span { range: TextRange::empty(span.range.start()), ..span }
}
fn line(&mut self, _span: Self::Span) -> usize {
fn span_line(&mut self, _span: Self::Span) -> usize {
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
1
}
fn column(&mut self, _span: Self::Span) -> usize {
fn span_column(&mut self, _span: Self::Span) -> usize {
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
1
}
}
impl server::Symbol for RaSpanServer<'_> {
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
// FIXME: nfc-normalize and validate idents
Ok(<Self as server::Server>::intern_symbol(string))
}
}
impl server::TokenStream for RaSpanServer<'_> {}
impl server::Span for RaSpanServer<'_> {}
impl server::Symbol for RaSpanServer<'_> {}
impl server::Server for RaSpanServer<'_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {

View file

@ -61,13 +61,19 @@ impl server::FreeFunctions for SpanIdServer<'_> {
}
fn emit_diagnostic(&mut self, _: Diagnostic<Self::Span>) {}
}
impl server::TokenStream for SpanIdServer<'_> {
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
fn tt_drop(&mut self, stream: Self::TokenStream) {
drop(stream);
}
fn tt_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
stream.clone()
}
fn tt_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
fn tt_from_str(&mut self, src: &str) -> Self::TokenStream {
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
Self::TokenStream::from_str(
&format!("compile_error!(\"failed to parse str to token stream: {e}\")"),
@ -76,18 +82,18 @@ impl server::TokenStream for SpanIdServer<'_> {
.unwrap()
})
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
fn tt_to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
}
fn from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
fn tt_from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
Self::TokenStream::new(vec![tree])
}
fn expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
fn tt_expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
Ok(self_.clone())
}
fn concat_trees(
fn tt_concat_trees(
&mut self,
base: Option<Self::TokenStream>,
trees: Vec<TokenTree<Self::Span>>,
@ -103,7 +109,7 @@ impl server::TokenStream for SpanIdServer<'_> {
}
}
fn concat_streams(
fn tt_concat_streams(
&mut self,
base: Option<Self::TokenStream>,
streams: Vec<Self::TokenStream>,
@ -115,49 +121,47 @@ impl server::TokenStream for SpanIdServer<'_> {
stream
}
fn into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
fn tt_into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
(*stream.0).clone()
}
}
impl server::Span for SpanIdServer<'_> {
fn debug(&mut self, span: Self::Span) -> String {
fn span_debug(&mut self, span: Self::Span) -> String {
format!("{:?}", span.0)
}
fn file(&mut self, _span: Self::Span) -> String {
fn span_file(&mut self, _span: Self::Span) -> String {
String::new()
}
fn local_file(&mut self, _span: Self::Span) -> Option<String> {
fn span_local_file(&mut self, _span: Self::Span) -> Option<String> {
None
}
fn save_span(&mut self, _span: Self::Span) -> usize {
fn span_save_span(&mut self, _span: Self::Span) -> usize {
0
}
fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
fn span_recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
self.call_site
}
/// Recent feature, not yet in the proc_macro
///
/// See PR:
/// https://github.com/rust-lang/rust/pull/55780
fn source_text(&mut self, _span: Self::Span) -> Option<String> {
fn span_source_text(&mut self, _span: Self::Span) -> Option<String> {
None
}
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
fn span_parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
None
}
fn source(&mut self, span: Self::Span) -> Self::Span {
fn span_source(&mut self, span: Self::Span) -> Self::Span {
span
}
fn byte_range(&mut self, _span: Self::Span) -> Range<usize> {
fn span_byte_range(&mut self, _span: Self::Span) -> Range<usize> {
Range { start: 0, end: 0 }
}
fn join(&mut self, first: Self::Span, _second: Self::Span) -> Option<Self::Span> {
fn span_join(&mut self, first: Self::Span, _second: Self::Span) -> Option<Self::Span> {
// Just return the first span again, because some macros will unwrap the result.
Some(first)
}
fn subspan(
fn span_subspan(
&mut self,
span: Self::Span,
_start: Bound<usize>,
@ -166,34 +170,38 @@ impl server::Span for SpanIdServer<'_> {
// Just return the span again, because some macros will unwrap the result.
Some(span)
}
fn resolved_at(&mut self, _span: Self::Span, _at: Self::Span) -> Self::Span {
fn span_resolved_at(&mut self, _span: Self::Span, _at: Self::Span) -> Self::Span {
self.call_site
}
fn end(&mut self, _self_: Self::Span) -> Self::Span {
fn span_end(&mut self, _self_: Self::Span) -> Self::Span {
self.call_site
}
fn start(&mut self, _self_: Self::Span) -> Self::Span {
fn span_start(&mut self, _self_: Self::Span) -> Self::Span {
self.call_site
}
fn line(&mut self, _span: Self::Span) -> usize {
fn span_line(&mut self, _span: Self::Span) -> usize {
1
}
fn column(&mut self, _span: Self::Span) -> usize {
fn span_column(&mut self, _span: Self::Span) -> usize {
1
}
}
impl server::Symbol for SpanIdServer<'_> {
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
// FIXME: nfc-normalize and validate idents
Ok(<Self as server::Server>::intern_symbol(string))
}
}
impl server::TokenStream for SpanIdServer<'_> {}
impl server::Span for SpanIdServer<'_> {}
impl server::Symbol for SpanIdServer<'_> {}
impl server::Server for SpanIdServer<'_> {
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
ExpnGlobals {