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

@ -2141,26 +2141,10 @@ impl ExprCollector<'_> {
block: ast::BlockExpr,
mk_block: impl FnOnce(Option<BlockId>, Box<[Statement]>, Option<ExprId>) -> Expr,
) -> ExprId {
let block_has_items = {
let statement_has_item = block.statements().any(|stmt| match stmt {
ast::Stmt::Item(_) => true,
// Macro calls can be both items and expressions. The syntax library always treats
// them as expressions here, so we undo that.
ast::Stmt::ExprStmt(es) => matches!(es.expr(), Some(ast::Expr::MacroExpr(_))),
_ => false,
});
statement_has_item
|| matches!(block.tail_expr(), Some(ast::Expr::MacroExpr(_)))
|| (block.may_carry_attributes() && block.attrs().next().is_some())
};
let block_id = if block_has_items {
let file_local_id = self.expander.ast_id_map().ast_id(&block);
let block_id = self.expander.ast_id_map().ast_id_for_block(&block).map(|file_local_id| {
let ast_id = self.expander.in_file(file_local_id);
Some(self.db.intern_block(BlockLoc { ast_id, module: self.module }))
} else {
None
};
self.db.intern_block(BlockLoc { ast_id, module: self.module })
});
let (module, def_map) =
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {

View file

@ -353,8 +353,8 @@ impl Printer<'_> {
let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it];
let _ = writeln!(
self,
"// AstId: {:?}, SyntaxContextId: {}, ExpandTo: {:?}",
ast_id.erase().into_raw(),
"// AstId: {:#?}, SyntaxContextId: {}, ExpandTo: {:?}",
ast_id.erase(),
ctxt,
expand_to
);
@ -377,7 +377,7 @@ impl Printer<'_> {
}
fn print_ast_id(&mut self, ast_id: ErasedFileAstId) {
wln!(self, "// AstId: {:?}", ast_id.into_raw());
wln!(self, "// AstId: {ast_id:#?}");
}
}

View file

