Use stable AST IDs

Instead of simple numbering, we hash important bits, like the name of the item.

This will allow for much better incrementality, e.g. when you add an item. Currently, this invalidates the IDs of all following items, which invalidates pretty much everything.
This commit is contained in:
Chayim Refael Friedman 2025-05-21 00:04:59 +03:00
parent 5b2c8bc9ae
commit 4bcf03e28b
22 changed files with 1220 additions and 546 deletions

View file

@ -3,6 +3,7 @@
mod version;
use proc_macro::bridge;
use span::ErasedFileAstId;
use std::{fmt, fs, io, time::SystemTime};
use libloading::Library;
@ -161,14 +162,20 @@ 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);
let result = self.inner.proc_macros.expand(
macro_name,
macro_body,
attributes,
def_site,
call_site,
mixed_site,
fixup_ast_id,
);
result.map_err(|e| e.into_string().unwrap_or_default())
}

View file

@ -41,7 +41,7 @@ use std::{
};
use paths::{Utf8Path, Utf8PathBuf};
use span::{Span, TokenId};
use span::{ErasedFileAstId, FIXUP_ERASED_FILE_AST_ID_MARKER, Span, TokenId};
use crate::server_impl::TokenStream;
@ -57,11 +57,16 @@ 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 }
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);
}
}
@ -101,6 +106,7 @@ impl ProcMacroSrv<'_> {
def_site,
call_site,
mixed_site,
self.fixup_ast_id,
)
.map(|tt| tt.0)
});
@ -156,25 +162,41 @@ 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) -> Self::Server;
fn make_server(
call_site: Self,
def_site: Self,
mixed_site: Self,
fixup_ast_id: ErasedFileAstId,
) -> 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) -> Self::Server {
fn make_server(
call_site: Self,
def_site: Self,
mixed_site: Self,
_fixup_ast_id: ErasedFileAstId,
) -> 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) -> Self::Server {
fn make_server(
call_site: Self,
def_site: Self,
mixed_site: Self,
fixup_ast_id: ErasedFileAstId,
) -> 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,6 +1,7 @@
//! Proc macro ABI
use proc_macro::bridge;
use span::ErasedFileAstId;
use crate::{ProcMacroKind, ProcMacroSrvSpan, server_impl::TopSubtree};
@ -22,6 +23,7 @@ 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);
@ -37,7 +39,7 @@ impl ProcMacros {
{
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site),
S::make_server(call_site, def_site, mixed_site, fixup_ast_id),
parsed_body,
cfg!(debug_assertions),
);
@ -48,7 +50,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),
S::make_server(call_site, def_site, mixed_site, fixup_ast_id),
parsed_body,
cfg!(debug_assertions),
);
@ -59,7 +61,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),
S::make_server(call_site, def_site, mixed_site, fixup_ast_id),
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::{FIXUP_ERASED_FILE_AST_ID_MARKER, Span};
use span::{ErasedFileAstId, Span};
use tt::{TextRange, TextSize};
use crate::server_impl::{from_token_tree, literal_from_str, token_stream::TokenStreamBuilder};
@ -28,6 +28,7 @@ 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 {
@ -181,10 +182,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 == FIXUP_ERASED_FILE_AST_ID_MARKER {
if first.anchor.ast_id == self.fixup_ast_id {
return Some(second);
}
if second.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
if second.anchor.ast_id == self.fixup_ast_id {
return Some(first);
}
// FIXME: Once we can talk back to the client, implement a "long join" request for anchors
@ -213,7 +214,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 == FIXUP_ERASED_FILE_AST_ID_MARKER {
if span.anchor.ast_id == self.fixup_ast_id {
return Some(span);
}
let length = span.range.len().into();
@ -256,7 +257,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 == FIXUP_ERASED_FILE_AST_ID_MARKER {
if span.anchor.ast_id == self.fixup_ast_id {
return span;
}
Span { range: TextRange::empty(span.range.end()), ..span }
@ -264,7 +265,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 == FIXUP_ERASED_FILE_AST_ID_MARKER {
if span.anchor.ast_id == self.fixup_ast_id {
return span;
}
Span { range: TextRange::empty(span.range.start()), ..span }
@ -318,7 +319,7 @@ mod tests {
range: TextRange::empty(TextSize::new(0)),
anchor: span::SpanAnchor {
file_id: EditionedFileId::current_edition(FileId::from_raw(0)),
ast_id: span::ErasedFileAstId::from_raw(0),
ast_id: span::ROOT_ERASED_FILE_AST_ID,
},
ctx: SyntaxContext::root(span::Edition::CURRENT),
};
@ -360,7 +361,7 @@ mod tests {
range: TextRange::empty(TextSize::new(0)),
anchor: span::SpanAnchor {
file_id: EditionedFileId::current_edition(FileId::from_raw(0)),
ast_id: span::ErasedFileAstId::from_raw(0),
ast_id: span::ROOT_ERASED_FILE_AST_ID,
},
ctx: SyntaxContext::root(span::Edition::CURRENT),
};

View file

@ -21,14 +21,14 @@ fn test_derive_empty() {
SUBTREE $$ 1 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT struct 42:2@0..6#ROOT2024
IDENT S 42:2@7..8#ROOT2024
PUNCH ; [alone] 42:2@8..9#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT struct 42:Root[0000, 0]@0..6#ROOT2024
IDENT S 42:Root[0000, 0]@7..8#ROOT2024
PUNCH ; [alone] 42:Root[0000, 0]@8..9#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024"#]],
);
}
@ -52,19 +52,19 @@ fn test_derive_error() {
LITERAL Str #[derive(DeriveError)] struct S ; 1
PUNCH ; [alone] 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT struct 42:2@0..6#ROOT2024
IDENT S 42:2@7..8#ROOT2024
PUNCH ; [alone] 42:2@8..9#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT struct 42:Root[0000, 0]@0..6#ROOT2024
IDENT S 42:Root[0000, 0]@7..8#ROOT2024
PUNCH ; [alone] 42:Root[0000, 0]@8..9#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT compile_error 42:2@0..100#ROOT2024
PUNCH ! [alone] 42:2@0..100#ROOT2024
SUBTREE () 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
LITERAL Str #[derive(DeriveError)] struct S ; 42:2@0..100#ROOT2024
PUNCH ; [alone] 42:2@0..100#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT compile_error 42:Root[0000, 0]@0..100#ROOT2024
PUNCH ! [alone] 42:Root[0000, 0]@0..100#ROOT2024
SUBTREE () 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Str #[derive(DeriveError)] struct S ; 42:Root[0000, 0]@0..100#ROOT2024
PUNCH ; [alone] 42:Root[0000, 0]@0..100#ROOT2024"#]],
);
}
@ -94,25 +94,25 @@ fn test_fn_like_macro_noop() {
PUNCH , [alone] 1
SUBTREE [] 1 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT ident 42:2@0..5#ROOT2024
PUNCH , [alone] 42:2@5..6#ROOT2024
LITERAL Integer 0 42:2@7..8#ROOT2024
PUNCH , [alone] 42:2@8..9#ROOT2024
LITERAL Integer 1 42:2@10..11#ROOT2024
PUNCH , [alone] 42:2@11..12#ROOT2024
SUBTREE [] 42:2@13..14#ROOT2024 42:2@14..15#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT ident 42:Root[0000, 0]@0..5#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@5..6#ROOT2024
LITERAL Integer 0 42:Root[0000, 0]@7..8#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@8..9#ROOT2024
LITERAL Integer 1 42:Root[0000, 0]@10..11#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@11..12#ROOT2024
SUBTREE [] 42:Root[0000, 0]@13..14#ROOT2024 42:Root[0000, 0]@14..15#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT ident 42:2@0..5#ROOT2024
PUNCH , [alone] 42:2@5..6#ROOT2024
LITERAL Integer 0 42:2@7..8#ROOT2024
PUNCH , [alone] 42:2@8..9#ROOT2024
LITERAL Integer 1 42:2@10..11#ROOT2024
PUNCH , [alone] 42:2@11..12#ROOT2024
SUBTREE [] 42:2@13..14#ROOT2024 42:2@14..15#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT ident 42:Root[0000, 0]@0..5#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@5..6#ROOT2024
LITERAL Integer 0 42:Root[0000, 0]@7..8#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@8..9#ROOT2024
LITERAL Integer 1 42:Root[0000, 0]@10..11#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@11..12#ROOT2024
SUBTREE [] 42:Root[0000, 0]@13..14#ROOT2024 42:Root[0000, 0]@14..15#ROOT2024"#]],
);
}
@ -134,17 +134,17 @@ fn test_fn_like_macro_clone_ident_subtree() {
PUNCH , [alone] 1
SUBTREE [] 1 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT ident 42:2@0..5#ROOT2024
PUNCH , [alone] 42:2@5..6#ROOT2024
SUBTREE [] 42:2@7..8#ROOT2024 42:2@8..9#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT ident 42:Root[0000, 0]@0..5#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@5..6#ROOT2024
SUBTREE [] 42:Root[0000, 0]@7..8#ROOT2024 42:Root[0000, 0]@8..9#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT ident 42:2@0..5#ROOT2024
PUNCH , [alone] 42:2@5..6#ROOT2024
SUBTREE [] 42:2@7..9#ROOT2024 42:2@7..9#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT ident 42:Root[0000, 0]@0..5#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@5..6#ROOT2024
SUBTREE [] 42:Root[0000, 0]@7..9#ROOT2024 42:Root[0000, 0]@7..9#ROOT2024"#]],
);
}
@ -162,13 +162,13 @@ fn test_fn_like_macro_clone_raw_ident() {
SUBTREE $$ 1 1
IDENT r#async 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT r#async 42:2@0..7#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT r#async 42:Root[0000, 0]@0..7#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT r#async 42:2@0..7#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT r#async 42:Root[0000, 0]@0..7#ROOT2024"#]],
);
}
@ -187,14 +187,14 @@ fn test_fn_like_fn_like_span_join() {
SUBTREE $$ 1 1
IDENT r#joined 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT foo 42:2@0..3#ROOT2024
IDENT bar 42:2@8..11#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT foo 42:Root[0000, 0]@0..3#ROOT2024
IDENT bar 42:Root[0000, 0]@8..11#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT r#joined 42:2@0..11#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT r#joined 42:Root[0000, 0]@0..11#ROOT2024"#]],
);
}
@ -216,17 +216,17 @@ fn test_fn_like_fn_like_span_ops() {
IDENT resolved_at_def_site 1
IDENT start_span 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT set_def_site 42:2@0..12#ROOT2024
IDENT resolved_at_def_site 42:2@13..33#ROOT2024
IDENT start_span 42:2@34..44#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT set_def_site 42:Root[0000, 0]@0..12#ROOT2024
IDENT resolved_at_def_site 42:Root[0000, 0]@13..33#ROOT2024
IDENT start_span 42:Root[0000, 0]@34..44#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT set_def_site 41:1@0..150#ROOT2024
IDENT resolved_at_def_site 42:2@13..33#ROOT2024
IDENT start_span 42:2@34..34#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT set_def_site 41:Root[0000, 0]@0..150#ROOT2024
IDENT resolved_at_def_site 42:Root[0000, 0]@13..33#ROOT2024
IDENT start_span 42:Root[0000, 0]@34..34#ROOT2024"#]],
);
}
@ -259,28 +259,28 @@ fn test_fn_like_mk_literals() {
PUNCH - [alone] 1
LITERAL Integer 123 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
LITERAL ByteStr byte_string 42:2@0..100#ROOT2024
LITERAL Char c 42:2@0..100#ROOT2024
LITERAL Str string 42:2@0..100#ROOT2024
LITERAL Str -string 42:2@0..100#ROOT2024
LITERAL CStr cstring 42:2@0..100#ROOT2024
LITERAL Float 3.14f64 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..100#ROOT2024
LITERAL Float 3.14f64 42:2@0..100#ROOT2024
LITERAL Float 3.14 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..100#ROOT2024
LITERAL Float 3.14 42:2@0..100#ROOT2024
LITERAL Integer 123i64 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..100#ROOT2024
LITERAL Integer 123i64 42:2@0..100#ROOT2024
LITERAL Integer 123 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..100#ROOT2024
LITERAL Integer 123 42:2@0..100#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
LITERAL ByteStr byte_string 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Char c 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Str string 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Str -string 42:Root[0000, 0]@0..100#ROOT2024
LITERAL CStr cstring 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Float 3.14f64 42:Root[0000, 0]@0..100#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Float 3.14f64 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Float 3.14 42:Root[0000, 0]@0..100#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Float 3.14 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Integer 123i64 42:Root[0000, 0]@0..100#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Integer 123i64 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Integer 123 42:Root[0000, 0]@0..100#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Integer 123 42:Root[0000, 0]@0..100#ROOT2024"#]],
);
}
@ -298,13 +298,13 @@ fn test_fn_like_mk_idents() {
IDENT standard 1
IDENT r#raw 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT standard 42:2@0..100#ROOT2024
IDENT r#raw 42:2@0..100#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT standard 42:Root[0000, 0]@0..100#ROOT2024
IDENT r#raw 42:Root[0000, 0]@0..100#ROOT2024"#]],
);
}
@ -360,51 +360,51 @@ fn test_fn_like_macro_clone_literals() {
PUNCH , [alone] 1
LITERAL CStr null 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
LITERAL Integer 1u16 42:2@0..4#ROOT2024
PUNCH , [alone] 42:2@4..5#ROOT2024
LITERAL Integer 2_u32 42:2@6..11#ROOT2024
PUNCH , [alone] 42:2@11..12#ROOT2024
PUNCH - [alone] 42:2@13..14#ROOT2024
LITERAL Integer 4i64 42:2@14..18#ROOT2024
PUNCH , [alone] 42:2@18..19#ROOT2024
LITERAL Float 3.14f32 42:2@20..27#ROOT2024
PUNCH , [alone] 42:2@27..28#ROOT2024
LITERAL Str hello bridge 42:2@29..43#ROOT2024
PUNCH , [alone] 42:2@43..44#ROOT2024
LITERAL Str suffixedsuffix 42:2@45..61#ROOT2024
PUNCH , [alone] 42:2@61..62#ROOT2024
LITERAL StrRaw(2) raw 42:2@63..73#ROOT2024
PUNCH , [alone] 42:2@73..74#ROOT2024
LITERAL Char a 42:2@75..78#ROOT2024
PUNCH , [alone] 42:2@78..79#ROOT2024
LITERAL Byte b 42:2@80..84#ROOT2024
PUNCH , [alone] 42:2@84..85#ROOT2024
LITERAL CStr null 42:2@86..93#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Integer 1u16 42:Root[0000, 0]@0..4#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@4..5#ROOT2024
LITERAL Integer 2_u32 42:Root[0000, 0]@6..11#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@11..12#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@13..14#ROOT2024
LITERAL Integer 4i64 42:Root[0000, 0]@14..18#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@18..19#ROOT2024
LITERAL Float 3.14f32 42:Root[0000, 0]@20..27#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@27..28#ROOT2024
LITERAL Str hello bridge 42:Root[0000, 0]@29..43#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@43..44#ROOT2024
LITERAL Str suffixedsuffix 42:Root[0000, 0]@45..61#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@61..62#ROOT2024
LITERAL StrRaw(2) raw 42:Root[0000, 0]@63..73#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@73..74#ROOT2024
LITERAL Char a 42:Root[0000, 0]@75..78#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@78..79#ROOT2024
LITERAL Byte b 42:Root[0000, 0]@80..84#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@84..85#ROOT2024
LITERAL CStr null 42:Root[0000, 0]@86..93#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
LITERAL Integer 1u16 42:2@0..4#ROOT2024
PUNCH , [alone] 42:2@4..5#ROOT2024
LITERAL Integer 2_u32 42:2@6..11#ROOT2024
PUNCH , [alone] 42:2@11..12#ROOT2024
PUNCH - [alone] 42:2@13..14#ROOT2024
LITERAL Integer 4i64 42:2@14..18#ROOT2024
PUNCH , [alone] 42:2@18..19#ROOT2024
LITERAL Float 3.14f32 42:2@20..27#ROOT2024
PUNCH , [alone] 42:2@27..28#ROOT2024
LITERAL Str hello bridge 42:2@29..43#ROOT2024
PUNCH , [alone] 42:2@43..44#ROOT2024
LITERAL Str suffixedsuffix 42:2@45..61#ROOT2024
PUNCH , [alone] 42:2@61..62#ROOT2024
LITERAL StrRaw(2) raw 42:2@63..73#ROOT2024
PUNCH , [alone] 42:2@73..74#ROOT2024
LITERAL Char a 42:2@75..78#ROOT2024
PUNCH , [alone] 42:2@78..79#ROOT2024
LITERAL Byte b 42:2@80..84#ROOT2024
PUNCH , [alone] 42:2@84..85#ROOT2024
LITERAL CStr null 42:2@86..93#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Integer 1u16 42:Root[0000, 0]@0..4#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@4..5#ROOT2024
LITERAL Integer 2_u32 42:Root[0000, 0]@6..11#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@11..12#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@13..14#ROOT2024
LITERAL Integer 4i64 42:Root[0000, 0]@14..18#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@18..19#ROOT2024
LITERAL Float 3.14f32 42:Root[0000, 0]@20..27#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@27..28#ROOT2024
LITERAL Str hello bridge 42:Root[0000, 0]@29..43#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@43..44#ROOT2024
LITERAL Str suffixedsuffix 42:Root[0000, 0]@45..61#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@61..62#ROOT2024
LITERAL StrRaw(2) raw 42:Root[0000, 0]@63..73#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@73..74#ROOT2024
LITERAL Char a 42:Root[0000, 0]@75..78#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@78..79#ROOT2024
LITERAL Byte b 42:Root[0000, 0]@80..84#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@84..85#ROOT2024
LITERAL CStr null 42:Root[0000, 0]@86..93#ROOT2024"#]],
);
}
@ -442,33 +442,33 @@ fn test_fn_like_macro_negative_literals() {
PUNCH - [alone] 1
LITERAL Float 2.7 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..1#ROOT2024
LITERAL Integer 1u16 42:2@1..5#ROOT2024
PUNCH , [alone] 42:2@5..6#ROOT2024
PUNCH - [alone] 42:2@7..8#ROOT2024
LITERAL Integer 2_u32 42:2@9..14#ROOT2024
PUNCH , [alone] 42:2@14..15#ROOT2024
PUNCH - [alone] 42:2@16..17#ROOT2024
LITERAL Float 3.14f32 42:2@17..24#ROOT2024
PUNCH , [alone] 42:2@24..25#ROOT2024
PUNCH - [alone] 42:2@26..27#ROOT2024
LITERAL Float 2.7 42:2@28..31#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@0..1#ROOT2024
LITERAL Integer 1u16 42:Root[0000, 0]@1..5#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@5..6#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@7..8#ROOT2024
LITERAL Integer 2_u32 42:Root[0000, 0]@9..14#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@14..15#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@16..17#ROOT2024
LITERAL Float 3.14f32 42:Root[0000, 0]@17..24#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@24..25#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@26..27#ROOT2024
LITERAL Float 2.7 42:Root[0000, 0]@28..31#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..1#ROOT2024
LITERAL Integer 1u16 42:2@1..5#ROOT2024
PUNCH , [alone] 42:2@5..6#ROOT2024
PUNCH - [alone] 42:2@7..8#ROOT2024
LITERAL Integer 2_u32 42:2@9..14#ROOT2024
PUNCH , [alone] 42:2@14..15#ROOT2024
PUNCH - [alone] 42:2@16..17#ROOT2024
LITERAL Float 3.14f32 42:2@17..24#ROOT2024
PUNCH , [alone] 42:2@24..25#ROOT2024
PUNCH - [alone] 42:2@26..27#ROOT2024
LITERAL Float 2.7 42:2@28..31#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@0..1#ROOT2024
LITERAL Integer 1u16 42:Root[0000, 0]@1..5#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@5..6#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@7..8#ROOT2024
LITERAL Integer 2_u32 42:Root[0000, 0]@9..14#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@14..15#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@16..17#ROOT2024
LITERAL Float 3.14f32 42:Root[0000, 0]@17..24#ROOT2024
PUNCH , [alone] 42:Root[0000, 0]@24..25#ROOT2024
PUNCH - [alone] 42:Root[0000, 0]@26..27#ROOT2024
LITERAL Float 2.7 42:Root[0000, 0]@28..31#ROOT2024"#]],
);
}
@ -498,21 +498,21 @@ fn test_attr_macro() {
LITERAL Str #[attr_error(some arguments)] mod m {} 1
PUNCH ; [alone] 1"#]],
expect![[r#"
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT mod 42:2@0..3#ROOT2024
IDENT m 42:2@4..5#ROOT2024
SUBTREE {} 42:2@6..7#ROOT2024 42:2@7..8#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT mod 42:Root[0000, 0]@0..3#ROOT2024
IDENT m 42:Root[0000, 0]@4..5#ROOT2024
SUBTREE {} 42:Root[0000, 0]@6..7#ROOT2024 42:Root[0000, 0]@7..8#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT some 42:2@0..4#ROOT2024
IDENT arguments 42:2@5..14#ROOT2024
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT some 42:Root[0000, 0]@0..4#ROOT2024
IDENT arguments 42:Root[0000, 0]@5..14#ROOT2024
SUBTREE $$ 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
IDENT compile_error 42:2@0..100#ROOT2024
PUNCH ! [alone] 42:2@0..100#ROOT2024
SUBTREE () 42:2@0..100#ROOT2024 42:2@0..100#ROOT2024
LITERAL Str #[attr_error(some arguments)] mod m {} 42:2@0..100#ROOT2024
PUNCH ; [alone] 42:2@0..100#ROOT2024"#]],
SUBTREE $$ 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
IDENT compile_error 42:Root[0000, 0]@0..100#ROOT2024
PUNCH ! [alone] 42:Root[0000, 0]@0..100#ROOT2024
SUBTREE () 42:Root[0000, 0]@0..100#ROOT2024 42:Root[0000, 0]@0..100#ROOT2024
LITERAL Str #[attr_error(some arguments)] mod m {} 42:Root[0000, 0]@0..100#ROOT2024
PUNCH ; [alone] 42:Root[0000, 0]@0..100#ROOT2024"#]],
);
}

View file

@ -1,7 +1,10 @@
//! utils used in proc-macro tests
use expect_test::Expect;
use span::{EditionedFileId, ErasedFileAstId, FileId, Span, SpanAnchor, SyntaxContext, TokenId};
use span::{
EditionedFileId, FIXUP_ERASED_FILE_AST_ID_MARKER, FileId, ROOT_ERASED_FILE_AST_ID, Span,
SpanAnchor, SyntaxContext, TokenId,
};
use tt::TextRange;
use crate::{EnvSnapshot, ProcMacroSrv, dylib, proc_macro_test_dylib_path};
@ -65,8 +68,17 @@ 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).unwrap();
let res = expander
.expand(
macro_name,
input_ts,
attr_ts,
def_site,
call_site,
mixed_site,
FIXUP_ERASED_FILE_AST_ID_MARKER,
)
.unwrap();
expect.assert_eq(&format!(
"{input_ts_string}\n\n{}\n\n{res:?}",
attr_ts_string.unwrap_or_default()
@ -76,7 +88,7 @@ fn assert_expand_impl(
range: TextRange::new(0.into(), 150.into()),
anchor: SpanAnchor {
file_id: EditionedFileId::current_edition(FileId::from_raw(41)),
ast_id: ErasedFileAstId::from_raw(1),
ast_id: ROOT_ERASED_FILE_AST_ID,
},
ctx: SyntaxContext::root(span::Edition::CURRENT),
};
@ -84,7 +96,7 @@ fn assert_expand_impl(
range: TextRange::new(0.into(), 100.into()),
anchor: SpanAnchor {
file_id: EditionedFileId::current_edition(FileId::from_raw(42)),
ast_id: ErasedFileAstId::from_raw(2),
ast_id: ROOT_ERASED_FILE_AST_ID,
},
ctx: SyntaxContext::root(span::Edition::CURRENT),
};
@ -98,7 +110,17 @@ 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).unwrap();
let res = expander
.expand(
macro_name,
fixture,
attr,
def_site,
call_site,
mixed_site,
FIXUP_ERASED_FILE_AST_ID_MARKER,
)
.unwrap();
expect_spanned
.assert_eq(&format!("{fixture_string}\n\n{}\n\n{res:#?}", attr_string.unwrap_or_default()));
}