Support spans with proc macro servers from before the ast id changes

The only thing changed is the value of the fixup ast id, so we just swap it.
This commit is contained in:
Chayim Refael Friedman 2025-06-12 15:32:08 +03:00
parent c15fc9a344
commit 3e834add61
10 changed files with 96 additions and 102 deletions

View file

@ -3,7 +3,6 @@
mod version;
use proc_macro::bridge;
use span::ErasedFileAstId;
use std::{fmt, fs, io, time::SystemTime};
use libloading::Library;
@ -162,20 +161,14 @@ impl Expander {
def_site: S,
call_site: S,
mixed_site: S,
fixup_ast_id: ErasedFileAstId,
) -> Result<TopSubtree<S>, String>
where
<S::Server as bridge::server::Types>::TokenStream: Default,
{
let result = self.inner.proc_macros.expand(
macro_name,
macro_body,
attributes,
def_site,
call_site,
mixed_site,
fixup_ast_id,
);
let result = self
.inner
.proc_macros
.expand(macro_name, macro_body, attributes, def_site, call_site, mixed_site);
result.map_err(|e| e.into_string().unwrap_or_default())
}

View file

@ -41,7 +41,7 @@ use std::{
};
use paths::{Utf8Path, Utf8PathBuf};
use span::{ErasedFileAstId, FIXUP_ERASED_FILE_AST_ID_MARKER, Span, TokenId};
use span::{Span, TokenId};
use crate::server_impl::TokenStream;
@ -57,16 +57,11 @@ pub const RUSTC_VERSION_STRING: &str = env!("RUSTC_VERSION");
pub struct ProcMacroSrv<'env> {
expanders: Mutex<HashMap<Utf8PathBuf, Arc<dylib::Expander>>>,
env: &'env EnvSnapshot,
fixup_ast_id: ErasedFileAstId,
}
impl<'env> ProcMacroSrv<'env> {
pub fn new(env: &'env EnvSnapshot) -> Self {
Self { expanders: Default::default(), env, fixup_ast_id: FIXUP_ERASED_FILE_AST_ID_MARKER }
}
pub fn set_fixup_ast_id(&mut self, fixup_ast_id: u32) {
self.fixup_ast_id = ErasedFileAstId::from_raw(fixup_ast_id);
Self { expanders: Default::default(), env }
}
}
@ -106,7 +101,6 @@ impl ProcMacroSrv<'_> {
def_site,
call_site,
mixed_site,
self.fixup_ast_id,
)
.map(|tt| tt.0)
});
@ -162,41 +156,25 @@ impl ProcMacroSrv<'_> {
pub trait ProcMacroSrvSpan: Copy + Send {
type Server: proc_macro::bridge::server::Server<TokenStream = TokenStream<Self>>;
fn make_server(
call_site: Self,
def_site: Self,
mixed_site: Self,
fixup_ast_id: ErasedFileAstId,
) -> Self::Server;
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server;
}
impl ProcMacroSrvSpan for TokenId {
type Server = server_impl::token_id::TokenIdServer;
fn make_server(
call_site: Self,
def_site: Self,
mixed_site: Self,
_fixup_ast_id: ErasedFileAstId,
) -> Self::Server {
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
Self::Server { call_site, def_site, mixed_site }
}
}
impl ProcMacroSrvSpan for Span {
type Server = server_impl::rust_analyzer_span::RaSpanServer;
fn make_server(
call_site: Self,
def_site: Self,
mixed_site: Self,
fixup_ast_id: ErasedFileAstId,
) -> Self::Server {
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
Self::Server {
call_site,
def_site,
mixed_site,
tracked_env_vars: Default::default(),
tracked_paths: Default::default(),
fixup_ast_id,
}
}
}

View file