@ -35,23 +35,23 @@ use a::{c, d::{e}};
#![no_std]
#![doc = " another file comment"]
// AstId: 1
// AstId: ExternCrate[5A82, 0]
pub(self) extern crate self as renamed;
// AstId: 2
// AstId: ExternCrate[7E1C, 0]
pub(super) extern crate bli;
// AstId: 3
// AstId: Use[0000, 0]
pub use crate::path::{nested, items as renamed, Trait as _};
// AstId: 4
// AstId: Use[0000, 1]
pub(self) use globs::*;
#[doc = " docs on import"]
// AstId: 5
// AstId: Use[0000, 2]
pub(self) use crate::{A, B};
// AstId: 6
// AstId: Use[0000, 3]
pub(self) use a::{c, d::{e}};
"##]],
);
@ -75,18 +75,18 @@ extern "C" {
"#,
expect![[r##"
#[on_extern_block]
// AstId: 1
// AstId: ExternBlock[0000, 0]
extern "C" {
#[on_extern_type]
// AstId: 2
// AstId: TypeAlias[9FDF, 0]
pub(self) type ExType;
#[on_extern_static]
// AstId: 3
// AstId: Static[43C1, 0]
pub(self) static EX_STATIC = _;
#[on_extern_fn]
// AstId: 4
// AstId: Fn[452D, 0]
pub(self) fn ex_fn;
}
"##]],
@ -124,39 +124,39 @@ enum E {
}
"#,
expect![[r#"
// AstId: 1
// AstId: Struct[DFF3, 0]
pub(self) struct Unit;
#[derive(Debug)]
// AstId: 2
// AstId: Struct[C7A1, 0]
pub(self) struct Struct {
#[doc = " fld docs"]
pub(self) fld,
}
// AstId: 3
// AstId: Struct[DAC2, 0]
pub(self) struct Tuple(
#[attr]
pub(self) 0,
);
// AstId: 4
// AstId: Union[2DBB, 0]
pub(self) union Ize {
pub(self) a,
pub(self) b,
}
// AstId: 5
// AstId: Enum[7FF8, 0]
pub(self) enum E
// AstId: 6
// AstId: Variant[C717, 0]
#[doc = " comment on Unit"]
Unit,
// AstId: 7
// AstId: Variant[AEAB, 0]
#[doc = " comment on Tuple"]
Tuple(
pub(self) 0,
),
// AstId: 8
// AstId: Variant[4B1B, 0]
Struct {
#[doc = " comment on a: u8"]
pub(self) a,
@ -185,23 +185,23 @@ trait Tr: SuperTrait + 'lifetime {
}
"#,
expect![[r#"
// AstId: 1
// AstId: Static[B393, 0]
pub static ST = _;
// AstId: 2
// AstId: Const[B309, 0]
pub(self) const _ = _;
#[attr]
#[inner_attr_in_fn]
// AstId: 3
// AstId: Fn[75E3, 0]
pub(self) fn f;
// AstId: 4
// AstId: Trait[2998, 0]
pub(self) trait Tr {
// AstId: 6
// AstId: TypeAlias[9F08, 0]
pub(self) type Assoc;
// AstId: 7
// AstId: Fn[6C0C, 0]
pub(self) fn method;
}
"#]],
@ -226,16 +226,16 @@ mod outline;
expect![[r##"
#[doc = " outer"]
#[doc = " inner"]
// AstId: 1
// AstId: Module[CF93, 0]
pub(self) mod inline {
// AstId: 3
// AstId: Use[0000, 0]
pub(self) use super::*;
// AstId: 4
// AstId: Fn[1B26, 0]
pub(self) fn fn_in_module;
}
// AstId: 2
// AstId: Module[8994, 0]
pub(self) mod outline;
"##]],
);
@ -254,13 +254,13 @@ pub macro m2() {}
m!();
"#,
expect![[r#"
// AstId: 1
// AstId: MacroRules[88CE, 0]
macro_rules! m { ... }
// AstId: 2
// AstId: MacroDef[DC34, 0]
pub macro m2 { ... }
// AstId: 3, SyntaxContextId: ROOT2024, ExpandTo: Items
// AstId: MacroCall[612F, 0], SyntaxContextId: ROOT2024, ExpandTo: Items
m!(...);
"#]],
);
@ -273,7 +273,7 @@ fn pub_self() {
pub(self) struct S;
"#,
expect![[r#"
// AstId: 1
// AstId: Struct[42E2, 0]
pub(self) struct S;
"#]],
)

View file

@ -35,9 +35,9 @@ macro_rules! f {
};
}
struct#0:1@58..64#14336# MyTraitMap2#0:2@31..42#ROOT2024# {#0:1@72..73#14336#
map#0:1@86..89#14336#:#0:1@89..90#14336# #0:1@89..90#14336#::#0:1@91..93#14336#std#0:1@93..96#14336#::#0:1@96..98#14336#collections#0:1@98..109#14336#::#0:1@109..111#14336#HashSet#0:1@111..118#14336#<#0:1@118..119#14336#(#0:1@119..120#14336#)#0:1@120..121#14336#>#0:1@121..122#14336#,#0:1@122..123#14336#
}#0:1@132..133#14336#
struct#0:MacroRules[8C8E, 0]@58..64#14336# MyTraitMap2#0:MacroCall[D499, 0]@31..42#ROOT2024# {#0:MacroRules[8C8E, 0]@72..73#14336#
map#0:MacroRules[8C8E, 0]@86..89#14336#:#0:MacroRules[8C8E, 0]@89..90#14336# #0:MacroRules[8C8E, 0]@89..90#14336#::#0:MacroRules[8C8E, 0]@91..93#14336#std#0:MacroRules[8C8E, 0]@93..96#14336#::#0:MacroRules[8C8E, 0]@96..98#14336#collections#0:MacroRules[8C8E, 0]@98..109#14336#::#0:MacroRules[8C8E, 0]@109..111#14336#HashSet#0:MacroRules[8C8E, 0]@111..118#14336#<#0:MacroRules[8C8E, 0]@118..119#14336#(#0:MacroRules[8C8E, 0]@119..120#14336#)#0:MacroRules[8C8E, 0]@120..121#14336#>#0:MacroRules[8C8E, 0]@121..122#14336#,#0:MacroRules[8C8E, 0]@122..123#14336#
}#0:MacroRules[8C8E, 0]@132..133#14336#
"#]],
);
}
@ -75,12 +75,12 @@ macro_rules! f {
};
}
fn#0:2@30..32#ROOT2024# main#0:2@33..37#ROOT2024#(#0:2@37..38#ROOT2024#)#0:2@38..39#ROOT2024# {#0:2@40..41#ROOT2024#
1#0:2@50..51#ROOT2024#;#0:2@51..52#ROOT2024#
1.0#0:2@61..64#ROOT2024#;#0:2@64..65#ROOT2024#
(#0:2@74..75#ROOT2024#(#0:2@75..76#ROOT2024#1#0:2@76..77#ROOT2024#,#0:2@77..78#ROOT2024# )#0:2@78..79#ROOT2024#,#0:2@79..80#ROOT2024# )#0:2@80..81#ROOT2024#.#0:2@81..82#ROOT2024#0#0:2@82..85#ROOT2024#.#0:2@82..85#ROOT2024#0#0:2@82..85#ROOT2024#;#0:2@85..86#ROOT2024#
let#0:2@95..98#ROOT2024# x#0:2@99..100#ROOT2024# =#0:2@101..102#ROOT2024# 1#0:2@103..104#ROOT2024#;#0:2@104..105#ROOT2024#
}#0:2@110..111#ROOT2024#
fn#0:MacroCall[D499, 0]@30..32#ROOT2024# main#0:MacroCall[D499, 0]@33..37#ROOT2024#(#0:MacroCall[D499, 0]@37..38#ROOT2024#)#0:MacroCall[D499, 0]@38..39#ROOT2024# {#0:MacroCall[D499, 0]@40..41#ROOT2024#
1#0:MacroCall[D499, 0]@50..51#ROOT2024#;#0:MacroCall[D499, 0]@51..52#ROOT2024#
1.0#0:MacroCall[D499, 0]@61..64#ROOT2024#;#0:MacroCall[D499, 0]@64..65#ROOT2024#
(#0:MacroCall[D499, 0]@74..75#ROOT2024#(#0:MacroCall[D499, 0]@75..76#ROOT2024#1#0:MacroCall[D499, 0]@76..77#ROOT2024#,#0:MacroCall[D499, 0]@77..78#ROOT2024# )#0:MacroCall[D499, 0]@78..79#ROOT2024#,#0:MacroCall[D499, 0]@79..80#ROOT2024# )#0:MacroCall[D499, 0]@80..81#ROOT2024#.#0:MacroCall[D499, 0]@81..82#ROOT2024#0#0:MacroCall[D499, 0]@82..85#ROOT2024#.#0:MacroCall[D499, 0]@82..85#ROOT2024#0#0:MacroCall[D499, 0]@82..85#ROOT2024#;#0:MacroCall[D499, 0]@85..86#ROOT2024#
let#0:MacroCall[D499, 0]@95..98#ROOT2024# x#0:MacroCall[D499, 0]@99..100#ROOT2024# =#0:MacroCall[D499, 0]@101..102#ROOT2024# 1#0:MacroCall[D499, 0]@103..104#ROOT2024#;#0:MacroCall[D499, 0]@104..105#ROOT2024#
}#0:MacroCall[D499, 0]@110..111#ROOT2024#
"#]],
@ -171,7 +171,7 @@ fn main(foo: ()) {
}
fn main(foo: ()) {
/* error: unresolved macro unresolved */"helloworld!"#0:3@236..321#ROOT2024#;
/* error: unresolved macro unresolved */"helloworld!"#0:Fn[B9C7, 0]@236..321#ROOT2024#;
}
}
@ -197,7 +197,7 @@ macro_rules! mk_struct {
#[macro_use]
mod foo;
struct#1:1@59..65#14336# Foo#0:2@32..35#ROOT2024#(#1:1@70..71#14336#u32#0:2@41..44#ROOT2024#)#1:1@74..75#14336#;#1:1@75..76#14336#
struct#1:MacroRules[E572, 0]@59..65#14336# Foo#0:MacroCall[BDD3, 0]@32..35#ROOT2024#(#1:MacroRules[E572, 0]@70..71#14336#u32#0:MacroCall[BDD3, 0]@41..44#ROOT2024#)#1:MacroRules[E572, 0]@74..75#14336#;#1:MacroRules[E572, 0]@75..76#14336#
"#]],
);
}

View file

@ -181,9 +181,9 @@ fn foo(&self) {
self.0. 1;
}
fn#0:1@45..47#ROOT2024# foo#0:1@48..51#ROOT2024#(#0:1@51..52#ROOT2024#&#0:1@52..53#ROOT2024#self#0:1@53..57#ROOT2024# )#0:1@57..58#ROOT2024# {#0:1@59..60#ROOT2024#
self#0:1@65..69#ROOT2024# .#0:1@69..70#ROOT2024#0#0:1@70..71#ROOT2024#.#0:1@71..72#ROOT2024#1#0:1@73..74#ROOT2024#;#0:1@74..75#ROOT2024#
}#0:1@76..77#ROOT2024#"#]],
fn#0:Fn[4D85, 0]@45..47#ROOT2024# foo#0:Fn[4D85, 0]@48..51#ROOT2024#(#0:Fn[4D85, 0]@51..52#ROOT2024#&#0:Fn[4D85, 0]@52..53#ROOT2024#self#0:Fn[4D85, 0]@53..57#ROOT2024# )#0:Fn[4D85, 0]@57..58#ROOT2024# {#0:Fn[4D85, 0]@59..60#ROOT2024#
self#0:Fn[4D85, 0]@65..69#ROOT2024# .#0:Fn[4D85, 0]@69..70#ROOT2024#0#0:Fn[4D85, 0]@70..71#ROOT2024#.#0:Fn[4D85, 0]@71..72#ROOT2024#1#0:Fn[4D85, 0]@73..74#ROOT2024#;#0:Fn[4D85, 0]@74..75#ROOT2024#
}#0:Fn[4D85, 0]@76..77#ROOT2024#"#]],
);
}

