Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs')
-rw-r--r--crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs117
1 files changed, 54 insertions, 63 deletions
diff --git a/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs b/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
index 5d9090c060..ec30630c10 100644
--- a/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
+++ b/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
@@ -10,7 +10,7 @@ use std::{
};
use intern::Symbol;
-use proc_macro::bridge::server;
+use rustc_proc_macro::bridge::server;
use span::{FIXUP_ERASED_FILE_AST_ID_MARKER, Span, TextRange, TextSize};
use crate::{
@@ -19,8 +19,6 @@ use crate::{
server_impl::literal_from_str,
};
-pub struct FreeFunctions;
-
pub struct RaSpanServer<'a> {
// FIXME: Report this back to the caller to track as dependencies
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
@@ -33,13 +31,28 @@ pub struct RaSpanServer<'a> {
}
impl server::Types for RaSpanServer<'_> {
- type FreeFunctions = FreeFunctions;
type TokenStream = crate::token_stream::TokenStream<Span>;
type Span = Span;
type Symbol = Symbol;
}
-impl server::FreeFunctions for RaSpanServer<'_> {
+impl server::Server for RaSpanServer<'_> {
+ fn globals(&mut self) -> ExpnGlobals<Self::Span> {
+ ExpnGlobals {
+ def_site: self.def_site,
+ call_site: self.call_site,
+ mixed_site: self.mixed_site,
+ }
+ }
+
+ fn intern_symbol(ident: &str) -> Self::Symbol {
+ Symbol::intern(ident)
+ }
+
+ fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
+ f(symbol.as_str())
+ }
+
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
None
}
@@ -58,13 +71,19 @@ impl server::FreeFunctions for RaSpanServer<'_> {
fn emit_diagnostic(&mut self, _: Diagnostic<Self::Span>) {
// FIXME handle diagnostic
}
-}
-impl server::TokenStream for RaSpanServer<'_> {
- fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
+ fn ts_drop(&mut self, stream: Self::TokenStream) {
+ drop(stream);
+ }
+
+ fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
+ stream.clone()
+ }
+
+ fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
stream.is_empty()
}
- fn from_str(&mut self, src: &str) -> Self::TokenStream {
+ fn ts_from_str(&mut self, src: &str) -> Self::TokenStream {
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
Self::TokenStream::from_str(
&format!("compile_error!(\"failed to parse str to token stream: {e}\")"),
@@ -73,15 +92,15 @@ impl server::TokenStream for RaSpanServer<'_> {
.unwrap()
})
}
- fn to_string(&mut self, stream: &Self::TokenStream) -> String {
+ fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
}
- fn from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
+ fn ts_from_token_tree(&mut self, tree: TokenTree<Self::Span>) -> Self::TokenStream {
Self::TokenStream::new(vec![tree])
}
- fn expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
+ fn ts_expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
// FIXME: requires db, more importantly this requires name resolution so we would need to
// eagerly expand this proc-macro, but we can't know that this proc-macro is eager until we
// expand it ...
@@ -90,7 +109,7 @@ impl server::TokenStream for RaSpanServer<'_> {
Ok(self_.clone())
}
- fn concat_trees(
+ fn ts_concat_trees(
&mut self,
base: Option<Self::TokenStream>,
trees: Vec<TokenTree<Self::Span>>,
@@ -106,7 +125,7 @@ impl server::TokenStream for RaSpanServer<'_> {
}
}
- fn concat_streams(
+ fn ts_concat_streams(
&mut self,
base: Option<Self::TokenStream>,
streams: Vec<Self::TokenStream>,
@@ -118,30 +137,26 @@ impl server::TokenStream for RaSpanServer<'_> {
stream
}
- fn into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
+ fn ts_into_trees(&mut self, stream: Self::TokenStream) -> Vec<TokenTree<Self::Span>> {
(*stream.0).clone()
}
-}
-impl server::Span for RaSpanServer<'_> {
- fn debug(&mut self, span: Self::Span) -> String {
+ fn span_debug(&mut self, span: Self::Span) -> String {
format!("{:?}", span)
}
- fn file(&mut self, _: Self::Span) -> String {
- // FIXME
- String::new()
+ fn span_file(&mut self, span: Self::Span) -> String {
+ self.callback.as_mut().map(|cb| cb.file(span.anchor.file_id.file_id())).unwrap_or_default()
}
- fn local_file(&mut self, _: Self::Span) -> Option<String> {
- // FIXME
- None
+ fn span_local_file(&mut self, span: Self::Span) -> Option<String> {
+ self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id()))
}
- fn save_span(&mut self, _span: Self::Span) -> usize {
+ fn span_save_span(&mut self, _span: Self::Span) -> usize {
// FIXME, quote is incompatible with third-party tools
// This is called by the quote proc-macro which is expanded when the proc-macro is compiled
// As such, r-a will never observe this
0
}
- fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
+ fn span_recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
// FIXME, quote is incompatible with third-party tools
// This is called by the expansion of quote!, r-a will observe this, but we don't have
// access to the spans that were encoded
@@ -151,27 +166,23 @@ impl server::Span for RaSpanServer<'_> {
///
/// See PR:
/// https://github.com/rust-lang/rust/pull/55780
- fn source_text(&mut self, span: Self::Span) -> Option<String> {
- let file_id = span.anchor.file_id;
- let start: u32 = span.range.start().into();
- let end: u32 = span.range.end().into();
-
- self.callback.as_mut()?.source_text(file_id.file_id().index(), start, end)
+ fn span_source_text(&mut self, span: Self::Span) -> Option<String> {
+ self.callback.as_mut()?.source_text(span)
}
- fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
+ fn span_parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
// FIXME requires db, looks up the parent call site
None
}
- fn source(&mut self, span: Self::Span) -> Self::Span {
+ fn span_source(&mut self, span: Self::Span) -> Self::Span {
// FIXME requires db, returns the top level call site
span
}
- fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
+ fn span_byte_range(&mut self, span: Self::Span) -> Range<usize> {
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
Range { start: span.range.start().into(), end: span.range.end().into() }
}
- fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
+ fn span_join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
// We can't modify the span range for fixup spans, those are meaningful to fixup, so just
// prefer the non-fixup span.
if first.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
@@ -199,7 +210,7 @@ impl server::Span for RaSpanServer<'_> {
ctx: second.ctx,
})
}
- fn subspan(
+ fn span_subspan(
&mut self,
span: Self::Span,
start: Bound<usize>,
@@ -243,11 +254,11 @@ impl server::Span for RaSpanServer<'_> {
})
}
- fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
+ fn span_resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
Span { ctx: at.ctx, ..span }
}
- fn end(&mut self, span: Self::Span) -> Self::Span {
+ fn span_end(&mut self, span: Self::Span) -> Self::Span {
// We can't modify the span range for fixup spans, those are meaningful to fixup.
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return span;
@@ -255,7 +266,7 @@ impl server::Span for RaSpanServer<'_> {
Span { range: TextRange::empty(span.range.end()), ..span }
}
- fn start(&mut self, span: Self::Span) -> Self::Span {
+ fn span_start(&mut self, span: Self::Span) -> Self::Span {
// We can't modify the span range for fixup spans, those are meaningful to fixup.
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
return span;
@@ -263,38 +274,18 @@ impl server::Span for RaSpanServer<'_> {
Span { range: TextRange::empty(span.range.start()), ..span }
}
- fn line(&mut self, _span: Self::Span) -> usize {
+ fn span_line(&mut self, _span: Self::Span) -> usize {
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
1
}
- fn column(&mut self, _span: Self::Span) -> usize {
+ fn span_column(&mut self, _span: Self::Span) -> usize {
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
1
}
-}
-impl server::Symbol for RaSpanServer<'_> {
- fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
+ fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
// FIXME: nfc-normalize and validate idents
Ok(<Self as server::Server>::intern_symbol(string))
}
}
-
-impl server::Server for RaSpanServer<'_> {
- fn globals(&mut self) -> ExpnGlobals<Self::Span> {
- ExpnGlobals {
- def_site: self.def_site,
- call_site: self.call_site,
- mixed_site: self.mixed_site,
- }
- }
-
- fn intern_symbol(ident: &str) -> Self::Symbol {
- Symbol::intern(ident)
- }
-
- fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
- f(symbol.as_str())
- }
-}