@ -1,7 +1,6 @@
//! Proc macro ABI
use proc_macro::bridge;
use span::ErasedFileAstId;
use crate::{ProcMacroKind, ProcMacroSrvSpan, server_impl::TopSubtree};
@ -23,7 +22,6 @@ impl ProcMacros {
def_site: S,
call_site: S,
mixed_site: S,
fixup_ast_id: ErasedFileAstId,
) -> Result<TopSubtree<S>, crate::PanicMessage> {
let parsed_body = crate::server_impl::TokenStream::with_subtree(macro_body);
@ -39,7 +37,7 @@ impl ProcMacros {
{
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site, fixup_ast_id),
S::make_server(call_site, def_site, mixed_site),
parsed_body,
cfg!(debug_assertions),
);
@ -50,7 +48,7 @@ impl ProcMacros {
bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site, fixup_ast_id),
S::make_server(call_site, def_site, mixed_site),
parsed_body,
cfg!(debug_assertions),
);
@ -61,7 +59,7 @@ impl ProcMacros {
bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site, fixup_ast_id),
S::make_server(call_site, def_site, mixed_site),
parsed_attributes,
parsed_body,
cfg!(debug_assertions),

View file

@ -11,7 +11,7 @@ use std::{
use intern::Symbol;
use proc_macro::bridge::{self, server};
use span::{ErasedFileAstId, Span};
use span::{FIXUP_ERASED_FILE_AST_ID_MARKER, Span};
use tt::{TextRange, TextSize};
use crate::server_impl::{from_token_tree, literal_from_str, token_stream::TokenStreamBuilder};
@ -28,7 +28,6 @@ pub struct RaSpanServer {
pub call_site: Span,
pub def_site: Span,
pub mixed_site: Span,
pub fixup_ast_id: ErasedFileAstId,
}
impl server::Types for RaSpanServer {
@ -182,10 +181,10 @@ impl server::Span for RaSpanServer {
fn 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 == self.fixup_ast_id {
if first.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return Some(second);
}
if second.anchor.ast_id == self.fixup_ast_id {
if second.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return Some(first);
}
// FIXME: Once we can talk back to the client, implement a "long join" request for anchors
@ -214,7 +213,7 @@ impl server::Span for RaSpanServer {
end: Bound<usize>,
) -> Option<Self::Span> {
// We can't modify the span range for fixup spans, those are meaningful to fixup.
if span.anchor.ast_id == self.fixup_ast_id {
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return Some(span);
}
let length = span.range.len().into();
@ -257,7 +256,7 @@ impl server::Span for RaSpanServer {
fn 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 == self.fixup_ast_id {
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return span;
}
Span { range: TextRange::empty(span.range.end()), ..span }
@ -265,7 +264,7 @@ impl server::Span for RaSpanServer {
fn 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 == self.fixup_ast_id {
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return span;
}
Span { range: TextRange::empty(span.range.start()), ..span }

View file

@ -2,8 +2,7 @@
use expect_test::Expect;
use span::{
EditionedFileId, FIXUP_ERASED_FILE_AST_ID_MARKER, FileId, ROOT_ERASED_FILE_AST_ID, Span,
SpanAnchor, SyntaxContext, TokenId,
EditionedFileId, FileId, ROOT_ERASED_FILE_AST_ID, Span, SpanAnchor, SyntaxContext, TokenId,
};
use tt::TextRange;
@ -68,17 +67,8 @@ fn assert_expand_impl(
let input_ts_string = format!("{input_ts:?}");
let attr_ts_string = attr_ts.as_ref().map(|it| format!("{it:?}"));
let res = expander
.expand(
macro_name,
input_ts,
attr_ts,
def_site,
call_site,
mixed_site,
FIXUP_ERASED_FILE_AST_ID_MARKER,
)
.unwrap();
let res =
expander.expand(macro_name, input_ts, attr_ts, def_site, call_site, mixed_site).unwrap();
expect.assert_eq(&format!(
"{input_ts_string}\n\n{}\n\n{res:?}",
attr_ts_string.unwrap_or_default()
@ -110,17 +100,7 @@ fn assert_expand_impl(
let fixture_string = format!("{fixture:?}");
let attr_string = attr.as_ref().map(|it| format!("{it:?}"));
let res = expander
.expand(
macro_name,
fixture,
attr,
def_site,
call_site,
mixed_site,
FIXUP_ERASED_FILE_AST_ID_MARKER,
)
.unwrap();
let res = expander.expand(macro_name, fixture, attr, def_site, call_site, mixed_site).unwrap();
expect_spanned
.assert_eq(&format!("{fixture_string}\n\n{}\n\n{res:#?}", attr_string.unwrap_or_default()));
}