View file

@ -277,8 +277,8 @@ mod tests {
assert_eq!(quoted.to_string(), "hello");
let t = format!("{quoted:#?}");
expect![[r#"
SUBTREE $$ 937550:0@0..0#ROOT2024 937550:0@0..0#ROOT2024
IDENT hello 937550:0@0..0#ROOT2024"#]]
SUBTREE $$ 937550:Root[0000, 0]@0..0#ROOT2024 937550:Root[0000, 0]@0..0#ROOT2024
IDENT hello 937550:Root[0000, 0]@0..0#ROOT2024"#]]
.assert_eq(&t);
}

View file

@ -2,7 +2,7 @@
use std::borrow::Borrow;
use either::Either;
use span::{AstIdNode, ErasedFileAstId, FileAstId, FileId, SyntaxContext};
use span::{ErasedFileAstId, FileAstId, FileId, SyntaxContext};
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize};
use crate::{
@ -106,7 +106,7 @@ impl FileRange {
/// It is stable across reparses, and can be used as salsa key/value.
pub type AstId<N> = crate::InFile<FileAstId<N>>;
impl<N: AstIdNode> AstId<N> {
impl<N: AstNode> AstId<N> {
pub fn to_node(&self, db: &dyn ExpandDatabase) -> N {
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
}

View file

@ -3,7 +3,9 @@
// FIXME: Move more of the nameres independent tests from
// crates\hir-def\src\macro_expansion_tests\mod.rs to this
use expect_test::expect;
use span::{Edition, EditionedFileId, ErasedFileAstId, FileId, Span, SpanAnchor, SyntaxContext};
use span::{
Edition, EditionedFileId, FileId, ROOT_ERASED_FILE_AST_ID, Span, SpanAnchor, SyntaxContext,
};
use stdx::format_to;
use tt::{TextRange, TextSize};
@ -24,7 +26,7 @@ fn check_(
def_edition,
SpanAnchor {
file_id: EditionedFileId::new(FileId::from_raw(0), def_edition),
ast_id: ErasedFileAstId::from_raw(0),
ast_id: ROOT_ERASED_FILE_AST_ID,
},
SyntaxContext::root(Edition::CURRENT),
decl,
@ -37,7 +39,7 @@ fn check_(
};
let call_anchor = SpanAnchor {
file_id: EditionedFileId::new(FileId::from_raw(1), call_edition),
ast_id: ErasedFileAstId::from_raw(0),
ast_id: ROOT_ERASED_FILE_AST_ID,
};
let arg_tt = syntax_bridge::parse_to_token_tree(
call_edition,
@ -110,8 +112,8 @@ fn unbalanced_brace() {
"#,
r#""#,
expect![[r#"
SUBTREE $$ 1:0@0..0#ROOT2024 1:0@0..0#ROOT2024
SUBTREE {} 0:0@9..10#ROOT2024 0:0@11..12#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..0#ROOT2024 1:Root[0000, 0]@0..0#ROOT2024
SUBTREE {} 0:Root[0000, 0]@9..10#ROOT2024 0:Root[0000, 0]@11..12#ROOT2024
{}"#]],
);
@ -133,25 +135,25 @@ fn token_mapping_smoke_test() {
struct MyTraitMap2
"#,
expect![[r#"
SUBTREE $$ 1:0@0..20#ROOT2024 1:0@0..20#ROOT2024
IDENT struct 0:0@34..40#ROOT2024
IDENT MyTraitMap2 1:0@8..19#ROOT2024
SUBTREE {} 0:0@48..49#ROOT2024 0:0@100..101#ROOT2024
IDENT map 0:0@58..61#ROOT2024
PUNCH : [alone] 0:0@61..62#ROOT2024
PUNCH : [joint] 0:0@63..64#ROOT2024
PUNCH : [alone] 0:0@64..65#ROOT2024
IDENT std 0:0@65..68#ROOT2024
PUNCH : [joint] 0:0@68..69#ROOT2024
PUNCH : [alone] 0:0@69..70#ROOT2024
IDENT collections 0:0@70..81#ROOT2024
PUNCH : [joint] 0:0@81..82#ROOT2024
PUNCH : [alone] 0:0@82..83#ROOT2024
IDENT HashSet 0:0@83..90#ROOT2024
PUNCH < [alone] 0:0@90..91#ROOT2024
SUBTREE () 0:0@91..92#ROOT2024 0:0@92..93#ROOT2024
PUNCH > [joint] 0:0@93..94#ROOT2024
PUNCH , [alone] 0:0@94..95#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..20#ROOT2024 1:Root[0000, 0]@0..20#ROOT2024
IDENT struct 0:Root[0000, 0]@34..40#ROOT2024
IDENT MyTraitMap2 1:Root[0000, 0]@8..19#ROOT2024
SUBTREE {} 0:Root[0000, 0]@48..49#ROOT2024 0:Root[0000, 0]@100..101#ROOT2024
IDENT map 0:Root[0000, 0]@58..61#ROOT2024
PUNCH : [alone] 0:Root[0000, 0]@61..62#ROOT2024
PUNCH : [joint] 0:Root[0000, 0]@63..64#ROOT2024
PUNCH : [alone] 0:Root[0000, 0]@64..65#ROOT2024
IDENT std 0:Root[0000, 0]@65..68#ROOT2024
PUNCH : [joint] 0:Root[0000, 0]@68..69#ROOT2024
PUNCH : [alone] 0:Root[0000, 0]@69..70#ROOT2024
IDENT collections 0:Root[0000, 0]@70..81#ROOT2024
PUNCH : [joint] 0:Root[0000, 0]@81..82#ROOT2024
PUNCH : [alone] 0:Root[0000, 0]@82..83#ROOT2024
IDENT HashSet 0:Root[0000, 0]@83..90#ROOT2024
PUNCH < [alone] 0:Root[0000, 0]@90..91#ROOT2024
SUBTREE () 0:Root[0000, 0]@91..92#ROOT2024 0:Root[0000, 0]@92..93#ROOT2024
PUNCH > [joint] 0:Root[0000, 0]@93..94#ROOT2024
PUNCH , [alone] 0:Root[0000, 0]@94..95#ROOT2024
struct MyTraitMap2 {
map: ::std::collections::HashSet<()>,
@ -180,28 +182,28 @@ fn main() {
}
"#,
expect![[r#"
SUBTREE $$ 1:0@0..63#ROOT2024 1:0@0..63#ROOT2024
IDENT fn 1:0@1..3#ROOT2024
IDENT main 1:0@4..8#ROOT2024
SUBTREE () 1:0@8..9#ROOT2024 1:0@9..10#ROOT2024
SUBTREE {} 1:0@11..12#ROOT2024 1:0@61..62#ROOT2024
LITERAL Integer 1 1:0@17..18#ROOT2024
PUNCH ; [alone] 1:0@18..19#ROOT2024
LITERAL Float 1.0 1:0@24..27#ROOT2024
PUNCH ; [alone] 1:0@27..28#ROOT2024
SUBTREE () 1:0@33..34#ROOT2024 1:0@39..40#ROOT2024
SUBTREE () 1:0@34..35#ROOT2024 1:0@37..38#ROOT2024
LITERAL Integer 1 1:0@35..36#ROOT2024
PUNCH , [alone] 1:0@36..37#ROOT2024
PUNCH , [alone] 1:0@38..39#ROOT2024
PUNCH . [alone] 1:0@40..41#ROOT2024
LITERAL Float 0.0 1:0@41..44#ROOT2024
PUNCH ; [alone] 1:0@44..45#ROOT2024
IDENT let 1:0@50..53#ROOT2024
IDENT x 1:0@54..55#ROOT2024
PUNCH = [alone] 1:0@56..57#ROOT2024
LITERAL Integer 1 1:0@58..59#ROOT2024
PUNCH ; [alone] 1:0@59..60#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..63#ROOT2024 1:Root[0000, 0]@0..63#ROOT2024
IDENT fn 1:Root[0000, 0]@1..3#ROOT2024
IDENT main 1:Root[0000, 0]@4..8#ROOT2024
SUBTREE () 1:Root[0000, 0]@8..9#ROOT2024 1:Root[0000, 0]@9..10#ROOT2024
SUBTREE {} 1:Root[0000, 0]@11..12#ROOT2024 1:Root[0000, 0]@61..62#ROOT2024
LITERAL Integer 1 1:Root[0000, 0]@17..18#ROOT2024
PUNCH ; [alone] 1:Root[0000, 0]@18..19#ROOT2024
LITERAL Float 1.0 1:Root[0000, 0]@24..27#ROOT2024
PUNCH ; [alone] 1:Root[0000, 0]@27..28#ROOT2024
SUBTREE () 1:Root[0000, 0]@33..34#ROOT2024 1:Root[0000, 0]@39..40#ROOT2024
SUBTREE () 1:Root[0000, 0]@34..35#ROOT2024 1:Root[0000, 0]@37..38#ROOT2024
LITERAL Integer 1 1:Root[0000, 0]@35..36#ROOT2024
PUNCH , [alone] 1:Root[0000, 0]@36..37#ROOT2024
PUNCH , [alone] 1:Root[0000, 0]@38..39#ROOT2024
PUNCH . [alone] 1:Root[0000, 0]@40..41#ROOT2024
LITERAL Float 0.0 1:Root[0000, 0]@41..44#ROOT2024
PUNCH ; [alone] 1:Root[0000, 0]@44..45#ROOT2024
IDENT let 1:Root[0000, 0]@50..53#ROOT2024
IDENT x 1:Root[0000, 0]@54..55#ROOT2024
PUNCH = [alone] 1:Root[0000, 0]@56..57#ROOT2024
LITERAL Integer 1 1:Root[0000, 0]@58..59#ROOT2024
PUNCH ; [alone] 1:Root[0000, 0]@59..60#ROOT2024
fn main(){
1;
@ -227,14 +229,14 @@ fn expr_2021() {
const { 1 },
"#,
expect![[r#"
SUBTREE $$ 1:0@0..25#ROOT2024 1:0@0..25#ROOT2024
IDENT _ 1:0@5..6#ROOT2024
PUNCH ; [joint] 0:0@36..37#ROOT2024
SUBTREE () 0:0@34..35#ROOT2024 0:0@34..35#ROOT2024
IDENT const 1:0@12..17#ROOT2024
SUBTREE {} 1:0@18..19#ROOT2024 1:0@22..23#ROOT2024
LITERAL Integer 1 1:0@20..21#ROOT2024
PUNCH ; [alone] 0:0@39..40#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..25#ROOT2024 1:Root[0000, 0]@0..25#ROOT2024
IDENT _ 1:Root[0000, 0]@5..6#ROOT2024
PUNCH ; [joint] 0:Root[0000, 0]@36..37#ROOT2024
SUBTREE () 0:Root[0000, 0]@34..35#ROOT2024 0:Root[0000, 0]@34..35#ROOT2024
IDENT const 1:Root[0000, 0]@12..17#ROOT2024
SUBTREE {} 1:Root[0000, 0]@18..19#ROOT2024 1:Root[0000, 0]@22..23#ROOT2024
LITERAL Integer 1 1:Root[0000, 0]@20..21#ROOT2024
PUNCH ; [alone] 0:Root[0000, 0]@39..40#ROOT2024
_;
(const {
@ -255,13 +257,13 @@ fn expr_2021() {
expect![[r#"
ExpandError {
inner: (
1:0@5..6#ROOT2024,
1:Root[0000, 0]@5..6#ROOT2024,
NoMatchingRule,
),
}
SUBTREE $$ 1:0@0..8#ROOT2024 1:0@0..8#ROOT2024
PUNCH ; [alone] 0:0@39..40#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..8#ROOT2024 1:Root[0000, 0]@0..8#ROOT2024
PUNCH ; [alone] 0:Root[0000, 0]@39..40#ROOT2024
;"#]],
);
@ -279,13 +281,13 @@ fn expr_2021() {
expect![[r#"
ExpandError {
inner: (
1:0@5..10#ROOT2024,
1:Root[0000, 0]@5..10#ROOT2024,
NoMatchingRule,
),
}
SUBTREE $$ 1:0@0..18#ROOT2024 1:0@0..18#ROOT2024
PUNCH ; [alone] 0:0@39..40#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..18#ROOT2024 1:Root[0000, 0]@0..18#ROOT2024
PUNCH ; [alone] 0:Root[0000, 0]@39..40#ROOT2024
;"#]],
);
@ -305,26 +307,26 @@ fn expr_2021() {
break 'foo bar,
"#,
expect![[r#"
SUBTREE $$ 1:0@0..76#ROOT2024 1:0@0..76#ROOT2024
LITERAL Integer 4 1:0@5..6#ROOT2024
PUNCH ; [joint] 0:0@41..42#ROOT2024
LITERAL Str literal 1:0@12..21#ROOT2024
PUNCH ; [joint] 0:0@41..42#ROOT2024
SUBTREE () 0:0@39..40#ROOT2024 0:0@39..40#ROOT2024
IDENT funcall 1:0@27..34#ROOT2024
SUBTREE () 1:0@34..35#ROOT2024 1:0@35..36#ROOT2024
PUNCH ; [joint] 0:0@41..42#ROOT2024
SUBTREE () 0:0@39..40#ROOT2024 0:0@39..40#ROOT2024
IDENT future 1:0@42..48#ROOT2024
PUNCH . [alone] 1:0@48..49#ROOT2024
IDENT await 1:0@49..54#ROOT2024
PUNCH ; [joint] 0:0@41..42#ROOT2024
SUBTREE () 0:0@39..40#ROOT2024 0:0@39..40#ROOT2024
IDENT break 1:0@60..65#ROOT2024
PUNCH ' [joint] 1:0@66..67#ROOT2024
IDENT foo 1:0@67..70#ROOT2024
IDENT bar 1:0@71..74#ROOT2024
PUNCH ; [alone] 0:0@44..45#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..76#ROOT2024 1:Root[0000, 0]@0..76#ROOT2024
LITERAL Integer 4 1:Root[0000, 0]@5..6#ROOT2024
PUNCH ; [joint] 0:Root[0000, 0]@41..42#ROOT2024
LITERAL Str literal 1:Root[0000, 0]@12..21#ROOT2024
PUNCH ; [joint] 0:Root[0000, 0]@41..42#ROOT2024
SUBTREE () 0:Root[0000, 0]@39..40#ROOT2024 0:Root[0000, 0]@39..40#ROOT2024
IDENT funcall 1:Root[0000, 0]@27..34#ROOT2024
SUBTREE () 1:Root[0000, 0]@34..35#ROOT2024 1:Root[0000, 0]@35..36#ROOT2024
PUNCH ; [joint] 0:Root[0000, 0]@41..42#ROOT2024
SUBTREE () 0:Root[0000, 0]@39..40#ROOT2024 0:Root[0000, 0]@39..40#ROOT2024
IDENT future 1:Root[0000, 0]@42..48#ROOT2024
PUNCH . [alone] 1:Root[0000, 0]@48..49#ROOT2024
IDENT await 1:Root[0000, 0]@49..54#ROOT2024
PUNCH ; [joint] 0:Root[0000, 0]@41..42#ROOT2024
SUBTREE () 0:Root[0000, 0]@39..40#ROOT2024 0:Root[0000, 0]@39..40#ROOT2024
IDENT break 1:Root[0000, 0]@60..65#ROOT2024
PUNCH ' [joint] 1:Root[0000, 0]@66..67#ROOT2024
IDENT foo 1:Root[0000, 0]@67..70#ROOT2024
IDENT bar 1:Root[0000, 0]@71..74#ROOT2024
PUNCH ; [alone] 0:Root[0000, 0]@44..45#ROOT2024
4;
"literal";
@ -346,13 +348,13 @@ fn expr_2021() {
expect![[r#"
ExpandError {
inner: (
1:0@5..6#ROOT2024,
1:Root[0000, 0]@5..6#ROOT2024,
NoMatchingRule,
),
}
SUBTREE $$ 1:0@0..8#ROOT2024 1:0@0..8#ROOT2024
PUNCH ; [alone] 0:0@44..45#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..8#ROOT2024 1:Root[0000, 0]@0..8#ROOT2024
PUNCH ; [alone] 0:Root[0000, 0]@44..45#ROOT2024
;"#]],
);
@ -370,88 +372,88 @@ fn minus_belongs_to_literal() {
check(
"-1",
expect![[r#"
SUBTREE $$ 1:0@0..2#ROOT2024 1:0@0..2#ROOT2024
PUNCH - [alone] 0:0@10..11#ROOT2024
LITERAL Integer 1 0:0@11..12#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..2#ROOT2024 1:Root[0000, 0]@0..2#ROOT2024
PUNCH - [alone] 0:Root[0000, 0]@10..11#ROOT2024
LITERAL Integer 1 0:Root[0000, 0]@11..12#ROOT2024
-1"#]],
);
check(
"- 1",
expect![[r#"
SUBTREE $$ 1:0@0..3#ROOT2024 1:0@0..3#ROOT2024
PUNCH - [alone] 0:0@10..11#ROOT2024
LITERAL Integer 1 0:0@11..12#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..3#ROOT2024 1:Root[0000, 0]@0..3#ROOT2024
PUNCH - [alone] 0:Root[0000, 0]@10..11#ROOT2024
LITERAL Integer 1 0:Root[0000, 0]@11..12#ROOT2024
-1"#]],
);
check(
"-2",
expect![[r#"
SUBTREE $$ 1:0@0..2#ROOT2024 1:0@0..2#ROOT2024
PUNCH - [alone] 0:0@25..26#ROOT2024
LITERAL Integer 2 0:0@27..28#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..2#ROOT2024 1:Root[0000, 0]@0..2#ROOT2024
PUNCH - [alone] 0:Root[0000, 0]@25..26#ROOT2024
LITERAL Integer 2 0:Root[0000, 0]@27..28#ROOT2024
-2"#]],
);
check(
"- 2",
expect![[r#"
SUBTREE $$ 1:0@0..3#ROOT2024 1:0@0..3#ROOT2024
PUNCH - [alone] 0:0@25..26#ROOT2024
LITERAL Integer 2 0:0@27..28#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..3#ROOT2024 1:Root[0000, 0]@0..3#ROOT2024
PUNCH - [alone] 0:Root[0000, 0]@25..26#ROOT2024
LITERAL Integer 2 0:Root[0000, 0]@27..28#ROOT2024
-2"#]],
);
check(
"-3.0",
expect![[r#"
SUBTREE $$ 1:0@0..4#ROOT2024 1:0@0..4#ROOT2024
PUNCH - [alone] 0:0@43..44#ROOT2024
LITERAL Float 3.0 0:0@45..48#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..4#ROOT2024 1:Root[0000, 0]@0..4#ROOT2024
PUNCH - [alone] 0:Root[0000, 0]@43..44#ROOT2024
LITERAL Float 3.0 0:Root[0000, 0]@45..48#ROOT2024
-3.0"#]],
);
check(
"- 3.0",
expect![[r#"
SUBTREE $$ 1:0@0..5#ROOT2024 1:0@0..5#ROOT2024
PUNCH - [alone] 0:0@43..44#ROOT2024
LITERAL Float 3.0 0:0@45..48#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..5#ROOT2024 1:Root[0000, 0]@0..5#ROOT2024
PUNCH - [alone] 0:Root[0000, 0]@43..44#ROOT2024
LITERAL Float 3.0 0:Root[0000, 0]@45..48#ROOT2024
-3.0"#]],
);
check(
"@1",
expect![[r#"
SUBTREE $$ 1:0@0..2#ROOT2024 1:0@0..2#ROOT2024
LITERAL Integer 1 1:0@1..2#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..2#ROOT2024 1:Root[0000, 0]@0..2#ROOT2024
LITERAL Integer 1 1:Root[0000, 0]@1..2#ROOT2024
1"#]],
);
check(
"@-1",
expect![[r#"
SUBTREE $$ 1:0@0..3#ROOT2024 1:0@0..3#ROOT2024
PUNCH - [alone] 1:0@1..2#ROOT2024
LITERAL Integer 1 1:0@2..3#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..3#ROOT2024 1:Root[0000, 0]@0..3#ROOT2024
PUNCH - [alone] 1:Root[0000, 0]@1..2#ROOT2024
LITERAL Integer 1 1:Root[0000, 0]@2..3#ROOT2024
-1"#]],
);
check(
"@1.0",
expect![[r#"
SUBTREE $$ 1:0@0..4#ROOT2024 1:0@0..4#ROOT2024
LITERAL Float 1.0 1:0@1..4#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..4#ROOT2024 1:Root[0000, 0]@0..4#ROOT2024
LITERAL Float 1.0 1:Root[0000, 0]@1..4#ROOT2024
1.0"#]],
);
check(
"@-1.0",
expect![[r#"
SUBTREE $$ 1:0@0..5#ROOT2024 1:0@0..5#ROOT2024
PUNCH - [alone] 1:0@1..2#ROOT2024
LITERAL Float 1.0 1:0@2..5#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..5#ROOT2024 1:Root[0000, 0]@0..5#ROOT2024
PUNCH - [alone] 1:Root[0000, 0]@1..2#ROOT2024
LITERAL Float 1.0 1:Root[0000, 0]@2..5#ROOT2024
-1.0"#]],
);
@ -460,16 +462,16 @@ fn minus_belongs_to_literal() {
expect![[r#"
ExpandError {
inner: (
1:0@1..2#ROOT2024,
1:Root[0000, 0]@1..2#ROOT2024,
BindingError(
"expected literal",
),
),
}
SUBTREE $$ 1:0@0..6#ROOT2024 1:0@0..6#ROOT2024
PUNCH - [joint] 1:0@1..2#ROOT2024
PUNCH - [alone] 1:0@2..3#ROOT2024
SUBTREE $$ 1:Root[0000, 0]@0..6#ROOT2024 1:Root[0000, 0]@0..6#ROOT2024
PUNCH - [joint] 1:Root[0000, 0]@1..2#ROOT2024
PUNCH - [alone] 1:Root[0000, 0]@2..3#ROOT2024
--"#]],
);

View file

@ -22,9 +22,10 @@ pub const HAS_GLOBAL_SPANS: u32 = 3;
pub const RUST_ANALYZER_SPAN_SUPPORT: u32 = 4;
/// Whether literals encode their kind as an additional u32 field and idents their rawness as a u32 field.
pub const EXTENDED_LEAF_DATA: u32 = 5;
pub const HASHED_AST_ID: u32 = 6;
/// Current API version of the proc-macro protocol.
pub const CURRENT_API_VERSION: u32 = EXTENDED_LEAF_DATA;
pub const CURRENT_API_VERSION: u32 = HASHED_AST_ID;
/// Represents requests sent from the client to the proc-macro-srv.
#[derive(Debug, Serialize, Deserialize)]
@ -54,7 +55,7 @@ pub enum SpanMode {
Id,
/// Rust Analyzer-specific span handling mode.
RustAnalyzer,
RustAnalyzer { fixup_ast_id: u32 },
}
/// Represents responses sent from the proc-macro-srv to the client.
@ -201,7 +202,9 @@ type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str)
#[cfg(test)]
mod tests {
use intern::{Symbol, sym};
use span::{Edition, ErasedFileAstId, Span, SpanAnchor, SyntaxContext, TextRange, TextSize};
use span::{
Edition, ROOT_ERASED_FILE_AST_ID, Span, SpanAnchor, SyntaxContext, TextRange, TextSize,
};
use tt::{
Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, Spacing, TopSubtree,
TopSubtreeBuilder,
@ -215,7 +218,7 @@ mod tests {
span::FileId::from_raw(0xe4e4e),
span::Edition::CURRENT,
),
ast_id: ErasedFileAstId::from_raw(0),
ast_id: ROOT_ERASED_FILE_AST_ID,
};
let mut builder = TopSubtreeBuilder::new(Delimiter {
@ -305,7 +308,7 @@ mod tests {
#[test]
fn test_proc_macro_rpc_works() {
let tt = fixture_token_tree();
for v in RUST_ANALYZER_SPAN_SUPPORT..=CURRENT_API_VERSION {
for v in HASHED_AST_ID..=CURRENT_API_VERSION {
let mut span_data_table = Default::default();
let task = ExpandMacro {
data: ExpandMacroData {

View file

@ -8,6 +8,7 @@ use std::{
};
use paths::AbsPath;
use span::FIXUP_ERASED_FILE_AST_ID_MARKER;
use stdx::JodChild;
use crate::{
@ -15,8 +16,7 @@ use crate::{
legacy_protocol::{
json::{read_json, write_json},
msg::{
CURRENT_API_VERSION, Message, RUST_ANALYZER_SPAN_SUPPORT, Request, Response,
ServerConfig, SpanMode,
CURRENT_API_VERSION, HASHED_AST_ID, Message, Request, Response, ServerConfig, SpanMode,
},
},
};
@ -71,7 +71,9 @@ impl ProcMacroServerProcess {
Ok(v) => {
tracing::info!("Proc-macro server version: {v}");
srv.version = v;
if srv.version >= RUST_ANALYZER_SPAN_SUPPORT {
if srv.version >= HASHED_AST_ID {
// We don't enable spans on versions prior to `HASHED_AST_ID`, because their ast id layout
// is different.
if let Ok(mode) = srv.enable_rust_analyzer_spans() {
srv.mode = mode;
}
@ -111,7 +113,11 @@ impl ProcMacroServerProcess {
/// Enable support for rust-analyzer span mode if the server supports it.
fn enable_rust_analyzer_spans(&self) -> Result<SpanMode, ServerError> {
let request = Request::SetConfig(ServerConfig { span_mode: SpanMode::RustAnalyzer });
let request = Request::SetConfig(ServerConfig {
span_mode: SpanMode::RustAnalyzer {
fixup_ast_id: FIXUP_ERASED_FILE_AST_ID_MARKER.into_raw(),
},
});
let response = self.send_task(request)?;
match response {

View file

@ -26,7 +26,7 @@ pub(crate) fn run() -> io::Result<()> {
let write_response = |msg: msg::Response| msg.write(write_json, &mut io::stdout().lock());
let env = EnvSnapshot::default();
let srv = proc_macro_srv::ProcMacroSrv::new(&env);
let mut srv = proc_macro_srv::ProcMacroSrv::new(&env);
let mut span_mode = SpanMode::Id;
@ -78,7 +78,7 @@ pub(crate) fn run() -> io::Result<()> {
})
.map_err(msg::PanicMessage)
}),
SpanMode::RustAnalyzer => msg::Response::ExpandMacroExtended({
SpanMode::RustAnalyzer { .. } => msg::Response::ExpandMacroExtended({
let mut span_data_table = deserialize_span_data_index_map(&span_data_table);
let def_site = span_data_table[def_site];
@ -122,6 +122,9 @@ pub(crate) fn run() -> io::Result<()> {
msg::Request::ApiVersionCheck {} => msg::Response::ApiVersionCheck(CURRENT_API_VERSION),
msg::Request::SetConfig(config) => {
span_mode = config.span_mode;
if let SpanMode::RustAnalyzer { fixup_ast_id } = span_mode {
srv.set_fixup_ast_id(fixup_ast_id);
}
msg::Response::SetConfig(config)
}
};

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()));
}

View file

@ -22,6 +22,9 @@ vfs.workspace = true
syntax.workspace = true
stdx.workspace = true
[dev-dependencies]
syntax.workspace = true
[features]
default = ["salsa"]

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,10 @@ mod hygiene;
mod map;
pub use self::{
ast_id::{AstIdMap, AstIdNode, ErasedFileAstId, FileAstId},
ast_id::{
AstIdMap, AstIdNode, ErasedFileAstId, FIXUP_ERASED_FILE_AST_ID_MARKER, FileAstId,
ROOT_ERASED_FILE_AST_ID,
},
hygiene::{SyntaxContext, Transparency},
map::{RealSpanMap, SpanMap},
};
@ -15,19 +18,6 @@ pub use syntax::Edition;
pub use text_size::{TextRange, TextSize};
pub use vfs::FileId;
// The first index is always the root node's AstId
/// The root ast id always points to the encompassing file, using this in spans is discouraged as
/// any range relative to it will be effectively absolute, ruining the entire point of anchored
/// relative text ranges.
pub const ROOT_ERASED_FILE_AST_ID: ErasedFileAstId = ErasedFileAstId::from_raw(0);
/// FileId used as the span for syntax node fixups. Any Span containing this file id is to be
/// considered fake.
pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
// we pick the second to last for this in case we ever consider making this a NonMaxU32, this
// is required to be stable for the proc-macro-server
ErasedFileAstId::from_raw(!0 - 1);
pub type Span = SpanData<SyntaxContext>;
impl Span {
@ -60,7 +50,7 @@ impl<Ctx: fmt::Debug> fmt::Debug for SpanData<Ctx> {
if f.alternate() {
fmt::Debug::fmt(&self.anchor.file_id.file_id().index(), f)?;
f.write_char(':')?;
fmt::Debug::fmt(&self.anchor.ast_id.into_raw(), f)?;
write!(f, "{:#?}", self.anchor.ast_id)?;
f.write_char('@')?;
fmt::Debug::fmt(&self.range, f)?;
f.write_char('#')?;
@ -85,7 +75,7 @@ impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.anchor.file_id.file_id().index(), f)?;
f.write_char(':')?;
fmt::Debug::fmt(&self.anchor.ast_id.into_raw(), f)?;
write!(f, "{:#?}", self.anchor.ast_id)?;
f.write_char('@')?;
fmt::Debug::fmt(&self.range, f)?;
f.write_char('#')?;
@ -101,7 +91,7 @@ pub struct SpanAnchor {
impl fmt::Debug for SpanAnchor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("SpanAnchor").field(&self.file_id).field(&self.ast_id.into_raw()).finish()
f.debug_tuple("SpanAnchor").field(&self.file_id).field(&self.ast_id).finish()
}
}

View file

@ -169,7 +169,7 @@ impl fmt::Display for RealSpanMap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "RealSpanMap({:?}):", self.file_id)?;
for span in self.pairs.iter() {
writeln!(f, "{}: {}", u32::from(span.0), span.1.into_raw())?;
writeln!(f, "{}: {:#?}", u32::from(span.0), span.1)?;
}
Ok(())
}

View file

@ -30,6 +30,16 @@ impl ast::Name {
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
pub fn text_non_mutable(&self) -> &str {
fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
green_ref.children().next().and_then(NodeOrToken::into_token).unwrap()
}
match self.syntax().green() {
Cow::Borrowed(green_ref) => first_token(green_ref).text(),
Cow::Owned(_) => unreachable!(),
}
}
}
impl ast::NameRef {