mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 14:03:35 +00:00
Implement server::Span::Join
This commit is contained in:
parent
6be83b8204
commit
428a34a9b4
7 changed files with 148 additions and 75 deletions
|
@ -108,6 +108,7 @@ register_builtin! {
|
|||
(format_args, FormatArgs) => format_args_expand,
|
||||
(const_format_args, ConstFormatArgs) => format_args_expand,
|
||||
(format_args_nl, FormatArgsNl) => format_args_nl_expand,
|
||||
(quote, Quote) => quote_expand,
|
||||
|
||||
EAGER:
|
||||
(compile_error, CompileError) => compile_error_expand,
|
||||
|
@ -770,3 +771,12 @@ fn option_env_expand(
|
|||
|
||||
ExpandResult::ok(expanded)
|
||||
}
|
||||
|
||||
fn quote_expand(
|
||||
_db: &dyn ExpandDatabase,
|
||||
_arg_id: MacroCallId,
|
||||
_tt: &tt::Subtree,
|
||||
_span: SpanData,
|
||||
) -> ExpandResult<tt::Subtree> {
|
||||
ExpandResult::only_err(ExpandError::other("quote! is not implemented"))
|
||||
}
|
||||
|
|
|
@ -388,6 +388,7 @@ pub mod known {
|
|||
log_syntax,
|
||||
module_path,
|
||||
option_env,
|
||||
quote,
|
||||
std_panic,
|
||||
stringify,
|
||||
trace_macros,
|
||||
|
|
|
@ -60,7 +60,14 @@ impl ProcMacroSrvSpan for TokenId {
|
|||
impl ProcMacroSrvSpan for Span {
|
||||
type Server = server::rust_analyzer_span::RaSpanServer;
|
||||
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
|
||||
Self::Server { interner: &server::SYMBOL_INTERNER, call_site, def_site, mixed_site }
|
||||
Self::Server {
|
||||
interner: &server::SYMBOL_INTERNER,
|
||||
call_site,
|
||||
def_site,
|
||||
mixed_site,
|
||||
tracked_env_vars: Default::default(),
|
||||
tracked_paths: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
iter,
|
||||
ops::{Bound, Range},
|
||||
};
|
||||
|
@ -35,6 +36,10 @@ pub struct FreeFunctions;
|
|||
|
||||
pub struct RaSpanServer {
|
||||
pub(crate) interner: SymbolInternerRef,
|
||||
// FIXME: Report this back to the caller to track as dependencies
|
||||
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
|
||||
// FIXME: Report this back to the caller to track as dependencies
|
||||
pub tracked_paths: HashSet<Box<str>>,
|
||||
pub call_site: Span,
|
||||
pub def_site: Span,
|
||||
pub mixed_site: Span,
|
||||
|
@ -49,11 +54,12 @@ impl server::Types for RaSpanServer {
|
|||
}
|
||||
|
||||
impl server::FreeFunctions for RaSpanServer {
|
||||
fn track_env_var(&mut self, _var: &str, _value: Option<&str>) {
|
||||
// FIXME: track env var accesses
|
||||
// https://github.com/rust-lang/rust/pull/71858
|
||||
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
|
||||
self.tracked_env_vars.insert(var.into(), value.map(Into::into));
|
||||
}
|
||||
fn track_path(&mut self, path: &str) {
|
||||
self.tracked_paths.insert(path.into());
|
||||
}
|
||||
fn track_path(&mut self, _path: &str) {}
|
||||
|
||||
fn literal_from_str(
|
||||
&mut self,
|
||||
|
@ -247,24 +253,38 @@ 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> {
|
||||
// FIXME requires db
|
||||
None
|
||||
}
|
||||
|
||||
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
|
||||
// FIXME handle span
|
||||
// FIXME requires db, looks up the parent call site
|
||||
None
|
||||
}
|
||||
fn source(&mut self, span: Self::Span) -> Self::Span {
|
||||
// FIXME handle span
|
||||
// FIXME requires db, returns the top level call site
|
||||
span
|
||||
}
|
||||
fn byte_range(&mut self, _span: Self::Span) -> Range<usize> {
|
||||
// FIXME handle span
|
||||
Range { start: 0, end: 0 }
|
||||
fn 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> {
|
||||
// Just return the first span again, because some macros will unwrap the result.
|
||||
Some(first)
|
||||
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
|
||||
if first.anchor != second.anchor {
|
||||
return None;
|
||||
}
|
||||
if first.ctx != second.ctx {
|
||||
if first.ctx.is_root() {
|
||||
return Some(second);
|
||||
} else if second.ctx.is_root() {
|
||||
return Some(first);
|
||||
}
|
||||
}
|
||||
Some(Span {
|
||||
range: first.range.cover(second.range),
|
||||
anchor: second.anchor,
|
||||
ctx: second.ctx,
|
||||
})
|
||||
}
|
||||
fn subspan(
|
||||
&mut self,
|
||||
|
|
|
@ -8,7 +8,7 @@ use expect_test::expect;
|
|||
|
||||
#[test]
|
||||
fn test_derive_empty() {
|
||||
assert_expand("DeriveEmpty", r#"struct S;"#, expect!["SUBTREE $$ 1 1"], expect!["SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"]);
|
||||
assert_expand("DeriveEmpty", r#"struct S;"#, expect!["SUBTREE $$ 1 1"], expect!["SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -24,12 +24,12 @@ fn test_derive_error() {
|
|||
LITERAL "#[derive(DeriveError)] struct S ;" 1
|
||||
PUNCH ; [alone] 1"##]],
|
||||
expect![[r##"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT compile_error SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ! [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE () SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "#[derive(DeriveError)] struct S ;" SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ; [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"##]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT compile_error SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ! [alone] SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE () SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "#[derive(DeriveError)] struct S ;" SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ; [alone] SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"##]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -48,14 +48,14 @@ fn test_fn_like_macro_noop() {
|
|||
PUNCH , [alone] 1
|
||||
SUBTREE [] 1 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT ident SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 0 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 1 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE [] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT ident SpanData { range: 0..5, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 5..6, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 0 SpanData { range: 7..8, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 8..9, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 1 SpanData { range: 10..11, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 11..12, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE [] SpanData { range: 13..14, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 14..15, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -70,10 +70,10 @@ fn test_fn_like_macro_clone_ident_subtree() {
|
|||
PUNCH , [alone] 1
|
||||
SUBTREE [] 1 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT ident SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE [] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT ident SpanData { range: 0..5, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 5..6, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE [] SpanData { range: 7..8, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 7..8, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -86,8 +86,22 @@ fn test_fn_like_macro_clone_raw_ident() {
|
|||
SUBTREE $$ 1 1
|
||||
IDENT r#async 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT r#async SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT r#async SpanData { range: 0..7, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fn_like_fn_like_span_join() {
|
||||
assert_expand(
|
||||
"fn_like_span_join",
|
||||
"foo bar",
|
||||
expect![[r#"
|
||||
SUBTREE $$ 1 1
|
||||
IDENT r#joined 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT r#joined SpanData { range: 0..11, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -106,14 +120,14 @@ fn test_fn_like_mk_literals() {
|
|||
LITERAL 123i64 1
|
||||
LITERAL 123 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL b"byte_string" SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 'c' SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "string" SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 3.14f64 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 3.14 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 123i64 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 123 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL b"byte_string" SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 'c' SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "string" SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 3.14f64 SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 3.14 SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 123i64 SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 123 SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -127,9 +141,9 @@ fn test_fn_like_mk_idents() {
|
|||
IDENT standard 1
|
||||
IDENT r#raw 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT standard SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT r#raw SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT standard SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT r#raw SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -151,17 +165,17 @@ fn test_fn_like_macro_clone_literals() {
|
|||
PUNCH , [alone] 1
|
||||
LITERAL "hello bridge" 1"#]],
|
||||
expect![[r#"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 1u16 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 2_u32 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH - [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 4i64 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 3.14f32 SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "hello bridge" SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 1u16 SpanData { range: 0..4, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 4..5, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 2_u32 SpanData { range: 6..11, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 11..12, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH - [alone] SpanData { range: 13..14, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 4i64 SpanData { range: 14..18, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 18..19, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL 3.14f32 SpanData { range: 20..27, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH , [alone] SpanData { range: 27..28, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "hello bridge" SpanData { range: 29..43, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -182,12 +196,12 @@ fn test_attr_macro() {
|
|||
LITERAL "#[attr_error(some arguments)] mod m {}" 1
|
||||
PUNCH ; [alone] 1"##]],
|
||||
expect![[r##"
|
||||
SUBTREE $$ SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT compile_error SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ! [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE () SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "#[attr_error(some arguments)] mod m {}" SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ; [alone] SpanData { range: 52..77, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"##]],
|
||||
SUBTREE $$ SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
IDENT compile_error SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ! [alone] SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
SUBTREE () SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) } SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
LITERAL "#[attr_error(some arguments)] mod m {}" SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }
|
||||
PUNCH ; [alone] SpanData { range: 0..100, anchor: SpanAnchor(FileId(42), 2), ctx: SyntaxContextId(0) }"##]],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -203,6 +217,7 @@ fn list_test_macros() {
|
|||
fn_like_clone_tokens [FuncLike]
|
||||
fn_like_mk_literals [FuncLike]
|
||||
fn_like_mk_idents [FuncLike]
|
||||
fn_like_span_join [FuncLike]
|
||||
attr_noop [Attr]
|
||||
attr_panic [Attr]
|
||||
attr_error [Attr]
|
||||
|
|
|
@ -10,14 +10,20 @@ use tt::TextRange;
|
|||
|
||||
use crate::{dylib, proc_macro_test_dylib_path, ProcMacroSrv};
|
||||
|
||||
fn parse_string<S: tt::Span>(code: &str, call_site: S) -> Option<crate::server::TokenStream<S>> {
|
||||
// This is a bit strange. We need to parse a string into a token stream into
|
||||
// order to create a tt::SubTree from it in fixtures. `into_subtree` is
|
||||
// implemented by all the ABIs we have so we arbitrarily choose one ABI to
|
||||
// write a `parse_string` function for and use that. The tests don't really
|
||||
// care which ABI we're using as the `into_subtree` function isn't part of
|
||||
// the ABI and shouldn't change between ABI versions.
|
||||
crate::server::TokenStream::from_str(code, call_site).ok()
|
||||
fn parse_string(call_site: TokenId, src: &str) -> crate::server::TokenStream<TokenId> {
|
||||
crate::server::TokenStream::with_subtree(
|
||||
mbe::parse_to_token_tree_static_span(call_site, src).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
fn parse_string_spanned(
|
||||
anchor: SpanAnchor,
|
||||
call_site: SyntaxContextId,
|
||||
src: &str,
|
||||
) -> crate::server::TokenStream<SpanData> {
|
||||
crate::server::TokenStream::with_subtree(
|
||||
mbe::parse_to_token_tree(anchor, call_site, src).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn assert_expand(macro_name: &str, ra_fixture: &str, expect: Expect, expect_s: Expect) {
|
||||
|
@ -47,8 +53,8 @@ fn assert_expand_impl(
|
|||
let def_site = TokenId(0);
|
||||
let call_site = TokenId(1);
|
||||
let mixed_site = TokenId(2);
|
||||
let input_ts = parse_string(input, call_site).unwrap();
|
||||
let attr_ts = attr.map(|attr| parse_string(attr, call_site).unwrap().into_subtree(call_site));
|
||||
let input_ts = parse_string(call_site, input);
|
||||
let attr_ts = attr.map(|attr| parse_string(call_site, attr).into_subtree(call_site));
|
||||
|
||||
let res = expander
|
||||
.expand(
|
||||
|
@ -71,7 +77,7 @@ fn assert_expand_impl(
|
|||
ctx: SyntaxContextId::ROOT,
|
||||
};
|
||||
let call_site = SpanData {
|
||||
range: TextRange::new(52.into(), 77.into()),
|
||||
range: TextRange::new(0.into(), 100.into()),
|
||||
anchor: SpanAnchor {
|
||||
file_id: FileId::from_raw(42),
|
||||
ast_id: ErasedFileAstId::from_raw(From::from(2)),
|
||||
|
@ -80,8 +86,10 @@ fn assert_expand_impl(
|
|||
};
|
||||
let mixed_site = call_site;
|
||||
|
||||
let fixture = parse_string(input, call_site).unwrap();
|
||||
let attr = attr.map(|attr| parse_string(attr, call_site).unwrap().into_subtree(call_site));
|
||||
let fixture = parse_string_spanned(call_site.anchor, call_site.ctx, input);
|
||||
let attr = attr.map(|attr| {
|
||||
parse_string_spanned(call_site.anchor, call_site.ctx, attr).into_subtree(call_site)
|
||||
});
|
||||
|
||||
let res = expander
|
||||
.expand(macro_name, fixture.into_subtree(call_site), attr, def_site, call_site, mixed_site)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//! Exports a few trivial procedural macros for testing.
|
||||
|
||||
#![warn(rust_2018_idioms, unused_lifetimes)]
|
||||
#![feature(proc_macro_span)]
|
||||
|
||||
use proc_macro::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
|
||||
|
||||
|
@ -49,6 +50,17 @@ pub fn fn_like_mk_idents(_args: TokenStream) -> TokenStream {
|
|||
TokenStream::from_iter(trees)
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn fn_like_span_join(args: TokenStream) -> TokenStream {
|
||||
let args = &mut args.into_iter();
|
||||
let first = args.next().unwrap();
|
||||
let second = args.next().unwrap();
|
||||
TokenStream::from(TokenTree::from(Ident::new_raw(
|
||||
"joined",
|
||||
first.span().join(second.span()).unwrap(),
|
||||
)))
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn attr_noop(_args: TokenStream, item: TokenStream) -> TokenStream {
|
||||
item
|
||||
|
|
Loading…
Reference in a new issue