diff options
Diffstat (limited to 'crates/ra_proc_macro_srv/src')
17 files changed, 0 insertions, 4280 deletions
diff --git a/crates/ra_proc_macro_srv/src/cli.rs b/crates/ra_proc_macro_srv/src/cli.rs deleted file mode 100644 index 1437794c9..000000000 --- a/crates/ra_proc_macro_srv/src/cli.rs +++ /dev/null | |||
@@ -1,39 +0,0 @@ | |||
1 | //! Driver for proc macro server | ||
2 | |||
3 | use crate::ProcMacroSrv; | ||
4 | use ra_proc_macro::msg::{self, Message}; | ||
5 | use std::io; | ||
6 | |||
7 | pub fn run() -> io::Result<()> { | ||
8 | let mut srv = ProcMacroSrv::default(); | ||
9 | |||
10 | while let Some(req) = read_request()? { | ||
11 | let res = match req { | ||
12 | msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), | ||
13 | msg::Request::ExpansionMacro(task) => { | ||
14 | srv.expand(&task).map(msg::Response::ExpansionMacro) | ||
15 | } | ||
16 | }; | ||
17 | |||
18 | let msg = res.unwrap_or_else(|err| { | ||
19 | msg::Response::Error(msg::ResponseError { | ||
20 | code: msg::ErrorCode::ExpansionError, | ||
21 | message: err, | ||
22 | }) | ||
23 | }); | ||
24 | |||
25 | if let Err(err) = write_response(msg) { | ||
26 | eprintln!("Write message error: {}", err); | ||
27 | } | ||
28 | } | ||
29 | |||
30 | Ok(()) | ||
31 | } | ||
32 | |||
33 | fn read_request() -> io::Result<Option<msg::Request>> { | ||
34 | msg::Request::read(&mut io::stdin().lock()) | ||
35 | } | ||
36 | |||
37 | fn write_response(msg: msg::Response) -> io::Result<()> { | ||
38 | msg.write(&mut io::stdout().lock()) | ||
39 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs deleted file mode 100644 index 9b6cc91ef..000000000 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ /dev/null | |||
@@ -1,224 +0,0 @@ | |||
1 | //! Handles dynamic library loading for proc macro | ||
2 | |||
3 | use crate::{proc_macro::bridge, rustc_server::TokenStream}; | ||
4 | use std::fs::File; | ||
5 | use std::path::{Path, PathBuf}; | ||
6 | |||
7 | use goblin::{mach::Mach, Object}; | ||
8 | use libloading::Library; | ||
9 | use memmap::Mmap; | ||
10 | use ra_proc_macro::ProcMacroKind; | ||
11 | use std::io; | ||
12 | |||
13 | const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; | ||
14 | |||
15 | fn invalid_data_err(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> io::Error { | ||
16 | io::Error::new(io::ErrorKind::InvalidData, e) | ||
17 | } | ||
18 | |||
19 | fn is_derive_registrar_symbol(symbol: &str) -> bool { | ||
20 | symbol.contains(NEW_REGISTRAR_SYMBOL) | ||
21 | } | ||
22 | |||
23 | fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> { | ||
24 | let file = File::open(file)?; | ||
25 | let buffer = unsafe { Mmap::map(&file)? }; | ||
26 | let object = Object::parse(&buffer).map_err(invalid_data_err)?; | ||
27 | |||
28 | let name = match object { | ||
29 | Object::Elf(elf) => { | ||
30 | let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?; | ||
31 | symbols.into_iter().find(|s| is_derive_registrar_symbol(s)).map(&str::to_owned) | ||
32 | } | ||
33 | Object::PE(pe) => pe | ||
34 | .exports | ||
35 | .iter() | ||
36 | .flat_map(|s| s.name) | ||
37 | .find(|s| is_derive_registrar_symbol(s)) | ||
38 | .map(&str::to_owned), | ||
39 | Object::Mach(Mach::Binary(binary)) => { | ||
40 | let exports = binary.exports().map_err(invalid_data_err)?; | ||
41 | exports | ||
42 | .iter() | ||
43 | .map(|s| { | ||
44 | // In macos doc: | ||
45 | // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html | ||
46 | // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be | ||
47 | // prepended with an underscore. | ||
48 | if s.name.starts_with('_') { | ||
49 | &s.name[1..] | ||
50 | } else { | ||
51 | &s.name | ||
52 | } | ||
53 | }) | ||
54 | .find(|s| is_derive_registrar_symbol(s)) | ||
55 | .map(&str::to_owned) | ||
56 | } | ||
57 | _ => return Ok(None), | ||
58 | }; | ||
59 | return Ok(name); | ||
60 | } | ||
61 | |||
62 | /// Loads dynamic library in platform dependent manner. | ||
63 | /// | ||
64 | /// For unix, you have to use RTLD_DEEPBIND flag to escape problems described | ||
65 | /// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample) | ||
66 | /// and [here](https://github.com/rust-lang/rust/issues/60593). | ||
67 | /// | ||
68 | /// Usage of RTLD_DEEPBIND | ||
69 | /// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1) | ||
70 | /// | ||
71 | /// It seems that on Windows that behaviour is default, so we do nothing in that case. | ||
72 | #[cfg(windows)] | ||
73 | fn load_library(file: &Path) -> Result<Library, libloading::Error> { | ||
74 | Library::new(file) | ||
75 | } | ||
76 | |||
77 | #[cfg(unix)] | ||
78 | fn load_library(file: &Path) -> Result<Library, libloading::Error> { | ||
79 | use libloading::os::unix::Library as UnixLibrary; | ||
80 | use std::os::raw::c_int; | ||
81 | |||
82 | const RTLD_NOW: c_int = 0x00002; | ||
83 | const RTLD_DEEPBIND: c_int = 0x00008; | ||
84 | |||
85 | UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into()) | ||
86 | } | ||
87 | |||
88 | struct ProcMacroLibraryLibloading { | ||
89 | // Hold the dylib to prevent it from unloading | ||
90 | _lib: Library, | ||
91 | exported_macros: Vec<bridge::client::ProcMacro>, | ||
92 | } | ||
93 | |||
94 | impl ProcMacroLibraryLibloading { | ||
95 | fn open(file: &Path) -> io::Result<Self> { | ||
96 | let symbol_name = find_registrar_symbol(file)?.ok_or_else(|| { | ||
97 | invalid_data_err(format!("Cannot find registrar symbol in file {}", file.display())) | ||
98 | })?; | ||
99 | |||
100 | let lib = load_library(file).map_err(invalid_data_err)?; | ||
101 | let exported_macros = { | ||
102 | let macros: libloading::Symbol<&&[bridge::client::ProcMacro]> = | ||
103 | unsafe { lib.get(symbol_name.as_bytes()) }.map_err(invalid_data_err)?; | ||
104 | macros.to_vec() | ||
105 | }; | ||
106 | |||
107 | Ok(ProcMacroLibraryLibloading { _lib: lib, exported_macros }) | ||
108 | } | ||
109 | } | ||
110 | |||
111 | pub struct Expander { | ||
112 | inner: ProcMacroLibraryLibloading, | ||
113 | } | ||
114 | |||
115 | impl Expander { | ||
116 | pub fn new(lib: &Path) -> io::Result<Expander> { | ||
117 | // Some libraries for dynamic loading require canonicalized path even when it is | ||
118 | // already absolute | ||
119 | let lib = lib.canonicalize()?; | ||
120 | |||
121 | let lib = ensure_file_with_lock_free_access(&lib)?; | ||
122 | |||
123 | let library = ProcMacroLibraryLibloading::open(&lib)?; | ||
124 | |||
125 | Ok(Expander { inner: library }) | ||
126 | } | ||
127 | |||
128 | pub fn expand( | ||
129 | &self, | ||
130 | macro_name: &str, | ||
131 | macro_body: &tt::Subtree, | ||
132 | attributes: Option<&tt::Subtree>, | ||
133 | ) -> Result<tt::Subtree, bridge::PanicMessage> { | ||
134 | let parsed_body = TokenStream::with_subtree(macro_body.clone()); | ||
135 | |||
136 | let parsed_attributes = attributes | ||
137 | .map_or(crate::rustc_server::TokenStream::new(), |attr| { | ||
138 | TokenStream::with_subtree(attr.clone()) | ||
139 | }); | ||
140 | |||
141 | for proc_macro in &self.inner.exported_macros { | ||
142 | match proc_macro { | ||
143 | bridge::client::ProcMacro::CustomDerive { trait_name, client, .. } | ||
144 | if *trait_name == macro_name => | ||
145 | { | ||
146 | let res = client.run( | ||
147 | &crate::proc_macro::bridge::server::SameThread, | ||
148 | crate::rustc_server::Rustc::default(), | ||
149 | parsed_body, | ||
150 | ); | ||
151 | return res.map(|it| it.subtree); | ||
152 | } | ||
153 | bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { | ||
154 | let res = client.run( | ||
155 | &crate::proc_macro::bridge::server::SameThread, | ||
156 | crate::rustc_server::Rustc::default(), | ||
157 | parsed_body, | ||
158 | ); | ||
159 | return res.map(|it| it.subtree); | ||
160 | } | ||
161 | bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { | ||
162 | let res = client.run( | ||
163 | &crate::proc_macro::bridge::server::SameThread, | ||
164 | crate::rustc_server::Rustc::default(), | ||
165 | parsed_attributes, | ||
166 | parsed_body, | ||
167 | ); | ||
168 | return res.map(|it| it.subtree); | ||
169 | } | ||
170 | _ => continue, | ||
171 | } | ||
172 | } | ||
173 | |||
174 | Err(bridge::PanicMessage::String("Nothing to expand".to_string())) | ||
175 | } | ||
176 | |||
177 | pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { | ||
178 | self.inner | ||
179 | .exported_macros | ||
180 | .iter() | ||
181 | .map(|proc_macro| match proc_macro { | ||
182 | bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { | ||
183 | (trait_name.to_string(), ProcMacroKind::CustomDerive) | ||
184 | } | ||
185 | bridge::client::ProcMacro::Bang { name, .. } => { | ||
186 | (name.to_string(), ProcMacroKind::FuncLike) | ||
187 | } | ||
188 | bridge::client::ProcMacro::Attr { name, .. } => { | ||
189 | (name.to_string(), ProcMacroKind::Attr) | ||
190 | } | ||
191 | }) | ||
192 | .collect() | ||
193 | } | ||
194 | } | ||
195 | |||
196 | /// Copy the dylib to temp directory to prevent locking in Windows | ||
197 | #[cfg(windows)] | ||
198 | fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> { | ||
199 | use std::{ffi::OsString, time::SystemTime}; | ||
200 | |||
201 | let mut to = std::env::temp_dir(); | ||
202 | |||
203 | let file_name = path.file_name().ok_or_else(|| { | ||
204 | io::Error::new( | ||
205 | io::ErrorKind::InvalidInput, | ||
206 | format!("File path is invalid: {}", path.display()), | ||
207 | ) | ||
208 | })?; | ||
209 | |||
210 | // generate a time deps unique number | ||
211 | let t = SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("Time went backwards"); | ||
212 | |||
213 | let mut unique_name = OsString::from(t.as_millis().to_string()); | ||
214 | unique_name.push(file_name); | ||
215 | |||
216 | to.push(unique_name); | ||
217 | std::fs::copy(path, &to).unwrap(); | ||
218 | Ok(to) | ||
219 | } | ||
220 | |||
221 | #[cfg(unix)] | ||
222 | fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> { | ||
223 | Ok(path.to_path_buf()) | ||
224 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs deleted file mode 100644 index 1fc2eef82..000000000 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ /dev/null | |||
@@ -1,69 +0,0 @@ | |||
1 | //! RA Proc Macro Server | ||
2 | //! | ||
3 | //! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code. | ||
4 | //! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander. | ||
5 | //! | ||
6 | //! But we adapt it to better fit RA needs: | ||
7 | //! | ||
8 | //! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with | ||
9 | //! RA than `proc-macro2` token stream. | ||
10 | //! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable` | ||
11 | //! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue) | ||
12 | |||
13 | #[allow(dead_code)] | ||
14 | #[doc(hidden)] | ||
15 | mod proc_macro; | ||
16 | |||
17 | #[doc(hidden)] | ||
18 | mod rustc_server; | ||
19 | |||
20 | mod dylib; | ||
21 | |||
22 | use proc_macro::bridge::client::TokenStream; | ||
23 | use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; | ||
24 | use std::{ | ||
25 | collections::{hash_map::Entry, HashMap}, | ||
26 | fs, | ||
27 | path::{Path, PathBuf}, | ||
28 | time::SystemTime, | ||
29 | }; | ||
30 | |||
31 | #[derive(Default)] | ||
32 | pub(crate) struct ProcMacroSrv { | ||
33 | expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>, | ||
34 | } | ||
35 | |||
36 | impl ProcMacroSrv { | ||
37 | pub fn expand(&mut self, task: &ExpansionTask) -> Result<ExpansionResult, String> { | ||
38 | let expander = self.expander(&task.lib)?; | ||
39 | match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { | ||
40 | Ok(expansion) => Ok(ExpansionResult { expansion }), | ||
41 | Err(msg) => { | ||
42 | Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result<ListMacrosResult, String> { | ||
48 | let expander = self.expander(&task.lib)?; | ||
49 | Ok(ListMacrosResult { macros: expander.list_macros() }) | ||
50 | } | ||
51 | |||
52 | fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> { | ||
53 | let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| { | ||
54 | format!("Failed to get file metadata for {}: {:?}", path.display(), err) | ||
55 | })?; | ||
56 | |||
57 | Ok(match self.expanders.entry((path.to_path_buf(), time)) { | ||
58 | Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| { | ||
59 | format!("Cannot create expander for {}: {:?}", path.display(), err) | ||
60 | })?), | ||
61 | Entry::Occupied(e) => e.into_mut(), | ||
62 | }) | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub mod cli; | ||
67 | |||
68 | #[cfg(test)] | ||
69 | mod tests; | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/buffer.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/buffer.rs deleted file mode 100644 index dae6ff1d1..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/buffer.rs +++ /dev/null | |||
@@ -1,149 +0,0 @@ | |||
1 | //! lib-proc-macro Buffer management for same-process client<->server communication. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/buffer.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use std::io::{self, Write}; | ||
7 | use std::mem; | ||
8 | use std::ops::{Deref, DerefMut}; | ||
9 | use std::slice; | ||
10 | |||
11 | #[repr(C)] | ||
12 | struct Slice<'a, T> { | ||
13 | data: &'a [T; 0], | ||
14 | len: usize, | ||
15 | } | ||
16 | |||
17 | unsafe impl<'a, T: Sync> Sync for Slice<'a, T> {} | ||
18 | unsafe impl<'a, T: Sync> Send for Slice<'a, T> {} | ||
19 | |||
20 | impl<'a, T> Copy for Slice<'a, T> {} | ||
21 | impl<'a, T> Clone for Slice<'a, T> { | ||
22 | fn clone(&self) -> Self { | ||
23 | *self | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl<'a, T> From<&'a [T]> for Slice<'a, T> { | ||
28 | fn from(xs: &'a [T]) -> Self { | ||
29 | Slice { data: unsafe { &*(xs.as_ptr() as *const [T; 0]) }, len: xs.len() } | ||
30 | } | ||
31 | } | ||
32 | |||
33 | impl<'a, T> Deref for Slice<'a, T> { | ||
34 | type Target = [T]; | ||
35 | fn deref(&self) -> &[T] { | ||
36 | unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } | ||
37 | } | ||
38 | } | ||
39 | |||
40 | #[repr(C)] | ||
41 | pub struct Buffer<T: Copy> { | ||
42 | data: *mut T, | ||
43 | len: usize, | ||
44 | capacity: usize, | ||
45 | extend_from_slice: extern "C" fn(Buffer<T>, Slice<'_, T>) -> Buffer<T>, | ||
46 | drop: extern "C" fn(Buffer<T>), | ||
47 | } | ||
48 | |||
49 | unsafe impl<T: Copy + Sync> Sync for Buffer<T> {} | ||
50 | unsafe impl<T: Copy + Send> Send for Buffer<T> {} | ||
51 | |||
52 | impl<T: Copy> Default for Buffer<T> { | ||
53 | fn default() -> Self { | ||
54 | Self::from(vec![]) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl<T: Copy> Deref for Buffer<T> { | ||
59 | type Target = [T]; | ||
60 | fn deref(&self) -> &[T] { | ||
61 | unsafe { slice::from_raw_parts(self.data as *const T, self.len) } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | impl<T: Copy> DerefMut for Buffer<T> { | ||
66 | fn deref_mut(&mut self) -> &mut [T] { | ||
67 | unsafe { slice::from_raw_parts_mut(self.data, self.len) } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | impl<T: Copy> Buffer<T> { | ||
72 | pub(super) fn new() -> Self { | ||
73 | Self::default() | ||
74 | } | ||
75 | |||
76 | pub(super) fn clear(&mut self) { | ||
77 | self.len = 0; | ||
78 | } | ||
79 | |||
80 | pub(super) fn take(&mut self) -> Self { | ||
81 | mem::take(self) | ||
82 | } | ||
83 | |||
84 | pub(super) fn extend_from_slice(&mut self, xs: &[T]) { | ||
85 | // Fast path to avoid going through an FFI call. | ||
86 | if let Some(final_len) = self.len.checked_add(xs.len()) { | ||
87 | if final_len <= self.capacity { | ||
88 | let dst = unsafe { slice::from_raw_parts_mut(self.data, self.capacity) }; | ||
89 | dst[self.len..][..xs.len()].copy_from_slice(xs); | ||
90 | self.len = final_len; | ||
91 | return; | ||
92 | } | ||
93 | } | ||
94 | let b = self.take(); | ||
95 | *self = (b.extend_from_slice)(b, Slice::from(xs)); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | impl Write for Buffer<u8> { | ||
100 | fn write(&mut self, xs: &[u8]) -> io::Result<usize> { | ||
101 | self.extend_from_slice(xs); | ||
102 | Ok(xs.len()) | ||
103 | } | ||
104 | |||
105 | fn write_all(&mut self, xs: &[u8]) -> io::Result<()> { | ||
106 | self.extend_from_slice(xs); | ||
107 | Ok(()) | ||
108 | } | ||
109 | |||
110 | fn flush(&mut self) -> io::Result<()> { | ||
111 | Ok(()) | ||
112 | } | ||
113 | } | ||
114 | |||
115 | impl<T: Copy> Drop for Buffer<T> { | ||
116 | fn drop(&mut self) { | ||
117 | let b = self.take(); | ||
118 | (b.drop)(b); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | impl<T: Copy> From<Vec<T>> for Buffer<T> { | ||
123 | fn from(mut v: Vec<T>) -> Self { | ||
124 | let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); | ||
125 | mem::forget(v); | ||
126 | |||
127 | // This utility function is nested in here because it can *only* | ||
128 | // be safely called on `Buffer`s created by *this* `proc_macro`. | ||
129 | fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> { | ||
130 | unsafe { | ||
131 | let Buffer { data, len, capacity, .. } = b; | ||
132 | mem::forget(b); | ||
133 | Vec::from_raw_parts(data, len, capacity) | ||
134 | } | ||
135 | } | ||
136 | |||
137 | extern "C" fn extend_from_slice<T: Copy>(b: Buffer<T>, xs: Slice<'_, T>) -> Buffer<T> { | ||
138 | let mut v = to_vec(b); | ||
139 | v.extend_from_slice(&xs); | ||
140 | Buffer::from(v) | ||
141 | } | ||
142 | |||
143 | extern "C" fn drop<T: Copy>(b: Buffer<T>) { | ||
144 | mem::drop(to_vec(b)); | ||
145 | } | ||
146 | |||
147 | Buffer { data, len, capacity, extend_from_slice, drop } | ||
148 | } | ||
149 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/client.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/client.rs deleted file mode 100644 index cb4b3bdb0..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/client.rs +++ /dev/null | |||
@@ -1,478 +0,0 @@ | |||
1 | //! lib-proc-macro Client-side types. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/client.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use super::*; | ||
7 | |||
8 | macro_rules! define_handles { | ||
9 | ( | ||
10 | 'owned: $($oty:ident,)* | ||
11 | 'interned: $($ity:ident,)* | ||
12 | ) => { | ||
13 | #[repr(C)] | ||
14 | #[allow(non_snake_case)] | ||
15 | pub struct HandleCounters { | ||
16 | $($oty: AtomicUsize,)* | ||
17 | $($ity: AtomicUsize,)* | ||
18 | } | ||
19 | |||
20 | impl HandleCounters { | ||
21 | // FIXME(eddyb) use a reference to the `static COUNTERS`, instead of | ||
22 | // a wrapper `fn` pointer, once `const fn` can reference `static`s. | ||
23 | extern "C" fn get() -> &'static Self { | ||
24 | static COUNTERS: HandleCounters = HandleCounters { | ||
25 | $($oty: AtomicUsize::new(1),)* | ||
26 | $($ity: AtomicUsize::new(1),)* | ||
27 | }; | ||
28 | &COUNTERS | ||
29 | } | ||
30 | } | ||
31 | |||
32 | // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`. | ||
33 | #[repr(C)] | ||
34 | #[allow(non_snake_case)] | ||
35 | pub(super) struct HandleStore<S: server::Types> { | ||
36 | $($oty: handle::OwnedStore<S::$oty>,)* | ||
37 | $($ity: handle::InternedStore<S::$ity>,)* | ||
38 | } | ||
39 | |||
40 | impl<S: server::Types> HandleStore<S> { | ||
41 | pub(super) fn new(handle_counters: &'static HandleCounters) -> Self { | ||
42 | HandleStore { | ||
43 | $($oty: handle::OwnedStore::new(&handle_counters.$oty),)* | ||
44 | $($ity: handle::InternedStore::new(&handle_counters.$ity),)* | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | $( | ||
50 | #[repr(C)] | ||
51 | pub struct $oty(pub(crate) handle::Handle); | ||
52 | // impl !Send for $oty {} | ||
53 | // impl !Sync for $oty {} | ||
54 | |||
55 | // Forward `Drop::drop` to the inherent `drop` method. | ||
56 | impl Drop for $oty { | ||
57 | fn drop(&mut self) { | ||
58 | $oty(self.0).drop(); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl<S> Encode<S> for $oty { | ||
63 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
64 | let handle = self.0; | ||
65 | mem::forget(self); | ||
66 | handle.encode(w, s); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>> | ||
71 | for Marked<S::$oty, $oty> | ||
72 | { | ||
73 | fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self { | ||
74 | s.$oty.take(handle::Handle::decode(r, &mut ())) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | impl<S> Encode<S> for &$oty { | ||
79 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
80 | self.0.encode(w, s); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl<'s, S: server::Types,> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>> | ||
85 | for &'s Marked<S::$oty, $oty> | ||
86 | { | ||
87 | fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self { | ||
88 | &s.$oty[handle::Handle::decode(r, &mut ())] | ||
89 | } | ||
90 | } | ||
91 | |||
92 | impl<S> Encode<S> for &mut $oty { | ||
93 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
94 | self.0.encode(w, s); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | impl<'s, S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>> | ||
99 | for &'s mut Marked<S::$oty, $oty> | ||
100 | { | ||
101 | fn decode( | ||
102 | r: &mut Reader<'_>, | ||
103 | s: &'s mut HandleStore<server::MarkedTypes<S>> | ||
104 | ) -> Self { | ||
105 | &mut s.$oty[handle::Handle::decode(r, &mut ())] | ||
106 | } | ||
107 | } | ||
108 | |||
109 | impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>> | ||
110 | for Marked<S::$oty, $oty> | ||
111 | { | ||
112 | fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) { | ||
113 | s.$oty.alloc(self).encode(w, s); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | impl<S> DecodeMut<'_, '_, S> for $oty { | ||
118 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
119 | $oty(handle::Handle::decode(r, s)) | ||
120 | } | ||
121 | } | ||
122 | )* | ||
123 | |||
124 | $( | ||
125 | #[repr(C)] | ||
126 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
127 | pub(crate) struct $ity(handle::Handle); | ||
128 | // impl !Send for $ity {} | ||
129 | // impl !Sync for $ity {} | ||
130 | |||
131 | impl<S> Encode<S> for $ity { | ||
132 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
133 | self.0.encode(w, s); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>> | ||
138 | for Marked<S::$ity, $ity> | ||
139 | { | ||
140 | fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self { | ||
141 | s.$ity.copy(handle::Handle::decode(r, &mut ())) | ||
142 | } | ||
143 | } | ||
144 | |||
145 | impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>> | ||
146 | for Marked<S::$ity, $ity> | ||
147 | { | ||
148 | fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) { | ||
149 | s.$ity.alloc(self).encode(w, s); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | impl<S> DecodeMut<'_, '_, S> for $ity { | ||
154 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
155 | $ity(handle::Handle::decode(r, s)) | ||
156 | } | ||
157 | } | ||
158 | )* | ||
159 | } | ||
160 | } | ||
161 | define_handles! { | ||
162 | 'owned: | ||
163 | TokenStream, | ||
164 | TokenStreamBuilder, | ||
165 | TokenStreamIter, | ||
166 | Group, | ||
167 | Literal, | ||
168 | SourceFile, | ||
169 | MultiSpan, | ||
170 | Diagnostic, | ||
171 | |||
172 | 'interned: | ||
173 | Punct, | ||
174 | Ident, | ||
175 | Span, | ||
176 | } | ||
177 | |||
178 | // FIXME(eddyb) generate these impls by pattern-matching on the | ||
179 | // names of methods - also could use the presence of `fn drop` | ||
180 | // to distinguish between 'owned and 'interned, above. | ||
181 | // Alternatively, special 'modes" could be listed of types in with_api | ||
182 | // instead of pattern matching on methods, here and in server decl. | ||
183 | |||
184 | impl Clone for TokenStream { | ||
185 | fn clone(&self) -> Self { | ||
186 | self.clone() | ||
187 | } | ||
188 | } | ||
189 | |||
190 | impl Clone for TokenStreamIter { | ||
191 | fn clone(&self) -> Self { | ||
192 | self.clone() | ||
193 | } | ||
194 | } | ||
195 | |||
196 | impl Clone for Group { | ||
197 | fn clone(&self) -> Self { | ||
198 | self.clone() | ||
199 | } | ||
200 | } | ||
201 | |||
202 | impl Clone for Literal { | ||
203 | fn clone(&self) -> Self { | ||
204 | self.clone() | ||
205 | } | ||
206 | } | ||
207 | |||
208 | impl fmt::Debug for Literal { | ||
209 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
210 | f.debug_struct("Literal") | ||
211 | // format the kind without quotes, as in `kind: Float` | ||
212 | // .field("kind", &format_args!("{}", &self.debug_kind())) | ||
213 | .field("symbol", &self.symbol()) | ||
214 | // format `Some("...")` on one line even in {:#?} mode | ||
215 | // .field("suffix", &format_args!("{:?}", &self.suffix())) | ||
216 | .field("span", &self.span()) | ||
217 | .finish() | ||
218 | } | ||
219 | } | ||
220 | |||
221 | impl Clone for SourceFile { | ||
222 | fn clone(&self) -> Self { | ||
223 | self.clone() | ||
224 | } | ||
225 | } | ||
226 | |||
227 | impl fmt::Debug for Span { | ||
228 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
229 | f.write_str(&self.debug()) | ||
230 | } | ||
231 | } | ||
232 | |||
233 | macro_rules! define_client_side { | ||
234 | ($($name:ident { | ||
235 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* | ||
236 | }),* $(,)?) => { | ||
237 | $(impl $name { | ||
238 | #[allow(unused)] | ||
239 | $(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)* { | ||
240 | panic!("hello"); | ||
241 | // Bridge::with(|bridge| { | ||
242 | // let mut b = bridge.cached_buffer.take(); | ||
243 | |||
244 | // b.clear(); | ||
245 | // api_tags::Method::$name(api_tags::$name::$method).encode(&mut b, &mut ()); | ||
246 | // reverse_encode!(b; $($arg),*); | ||
247 | |||
248 | // b = bridge.dispatch.call(b); | ||
249 | |||
250 | // let r = Result::<_, PanicMessage>::decode(&mut &b[..], &mut ()); | ||
251 | |||
252 | // bridge.cached_buffer = b; | ||
253 | |||
254 | // r.unwrap_or_else(|e| panic::resume_unwind(e.into())) | ||
255 | // }) | ||
256 | })* | ||
257 | })* | ||
258 | } | ||
259 | } | ||
260 | with_api!(self, self, define_client_side); | ||
261 | |||
262 | enum BridgeState<'a> { | ||
263 | /// No server is currently connected to this client. | ||
264 | NotConnected, | ||
265 | |||
266 | /// A server is connected and available for requests. | ||
267 | Connected(Bridge<'a>), | ||
268 | |||
269 | /// Access to the bridge is being exclusively acquired | ||
270 | /// (e.g., during `BridgeState::with`). | ||
271 | InUse, | ||
272 | } | ||
273 | |||
274 | enum BridgeStateL {} | ||
275 | |||
276 | impl<'a> scoped_cell::ApplyL<'a> for BridgeStateL { | ||
277 | type Out = BridgeState<'a>; | ||
278 | } | ||
279 | |||
280 | thread_local! { | ||
281 | static BRIDGE_STATE: scoped_cell::ScopedCell<BridgeStateL> = | ||
282 | scoped_cell::ScopedCell::new(BridgeState::NotConnected); | ||
283 | } | ||
284 | |||
285 | impl BridgeState<'_> { | ||
286 | /// Take exclusive control of the thread-local | ||
287 | /// `BridgeState`, and pass it to `f`, mutably. | ||
288 | /// The state will be restored after `f` exits, even | ||
289 | /// by panic, including modifications made to it by `f`. | ||
290 | /// | ||
291 | /// N.B., while `f` is running, the thread-local state | ||
292 | /// is `BridgeState::InUse`. | ||
293 | fn with<R>(f: impl FnOnce(&mut BridgeState<'_>) -> R) -> R { | ||
294 | BRIDGE_STATE.with(|state| { | ||
295 | state.replace(BridgeState::InUse, |mut state| { | ||
296 | // FIXME(#52812) pass `f` directly to `replace` when `RefMutL` is gone | ||
297 | f(&mut *state) | ||
298 | }) | ||
299 | }) | ||
300 | } | ||
301 | } | ||
302 | |||
303 | impl Bridge<'_> { | ||
304 | fn enter<R>(self, f: impl FnOnce() -> R) -> R { | ||
305 | // Hide the default panic output within `proc_macro` expansions. | ||
306 | // NB. the server can't do this because it may use a different libstd. | ||
307 | static HIDE_PANICS_DURING_EXPANSION: Once = Once::new(); | ||
308 | HIDE_PANICS_DURING_EXPANSION.call_once(|| { | ||
309 | let prev = panic::take_hook(); | ||
310 | panic::set_hook(Box::new(move |info| { | ||
311 | let hide = BridgeState::with(|state| match state { | ||
312 | BridgeState::NotConnected => false, | ||
313 | BridgeState::Connected(_) | BridgeState::InUse => true, | ||
314 | }); | ||
315 | if !hide { | ||
316 | prev(info) | ||
317 | } | ||
318 | })); | ||
319 | }); | ||
320 | |||
321 | BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f)) | ||
322 | } | ||
323 | |||
324 | fn with<R>(f: impl FnOnce(&mut Bridge<'_>) -> R) -> R { | ||
325 | BridgeState::with(|state| match state { | ||
326 | BridgeState::NotConnected => { | ||
327 | panic!("procedural macro API is used outside of a procedural macro"); | ||
328 | } | ||
329 | BridgeState::InUse => { | ||
330 | panic!("procedural macro API is used while it's already in use"); | ||
331 | } | ||
332 | BridgeState::Connected(bridge) => f(bridge), | ||
333 | }) | ||
334 | } | ||
335 | } | ||
336 | |||
337 | /// A client-side "global object" (usually a function pointer), | ||
338 | /// which may be using a different `proc_macro` from the one | ||
339 | /// used by the server, but can be interacted with compatibly. | ||
340 | /// | ||
341 | /// N.B., `F` must have FFI-friendly memory layout (e.g., a pointer). | ||
342 | /// The call ABI of function pointers used for `F` doesn't | ||
343 | /// need to match between server and client, since it's only | ||
344 | /// passed between them and (eventually) called by the client. | ||
345 | #[repr(C)] | ||
346 | #[derive(Copy, Clone)] | ||
347 | pub struct Client<F> { | ||
348 | // FIXME(eddyb) use a reference to the `static COUNTERS`, instead of | ||
349 | // a wrapper `fn` pointer, once `const fn` can reference `static`s. | ||
350 | pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters, | ||
351 | pub(super) run: extern "C" fn(Bridge<'_>, F) -> Buffer<u8>, | ||
352 | pub(super) f: F, | ||
353 | } | ||
354 | |||
355 | /// Client-side helper for handling client panics, entering the bridge, | ||
356 | /// deserializing input and serializing output. | ||
357 | // FIXME(eddyb) maybe replace `Bridge::enter` with this? | ||
358 | fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>( | ||
359 | mut bridge: Bridge<'_>, | ||
360 | f: impl FnOnce(A) -> R, | ||
361 | ) -> Buffer<u8> { | ||
362 | // The initial `cached_buffer` contains the input. | ||
363 | let mut b = bridge.cached_buffer.take(); | ||
364 | |||
365 | panic::catch_unwind(panic::AssertUnwindSafe(|| { | ||
366 | bridge.enter(|| { | ||
367 | let reader = &mut &b[..]; | ||
368 | let input = A::decode(reader, &mut ()); | ||
369 | |||
370 | // Put the `cached_buffer` back in the `Bridge`, for requests. | ||
371 | Bridge::with(|bridge| bridge.cached_buffer = b.take()); | ||
372 | |||
373 | let output = f(input); | ||
374 | |||
375 | // Take the `cached_buffer` back out, for the output value. | ||
376 | b = Bridge::with(|bridge| bridge.cached_buffer.take()); | ||
377 | |||
378 | // HACK(eddyb) Separate encoding a success value (`Ok(output)`) | ||
379 | // from encoding a panic (`Err(e: PanicMessage)`) to avoid | ||
380 | // having handles outside the `bridge.enter(|| ...)` scope, and | ||
381 | // to catch panics that could happen while encoding the success. | ||
382 | // | ||
383 | // Note that panics should be impossible beyond this point, but | ||
384 | // this is defensively trying to avoid any accidental panicking | ||
385 | // reaching the `extern "C"` (which should `abort` but may not | ||
386 | // at the moment, so this is also potentially preventing UB). | ||
387 | b.clear(); | ||
388 | Ok::<_, ()>(output).encode(&mut b, &mut ()); | ||
389 | }) | ||
390 | })) | ||
391 | .map_err(PanicMessage::from) | ||
392 | .unwrap_or_else(|e| { | ||
393 | b.clear(); | ||
394 | Err::<(), _>(e).encode(&mut b, &mut ()); | ||
395 | }); | ||
396 | b | ||
397 | } | ||
398 | |||
399 | impl Client<fn(crate::TokenStream) -> crate::TokenStream> { | ||
400 | pub fn expand1(f: fn(crate::TokenStream) -> crate::TokenStream) -> Self { | ||
401 | extern "C" fn run( | ||
402 | bridge: Bridge<'_>, | ||
403 | f: impl FnOnce(crate::TokenStream) -> crate::TokenStream, | ||
404 | ) -> Buffer<u8> { | ||
405 | run_client(bridge, |input| f(crate::TokenStream(input)).0) | ||
406 | } | ||
407 | Client { get_handle_counters: HandleCounters::get, run, f } | ||
408 | } | ||
409 | } | ||
410 | |||
411 | impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> { | ||
412 | pub fn expand2(f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream) -> Self { | ||
413 | extern "C" fn run( | ||
414 | bridge: Bridge<'_>, | ||
415 | f: impl FnOnce(crate::TokenStream, crate::TokenStream) -> crate::TokenStream, | ||
416 | ) -> Buffer<u8> { | ||
417 | run_client(bridge, |(input, input2)| { | ||
418 | f(crate::TokenStream(input), crate::TokenStream(input2)).0 | ||
419 | }) | ||
420 | } | ||
421 | Client { get_handle_counters: HandleCounters::get, run, f } | ||
422 | } | ||
423 | } | ||
424 | |||
425 | #[repr(C)] | ||
426 | #[derive(Copy, Clone)] | ||
427 | pub enum ProcMacro { | ||
428 | CustomDerive { | ||
429 | trait_name: &'static str, | ||
430 | attributes: &'static [&'static str], | ||
431 | client: Client<fn(crate::TokenStream) -> crate::TokenStream>, | ||
432 | }, | ||
433 | |||
434 | Attr { | ||
435 | name: &'static str, | ||
436 | client: Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream>, | ||
437 | }, | ||
438 | |||
439 | Bang { | ||
440 | name: &'static str, | ||
441 | client: Client<fn(crate::TokenStream) -> crate::TokenStream>, | ||
442 | }, | ||
443 | } | ||
444 | |||
445 | impl std::fmt::Debug for ProcMacro { | ||
446 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
447 | write!(f, "ProcMacro {{ name: {} }}", self.name()) | ||
448 | } | ||
449 | } | ||
450 | |||
451 | impl ProcMacro { | ||
452 | pub fn name(&self) -> &'static str { | ||
453 | match self { | ||
454 | ProcMacro::CustomDerive { trait_name, .. } => trait_name, | ||
455 | ProcMacro::Attr { name, .. } => name, | ||
456 | ProcMacro::Bang { name, .. } => name, | ||
457 | } | ||
458 | } | ||
459 | |||
460 | pub fn custom_derive( | ||
461 | trait_name: &'static str, | ||
462 | attributes: &'static [&'static str], | ||
463 | expand: fn(crate::TokenStream) -> crate::TokenStream, | ||
464 | ) -> Self { | ||
465 | ProcMacro::CustomDerive { trait_name, attributes, client: Client::expand1(expand) } | ||
466 | } | ||
467 | |||
468 | pub fn attr( | ||
469 | name: &'static str, | ||
470 | expand: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream, | ||
471 | ) -> Self { | ||
472 | ProcMacro::Attr { name, client: Client::expand2(expand) } | ||
473 | } | ||
474 | |||
475 | pub fn bang(name: &'static str, expand: fn(crate::TokenStream) -> crate::TokenStream) -> Self { | ||
476 | ProcMacro::Bang { name, client: Client::expand1(expand) } | ||
477 | } | ||
478 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs deleted file mode 100644 index 273a97715..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | //! lib-proc-macro Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/closure.rs# | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | #[repr(C)] | ||
7 | pub struct Closure<'a, A, R> { | ||
8 | call: unsafe extern "C" fn(&mut Env, A) -> R, | ||
9 | env: &'a mut Env, | ||
10 | } | ||
11 | |||
12 | struct Env; | ||
13 | |||
14 | // impl<'a, A, R> !Sync for Closure<'a, A, R> {} | ||
15 | // impl<'a, A, R> !Send for Closure<'a, A, R> {} | ||
16 | |||
17 | impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> { | ||
18 | fn from(f: &'a mut F) -> Self { | ||
19 | unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: &mut Env, arg: A) -> R { | ||
20 | (*(env as *mut _ as *mut F))(arg) | ||
21 | } | ||
22 | Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } } | ||
23 | } | ||
24 | } | ||
25 | |||
26 | impl<'a, A, R> Closure<'a, A, R> { | ||
27 | pub fn call(&mut self, arg: A) -> R { | ||
28 | unsafe { (self.call)(self.env, arg) } | ||
29 | } | ||
30 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/handle.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/handle.rs deleted file mode 100644 index a2f77b5ac..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/handle.rs +++ /dev/null | |||
@@ -1,73 +0,0 @@ | |||
1 | //! lib-proc-macro Server-side handles and storage for per-handle data. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/handle.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use std::collections::{BTreeMap, HashMap}; | ||
7 | use std::hash::Hash; | ||
8 | use std::num::NonZeroU32; | ||
9 | use std::ops::{Index, IndexMut}; | ||
10 | use std::sync::atomic::{AtomicUsize, Ordering}; | ||
11 | |||
12 | pub(super) type Handle = NonZeroU32; | ||
13 | |||
14 | pub(super) struct OwnedStore<T: 'static> { | ||
15 | counter: &'static AtomicUsize, | ||
16 | data: BTreeMap<Handle, T>, | ||
17 | } | ||
18 | |||
19 | impl<T> OwnedStore<T> { | ||
20 | pub(super) fn new(counter: &'static AtomicUsize) -> Self { | ||
21 | // Ensure the handle counter isn't 0, which would panic later, | ||
22 | // when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`. | ||
23 | assert_ne!(counter.load(Ordering::SeqCst), 0); | ||
24 | |||
25 | OwnedStore { counter, data: BTreeMap::new() } | ||
26 | } | ||
27 | } | ||
28 | |||
29 | impl<T> OwnedStore<T> { | ||
30 | pub(super) fn alloc(&mut self, x: T) -> Handle { | ||
31 | let counter = self.counter.fetch_add(1, Ordering::SeqCst); | ||
32 | let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed"); | ||
33 | assert!(self.data.insert(handle, x).is_none()); | ||
34 | handle | ||
35 | } | ||
36 | |||
37 | pub(super) fn take(&mut self, h: Handle) -> T { | ||
38 | self.data.remove(&h).expect("use-after-free in `proc_macro` handle") | ||
39 | } | ||
40 | } | ||
41 | |||
42 | impl<T> Index<Handle> for OwnedStore<T> { | ||
43 | type Output = T; | ||
44 | fn index(&self, h: Handle) -> &T { | ||
45 | self.data.get(&h).expect("use-after-free in `proc_macro` handle") | ||
46 | } | ||
47 | } | ||
48 | |||
49 | impl<T> IndexMut<Handle> for OwnedStore<T> { | ||
50 | fn index_mut(&mut self, h: Handle) -> &mut T { | ||
51 | self.data.get_mut(&h).expect("use-after-free in `proc_macro` handle") | ||
52 | } | ||
53 | } | ||
54 | |||
55 | pub(super) struct InternedStore<T: 'static> { | ||
56 | owned: OwnedStore<T>, | ||
57 | interner: HashMap<T, Handle>, | ||
58 | } | ||
59 | |||
60 | impl<T: Copy + Eq + Hash> InternedStore<T> { | ||
61 | pub(super) fn new(counter: &'static AtomicUsize) -> Self { | ||
62 | InternedStore { owned: OwnedStore::new(counter), interner: HashMap::new() } | ||
63 | } | ||
64 | |||
65 | pub(super) fn alloc(&mut self, x: T) -> Handle { | ||
66 | let owned = &mut self.owned; | ||
67 | *self.interner.entry(x).or_insert_with(|| owned.alloc(x)) | ||
68 | } | ||
69 | |||
70 | pub(super) fn copy(&mut self, h: Handle) -> T { | ||
71 | self.owned[h] | ||
72 | } | ||
73 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/mod.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/mod.rs deleted file mode 100644 index aeb05aad4..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/mod.rs +++ /dev/null | |||
@@ -1,408 +0,0 @@ | |||
1 | //! lib-proc-macro Internal interface for communicating between a `proc_macro` client | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/mod.rs | ||
4 | //! augmented with removing unstable features | ||
5 | //! | ||
6 | //! Internal interface for communicating between a `proc_macro` client | ||
7 | //! (a proc macro crate) and a `proc_macro` server (a compiler front-end). | ||
8 | //! | ||
9 | //! Serialization (with C ABI buffers) and unique integer handles are employed | ||
10 | //! to allow safely interfacing between two copies of `proc_macro` built | ||
11 | //! (from the same source) by different compilers with potentially mismatching | ||
12 | //! Rust ABIs (e.g., stage0/bin/rustc vs stage1/bin/rustc during bootstrap). | ||
13 | |||
14 | #![deny(unsafe_code)] | ||
15 | |||
16 | pub use crate::proc_macro::{Delimiter, Level, LineColumn, Spacing}; | ||
17 | use std::fmt; | ||
18 | use std::hash::Hash; | ||
19 | use std::marker; | ||
20 | use std::mem; | ||
21 | use std::ops::Bound; | ||
22 | use std::panic; | ||
23 | use std::sync::atomic::AtomicUsize; | ||
24 | use std::sync::Once; | ||
25 | use std::thread; | ||
26 | |||
27 | /// Higher-order macro describing the server RPC API, allowing automatic | ||
28 | /// generation of type-safe Rust APIs, both client-side and server-side. | ||
29 | /// | ||
30 | /// `with_api!(MySelf, my_self, my_macro)` expands to: | ||
31 | /// ```rust,ignore (pseudo-code) | ||
32 | /// my_macro! { | ||
33 | /// // ... | ||
34 | /// Literal { | ||
35 | /// // ... | ||
36 | /// fn character(ch: char) -> MySelf::Literal; | ||
37 | /// // ... | ||
38 | /// fn span(my_self: &MySelf::Literal) -> MySelf::Span; | ||
39 | /// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span); | ||
40 | /// }, | ||
41 | /// // ... | ||
42 | /// } | ||
43 | /// ``` | ||
44 | /// | ||
45 | /// The first two arguments serve to customize the arguments names | ||
46 | /// and argument/return types, to enable several different usecases: | ||
47 | /// | ||
48 | /// If `my_self` is just `self`, then each `fn` signature can be used | ||
49 | /// as-is for a method. If it's anything else (`self_` in practice), | ||
50 | /// then the signatures don't have a special `self` argument, and | ||
51 | /// can, therefore, have a different one introduced. | ||
52 | /// | ||
53 | /// If `MySelf` is just `Self`, then the types are only valid inside | ||
54 | /// a trait or a trait impl, where the trait has associated types | ||
55 | /// for each of the API types. If non-associated types are desired, | ||
56 | /// a module name (`self` in practice) can be used instead of `Self`. | ||
57 | macro_rules! with_api { | ||
58 | ($S:ident, $self:ident, $m:ident) => { | ||
59 | $m! { | ||
60 | TokenStream { | ||
61 | fn drop($self: $S::TokenStream); | ||
62 | fn clone($self: &$S::TokenStream) -> $S::TokenStream; | ||
63 | fn new() -> $S::TokenStream; | ||
64 | fn is_empty($self: &$S::TokenStream) -> bool; | ||
65 | fn from_str(src: &str) -> $S::TokenStream; | ||
66 | fn to_string($self: &$S::TokenStream) -> String; | ||
67 | fn from_token_tree( | ||
68 | tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>, | ||
69 | ) -> $S::TokenStream; | ||
70 | fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter; | ||
71 | }, | ||
72 | TokenStreamBuilder { | ||
73 | fn drop($self: $S::TokenStreamBuilder); | ||
74 | fn new() -> $S::TokenStreamBuilder; | ||
75 | fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream); | ||
76 | fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream; | ||
77 | }, | ||
78 | TokenStreamIter { | ||
79 | fn drop($self: $S::TokenStreamIter); | ||
80 | fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter; | ||
81 | fn next( | ||
82 | $self: &mut $S::TokenStreamIter, | ||
83 | ) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>; | ||
84 | }, | ||
85 | Group { | ||
86 | fn drop($self: $S::Group); | ||
87 | fn clone($self: &$S::Group) -> $S::Group; | ||
88 | fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group; | ||
89 | fn delimiter($self: &$S::Group) -> Delimiter; | ||
90 | fn stream($self: &$S::Group) -> $S::TokenStream; | ||
91 | fn span($self: &$S::Group) -> $S::Span; | ||
92 | fn span_open($self: &$S::Group) -> $S::Span; | ||
93 | fn span_close($self: &$S::Group) -> $S::Span; | ||
94 | fn set_span($self: &mut $S::Group, span: $S::Span); | ||
95 | }, | ||
96 | Punct { | ||
97 | fn new(ch: char, spacing: Spacing) -> $S::Punct; | ||
98 | fn as_char($self: $S::Punct) -> char; | ||
99 | fn spacing($self: $S::Punct) -> Spacing; | ||
100 | fn span($self: $S::Punct) -> $S::Span; | ||
101 | fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct; | ||
102 | }, | ||
103 | Ident { | ||
104 | fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident; | ||
105 | fn span($self: $S::Ident) -> $S::Span; | ||
106 | fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident; | ||
107 | }, | ||
108 | Literal { | ||
109 | fn drop($self: $S::Literal); | ||
110 | fn clone($self: &$S::Literal) -> $S::Literal; | ||
111 | fn debug_kind($self: &$S::Literal) -> String; | ||
112 | fn symbol($self: &$S::Literal) -> String; | ||
113 | fn suffix($self: &$S::Literal) -> Option<String>; | ||
114 | fn integer(n: &str) -> $S::Literal; | ||
115 | fn typed_integer(n: &str, kind: &str) -> $S::Literal; | ||
116 | fn float(n: &str) -> $S::Literal; | ||
117 | fn f32(n: &str) -> $S::Literal; | ||
118 | fn f64(n: &str) -> $S::Literal; | ||
119 | fn string(string: &str) -> $S::Literal; | ||
120 | fn character(ch: char) -> $S::Literal; | ||
121 | fn byte_string(bytes: &[u8]) -> $S::Literal; | ||
122 | fn span($self: &$S::Literal) -> $S::Span; | ||
123 | fn set_span($self: &mut $S::Literal, span: $S::Span); | ||
124 | fn subspan( | ||
125 | $self: &$S::Literal, | ||
126 | start: Bound<usize>, | ||
127 | end: Bound<usize>, | ||
128 | ) -> Option<$S::Span>; | ||
129 | }, | ||
130 | SourceFile { | ||
131 | fn drop($self: $S::SourceFile); | ||
132 | fn clone($self: &$S::SourceFile) -> $S::SourceFile; | ||
133 | fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool; | ||
134 | fn path($self: &$S::SourceFile) -> String; | ||
135 | fn is_real($self: &$S::SourceFile) -> bool; | ||
136 | }, | ||
137 | MultiSpan { | ||
138 | fn drop($self: $S::MultiSpan); | ||
139 | fn new() -> $S::MultiSpan; | ||
140 | fn push($self: &mut $S::MultiSpan, span: $S::Span); | ||
141 | }, | ||
142 | Diagnostic { | ||
143 | fn drop($self: $S::Diagnostic); | ||
144 | fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic; | ||
145 | fn sub( | ||
146 | $self: &mut $S::Diagnostic, | ||
147 | level: Level, | ||
148 | msg: &str, | ||
149 | span: $S::MultiSpan, | ||
150 | ); | ||
151 | fn emit($self: $S::Diagnostic); | ||
152 | }, | ||
153 | Span { | ||
154 | fn debug($self: $S::Span) -> String; | ||
155 | fn def_site() -> $S::Span; | ||
156 | fn call_site() -> $S::Span; | ||
157 | fn mixed_site() -> $S::Span; | ||
158 | fn source_file($self: $S::Span) -> $S::SourceFile; | ||
159 | fn parent($self: $S::Span) -> Option<$S::Span>; | ||
160 | fn source($self: $S::Span) -> $S::Span; | ||
161 | fn start($self: $S::Span) -> LineColumn; | ||
162 | fn end($self: $S::Span) -> LineColumn; | ||
163 | fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; | ||
164 | fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; | ||
165 | fn source_text($self: $S::Span) -> Option<String>; | ||
166 | }, | ||
167 | } | ||
168 | }; | ||
169 | } | ||
170 | |||
171 | // FIXME(eddyb) this calls `encode` for each argument, but in reverse, | ||
172 | // to avoid borrow conflicts from borrows started by `&mut` arguments. | ||
173 | macro_rules! reverse_encode { | ||
174 | ($writer:ident;) => {}; | ||
175 | ($writer:ident; $first:ident $(, $rest:ident)*) => { | ||
176 | reverse_encode!($writer; $($rest),*); | ||
177 | $first.encode(&mut $writer, &mut ()); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | // FIXME(eddyb) this calls `decode` for each argument, but in reverse, | ||
182 | // to avoid borrow conflicts from borrows started by `&mut` arguments. | ||
183 | macro_rules! reverse_decode { | ||
184 | ($reader:ident, $s:ident;) => {}; | ||
185 | ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => { | ||
186 | reverse_decode!($reader, $s; $($rest: $rest_ty),*); | ||
187 | let $first = <$first_ty>::decode(&mut $reader, $s); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | #[allow(unsafe_code)] | ||
192 | mod buffer; | ||
193 | #[forbid(unsafe_code)] | ||
194 | pub mod client; | ||
195 | #[allow(unsafe_code)] | ||
196 | mod closure; | ||
197 | #[forbid(unsafe_code)] | ||
198 | mod handle; | ||
199 | #[macro_use] | ||
200 | #[forbid(unsafe_code)] | ||
201 | mod rpc; | ||
202 | #[allow(unsafe_code)] | ||
203 | mod scoped_cell; | ||
204 | #[forbid(unsafe_code)] | ||
205 | pub mod server; | ||
206 | |||
207 | use buffer::Buffer; | ||
208 | pub use rpc::PanicMessage; | ||
209 | use rpc::{Decode, DecodeMut, Encode, Reader, Writer}; | ||
210 | |||
211 | /// An active connection between a server and a client. | ||
212 | /// The server creates the bridge (`Bridge::run_server` in `server.rs`), | ||
213 | /// then passes it to the client through the function pointer in the `run` | ||
214 | /// field of `client::Client`. The client holds its copy of the `Bridge` | ||
215 | /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`). | ||
216 | #[repr(C)] | ||
217 | pub struct Bridge<'a> { | ||
218 | /// Reusable buffer (only `clear`-ed, never shrunk), primarily | ||
219 | /// used for making requests, but also for passing input to client. | ||
220 | cached_buffer: Buffer<u8>, | ||
221 | |||
222 | /// Server-side function that the client uses to make requests. | ||
223 | dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>, | ||
224 | } | ||
225 | |||
226 | // impl<'a> !Sync for Bridge<'a> {} | ||
227 | // impl<'a> !Send for Bridge<'a> {} | ||
228 | |||
229 | #[forbid(unsafe_code)] | ||
230 | #[allow(non_camel_case_types)] | ||
231 | mod api_tags { | ||
232 | use super::rpc::{DecodeMut, Encode, Reader, Writer}; | ||
233 | |||
234 | macro_rules! declare_tags { | ||
235 | ($($name:ident { | ||
236 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* | ||
237 | }),* $(,)?) => { | ||
238 | $( | ||
239 | pub(super) enum $name { | ||
240 | $($method),* | ||
241 | } | ||
242 | rpc_encode_decode!(enum $name { $($method),* }); | ||
243 | )* | ||
244 | |||
245 | |||
246 | pub(super) enum Method { | ||
247 | $($name($name)),* | ||
248 | } | ||
249 | rpc_encode_decode!(enum Method { $($name(m)),* }); | ||
250 | } | ||
251 | } | ||
252 | with_api!(self, self, declare_tags); | ||
253 | } | ||
254 | |||
255 | /// Helper to wrap associated types to allow trait impl dispatch. | ||
256 | /// That is, normally a pair of impls for `T::Foo` and `T::Bar` | ||
257 | /// can overlap, but if the impls are, instead, on types like | ||
258 | /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't. | ||
259 | trait Mark { | ||
260 | type Unmarked; | ||
261 | fn mark(unmarked: Self::Unmarked) -> Self; | ||
262 | } | ||
263 | |||
264 | /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details). | ||
265 | trait Unmark { | ||
266 | type Unmarked; | ||
267 | fn unmark(self) -> Self::Unmarked; | ||
268 | } | ||
269 | |||
270 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
271 | struct Marked<T, M> { | ||
272 | value: T, | ||
273 | _marker: marker::PhantomData<M>, | ||
274 | } | ||
275 | |||
276 | impl<T, M> Mark for Marked<T, M> { | ||
277 | type Unmarked = T; | ||
278 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
279 | Marked { value: unmarked, _marker: marker::PhantomData } | ||
280 | } | ||
281 | } | ||
282 | impl<T, M> Unmark for Marked<T, M> { | ||
283 | type Unmarked = T; | ||
284 | fn unmark(self) -> Self::Unmarked { | ||
285 | self.value | ||
286 | } | ||
287 | } | ||
288 | impl<'a, T, M> Unmark for &'a Marked<T, M> { | ||
289 | type Unmarked = &'a T; | ||
290 | fn unmark(self) -> Self::Unmarked { | ||
291 | &self.value | ||
292 | } | ||
293 | } | ||
294 | impl<'a, T, M> Unmark for &'a mut Marked<T, M> { | ||
295 | type Unmarked = &'a mut T; | ||
296 | fn unmark(self) -> Self::Unmarked { | ||
297 | &mut self.value | ||
298 | } | ||
299 | } | ||
300 | |||
301 | impl<T: Mark> Mark for Option<T> { | ||
302 | type Unmarked = Option<T::Unmarked>; | ||
303 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
304 | unmarked.map(T::mark) | ||
305 | } | ||
306 | } | ||
307 | impl<T: Unmark> Unmark for Option<T> { | ||
308 | type Unmarked = Option<T::Unmarked>; | ||
309 | fn unmark(self) -> Self::Unmarked { | ||
310 | self.map(T::unmark) | ||
311 | } | ||
312 | } | ||
313 | |||
314 | macro_rules! mark_noop { | ||
315 | ($($ty:ty),* $(,)?) => { | ||
316 | $( | ||
317 | impl Mark for $ty { | ||
318 | type Unmarked = Self; | ||
319 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
320 | unmarked | ||
321 | } | ||
322 | } | ||
323 | impl Unmark for $ty { | ||
324 | type Unmarked = Self; | ||
325 | fn unmark(self) -> Self::Unmarked { | ||
326 | self | ||
327 | } | ||
328 | } | ||
329 | )* | ||
330 | } | ||
331 | } | ||
332 | mark_noop! { | ||
333 | (), | ||
334 | bool, | ||
335 | char, | ||
336 | &'_ [u8], | ||
337 | &'_ str, | ||
338 | String, | ||
339 | Delimiter, | ||
340 | Level, | ||
341 | LineColumn, | ||
342 | Spacing, | ||
343 | Bound<usize>, | ||
344 | } | ||
345 | |||
346 | rpc_encode_decode!( | ||
347 | enum Delimiter { | ||
348 | Parenthesis, | ||
349 | Brace, | ||
350 | Bracket, | ||
351 | None, | ||
352 | } | ||
353 | ); | ||
354 | rpc_encode_decode!( | ||
355 | enum Level { | ||
356 | Error, | ||
357 | Warning, | ||
358 | Note, | ||
359 | Help, | ||
360 | } | ||
361 | ); | ||
362 | rpc_encode_decode!(struct LineColumn { line, column }); | ||
363 | rpc_encode_decode!( | ||
364 | enum Spacing { | ||
365 | Alone, | ||
366 | Joint, | ||
367 | } | ||
368 | ); | ||
369 | |||
370 | #[derive(Clone)] | ||
371 | pub enum TokenTree<G, P, I, L> { | ||
372 | Group(G), | ||
373 | Punct(P), | ||
374 | Ident(I), | ||
375 | Literal(L), | ||
376 | } | ||
377 | |||
378 | impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> { | ||
379 | type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>; | ||
380 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
381 | match unmarked { | ||
382 | TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)), | ||
383 | TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)), | ||
384 | TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)), | ||
385 | TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)), | ||
386 | } | ||
387 | } | ||
388 | } | ||
389 | impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> { | ||
390 | type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>; | ||
391 | fn unmark(self) -> Self::Unmarked { | ||
392 | match self { | ||
393 | TokenTree::Group(tt) => TokenTree::Group(tt.unmark()), | ||
394 | TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()), | ||
395 | TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()), | ||
396 | TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()), | ||
397 | } | ||
398 | } | ||
399 | } | ||
400 | |||
401 | rpc_encode_decode!( | ||
402 | enum TokenTree<G, P, I, L> { | ||
403 | Group(tt), | ||
404 | Punct(tt), | ||
405 | Ident(tt), | ||
406 | Literal(tt), | ||
407 | } | ||
408 | ); | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/rpc.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/rpc.rs deleted file mode 100644 index 3528d5c99..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/rpc.rs +++ /dev/null | |||
@@ -1,311 +0,0 @@ | |||
1 | //! lib-proc-macro Serialization for client-server communication. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/rpc.rs | ||
4 | //! augmented with removing unstable features | ||
5 | //! | ||
6 | //! Serialization for client-server communication. | ||
7 | |||
8 | use std::any::Any; | ||
9 | use std::char; | ||
10 | use std::io::Write; | ||
11 | use std::num::NonZeroU32; | ||
12 | use std::ops::Bound; | ||
13 | use std::str; | ||
14 | |||
15 | pub(super) type Writer = super::buffer::Buffer<u8>; | ||
16 | |||
17 | pub(super) trait Encode<S>: Sized { | ||
18 | fn encode(self, w: &mut Writer, s: &mut S); | ||
19 | } | ||
20 | |||
21 | pub(super) type Reader<'a> = &'a [u8]; | ||
22 | |||
23 | pub(super) trait Decode<'a, 's, S>: Sized { | ||
24 | fn decode(r: &mut Reader<'a>, s: &'s S) -> Self; | ||
25 | } | ||
26 | |||
27 | pub(super) trait DecodeMut<'a, 's, S>: Sized { | ||
28 | fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self; | ||
29 | } | ||
30 | |||
31 | macro_rules! rpc_encode_decode { | ||
32 | (le $ty:ty) => { | ||
33 | impl<S> Encode<S> for $ty { | ||
34 | fn encode(self, w: &mut Writer, _: &mut S) { | ||
35 | w.write_all(&self.to_le_bytes()).unwrap(); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl<S> DecodeMut<'_, '_, S> for $ty { | ||
40 | fn decode(r: &mut Reader<'_>, _: &mut S) -> Self { | ||
41 | const N: usize = ::std::mem::size_of::<$ty>(); | ||
42 | |||
43 | let mut bytes = [0; N]; | ||
44 | bytes.copy_from_slice(&r[..N]); | ||
45 | *r = &r[N..]; | ||
46 | |||
47 | Self::from_le_bytes(bytes) | ||
48 | } | ||
49 | } | ||
50 | }; | ||
51 | (struct $name:ident { $($field:ident),* $(,)? }) => { | ||
52 | impl<S> Encode<S> for $name { | ||
53 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
54 | $(self.$field.encode(w, s);)* | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl<S> DecodeMut<'_, '_, S> for $name { | ||
59 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
60 | $name { | ||
61 | $($field: DecodeMut::decode(r, s)),* | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | }; | ||
66 | (enum $name:ident $(<$($T:ident),+>)? { $($variant:ident $(($field:ident))*),* $(,)? }) => { | ||
67 | impl<S, $($($T: Encode<S>),+)?> Encode<S> for $name $(<$($T),+>)? { | ||
68 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
69 | // HACK(eddyb): `Tag` enum duplicated between the | ||
70 | // two impls as there's no other place to stash it. | ||
71 | #[allow(non_upper_case_globals)] | ||
72 | mod tag { | ||
73 | #[repr(u8)] enum Tag { $($variant),* } | ||
74 | |||
75 | $(pub const $variant: u8 = Tag::$variant as u8;)* | ||
76 | } | ||
77 | |||
78 | match self { | ||
79 | $($name::$variant $(($field))* => { | ||
80 | tag::$variant.encode(w, s); | ||
81 | $($field.encode(w, s);)* | ||
82 | })* | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | impl<'a, S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)?> DecodeMut<'a, '_, S> | ||
88 | for $name $(<$($T),+>)? | ||
89 | { | ||
90 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
91 | // HACK(eddyb): `Tag` enum duplicated between the | ||
92 | // two impls as there's no other place to stash it. | ||
93 | #[allow(non_upper_case_globals)] | ||
94 | mod tag { | ||
95 | #[repr(u8)] enum Tag { $($variant),* } | ||
96 | |||
97 | $(pub const $variant: u8 = Tag::$variant as u8;)* | ||
98 | } | ||
99 | |||
100 | match u8::decode(r, s) { | ||
101 | $(tag::$variant => { | ||
102 | $(let $field = DecodeMut::decode(r, s);)* | ||
103 | $name::$variant $(($field))* | ||
104 | })* | ||
105 | _ => unreachable!(), | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | impl<S> Encode<S> for () { | ||
113 | fn encode(self, _: &mut Writer, _: &mut S) {} | ||
114 | } | ||
115 | |||
116 | impl<S> DecodeMut<'_, '_, S> for () { | ||
117 | fn decode(_: &mut Reader<'_>, _: &mut S) -> Self {} | ||
118 | } | ||
119 | |||
120 | impl<S> Encode<S> for u8 { | ||
121 | fn encode(self, w: &mut Writer, _: &mut S) { | ||
122 | w.write_all(&[self]).unwrap(); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | impl<S> DecodeMut<'_, '_, S> for u8 { | ||
127 | fn decode(r: &mut Reader<'_>, _: &mut S) -> Self { | ||
128 | let x = r[0]; | ||
129 | *r = &r[1..]; | ||
130 | x | ||
131 | } | ||
132 | } | ||
133 | |||
134 | rpc_encode_decode!(le u32); | ||
135 | rpc_encode_decode!(le usize); | ||
136 | |||
137 | impl<S> Encode<S> for bool { | ||
138 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
139 | (self as u8).encode(w, s); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | impl<S> DecodeMut<'_, '_, S> for bool { | ||
144 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
145 | match u8::decode(r, s) { | ||
146 | 0 => false, | ||
147 | 1 => true, | ||
148 | _ => unreachable!(), | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | impl<S> Encode<S> for char { | ||
154 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
155 | (self as u32).encode(w, s); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | impl<S> DecodeMut<'_, '_, S> for char { | ||
160 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
161 | char::from_u32(u32::decode(r, s)).unwrap() | ||
162 | } | ||
163 | } | ||
164 | |||
165 | impl<S> Encode<S> for NonZeroU32 { | ||
166 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
167 | self.get().encode(w, s); | ||
168 | } | ||
169 | } | ||
170 | |||
171 | impl<S> DecodeMut<'_, '_, S> for NonZeroU32 { | ||
172 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
173 | Self::new(u32::decode(r, s)).unwrap() | ||
174 | } | ||
175 | } | ||
176 | |||
177 | impl<S, A: Encode<S>, B: Encode<S>> Encode<S> for (A, B) { | ||
178 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
179 | self.0.encode(w, s); | ||
180 | self.1.encode(w, s); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | impl<'a, S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> | ||
185 | for (A, B) | ||
186 | { | ||
187 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
188 | (DecodeMut::decode(r, s), DecodeMut::decode(r, s)) | ||
189 | } | ||
190 | } | ||
191 | |||
192 | rpc_encode_decode!( | ||
193 | enum Bound<T> { | ||
194 | Included(x), | ||
195 | Excluded(x), | ||
196 | Unbounded, | ||
197 | } | ||
198 | ); | ||
199 | |||
200 | rpc_encode_decode!( | ||
201 | enum Option<T> { | ||
202 | None, | ||
203 | Some(x), | ||
204 | } | ||
205 | ); | ||
206 | |||
207 | rpc_encode_decode!( | ||
208 | enum Result<T, E> { | ||
209 | Ok(x), | ||
210 | Err(e), | ||
211 | } | ||
212 | ); | ||
213 | |||
214 | impl<S> Encode<S> for &[u8] { | ||
215 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
216 | self.len().encode(w, s); | ||
217 | w.write_all(self).unwrap(); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | impl<'a, S> DecodeMut<'a, '_, S> for &'a [u8] { | ||
222 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
223 | let len = usize::decode(r, s); | ||
224 | let xs = &r[..len]; | ||
225 | *r = &r[len..]; | ||
226 | xs | ||
227 | } | ||
228 | } | ||
229 | |||
230 | impl<S> Encode<S> for &str { | ||
231 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
232 | self.as_bytes().encode(w, s); | ||
233 | } | ||
234 | } | ||
235 | |||
236 | impl<'a, S> DecodeMut<'a, '_, S> for &'a str { | ||
237 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
238 | str::from_utf8(<&[u8]>::decode(r, s)).unwrap() | ||
239 | } | ||
240 | } | ||
241 | |||
242 | impl<S> Encode<S> for String { | ||
243 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
244 | self[..].encode(w, s); | ||
245 | } | ||
246 | } | ||
247 | |||
248 | impl<S> DecodeMut<'_, '_, S> for String { | ||
249 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
250 | <&str>::decode(r, s).to_string() | ||
251 | } | ||
252 | } | ||
253 | |||
254 | /// Simplied version of panic payloads, ignoring | ||
255 | /// types other than `&'static str` and `String`. | ||
256 | #[derive(Debug)] | ||
257 | pub enum PanicMessage { | ||
258 | StaticStr(&'static str), | ||
259 | String(String), | ||
260 | Unknown, | ||
261 | } | ||
262 | |||
263 | impl From<Box<dyn Any + Send>> for PanicMessage { | ||
264 | fn from(payload: Box<dyn Any + Send + 'static>) -> Self { | ||
265 | if let Some(s) = payload.downcast_ref::<&'static str>() { | ||
266 | return PanicMessage::StaticStr(s); | ||
267 | } | ||
268 | if let Ok(s) = payload.downcast::<String>() { | ||
269 | return PanicMessage::String(*s); | ||
270 | } | ||
271 | PanicMessage::Unknown | ||
272 | } | ||
273 | } | ||
274 | |||
275 | impl Into<Box<dyn Any + Send>> for PanicMessage { | ||
276 | fn into(self) -> Box<dyn Any + Send> { | ||
277 | match self { | ||
278 | PanicMessage::StaticStr(s) => Box::new(s), | ||
279 | PanicMessage::String(s) => Box::new(s), | ||
280 | PanicMessage::Unknown => { | ||
281 | struct UnknownPanicMessage; | ||
282 | Box::new(UnknownPanicMessage) | ||
283 | } | ||
284 | } | ||
285 | } | ||
286 | } | ||
287 | |||
288 | impl PanicMessage { | ||
289 | pub fn as_str(&self) -> Option<&str> { | ||
290 | match self { | ||
291 | PanicMessage::StaticStr(s) => Some(s), | ||
292 | PanicMessage::String(s) => Some(s), | ||
293 | PanicMessage::Unknown => None, | ||
294 | } | ||
295 | } | ||
296 | } | ||
297 | |||
298 | impl<S> Encode<S> for PanicMessage { | ||
299 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
300 | self.as_str().encode(w, s); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | impl<S> DecodeMut<'_, '_, S> for PanicMessage { | ||
305 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
306 | match Option::<String>::decode(r, s) { | ||
307 | Some(s) => PanicMessage::String(s), | ||
308 | None => PanicMessage::Unknown, | ||
309 | } | ||
310 | } | ||
311 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/scoped_cell.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/scoped_cell.rs deleted file mode 100644 index 6ef7ea43c..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/scoped_cell.rs +++ /dev/null | |||
@@ -1,84 +0,0 @@ | |||
1 | //! lib-proc-macro `Cell` variant for (scoped) existential lifetimes. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/scoped_cell.rs#L1 | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use std::cell::Cell; | ||
7 | use std::mem; | ||
8 | use std::ops::{Deref, DerefMut}; | ||
9 | |||
10 | /// Type lambda application, with a lifetime. | ||
11 | #[allow(unused_lifetimes)] | ||
12 | pub trait ApplyL<'a> { | ||
13 | type Out; | ||
14 | } | ||
15 | |||
16 | /// Type lambda taking a lifetime, i.e., `Lifetime -> Type`. | ||
17 | pub trait LambdaL: for<'a> ApplyL<'a> {} | ||
18 | |||
19 | impl<T: for<'a> ApplyL<'a>> LambdaL for T {} | ||
20 | |||
21 | // HACK(eddyb) work around projection limitations with a newtype | ||
22 | // FIXME(#52812) replace with `&'a mut <T as ApplyL<'b>>::Out` | ||
23 | pub struct RefMutL<'a, 'b, T: LambdaL>(&'a mut <T as ApplyL<'b>>::Out); | ||
24 | |||
25 | impl<'a, 'b, T: LambdaL> Deref for RefMutL<'a, 'b, T> { | ||
26 | type Target = <T as ApplyL<'b>>::Out; | ||
27 | fn deref(&self) -> &Self::Target { | ||
28 | self.0 | ||
29 | } | ||
30 | } | ||
31 | |||
32 | impl<'a, 'b, T: LambdaL> DerefMut for RefMutL<'a, 'b, T> { | ||
33 | fn deref_mut(&mut self) -> &mut Self::Target { | ||
34 | self.0 | ||
35 | } | ||
36 | } | ||
37 | |||
38 | pub struct ScopedCell<T: LambdaL>(Cell<<T as ApplyL<'static>>::Out>); | ||
39 | |||
40 | impl<T: LambdaL> ScopedCell<T> { | ||
41 | pub fn new(value: <T as ApplyL<'static>>::Out) -> Self { | ||
42 | ScopedCell(Cell::new(value)) | ||
43 | } | ||
44 | |||
45 | /// Sets the value in `self` to `replacement` while | ||
46 | /// running `f`, which gets the old value, mutably. | ||
47 | /// The old value will be restored after `f` exits, even | ||
48 | /// by panic, including modifications made to it by `f`. | ||
49 | pub fn replace<'a, R>( | ||
50 | &self, | ||
51 | replacement: <T as ApplyL<'a>>::Out, | ||
52 | f: impl for<'b, 'c> FnOnce(RefMutL<'b, 'c, T>) -> R, | ||
53 | ) -> R { | ||
54 | /// Wrapper that ensures that the cell always gets filled | ||
55 | /// (with the original state, optionally changed by `f`), | ||
56 | /// even if `f` had panicked. | ||
57 | struct PutBackOnDrop<'a, T: LambdaL> { | ||
58 | cell: &'a ScopedCell<T>, | ||
59 | value: Option<<T as ApplyL<'static>>::Out>, | ||
60 | } | ||
61 | |||
62 | impl<'a, T: LambdaL> Drop for PutBackOnDrop<'a, T> { | ||
63 | fn drop(&mut self) { | ||
64 | self.cell.0.set(self.value.take().unwrap()); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | let mut put_back_on_drop = PutBackOnDrop { | ||
69 | cell: self, | ||
70 | value: Some(self.0.replace(unsafe { | ||
71 | let erased = mem::transmute_copy(&replacement); | ||
72 | mem::forget(replacement); | ||
73 | erased | ||
74 | })), | ||
75 | }; | ||
76 | |||
77 | f(RefMutL(put_back_on_drop.value.as_mut().unwrap())) | ||
78 | } | ||
79 | |||
80 | /// Sets the value in `self` to `value` while running `f`. | ||
81 | pub fn set<R>(&self, value: <T as ApplyL<'_>>::Out, f: impl FnOnce() -> R) -> R { | ||
82 | self.replace(value, |_| f()) | ||
83 | } | ||
84 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/server.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/server.rs deleted file mode 100644 index 45d41ac02..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/bridge/server.rs +++ /dev/null | |||
@@ -1,323 +0,0 @@ | |||
1 | //! lib-proc-macro server-side traits | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/server.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use super::*; | ||
7 | |||
8 | // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`. | ||
9 | use super::client::HandleStore; | ||
10 | |||
11 | /// Declare an associated item of one of the traits below, optionally | ||
12 | /// adjusting it (i.e., adding bounds to types and default bodies to methods). | ||
13 | macro_rules! associated_item { | ||
14 | (type TokenStream) => | ||
15 | (type TokenStream: 'static + Clone;); | ||
16 | (type TokenStreamBuilder) => | ||
17 | (type TokenStreamBuilder: 'static;); | ||
18 | (type TokenStreamIter) => | ||
19 | (type TokenStreamIter: 'static + Clone;); | ||
20 | (type Group) => | ||
21 | (type Group: 'static + Clone;); | ||
22 | (type Punct) => | ||
23 | (type Punct: 'static + Copy + Eq + Hash;); | ||
24 | (type Ident) => | ||
25 | (type Ident: 'static + Copy + Eq + Hash;); | ||
26 | (type Literal) => | ||
27 | (type Literal: 'static + Clone;); | ||
28 | (type SourceFile) => | ||
29 | (type SourceFile: 'static + Clone;); | ||
30 | (type MultiSpan) => | ||
31 | (type MultiSpan: 'static;); | ||
32 | (type Diagnostic) => | ||
33 | (type Diagnostic: 'static;); | ||
34 | (type Span) => | ||
35 | (type Span: 'static + Copy + Eq + Hash;); | ||
36 | (fn drop(&mut self, $arg:ident: $arg_ty:ty)) => | ||
37 | (fn drop(&mut self, $arg: $arg_ty) { mem::drop($arg) }); | ||
38 | (fn clone(&mut self, $arg:ident: $arg_ty:ty) -> $ret_ty:ty) => | ||
39 | (fn clone(&mut self, $arg: $arg_ty) -> $ret_ty { $arg.clone() }); | ||
40 | ($($item:tt)*) => ($($item)*;) | ||
41 | } | ||
42 | |||
43 | macro_rules! declare_server_traits { | ||
44 | ($($name:ident { | ||
45 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* | ||
46 | }),* $(,)?) => { | ||
47 | pub trait Types { | ||
48 | $(associated_item!(type $name);)* | ||
49 | } | ||
50 | |||
51 | $(pub trait $name: Types { | ||
52 | $(associated_item!(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?);)* | ||
53 | })* | ||
54 | |||
55 | pub trait Server: Types $(+ $name)* {} | ||
56 | impl<S: Types $(+ $name)*> Server for S {} | ||
57 | } | ||
58 | } | ||
59 | with_api!(Self, self_, declare_server_traits); | ||
60 | |||
61 | pub(super) struct MarkedTypes<S: Types>(S); | ||
62 | |||
63 | macro_rules! define_mark_types_impls { | ||
64 | ($($name:ident { | ||
65 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* | ||
66 | }),* $(,)?) => { | ||
67 | impl<S: Types> Types for MarkedTypes<S> { | ||
68 | $(type $name = Marked<S::$name, client::$name>;)* | ||
69 | } | ||
70 | |||
71 | $(impl<S: $name> $name for MarkedTypes<S> { | ||
72 | $(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)? { | ||
73 | <_>::mark($name::$method(&mut self.0, $($arg.unmark()),*)) | ||
74 | })* | ||
75 | })* | ||
76 | } | ||
77 | } | ||
78 | with_api!(Self, self_, define_mark_types_impls); | ||
79 | |||
80 | struct Dispatcher<S: Types> { | ||
81 | handle_store: HandleStore<S>, | ||
82 | server: S, | ||
83 | } | ||
84 | |||
85 | macro_rules! define_dispatcher_impl { | ||
86 | ($($name:ident { | ||
87 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* | ||
88 | }),* $(,)?) => { | ||
89 | // FIXME(eddyb) `pub` only for `ExecutionStrategy` below. | ||
90 | pub trait DispatcherTrait { | ||
91 | // HACK(eddyb) these are here to allow `Self::$name` to work below. | ||
92 | $(type $name;)* | ||
93 | fn dispatch(&mut self, b: Buffer<u8>) -> Buffer<u8>; | ||
94 | } | ||
95 | |||
96 | impl<S: Server> DispatcherTrait for Dispatcher<MarkedTypes<S>> { | ||
97 | $(type $name = <MarkedTypes<S> as Types>::$name;)* | ||
98 | fn dispatch(&mut self, mut b: Buffer<u8>) -> Buffer<u8> { | ||
99 | let Dispatcher { handle_store, server } = self; | ||
100 | |||
101 | let mut reader = &b[..]; | ||
102 | match api_tags::Method::decode(&mut reader, &mut ()) { | ||
103 | $(api_tags::Method::$name(m) => match m { | ||
104 | $(api_tags::$name::$method => { | ||
105 | let mut call_method = || { | ||
106 | reverse_decode!(reader, handle_store; $($arg: $arg_ty),*); | ||
107 | $name::$method(server, $($arg),*) | ||
108 | }; | ||
109 | // HACK(eddyb) don't use `panic::catch_unwind` in a panic. | ||
110 | // If client and server happen to use the same `libstd`, | ||
111 | // `catch_unwind` asserts that the panic counter was 0, | ||
112 | // even when the closure passed to it didn't panic. | ||
113 | let r = if thread::panicking() { | ||
114 | Ok(call_method()) | ||
115 | } else { | ||
116 | panic::catch_unwind(panic::AssertUnwindSafe(call_method)) | ||
117 | .map_err(PanicMessage::from) | ||
118 | }; | ||
119 | |||
120 | b.clear(); | ||
121 | r.encode(&mut b, handle_store); | ||
122 | })* | ||
123 | }),* | ||
124 | } | ||
125 | b | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | with_api!(Self, self_, define_dispatcher_impl); | ||
131 | |||
132 | pub trait ExecutionStrategy { | ||
133 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
134 | &self, | ||
135 | dispatcher: &mut impl DispatcherTrait, | ||
136 | input: Buffer<u8>, | ||
137 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
138 | client_data: D, | ||
139 | ) -> Buffer<u8>; | ||
140 | } | ||
141 | |||
142 | pub struct SameThread; | ||
143 | |||
144 | impl ExecutionStrategy for SameThread { | ||
145 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
146 | &self, | ||
147 | dispatcher: &mut impl DispatcherTrait, | ||
148 | input: Buffer<u8>, | ||
149 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
150 | client_data: D, | ||
151 | ) -> Buffer<u8> { | ||
152 | let mut dispatch = |b| dispatcher.dispatch(b); | ||
153 | |||
154 | run_client(Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, client_data) | ||
155 | } | ||
156 | } | ||
157 | |||
158 | // NOTE(eddyb) Two implementations are provided, the second one is a bit | ||
159 | // faster but neither is anywhere near as fast as same-thread execution. | ||
160 | |||
161 | pub struct CrossThread1; | ||
162 | |||
163 | impl ExecutionStrategy for CrossThread1 { | ||
164 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
165 | &self, | ||
166 | dispatcher: &mut impl DispatcherTrait, | ||
167 | input: Buffer<u8>, | ||
168 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
169 | client_data: D, | ||
170 | ) -> Buffer<u8> { | ||
171 | use std::sync::mpsc::channel; | ||
172 | |||
173 | let (req_tx, req_rx) = channel(); | ||
174 | let (res_tx, res_rx) = channel(); | ||
175 | |||
176 | let join_handle = thread::spawn(move || { | ||
177 | let mut dispatch = |b| { | ||
178 | req_tx.send(b).unwrap(); | ||
179 | res_rx.recv().unwrap() | ||
180 | }; | ||
181 | |||
182 | run_client( | ||
183 | Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, | ||
184 | client_data, | ||
185 | ) | ||
186 | }); | ||
187 | |||
188 | for b in req_rx { | ||
189 | res_tx.send(dispatcher.dispatch(b)).unwrap(); | ||
190 | } | ||
191 | |||
192 | join_handle.join().unwrap() | ||
193 | } | ||
194 | } | ||
195 | |||
196 | pub struct CrossThread2; | ||
197 | |||
198 | impl ExecutionStrategy for CrossThread2 { | ||
199 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
200 | &self, | ||
201 | dispatcher: &mut impl DispatcherTrait, | ||
202 | input: Buffer<u8>, | ||
203 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
204 | client_data: D, | ||
205 | ) -> Buffer<u8> { | ||
206 | use std::sync::{Arc, Mutex}; | ||
207 | |||
208 | enum State<T> { | ||
209 | Req(T), | ||
210 | Res(T), | ||
211 | } | ||
212 | |||
213 | let mut state = Arc::new(Mutex::new(State::Res(Buffer::new()))); | ||
214 | |||
215 | let server_thread = thread::current(); | ||
216 | let state2 = state.clone(); | ||
217 | let join_handle = thread::spawn(move || { | ||
218 | let mut dispatch = |b| { | ||
219 | *state2.lock().unwrap() = State::Req(b); | ||
220 | server_thread.unpark(); | ||
221 | loop { | ||
222 | thread::park(); | ||
223 | if let State::Res(b) = &mut *state2.lock().unwrap() { | ||
224 | break b.take(); | ||
225 | } | ||
226 | } | ||
227 | }; | ||
228 | |||
229 | let r = run_client( | ||
230 | Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, | ||
231 | client_data, | ||
232 | ); | ||
233 | |||
234 | // Wake up the server so it can exit the dispatch loop. | ||
235 | drop(state2); | ||
236 | server_thread.unpark(); | ||
237 | |||
238 | r | ||
239 | }); | ||
240 | |||
241 | // Check whether `state2` was dropped, to know when to stop. | ||
242 | while Arc::get_mut(&mut state).is_none() { | ||
243 | thread::park(); | ||
244 | let mut b = match &mut *state.lock().unwrap() { | ||
245 | State::Req(b) => b.take(), | ||
246 | _ => continue, | ||
247 | }; | ||
248 | b = dispatcher.dispatch(b.take()); | ||
249 | *state.lock().unwrap() = State::Res(b); | ||
250 | join_handle.thread().unpark(); | ||
251 | } | ||
252 | |||
253 | join_handle.join().unwrap() | ||
254 | } | ||
255 | } | ||
256 | |||
257 | fn run_server< | ||
258 | S: Server, | ||
259 | I: Encode<HandleStore<MarkedTypes<S>>>, | ||
260 | O: for<'a, 's> DecodeMut<'a, 's, HandleStore<MarkedTypes<S>>>, | ||
261 | D: Copy + Send + 'static, | ||
262 | >( | ||
263 | strategy: &impl ExecutionStrategy, | ||
264 | handle_counters: &'static client::HandleCounters, | ||
265 | server: S, | ||
266 | input: I, | ||
267 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
268 | client_data: D, | ||
269 | ) -> Result<O, PanicMessage> { | ||
270 | let mut dispatcher = | ||
271 | Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) }; | ||
272 | |||
273 | let mut b = Buffer::new(); | ||
274 | input.encode(&mut b, &mut dispatcher.handle_store); | ||
275 | |||
276 | b = strategy.run_bridge_and_client(&mut dispatcher, b, run_client, client_data); | ||
277 | |||
278 | Result::decode(&mut &b[..], &mut dispatcher.handle_store) | ||
279 | } | ||
280 | |||
281 | impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> { | ||
282 | pub fn run<S: Server>( | ||
283 | &self, | ||
284 | strategy: &impl ExecutionStrategy, | ||
285 | server: S, | ||
286 | input: S::TokenStream, | ||
287 | ) -> Result<S::TokenStream, PanicMessage> { | ||
288 | let client::Client { get_handle_counters, run, f } = *self; | ||
289 | run_server( | ||
290 | strategy, | ||
291 | get_handle_counters(), | ||
292 | server, | ||
293 | <MarkedTypes<S> as Types>::TokenStream::mark(input), | ||
294 | run, | ||
295 | f, | ||
296 | ) | ||
297 | .map(<MarkedTypes<S> as Types>::TokenStream::unmark) | ||
298 | } | ||
299 | } | ||
300 | |||
301 | impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> { | ||
302 | pub fn run<S: Server>( | ||
303 | &self, | ||
304 | strategy: &impl ExecutionStrategy, | ||
305 | server: S, | ||
306 | input: S::TokenStream, | ||
307 | input2: S::TokenStream, | ||
308 | ) -> Result<S::TokenStream, PanicMessage> { | ||
309 | let client::Client { get_handle_counters, run, f } = *self; | ||
310 | run_server( | ||
311 | strategy, | ||
312 | get_handle_counters(), | ||
313 | server, | ||
314 | ( | ||
315 | <MarkedTypes<S> as Types>::TokenStream::mark(input), | ||
316 | <MarkedTypes<S> as Types>::TokenStream::mark(input2), | ||
317 | ), | ||
318 | run, | ||
319 | f, | ||
320 | ) | ||
321 | .map(<MarkedTypes<S> as Types>::TokenStream::unmark) | ||
322 | } | ||
323 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/diagnostic.rs b/crates/ra_proc_macro_srv/src/proc_macro/diagnostic.rs deleted file mode 100644 index 55d93917c..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/diagnostic.rs +++ /dev/null | |||
@@ -1,172 +0,0 @@ | |||
1 | //! lib-proc-macro diagnostic | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/diagnostic.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use crate::proc_macro::Span; | ||
7 | |||
8 | /// An enum representing a diagnostic level. | ||
9 | #[derive(Copy, Clone, Debug)] | ||
10 | #[non_exhaustive] | ||
11 | pub enum Level { | ||
12 | /// An error. | ||
13 | Error, | ||
14 | /// A warning. | ||
15 | Warning, | ||
16 | /// A note. | ||
17 | Note, | ||
18 | /// A help message. | ||
19 | Help, | ||
20 | } | ||
21 | |||
22 | /// Trait implemented by types that can be converted into a set of `Span`s. | ||
23 | pub trait MultiSpan { | ||
24 | /// Converts `self` into a `Vec<Span>`. | ||
25 | fn into_spans(self) -> Vec<Span>; | ||
26 | } | ||
27 | |||
28 | impl MultiSpan for Span { | ||
29 | fn into_spans(self) -> Vec<Span> { | ||
30 | vec![self] | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl MultiSpan for Vec<Span> { | ||
35 | fn into_spans(self) -> Vec<Span> { | ||
36 | self | ||
37 | } | ||
38 | } | ||
39 | |||
40 | impl<'a> MultiSpan for &'a [Span] { | ||
41 | fn into_spans(self) -> Vec<Span> { | ||
42 | self.to_vec() | ||
43 | } | ||
44 | } | ||
45 | |||
46 | /// A structure representing a diagnostic message and associated children | ||
47 | /// messages. | ||
48 | #[derive(Clone, Debug)] | ||
49 | pub struct Diagnostic { | ||
50 | level: Level, | ||
51 | message: String, | ||
52 | spans: Vec<Span>, | ||
53 | children: Vec<Diagnostic>, | ||
54 | } | ||
55 | |||
56 | macro_rules! diagnostic_child_methods { | ||
57 | ($spanned:ident, $regular:ident, $level:expr) => { | ||
58 | /// Adds a new child diagnostic message to `self` with the level | ||
59 | /// identified by this method's name with the given `spans` and | ||
60 | /// `message`. | ||
61 | pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic | ||
62 | where | ||
63 | S: MultiSpan, | ||
64 | T: Into<String>, | ||
65 | { | ||
66 | self.children.push(Diagnostic::spanned(spans, $level, message)); | ||
67 | self | ||
68 | } | ||
69 | |||
70 | /// Adds a new child diagnostic message to `self` with the level | ||
71 | /// identified by this method's name with the given `message`. | ||
72 | pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic { | ||
73 | self.children.push(Diagnostic::new($level, message)); | ||
74 | self | ||
75 | } | ||
76 | }; | ||
77 | } | ||
78 | |||
79 | /// Iterator over the children diagnostics of a `Diagnostic`. | ||
80 | #[derive(Debug, Clone)] | ||
81 | pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>); | ||
82 | |||
83 | impl<'a> Iterator for Children<'a> { | ||
84 | type Item = &'a Diagnostic; | ||
85 | |||
86 | fn next(&mut self) -> Option<Self::Item> { | ||
87 | self.0.next() | ||
88 | } | ||
89 | } | ||
90 | |||
91 | impl Diagnostic { | ||
92 | /// Creates a new diagnostic with the given `level` and `message`. | ||
93 | pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic { | ||
94 | Diagnostic { level: level, message: message.into(), spans: vec![], children: vec![] } | ||
95 | } | ||
96 | |||
97 | /// Creates a new diagnostic with the given `level` and `message` pointing to | ||
98 | /// the given set of `spans`. | ||
99 | pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic | ||
100 | where | ||
101 | S: MultiSpan, | ||
102 | T: Into<String>, | ||
103 | { | ||
104 | Diagnostic { | ||
105 | level: level, | ||
106 | message: message.into(), | ||
107 | spans: spans.into_spans(), | ||
108 | children: vec![], | ||
109 | } | ||
110 | } | ||
111 | |||
112 | diagnostic_child_methods!(span_error, error, Level::Error); | ||
113 | diagnostic_child_methods!(span_warning, warning, Level::Warning); | ||
114 | diagnostic_child_methods!(span_note, note, Level::Note); | ||
115 | diagnostic_child_methods!(span_help, help, Level::Help); | ||
116 | |||
117 | /// Returns the diagnostic `level` for `self`. | ||
118 | pub fn level(&self) -> Level { | ||
119 | self.level | ||
120 | } | ||
121 | |||
122 | /// Sets the level in `self` to `level`. | ||
123 | pub fn set_level(&mut self, level: Level) { | ||
124 | self.level = level; | ||
125 | } | ||
126 | |||
127 | /// Returns the message in `self`. | ||
128 | pub fn message(&self) -> &str { | ||
129 | &self.message | ||
130 | } | ||
131 | |||
132 | /// Sets the message in `self` to `message`. | ||
133 | pub fn set_message<T: Into<String>>(&mut self, message: T) { | ||
134 | self.message = message.into(); | ||
135 | } | ||
136 | |||
137 | /// Returns the `Span`s in `self`. | ||
138 | pub fn spans(&self) -> &[Span] { | ||
139 | &self.spans | ||
140 | } | ||
141 | |||
142 | /// Sets the `Span`s in `self` to `spans`. | ||
143 | pub fn set_spans<S: MultiSpan>(&mut self, spans: S) { | ||
144 | self.spans = spans.into_spans(); | ||
145 | } | ||
146 | |||
147 | /// Returns an iterator over the children diagnostics of `self`. | ||
148 | pub fn children(&self) -> Children<'_> { | ||
149 | Children(self.children.iter()) | ||
150 | } | ||
151 | |||
152 | /// Emit the diagnostic. | ||
153 | pub fn emit(self) { | ||
154 | fn to_internal(spans: Vec<Span>) -> crate::proc_macro::bridge::client::MultiSpan { | ||
155 | let mut multi_span = crate::proc_macro::bridge::client::MultiSpan::new(); | ||
156 | for span in spans { | ||
157 | multi_span.push(span.0); | ||
158 | } | ||
159 | multi_span | ||
160 | } | ||
161 | |||
162 | let mut diag = crate::proc_macro::bridge::client::Diagnostic::new( | ||
163 | self.level, | ||
164 | &self.message[..], | ||
165 | to_internal(self.spans), | ||
166 | ); | ||
167 | for c in self.children { | ||
168 | diag.sub(c.level, &c.message[..], to_internal(c.spans)); | ||
169 | } | ||
170 | diag.emit(); | ||
171 | } | ||
172 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/mod.rs b/crates/ra_proc_macro_srv/src/proc_macro/mod.rs deleted file mode 100644 index ee0dc9722..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/mod.rs +++ /dev/null | |||
@@ -1,926 +0,0 @@ | |||
1 | //! lib-proc-macro main module | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/lib.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | // NOTE(@edwin0cheng): | ||
7 | // Because we just copy the bridge module from rustc for ABI compatible | ||
8 | // There are some unused stuffs inside it. | ||
9 | // We suppress these warning here. | ||
10 | #[doc(hidden)] | ||
11 | #[allow(unused_macros)] | ||
12 | #[allow(unused_variables)] | ||
13 | pub mod bridge; | ||
14 | |||
15 | mod diagnostic; | ||
16 | |||
17 | pub use diagnostic::{Diagnostic, Level, MultiSpan}; | ||
18 | |||
19 | use std::ops::{Bound, RangeBounds}; | ||
20 | use std::path::PathBuf; | ||
21 | use std::str::FromStr; | ||
22 | use std::{fmt, iter, mem}; | ||
23 | |||
24 | /// The main type provided by this crate, representing an abstract stream of | ||
25 | /// tokens, or, more specifically, a sequence of token trees. | ||
26 | /// The type provide interfaces for iterating over those token trees and, conversely, | ||
27 | /// collecting a number of token trees into one stream. | ||
28 | /// | ||
29 | /// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]` | ||
30 | /// and `#[proc_macro_derive]` definitions. | ||
31 | #[derive(Clone)] | ||
32 | pub struct TokenStream(bridge::client::TokenStream); | ||
33 | |||
34 | /// Error returned from `TokenStream::from_str` | ||
35 | #[derive(Debug)] | ||
36 | pub struct LexError { | ||
37 | _inner: (), | ||
38 | } | ||
39 | |||
40 | impl TokenStream { | ||
41 | /// Returns an empty `TokenStream` containing no token trees. | ||
42 | pub fn new() -> TokenStream { | ||
43 | TokenStream(bridge::client::TokenStream::new()) | ||
44 | } | ||
45 | |||
46 | /// Checks if this `TokenStream` is empty. | ||
47 | pub fn is_empty(&self) -> bool { | ||
48 | self.0.is_empty() | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /// Attempts to break the string into tokens and parse those tokens into a token stream. | ||
53 | /// May fail for a number of reasons, for example, if the string contains unbalanced delimiters | ||
54 | /// or characters not existing in the language. | ||
55 | /// All tokens in the parsed stream get `Span::call_site()` spans. | ||
56 | /// | ||
57 | /// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to | ||
58 | /// change these errors into `LexError`s later. | ||
59 | impl FromStr for TokenStream { | ||
60 | type Err = LexError; | ||
61 | |||
62 | fn from_str(src: &str) -> Result<TokenStream, LexError> { | ||
63 | Ok(TokenStream(bridge::client::TokenStream::from_str(src))) | ||
64 | } | ||
65 | } | ||
66 | |||
67 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
68 | // based on it (the reverse of the usual relationship between the two). | ||
69 | // impl ToString for TokenStream { | ||
70 | // fn to_string(&self) -> String { | ||
71 | // self.0.to_string() | ||
72 | // } | ||
73 | // } | ||
74 | |||
75 | /// Prints the token stream as a string that is supposed to be losslessly convertible back | ||
76 | /// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s | ||
77 | /// with `Delimiter::None` delimiters and negative numeric literals. | ||
78 | impl fmt::Display for TokenStream { | ||
79 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
80 | f.write_str(&self.to_string()) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /// Prints token in a form convenient for debugging. | ||
85 | impl fmt::Debug for TokenStream { | ||
86 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
87 | f.write_str("TokenStream ")?; | ||
88 | f.debug_list().entries(self.clone()).finish() | ||
89 | } | ||
90 | } | ||
91 | |||
92 | /// Creates a token stream containing a single token tree. | ||
93 | impl From<TokenTree> for TokenStream { | ||
94 | fn from(tree: TokenTree) -> TokenStream { | ||
95 | TokenStream(bridge::client::TokenStream::from_token_tree(match tree { | ||
96 | TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0), | ||
97 | TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0), | ||
98 | TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0), | ||
99 | TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0), | ||
100 | })) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | /// Collects a number of token trees into a single stream. | ||
105 | impl iter::FromIterator<TokenTree> for TokenStream { | ||
106 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { | ||
107 | trees.into_iter().map(TokenStream::from).collect() | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /// A "flattening" operation on token streams, collects token trees | ||
112 | /// from multiple token streams into a single stream. | ||
113 | impl iter::FromIterator<TokenStream> for TokenStream { | ||
114 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { | ||
115 | let mut builder = bridge::client::TokenStreamBuilder::new(); | ||
116 | streams.into_iter().for_each(|stream| builder.push(stream.0)); | ||
117 | TokenStream(builder.build()) | ||
118 | } | ||
119 | } | ||
120 | |||
121 | impl Extend<TokenTree> for TokenStream { | ||
122 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) { | ||
123 | self.extend(trees.into_iter().map(TokenStream::from)); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | impl Extend<TokenStream> for TokenStream { | ||
128 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { | ||
129 | // FIXME(eddyb) Use an optimized implementation if/when possible. | ||
130 | *self = iter::once(mem::replace(self, Self::new())).chain(streams).collect(); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /// Public implementation details for the `TokenStream` type, such as iterators. | ||
135 | pub mod token_stream { | ||
136 | use crate::proc_macro::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree}; | ||
137 | |||
138 | /// An iterator over `TokenStream`'s `TokenTree`s. | ||
139 | /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups, | ||
140 | /// and returns whole groups as token trees. | ||
141 | #[derive(Clone)] | ||
142 | pub struct IntoIter(bridge::client::TokenStreamIter); | ||
143 | |||
144 | impl Iterator for IntoIter { | ||
145 | type Item = TokenTree; | ||
146 | |||
147 | fn next(&mut self) -> Option<TokenTree> { | ||
148 | self.0.next().map(|tree| match tree { | ||
149 | bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)), | ||
150 | bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)), | ||
151 | bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)), | ||
152 | bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)), | ||
153 | }) | ||
154 | } | ||
155 | } | ||
156 | |||
157 | impl IntoIterator for TokenStream { | ||
158 | type Item = TokenTree; | ||
159 | type IntoIter = IntoIter; | ||
160 | |||
161 | fn into_iter(self) -> IntoIter { | ||
162 | IntoIter(self.0.into_iter()) | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /// A region of source code, along with macro expansion information. | ||
168 | #[derive(Copy, Clone)] | ||
169 | pub struct Span(bridge::client::Span); | ||
170 | |||
171 | macro_rules! diagnostic_method { | ||
172 | ($name:ident, $level:expr) => { | ||
173 | /// Creates a new `Diagnostic` with the given `message` at the span | ||
174 | /// `self`. | ||
175 | pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic { | ||
176 | Diagnostic::spanned(self, $level, message) | ||
177 | } | ||
178 | }; | ||
179 | } | ||
180 | |||
181 | impl Span { | ||
182 | /// A span that resolves at the macro definition site. | ||
183 | pub fn def_site() -> Span { | ||
184 | Span(bridge::client::Span::def_site()) | ||
185 | } | ||
186 | |||
187 | /// The span of the invocation of the current procedural macro. | ||
188 | /// Identifiers created with this span will be resolved as if they were written | ||
189 | /// directly at the macro call location (call-site hygiene) and other code | ||
190 | /// at the macro call site will be able to refer to them as well. | ||
191 | pub fn call_site() -> Span { | ||
192 | Span(bridge::client::Span::call_site()) | ||
193 | } | ||
194 | |||
195 | /// A span that represents `macro_rules` hygiene, and sometimes resolves at the macro | ||
196 | /// definition site (local variables, labels, `$crate`) and sometimes at the macro | ||
197 | /// call site (everything else). | ||
198 | /// The span location is taken from the call-site. | ||
199 | pub fn mixed_site() -> Span { | ||
200 | Span(bridge::client::Span::mixed_site()) | ||
201 | } | ||
202 | |||
203 | /// The original source file into which this span points. | ||
204 | pub fn source_file(&self) -> SourceFile { | ||
205 | SourceFile(self.0.source_file()) | ||
206 | } | ||
207 | |||
208 | /// The `Span` for the tokens in the previous macro expansion from which | ||
209 | /// `self` was generated from, if any. | ||
210 | pub fn parent(&self) -> Option<Span> { | ||
211 | self.0.parent().map(Span) | ||
212 | } | ||
213 | |||
214 | /// The span for the origin source code that `self` was generated from. If | ||
215 | /// this `Span` wasn't generated from other macro expansions then the return | ||
216 | /// value is the same as `*self`. | ||
217 | pub fn source(&self) -> Span { | ||
218 | Span(self.0.source()) | ||
219 | } | ||
220 | |||
221 | /// Gets the starting line/column in the source file for this span. | ||
222 | pub fn start(&self) -> LineColumn { | ||
223 | self.0.start() | ||
224 | } | ||
225 | |||
226 | /// Gets the ending line/column in the source file for this span. | ||
227 | pub fn end(&self) -> LineColumn { | ||
228 | self.0.end() | ||
229 | } | ||
230 | |||
231 | /// Creates a new span encompassing `self` and `other`. | ||
232 | /// | ||
233 | /// Returns `None` if `self` and `other` are from different files. | ||
234 | pub fn join(&self, other: Span) -> Option<Span> { | ||
235 | self.0.join(other.0).map(Span) | ||
236 | } | ||
237 | |||
238 | /// Creates a new span with the same line/column information as `self` but | ||
239 | /// that resolves symbols as though it were at `other`. | ||
240 | pub fn resolved_at(&self, other: Span) -> Span { | ||
241 | Span(self.0.resolved_at(other.0)) | ||
242 | } | ||
243 | |||
244 | /// Creates a new span with the same name resolution behavior as `self` but | ||
245 | /// with the line/column information of `other`. | ||
246 | pub fn located_at(&self, other: Span) -> Span { | ||
247 | other.resolved_at(*self) | ||
248 | } | ||
249 | |||
250 | /// Compares to spans to see if they're equal. | ||
251 | pub fn eq(&self, other: &Span) -> bool { | ||
252 | self.0 == other.0 | ||
253 | } | ||
254 | |||
255 | /// Returns the source text behind a span. This preserves the original source | ||
256 | /// code, including spaces and comments. It only returns a result if the span | ||
257 | /// corresponds to real source code. | ||
258 | /// | ||
259 | /// Note: The observable result of a macro should only rely on the tokens and | ||
260 | /// not on this source text. The result of this function is a best effort to | ||
261 | /// be used for diagnostics only. | ||
262 | pub fn source_text(&self) -> Option<String> { | ||
263 | self.0.source_text() | ||
264 | } | ||
265 | |||
266 | diagnostic_method!(error, Level::Error); | ||
267 | diagnostic_method!(warning, Level::Warning); | ||
268 | diagnostic_method!(note, Level::Note); | ||
269 | diagnostic_method!(help, Level::Help); | ||
270 | } | ||
271 | |||
272 | /// Prints a span in a form convenient for debugging. | ||
273 | impl fmt::Debug for Span { | ||
274 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
275 | self.0.fmt(f) | ||
276 | } | ||
277 | } | ||
278 | |||
279 | /// A line-column pair representing the start or end of a `Span`. | ||
280 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
281 | pub struct LineColumn { | ||
282 | /// The 1-indexed line in the source file on which the span starts or ends (inclusive). | ||
283 | pub line: usize, | ||
284 | /// The 0-indexed column (in UTF-8 characters) in the source file on which | ||
285 | /// the span starts or ends (inclusive). | ||
286 | pub column: usize, | ||
287 | } | ||
288 | |||
289 | /// The source file of a given `Span`. | ||
290 | #[derive(Clone)] | ||
291 | pub struct SourceFile(bridge::client::SourceFile); | ||
292 | |||
293 | impl SourceFile { | ||
294 | /// Gets the path to this source file. | ||
295 | /// | ||
296 | /// ### Note | ||
297 | /// If the code span associated with this `SourceFile` was generated by an external macro, this | ||
298 | /// macro, this may not be an actual path on the filesystem. Use [`is_real`] to check. | ||
299 | /// | ||
300 | /// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on | ||
301 | /// the command line, the path as given may not actually be valid. | ||
302 | /// | ||
303 | /// [`is_real`]: #method.is_real | ||
304 | pub fn path(&self) -> PathBuf { | ||
305 | PathBuf::from(self.0.path()) | ||
306 | } | ||
307 | |||
308 | /// Returns `true` if this source file is a real source file, and not generated by an external | ||
309 | /// macro's expansion. | ||
310 | pub fn is_real(&self) -> bool { | ||
311 | // This is a hack until intercrate spans are implemented and we can have real source files | ||
312 | // for spans generated in external macros. | ||
313 | // https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368 | ||
314 | self.0.is_real() | ||
315 | } | ||
316 | } | ||
317 | |||
318 | impl fmt::Debug for SourceFile { | ||
319 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
320 | f.debug_struct("SourceFile") | ||
321 | .field("path", &self.path()) | ||
322 | .field("is_real", &self.is_real()) | ||
323 | .finish() | ||
324 | } | ||
325 | } | ||
326 | |||
327 | impl PartialEq for SourceFile { | ||
328 | fn eq(&self, other: &Self) -> bool { | ||
329 | self.0.eq(&other.0) | ||
330 | } | ||
331 | } | ||
332 | |||
333 | impl Eq for SourceFile {} | ||
334 | |||
335 | /// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`). | ||
336 | #[derive(Clone)] | ||
337 | pub enum TokenTree { | ||
338 | /// A token stream surrounded by bracket delimiters. | ||
339 | Group(Group), | ||
340 | /// An identifier. | ||
341 | Ident(Ident), | ||
342 | /// A single punctuation character (`+`, `,`, `$`, etc.). | ||
343 | Punct(Punct), | ||
344 | /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc. | ||
345 | Literal(Literal), | ||
346 | } | ||
347 | |||
348 | impl TokenTree { | ||
349 | /// Returns the span of this tree, delegating to the `span` method of | ||
350 | /// the contained token or a delimited stream. | ||
351 | pub fn span(&self) -> Span { | ||
352 | match *self { | ||
353 | TokenTree::Group(ref t) => t.span(), | ||
354 | TokenTree::Ident(ref t) => t.span(), | ||
355 | TokenTree::Punct(ref t) => t.span(), | ||
356 | TokenTree::Literal(ref t) => t.span(), | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /// Configures the span for *only this token*. | ||
361 | /// | ||
362 | /// Note that if this token is a `Group` then this method will not configure | ||
363 | /// the span of each of the internal tokens, this will simply delegate to | ||
364 | /// the `set_span` method of each variant. | ||
365 | pub fn set_span(&mut self, span: Span) { | ||
366 | match *self { | ||
367 | TokenTree::Group(ref mut t) => t.set_span(span), | ||
368 | TokenTree::Ident(ref mut t) => t.set_span(span), | ||
369 | TokenTree::Punct(ref mut t) => t.set_span(span), | ||
370 | TokenTree::Literal(ref mut t) => t.set_span(span), | ||
371 | } | ||
372 | } | ||
373 | } | ||
374 | |||
375 | /// Prints token tree in a form convenient for debugging. | ||
376 | impl fmt::Debug for TokenTree { | ||
377 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
378 | // Each of these has the name in the struct type in the derived debug, | ||
379 | // so don't bother with an extra layer of indirection | ||
380 | match *self { | ||
381 | TokenTree::Group(ref tt) => tt.fmt(f), | ||
382 | TokenTree::Ident(ref tt) => tt.fmt(f), | ||
383 | TokenTree::Punct(ref tt) => tt.fmt(f), | ||
384 | TokenTree::Literal(ref tt) => tt.fmt(f), | ||
385 | } | ||
386 | } | ||
387 | } | ||
388 | |||
389 | impl From<Group> for TokenTree { | ||
390 | fn from(g: Group) -> TokenTree { | ||
391 | TokenTree::Group(g) | ||
392 | } | ||
393 | } | ||
394 | |||
395 | impl From<Ident> for TokenTree { | ||
396 | fn from(g: Ident) -> TokenTree { | ||
397 | TokenTree::Ident(g) | ||
398 | } | ||
399 | } | ||
400 | |||
401 | impl From<Punct> for TokenTree { | ||
402 | fn from(g: Punct) -> TokenTree { | ||
403 | TokenTree::Punct(g) | ||
404 | } | ||
405 | } | ||
406 | |||
407 | impl From<Literal> for TokenTree { | ||
408 | fn from(g: Literal) -> TokenTree { | ||
409 | TokenTree::Literal(g) | ||
410 | } | ||
411 | } | ||
412 | |||
413 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
414 | // based on it (the reverse of the usual relationship between the two). | ||
415 | // impl ToString for TokenTree { | ||
416 | // fn to_string(&self) -> String { | ||
417 | // match *self { | ||
418 | // TokenTree::Group(ref t) => t.to_string(), | ||
419 | // TokenTree::Ident(ref t) => t.to_string(), | ||
420 | // TokenTree::Punct(ref t) => t.to_string(), | ||
421 | // TokenTree::Literal(ref t) => t.to_string(), | ||
422 | // } | ||
423 | // } | ||
424 | // } | ||
425 | |||
426 | /// Prints the token tree as a string that is supposed to be losslessly convertible back | ||
427 | /// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s | ||
428 | /// with `Delimiter::None` delimiters and negative numeric literals. | ||
429 | impl fmt::Display for TokenTree { | ||
430 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
431 | f.write_str(&self.to_string()) | ||
432 | } | ||
433 | } | ||
434 | |||
435 | /// A delimited token stream. | ||
436 | /// | ||
437 | /// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s. | ||
438 | #[derive(Clone)] | ||
439 | pub struct Group(bridge::client::Group); | ||
440 | |||
441 | /// Describes how a sequence of token trees is delimited. | ||
442 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
443 | pub enum Delimiter { | ||
444 | /// `( ... )` | ||
445 | Parenthesis, | ||
446 | /// `{ ... }` | ||
447 | Brace, | ||
448 | /// `[ ... ]` | ||
449 | Bracket, | ||
450 | /// `Ø ... Ø` | ||
451 | /// An implicit delimiter, that may, for example, appear around tokens coming from a | ||
452 | /// "macro variable" `$var`. It is important to preserve operator priorities in cases like | ||
453 | /// `$var * 3` where `$var` is `1 + 2`. | ||
454 | /// Implicit delimiters may not survive roundtrip of a token stream through a string. | ||
455 | None, | ||
456 | } | ||
457 | |||
458 | impl Group { | ||
459 | /// Creates a new `Group` with the given delimiter and token stream. | ||
460 | /// | ||
461 | /// This constructor will set the span for this group to | ||
462 | /// `Span::call_site()`. To change the span you can use the `set_span` | ||
463 | /// method below. | ||
464 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { | ||
465 | Group(bridge::client::Group::new(delimiter, stream.0)) | ||
466 | } | ||
467 | |||
468 | /// Returns the delimiter of this `Group` | ||
469 | pub fn delimiter(&self) -> Delimiter { | ||
470 | self.0.delimiter() | ||
471 | } | ||
472 | |||
473 | /// Returns the `TokenStream` of tokens that are delimited in this `Group`. | ||
474 | /// | ||
475 | /// Note that the returned token stream does not include the delimiter | ||
476 | /// returned above. | ||
477 | pub fn stream(&self) -> TokenStream { | ||
478 | TokenStream(self.0.stream()) | ||
479 | } | ||
480 | |||
481 | /// Returns the span for the delimiters of this token stream, spanning the | ||
482 | /// entire `Group`. | ||
483 | /// | ||
484 | /// ```text | ||
485 | /// pub fn span(&self) -> Span { | ||
486 | /// ^^^^^^^ | ||
487 | /// ``` | ||
488 | pub fn span(&self) -> Span { | ||
489 | Span(self.0.span()) | ||
490 | } | ||
491 | |||
492 | /// Returns the span pointing to the opening delimiter of this group. | ||
493 | /// | ||
494 | /// ```text | ||
495 | /// pub fn span_open(&self) -> Span { | ||
496 | /// ^ | ||
497 | /// ``` | ||
498 | pub fn span_open(&self) -> Span { | ||
499 | Span(self.0.span_open()) | ||
500 | } | ||
501 | |||
502 | /// Returns the span pointing to the closing delimiter of this group. | ||
503 | /// | ||
504 | /// ```text | ||
505 | /// pub fn span_close(&self) -> Span { | ||
506 | /// ^ | ||
507 | /// ``` | ||
508 | pub fn span_close(&self) -> Span { | ||
509 | Span(self.0.span_close()) | ||
510 | } | ||
511 | |||
512 | /// Configures the span for this `Group`'s delimiters, but not its internal | ||
513 | /// tokens. | ||
514 | /// | ||
515 | /// This method will **not** set the span of all the internal tokens spanned | ||
516 | /// by this group, but rather it will only set the span of the delimiter | ||
517 | /// tokens at the level of the `Group`. | ||
518 | pub fn set_span(&mut self, span: Span) { | ||
519 | self.0.set_span(span.0); | ||
520 | } | ||
521 | } | ||
522 | |||
523 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
524 | // based on it (the reverse of the usual relationship between the two). | ||
525 | // impl ToString for Group { | ||
526 | // fn to_string(&self) -> String { | ||
527 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
528 | // } | ||
529 | // } | ||
530 | |||
531 | /// Prints the group as a string that should be losslessly convertible back | ||
532 | /// into the same group (modulo spans), except for possibly `TokenTree::Group`s | ||
533 | /// with `Delimiter::None` delimiters. | ||
534 | impl fmt::Display for Group { | ||
535 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
536 | f.write_str(&self.to_string()) | ||
537 | } | ||
538 | } | ||
539 | |||
540 | impl fmt::Debug for Group { | ||
541 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
542 | f.debug_struct("Group") | ||
543 | .field("delimiter", &self.delimiter()) | ||
544 | .field("stream", &self.stream()) | ||
545 | .field("span", &self.span()) | ||
546 | .finish() | ||
547 | } | ||
548 | } | ||
549 | |||
550 | /// An `Punct` is an single punctuation character like `+`, `-` or `#`. | ||
551 | /// | ||
552 | /// Multi-character operators like `+=` are represented as two instances of `Punct` with different | ||
553 | /// forms of `Spacing` returned. | ||
554 | #[derive(Clone)] | ||
555 | pub struct Punct(bridge::client::Punct); | ||
556 | |||
557 | /// Whether an `Punct` is followed immediately by another `Punct` or | ||
558 | /// followed by another token or whitespace. | ||
559 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
560 | pub enum Spacing { | ||
561 | /// e.g., `+` is `Alone` in `+ =`, `+ident` or `+()`. | ||
562 | Alone, | ||
563 | /// e.g., `+` is `Joint` in `+=` or `'#`. | ||
564 | /// Additionally, single quote `'` can join with identifiers to form lifetimes `'ident`. | ||
565 | Joint, | ||
566 | } | ||
567 | |||
568 | impl Punct { | ||
569 | /// Creates a new `Punct` from the given character and spacing. | ||
570 | /// The `ch` argument must be a valid punctuation character permitted by the language, | ||
571 | /// otherwise the function will panic. | ||
572 | /// | ||
573 | /// The returned `Punct` will have the default span of `Span::call_site()` | ||
574 | /// which can be further configured with the `set_span` method below. | ||
575 | pub fn new(ch: char, spacing: Spacing) -> Punct { | ||
576 | Punct(bridge::client::Punct::new(ch, spacing)) | ||
577 | } | ||
578 | |||
579 | /// Returns the value of this punctuation character as `char`. | ||
580 | pub fn as_char(&self) -> char { | ||
581 | self.0.as_char() | ||
582 | } | ||
583 | |||
584 | /// Returns the spacing of this punctuation character, indicating whether it's immediately | ||
585 | /// followed by another `Punct` in the token stream, so they can potentially be combined into | ||
586 | /// a multi-character operator (`Joint`), or it's followed by some other token or whitespace | ||
587 | /// (`Alone`) so the operator has certainly ended. | ||
588 | pub fn spacing(&self) -> Spacing { | ||
589 | self.0.spacing() | ||
590 | } | ||
591 | |||
592 | /// Returns the span for this punctuation character. | ||
593 | pub fn span(&self) -> Span { | ||
594 | Span(self.0.span()) | ||
595 | } | ||
596 | |||
597 | /// Configure the span for this punctuation character. | ||
598 | pub fn set_span(&mut self, span: Span) { | ||
599 | self.0 = self.0.with_span(span.0); | ||
600 | } | ||
601 | } | ||
602 | |||
603 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
604 | // based on it (the reverse of the usual relationship between the two). | ||
605 | // impl ToString for Punct { | ||
606 | // fn to_string(&self) -> String { | ||
607 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
608 | // } | ||
609 | // } | ||
610 | |||
611 | /// Prints the punctuation character as a string that should be losslessly convertible | ||
612 | /// back into the same character. | ||
613 | impl fmt::Display for Punct { | ||
614 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
615 | f.write_str(&self.to_string()) | ||
616 | } | ||
617 | } | ||
618 | |||
619 | impl fmt::Debug for Punct { | ||
620 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
621 | f.debug_struct("Punct") | ||
622 | .field("ch", &self.as_char()) | ||
623 | .field("spacing", &self.spacing()) | ||
624 | .field("span", &self.span()) | ||
625 | .finish() | ||
626 | } | ||
627 | } | ||
628 | |||
629 | /// An identifier (`ident`). | ||
630 | #[derive(Clone, PartialEq, Eq, Hash)] | ||
631 | pub struct Ident(bridge::client::Ident); | ||
632 | |||
633 | impl Ident { | ||
634 | /// Creates a new `Ident` with the given `string` as well as the specified | ||
635 | /// `span`. | ||
636 | /// The `string` argument must be a valid identifier permitted by the | ||
637 | /// language, otherwise the function will panic. | ||
638 | /// | ||
639 | /// Note that `span`, currently in rustc, configures the hygiene information | ||
640 | /// for this identifier. | ||
641 | /// | ||
642 | /// As of this time `Span::call_site()` explicitly opts-in to "call-site" hygiene | ||
643 | /// meaning that identifiers created with this span will be resolved as if they were written | ||
644 | /// directly at the location of the macro call, and other code at the macro call site will be | ||
645 | /// able to refer to them as well. | ||
646 | /// | ||
647 | /// Later spans like `Span::def_site()` will allow to opt-in to "definition-site" hygiene | ||
648 | /// meaning that identifiers created with this span will be resolved at the location of the | ||
649 | /// macro definition and other code at the macro call site will not be able to refer to them. | ||
650 | /// | ||
651 | /// Due to the current importance of hygiene this constructor, unlike other | ||
652 | /// tokens, requires a `Span` to be specified at construction. | ||
653 | pub fn new(string: &str, span: Span) -> Ident { | ||
654 | Ident(bridge::client::Ident::new(string, span.0, false)) | ||
655 | } | ||
656 | |||
657 | /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). | ||
658 | pub fn new_raw(string: &str, span: Span) -> Ident { | ||
659 | Ident(bridge::client::Ident::new(string, span.0, true)) | ||
660 | } | ||
661 | |||
662 | /// Returns the span of this `Ident`, encompassing the entire string returned | ||
663 | /// by `as_str`. | ||
664 | pub fn span(&self) -> Span { | ||
665 | Span(self.0.span()) | ||
666 | } | ||
667 | |||
668 | /// Configures the span of this `Ident`, possibly changing its hygiene context. | ||
669 | pub fn set_span(&mut self, span: Span) { | ||
670 | self.0 = self.0.with_span(span.0); | ||
671 | } | ||
672 | } | ||
673 | |||
674 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
675 | // based on it (the reverse of the usual relationship between the two). | ||
676 | // impl ToString for Ident { | ||
677 | // fn to_string(&self) -> String { | ||
678 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
679 | // } | ||
680 | // } | ||
681 | |||
682 | /// Prints the identifier as a string that should be losslessly convertible | ||
683 | /// back into the same identifier. | ||
684 | impl fmt::Display for Ident { | ||
685 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
686 | f.write_str(&self.to_string()) | ||
687 | } | ||
688 | } | ||
689 | |||
690 | impl fmt::Debug for Ident { | ||
691 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
692 | f.debug_struct("Ident") | ||
693 | .field("ident", &self.to_string()) | ||
694 | .field("span", &self.span()) | ||
695 | .finish() | ||
696 | } | ||
697 | } | ||
698 | |||
699 | /// A literal string (`"hello"`), byte string (`b"hello"`), | ||
700 | /// character (`'a'`), byte character (`b'a'`), an integer or floating point number | ||
701 | /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`). | ||
702 | /// Boolean literals like `true` and `false` do not belong here, they are `Ident`s. | ||
703 | #[derive(Clone)] | ||
704 | pub struct Literal(bridge::client::Literal); | ||
705 | |||
706 | macro_rules! suffixed_int_literals { | ||
707 | ($($name:ident => $kind:ident,)*) => ($( | ||
708 | /// Creates a new suffixed integer literal with the specified value. | ||
709 | /// | ||
710 | /// This function will create an integer like `1u32` where the integer | ||
711 | /// value specified is the first part of the token and the integral is | ||
712 | /// also suffixed at the end. | ||
713 | /// Literals created from negative numbers may not survive round-trips through | ||
714 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
715 | /// | ||
716 | /// Literals created through this method have the `Span::call_site()` | ||
717 | /// span by default, which can be configured with the `set_span` method | ||
718 | /// below. | ||
719 | pub fn $name(n: $kind) -> Literal { | ||
720 | Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind))) | ||
721 | } | ||
722 | )*) | ||
723 | } | ||
724 | |||
725 | macro_rules! unsuffixed_int_literals { | ||
726 | ($($name:ident => $kind:ident,)*) => ($( | ||
727 | /// Creates a new unsuffixed integer literal with the specified value. | ||
728 | /// | ||
729 | /// This function will create an integer like `1` where the integer | ||
730 | /// value specified is the first part of the token. No suffix is | ||
731 | /// specified on this token, meaning that invocations like | ||
732 | /// `Literal::i8_unsuffixed(1)` are equivalent to | ||
733 | /// `Literal::u32_unsuffixed(1)`. | ||
734 | /// Literals created from negative numbers may not survive rountrips through | ||
735 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
736 | /// | ||
737 | /// Literals created through this method have the `Span::call_site()` | ||
738 | /// span by default, which can be configured with the `set_span` method | ||
739 | /// below. | ||
740 | pub fn $name(n: $kind) -> Literal { | ||
741 | Literal(bridge::client::Literal::integer(&n.to_string())) | ||
742 | } | ||
743 | )*) | ||
744 | } | ||
745 | |||
746 | impl Literal { | ||
747 | suffixed_int_literals! { | ||
748 | u8_suffixed => u8, | ||
749 | u16_suffixed => u16, | ||
750 | u32_suffixed => u32, | ||
751 | u64_suffixed => u64, | ||
752 | u128_suffixed => u128, | ||
753 | usize_suffixed => usize, | ||
754 | i8_suffixed => i8, | ||
755 | i16_suffixed => i16, | ||
756 | i32_suffixed => i32, | ||
757 | i64_suffixed => i64, | ||
758 | i128_suffixed => i128, | ||
759 | isize_suffixed => isize, | ||
760 | } | ||
761 | |||
762 | unsuffixed_int_literals! { | ||
763 | u8_unsuffixed => u8, | ||
764 | u16_unsuffixed => u16, | ||
765 | u32_unsuffixed => u32, | ||
766 | u64_unsuffixed => u64, | ||
767 | u128_unsuffixed => u128, | ||
768 | usize_unsuffixed => usize, | ||
769 | i8_unsuffixed => i8, | ||
770 | i16_unsuffixed => i16, | ||
771 | i32_unsuffixed => i32, | ||
772 | i64_unsuffixed => i64, | ||
773 | i128_unsuffixed => i128, | ||
774 | isize_unsuffixed => isize, | ||
775 | } | ||
776 | |||
777 | /// Creates a new unsuffixed floating-point literal. | ||
778 | /// | ||
779 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where | ||
780 | /// the float's value is emitted directly into the token but no suffix is | ||
781 | /// used, so it may be inferred to be a `f64` later in the compiler. | ||
782 | /// Literals created from negative numbers may not survive rountrips through | ||
783 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
784 | /// | ||
785 | /// # Panics | ||
786 | /// | ||
787 | /// This function requires that the specified float is finite, for | ||
788 | /// example if it is infinity or NaN this function will panic. | ||
789 | pub fn f32_unsuffixed(n: f32) -> Literal { | ||
790 | if !n.is_finite() { | ||
791 | panic!("Invalid float literal {}", n); | ||
792 | } | ||
793 | Literal(bridge::client::Literal::float(&n.to_string())) | ||
794 | } | ||
795 | |||
796 | /// Creates a new suffixed floating-point literal. | ||
797 | /// | ||
798 | /// This constructor will create a literal like `1.0f32` where the value | ||
799 | /// specified is the preceding part of the token and `f32` is the suffix of | ||
800 | /// the token. This token will always be inferred to be an `f32` in the | ||
801 | /// compiler. | ||
802 | /// Literals created from negative numbers may not survive rountrips through | ||
803 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
804 | /// | ||
805 | /// # Panics | ||
806 | /// | ||
807 | /// This function requires that the specified float is finite, for | ||
808 | /// example if it is infinity or NaN this function will panic. | ||
809 | pub fn f32_suffixed(n: f32) -> Literal { | ||
810 | if !n.is_finite() { | ||
811 | panic!("Invalid float literal {}", n); | ||
812 | } | ||
813 | Literal(bridge::client::Literal::f32(&n.to_string())) | ||
814 | } | ||
815 | |||
816 | /// Creates a new unsuffixed floating-point literal. | ||
817 | /// | ||
818 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where | ||
819 | /// the float's value is emitted directly into the token but no suffix is | ||
820 | /// used, so it may be inferred to be a `f64` later in the compiler. | ||
821 | /// Literals created from negative numbers may not survive rountrips through | ||
822 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
823 | /// | ||
824 | /// # Panics | ||
825 | /// | ||
826 | /// This function requires that the specified float is finite, for | ||
827 | /// example if it is infinity or NaN this function will panic. | ||
828 | pub fn f64_unsuffixed(n: f64) -> Literal { | ||
829 | if !n.is_finite() { | ||
830 | panic!("Invalid float literal {}", n); | ||
831 | } | ||
832 | Literal(bridge::client::Literal::float(&n.to_string())) | ||
833 | } | ||
834 | |||
835 | /// Creates a new suffixed floating-point literal. | ||
836 | /// | ||
837 | /// This constructor will create a literal like `1.0f64` where the value | ||
838 | /// specified is the preceding part of the token and `f64` is the suffix of | ||
839 | /// the token. This token will always be inferred to be an `f64` in the | ||
840 | /// compiler. | ||
841 | /// Literals created from negative numbers may not survive rountrips through | ||
842 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
843 | /// | ||
844 | /// # Panics | ||
845 | /// | ||
846 | /// This function requires that the specified float is finite, for | ||
847 | /// example if it is infinity or NaN this function will panic. | ||
848 | pub fn f64_suffixed(n: f64) -> Literal { | ||
849 | if !n.is_finite() { | ||
850 | panic!("Invalid float literal {}", n); | ||
851 | } | ||
852 | Literal(bridge::client::Literal::f64(&n.to_string())) | ||
853 | } | ||
854 | |||
855 | /// String literal. | ||
856 | pub fn string(string: &str) -> Literal { | ||
857 | Literal(bridge::client::Literal::string(string)) | ||
858 | } | ||
859 | |||
860 | /// Character literal. | ||
861 | pub fn character(ch: char) -> Literal { | ||
862 | Literal(bridge::client::Literal::character(ch)) | ||
863 | } | ||
864 | |||
865 | /// Byte string literal. | ||
866 | pub fn byte_string(bytes: &[u8]) -> Literal { | ||
867 | Literal(bridge::client::Literal::byte_string(bytes)) | ||
868 | } | ||
869 | |||
870 | /// Returns the span encompassing this literal. | ||
871 | pub fn span(&self) -> Span { | ||
872 | Span(self.0.span()) | ||
873 | } | ||
874 | |||
875 | /// Configures the span associated for this literal. | ||
876 | pub fn set_span(&mut self, span: Span) { | ||
877 | self.0.set_span(span.0); | ||
878 | } | ||
879 | |||
880 | /// Returns a `Span` that is a subset of `self.span()` containing only the | ||
881 | /// source bytes in range `range`. Returns `None` if the would-be trimmed | ||
882 | /// span is outside the bounds of `self`. | ||
883 | // FIXME(SergioBenitez): check that the byte range starts and ends at a | ||
884 | // UTF-8 boundary of the source. otherwise, it's likely that a panic will | ||
885 | // occur elsewhere when the source text is printed. | ||
886 | // FIXME(SergioBenitez): there is no way for the user to know what | ||
887 | // `self.span()` actually maps to, so this method can currently only be | ||
888 | // called blindly. For example, `to_string()` for the character 'c' returns | ||
889 | // "'\u{63}'"; there is no way for the user to know whether the source text | ||
890 | // was 'c' or whether it was '\u{63}'. | ||
891 | pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { | ||
892 | // HACK(eddyb) something akin to `Option::cloned`, but for `Bound<&T>`. | ||
893 | fn cloned_bound<T: Clone>(bound: Bound<&T>) -> Bound<T> { | ||
894 | match bound { | ||
895 | Bound::Included(x) => Bound::Included(x.clone()), | ||
896 | Bound::Excluded(x) => Bound::Excluded(x.clone()), | ||
897 | Bound::Unbounded => Bound::Unbounded, | ||
898 | } | ||
899 | } | ||
900 | |||
901 | self.0.subspan(cloned_bound(range.start_bound()), cloned_bound(range.end_bound())).map(Span) | ||
902 | } | ||
903 | } | ||
904 | |||
905 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
906 | // based on it (the reverse of the usual relationship between the two). | ||
907 | // impl ToString for Literal { | ||
908 | // fn to_string(&self) -> String { | ||
909 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
910 | // } | ||
911 | // } | ||
912 | |||
913 | /// Prints the literal as a string that should be losslessly convertible | ||
914 | /// back into the same literal (except for possible rounding for floating point literals). | ||
915 | impl fmt::Display for Literal { | ||
916 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
917 | f.write_str(&self.to_string()) | ||
918 | } | ||
919 | } | ||
920 | |||
921 | impl fmt::Debug for Literal { | ||
922 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
923 | // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. | ||
924 | self.0.fmt(f) | ||
925 | } | ||
926 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/rustc_server.rs b/crates/ra_proc_macro_srv/src/rustc_server.rs deleted file mode 100644 index d534d1337..000000000 --- a/crates/ra_proc_macro_srv/src/rustc_server.rs +++ /dev/null | |||
@@ -1,704 +0,0 @@ | |||
1 | //! Rustc proc-macro server implementation with tt | ||
2 | //! | ||
3 | //! Based on idea from https://github.com/fedochet/rust-proc-macro-expander | ||
4 | //! The lib-proc-macro server backend is `TokenStream`-agnostic, such that | ||
5 | //! we could provide any TokenStream implementation. | ||
6 | //! The original idea from fedochet is using proc-macro2 as backend, | ||
7 | //! we use tt instead for better intergation with RA. | ||
8 | //! | ||
9 | //! FIXME: No span and source file information is implemented yet | ||
10 | |||
11 | use crate::proc_macro::bridge::{self, server}; | ||
12 | |||
13 | use std::collections::{Bound, HashMap}; | ||
14 | use std::hash::Hash; | ||
15 | use std::iter::FromIterator; | ||
16 | use std::str::FromStr; | ||
17 | use std::{ascii, vec::IntoIter}; | ||
18 | |||
19 | type Group = tt::Subtree; | ||
20 | type TokenTree = tt::TokenTree; | ||
21 | type Punct = tt::Punct; | ||
22 | type Spacing = tt::Spacing; | ||
23 | type Literal = tt::Literal; | ||
24 | type Span = tt::TokenId; | ||
25 | |||
26 | #[derive(Debug, Clone)] | ||
27 | pub struct TokenStream { | ||
28 | pub subtree: tt::Subtree, | ||
29 | } | ||
30 | |||
31 | impl TokenStream { | ||
32 | pub fn new() -> Self { | ||
33 | TokenStream { subtree: Default::default() } | ||
34 | } | ||
35 | |||
36 | pub fn with_subtree(subtree: tt::Subtree) -> Self { | ||
37 | TokenStream { subtree } | ||
38 | } | ||
39 | |||
40 | pub fn is_empty(&self) -> bool { | ||
41 | self.subtree.token_trees.is_empty() | ||
42 | } | ||
43 | } | ||
44 | |||
45 | /// Creates a token stream containing a single token tree. | ||
46 | impl From<TokenTree> for TokenStream { | ||
47 | fn from(tree: TokenTree) -> TokenStream { | ||
48 | TokenStream { subtree: tt::Subtree { delimiter: None, token_trees: vec![tree] } } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /// Collects a number of token trees into a single stream. | ||
53 | impl FromIterator<TokenTree> for TokenStream { | ||
54 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { | ||
55 | trees.into_iter().map(TokenStream::from).collect() | ||
56 | } | ||
57 | } | ||
58 | |||
59 | /// A "flattening" operation on token streams, collects token trees | ||
60 | /// from multiple token streams into a single stream. | ||
61 | impl FromIterator<TokenStream> for TokenStream { | ||
62 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { | ||
63 | let mut builder = TokenStreamBuilder::new(); | ||
64 | streams.into_iter().for_each(|stream| builder.push(stream)); | ||
65 | builder.build() | ||
66 | } | ||
67 | } | ||
68 | |||
69 | impl Extend<TokenTree> for TokenStream { | ||
70 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) { | ||
71 | self.extend(trees.into_iter().map(TokenStream::from)); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | impl Extend<TokenStream> for TokenStream { | ||
76 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { | ||
77 | for item in streams { | ||
78 | for tkn in item { | ||
79 | match tkn { | ||
80 | tt::TokenTree::Subtree(subtree) if subtree.delimiter.is_none() => { | ||
81 | self.subtree.token_trees.extend(subtree.token_trees); | ||
82 | } | ||
83 | _ => { | ||
84 | self.subtree.token_trees.push(tkn); | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | type Level = crate::proc_macro::Level; | ||
93 | type LineColumn = crate::proc_macro::LineColumn; | ||
94 | type SourceFile = crate::proc_macro::SourceFile; | ||
95 | |||
96 | /// A structure representing a diagnostic message and associated children | ||
97 | /// messages. | ||
98 | #[derive(Clone, Debug)] | ||
99 | pub struct Diagnostic { | ||
100 | level: Level, | ||
101 | message: String, | ||
102 | spans: Vec<Span>, | ||
103 | children: Vec<Diagnostic>, | ||
104 | } | ||
105 | |||
106 | impl Diagnostic { | ||
107 | /// Creates a new diagnostic with the given `level` and `message`. | ||
108 | pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic { | ||
109 | Diagnostic { level, message: message.into(), spans: vec![], children: vec![] } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | // Rustc Server Ident has to be `Copyable` | ||
114 | // We use a stub here for bypassing | ||
115 | #[derive(Hash, Eq, PartialEq, Copy, Clone)] | ||
116 | pub struct IdentId(u32); | ||
117 | |||
118 | #[derive(Clone, Hash, Eq, PartialEq)] | ||
119 | struct IdentData(tt::Ident); | ||
120 | |||
121 | #[derive(Default)] | ||
122 | struct IdentInterner { | ||
123 | idents: HashMap<IdentData, u32>, | ||
124 | ident_data: Vec<IdentData>, | ||
125 | } | ||
126 | |||
127 | impl IdentInterner { | ||
128 | fn intern(&mut self, data: &IdentData) -> u32 { | ||
129 | if let Some(index) = self.idents.get(data) { | ||
130 | return *index; | ||
131 | } | ||
132 | |||
133 | let index = self.idents.len() as u32; | ||
134 | self.ident_data.push(data.clone()); | ||
135 | self.idents.insert(data.clone(), index); | ||
136 | index | ||
137 | } | ||
138 | |||
139 | fn get(&self, index: u32) -> &IdentData { | ||
140 | &self.ident_data[index as usize] | ||
141 | } | ||
142 | |||
143 | #[allow(unused)] | ||
144 | fn get_mut(&mut self, index: u32) -> &mut IdentData { | ||
145 | self.ident_data.get_mut(index as usize).expect("Should be consistent") | ||
146 | } | ||
147 | } | ||
148 | |||
149 | pub struct TokenStreamBuilder { | ||
150 | acc: TokenStream, | ||
151 | } | ||
152 | |||
153 | /// Public implementation details for the `TokenStream` type, such as iterators. | ||
154 | pub mod token_stream { | ||
155 | use std::str::FromStr; | ||
156 | |||
157 | use super::{TokenStream, TokenTree}; | ||
158 | |||
159 | /// An iterator over `TokenStream`'s `TokenTree`s. | ||
160 | /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups, | ||
161 | /// and returns whole groups as token trees. | ||
162 | impl IntoIterator for TokenStream { | ||
163 | type Item = TokenTree; | ||
164 | type IntoIter = super::IntoIter<TokenTree>; | ||
165 | |||
166 | fn into_iter(self) -> Self::IntoIter { | ||
167 | self.subtree.token_trees.into_iter() | ||
168 | } | ||
169 | } | ||
170 | |||
171 | type LexError = String; | ||
172 | |||
173 | /// Attempts to break the string into tokens and parse those tokens into a token stream. | ||
174 | /// May fail for a number of reasons, for example, if the string contains unbalanced delimiters | ||
175 | /// or characters not existing in the language. | ||
176 | /// All tokens in the parsed stream get `Span::call_site()` spans. | ||
177 | /// | ||
178 | /// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to | ||
179 | /// change these errors into `LexError`s later. | ||
180 | impl FromStr for TokenStream { | ||
181 | type Err = LexError; | ||
182 | |||
183 | fn from_str(src: &str) -> Result<TokenStream, LexError> { | ||
184 | let (subtree, _token_map) = | ||
185 | ra_mbe::parse_to_token_tree(src).ok_or("Failed to parse from mbe")?; | ||
186 | |||
187 | let tt: tt::TokenTree = subtree.into(); | ||
188 | Ok(tt.into()) | ||
189 | } | ||
190 | } | ||
191 | |||
192 | impl ToString for TokenStream { | ||
193 | fn to_string(&self) -> String { | ||
194 | let tt = self.subtree.clone().into(); | ||
195 | to_text(&tt) | ||
196 | } | ||
197 | } | ||
198 | |||
199 | fn to_text(tkn: &tt::TokenTree) -> String { | ||
200 | match tkn { | ||
201 | tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident.text.clone().into(), | ||
202 | tt::TokenTree::Leaf(tt::Leaf::Literal(literal)) => literal.text.clone().into(), | ||
203 | tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => format!("{}", punct.char), | ||
204 | tt::TokenTree::Subtree(subtree) => { | ||
205 | let content = subtree | ||
206 | .token_trees | ||
207 | .iter() | ||
208 | .map(|tkn| { | ||
209 | let s = to_text(tkn); | ||
210 | if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn { | ||
211 | if punct.spacing == tt::Spacing::Alone { | ||
212 | return s + " "; | ||
213 | } | ||
214 | } | ||
215 | s | ||
216 | }) | ||
217 | .collect::<Vec<_>>() | ||
218 | .concat(); | ||
219 | let (open, close) = match subtree.delimiter.map(|it| it.kind) { | ||
220 | None => ("", ""), | ||
221 | Some(tt::DelimiterKind::Brace) => ("{", "}"), | ||
222 | Some(tt::DelimiterKind::Parenthesis) => ("(", ")"), | ||
223 | Some(tt::DelimiterKind::Bracket) => ("[", "]"), | ||
224 | }; | ||
225 | format!("{}{}{}", open, content, close) | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | impl TokenStreamBuilder { | ||
232 | fn new() -> TokenStreamBuilder { | ||
233 | TokenStreamBuilder { acc: TokenStream::new() } | ||
234 | } | ||
235 | |||
236 | fn push(&mut self, stream: TokenStream) { | ||
237 | self.acc.extend(stream.into_iter()) | ||
238 | } | ||
239 | |||
240 | fn build(self) -> TokenStream { | ||
241 | self.acc | ||
242 | } | ||
243 | } | ||
244 | |||
245 | #[derive(Clone)] | ||
246 | pub struct TokenStreamIter { | ||
247 | trees: IntoIter<TokenTree>, | ||
248 | } | ||
249 | |||
250 | #[derive(Default)] | ||
251 | pub struct Rustc { | ||
252 | ident_interner: IdentInterner, | ||
253 | // FIXME: store span information here. | ||
254 | } | ||
255 | |||
256 | impl server::Types for Rustc { | ||
257 | type TokenStream = TokenStream; | ||
258 | type TokenStreamBuilder = TokenStreamBuilder; | ||
259 | type TokenStreamIter = TokenStreamIter; | ||
260 | type Group = Group; | ||
261 | type Punct = Punct; | ||
262 | type Ident = IdentId; | ||
263 | type Literal = Literal; | ||
264 | type SourceFile = SourceFile; | ||
265 | type Diagnostic = Diagnostic; | ||
266 | type Span = Span; | ||
267 | type MultiSpan = Vec<Span>; | ||
268 | } | ||
269 | |||
270 | impl server::TokenStream for Rustc { | ||
271 | fn new(&mut self) -> Self::TokenStream { | ||
272 | Self::TokenStream::new() | ||
273 | } | ||
274 | |||
275 | fn is_empty(&mut self, stream: &Self::TokenStream) -> bool { | ||
276 | stream.is_empty() | ||
277 | } | ||
278 | fn from_str(&mut self, src: &str) -> Self::TokenStream { | ||
279 | Self::TokenStream::from_str(src).expect("cannot parse string") | ||
280 | } | ||
281 | fn to_string(&mut self, stream: &Self::TokenStream) -> String { | ||
282 | stream.to_string() | ||
283 | } | ||
284 | fn from_token_tree( | ||
285 | &mut self, | ||
286 | tree: bridge::TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>, | ||
287 | ) -> Self::TokenStream { | ||
288 | match tree { | ||
289 | bridge::TokenTree::Group(group) => { | ||
290 | let tree = TokenTree::from(group); | ||
291 | Self::TokenStream::from_iter(vec![tree]) | ||
292 | } | ||
293 | |||
294 | bridge::TokenTree::Ident(IdentId(index)) => { | ||
295 | let IdentData(ident) = self.ident_interner.get(index).clone(); | ||
296 | let ident: tt::Ident = ident; | ||
297 | let leaf = tt::Leaf::from(ident); | ||
298 | let tree = TokenTree::from(leaf); | ||
299 | Self::TokenStream::from_iter(vec![tree]) | ||
300 | } | ||
301 | |||
302 | bridge::TokenTree::Literal(literal) => { | ||
303 | let leaf = tt::Leaf::from(literal); | ||
304 | let tree = TokenTree::from(leaf); | ||
305 | Self::TokenStream::from_iter(vec![tree]) | ||
306 | } | ||
307 | |||
308 | bridge::TokenTree::Punct(p) => { | ||
309 | let leaf = tt::Leaf::from(p); | ||
310 | let tree = TokenTree::from(leaf); | ||
311 | Self::TokenStream::from_iter(vec![tree]) | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | |||
316 | fn into_iter(&mut self, stream: Self::TokenStream) -> Self::TokenStreamIter { | ||
317 | let trees: Vec<TokenTree> = stream.into_iter().collect(); | ||
318 | TokenStreamIter { trees: trees.into_iter() } | ||
319 | } | ||
320 | } | ||
321 | |||
322 | impl server::TokenStreamBuilder for Rustc { | ||
323 | fn new(&mut self) -> Self::TokenStreamBuilder { | ||
324 | Self::TokenStreamBuilder::new() | ||
325 | } | ||
326 | fn push(&mut self, builder: &mut Self::TokenStreamBuilder, stream: Self::TokenStream) { | ||
327 | builder.push(stream) | ||
328 | } | ||
329 | fn build(&mut self, builder: Self::TokenStreamBuilder) -> Self::TokenStream { | ||
330 | builder.build() | ||
331 | } | ||
332 | } | ||
333 | |||
334 | impl server::TokenStreamIter for Rustc { | ||
335 | fn next( | ||
336 | &mut self, | ||
337 | iter: &mut Self::TokenStreamIter, | ||
338 | ) -> Option<bridge::TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> { | ||
339 | iter.trees.next().map(|tree| match tree { | ||
340 | TokenTree::Subtree(group) => bridge::TokenTree::Group(group), | ||
341 | TokenTree::Leaf(tt::Leaf::Ident(ident)) => { | ||
342 | bridge::TokenTree::Ident(IdentId(self.ident_interner.intern(&IdentData(ident)))) | ||
343 | } | ||
344 | TokenTree::Leaf(tt::Leaf::Literal(literal)) => bridge::TokenTree::Literal(literal), | ||
345 | TokenTree::Leaf(tt::Leaf::Punct(punct)) => bridge::TokenTree::Punct(punct), | ||
346 | }) | ||
347 | } | ||
348 | } | ||
349 | |||
350 | fn delim_to_internal(d: bridge::Delimiter) -> Option<tt::Delimiter> { | ||
351 | let kind = match d { | ||
352 | bridge::Delimiter::Parenthesis => tt::DelimiterKind::Parenthesis, | ||
353 | bridge::Delimiter::Brace => tt::DelimiterKind::Brace, | ||
354 | bridge::Delimiter::Bracket => tt::DelimiterKind::Bracket, | ||
355 | bridge::Delimiter::None => return None, | ||
356 | }; | ||
357 | Some(tt::Delimiter { id: tt::TokenId::unspecified(), kind }) | ||
358 | } | ||
359 | |||
360 | fn delim_to_external(d: Option<tt::Delimiter>) -> bridge::Delimiter { | ||
361 | match d.map(|it| it.kind) { | ||
362 | Some(tt::DelimiterKind::Parenthesis) => bridge::Delimiter::Parenthesis, | ||
363 | Some(tt::DelimiterKind::Brace) => bridge::Delimiter::Brace, | ||
364 | Some(tt::DelimiterKind::Bracket) => bridge::Delimiter::Bracket, | ||
365 | None => bridge::Delimiter::None, | ||
366 | } | ||
367 | } | ||
368 | |||
369 | fn spacing_to_internal(spacing: bridge::Spacing) -> Spacing { | ||
370 | match spacing { | ||
371 | bridge::Spacing::Alone => Spacing::Alone, | ||
372 | bridge::Spacing::Joint => Spacing::Joint, | ||
373 | } | ||
374 | } | ||
375 | |||
376 | fn spacing_to_external(spacing: Spacing) -> bridge::Spacing { | ||
377 | match spacing { | ||
378 | Spacing::Alone => bridge::Spacing::Alone, | ||
379 | Spacing::Joint => bridge::Spacing::Joint, | ||
380 | } | ||
381 | } | ||
382 | |||
383 | impl server::Group for Rustc { | ||
384 | fn new(&mut self, delimiter: bridge::Delimiter, stream: Self::TokenStream) -> Self::Group { | ||
385 | Self::Group { | ||
386 | delimiter: delim_to_internal(delimiter), | ||
387 | token_trees: stream.subtree.token_trees, | ||
388 | } | ||
389 | } | ||
390 | fn delimiter(&mut self, group: &Self::Group) -> bridge::Delimiter { | ||
391 | delim_to_external(group.delimiter) | ||
392 | } | ||
393 | |||
394 | // NOTE: Return value of do not include delimiter | ||
395 | fn stream(&mut self, group: &Self::Group) -> Self::TokenStream { | ||
396 | TokenStream { | ||
397 | subtree: tt::Subtree { delimiter: None, token_trees: group.token_trees.clone() }, | ||
398 | } | ||
399 | } | ||
400 | |||
401 | fn span(&mut self, group: &Self::Group) -> Self::Span { | ||
402 | group.delimiter.map(|it| it.id).unwrap_or_else(|| tt::TokenId::unspecified()) | ||
403 | } | ||
404 | |||
405 | fn set_span(&mut self, _group: &mut Self::Group, _span: Self::Span) { | ||
406 | // FIXME handle span | ||
407 | } | ||
408 | |||
409 | fn span_open(&mut self, _group: &Self::Group) -> Self::Span { | ||
410 | // FIXME handle span | ||
411 | // MySpan(self.span_interner.intern(&MySpanData(group.span_open()))) | ||
412 | tt::TokenId::unspecified() | ||
413 | } | ||
414 | |||
415 | fn span_close(&mut self, _group: &Self::Group) -> Self::Span { | ||
416 | // FIXME handle span | ||
417 | tt::TokenId::unspecified() | ||
418 | } | ||
419 | } | ||
420 | |||
421 | impl server::Punct for Rustc { | ||
422 | fn new(&mut self, ch: char, spacing: bridge::Spacing) -> Self::Punct { | ||
423 | tt::Punct { | ||
424 | char: ch, | ||
425 | spacing: spacing_to_internal(spacing), | ||
426 | id: tt::TokenId::unspecified(), | ||
427 | } | ||
428 | } | ||
429 | fn as_char(&mut self, punct: Self::Punct) -> char { | ||
430 | punct.char | ||
431 | } | ||
432 | fn spacing(&mut self, punct: Self::Punct) -> bridge::Spacing { | ||
433 | spacing_to_external(punct.spacing) | ||
434 | } | ||
435 | fn span(&mut self, _punct: Self::Punct) -> Self::Span { | ||
436 | // FIXME handle span | ||
437 | tt::TokenId::unspecified() | ||
438 | } | ||
439 | fn with_span(&mut self, punct: Self::Punct, _span: Self::Span) -> Self::Punct { | ||
440 | // FIXME handle span | ||
441 | punct | ||
442 | } | ||
443 | } | ||
444 | |||
445 | impl server::Ident for Rustc { | ||
446 | fn new(&mut self, string: &str, _span: Self::Span, _is_raw: bool) -> Self::Ident { | ||
447 | IdentId( | ||
448 | self.ident_interner.intern(&IdentData(tt::Ident { | ||
449 | text: string.into(), | ||
450 | id: tt::TokenId::unspecified(), | ||
451 | })), | ||
452 | ) | ||
453 | } | ||
454 | |||
455 | fn span(&mut self, _ident: Self::Ident) -> Self::Span { | ||
456 | // FIXME handle span | ||
457 | tt::TokenId::unspecified() | ||
458 | } | ||
459 | fn with_span(&mut self, ident: Self::Ident, _span: Self::Span) -> Self::Ident { | ||
460 | // FIXME handle span | ||
461 | ident | ||
462 | } | ||
463 | } | ||
464 | |||
465 | impl server::Literal for Rustc { | ||
466 | fn debug_kind(&mut self, _literal: &Self::Literal) -> String { | ||
467 | // r-a: debug_kind and suffix are unsupported; corresponding client code has been changed to not call these. | ||
468 | // They must still be present to be ABI-compatible and work with upstream proc_macro. | ||
469 | "".to_owned() | ||
470 | } | ||
471 | fn symbol(&mut self, literal: &Self::Literal) -> String { | ||
472 | literal.text.to_string() | ||
473 | } | ||
474 | fn suffix(&mut self, _literal: &Self::Literal) -> Option<String> { | ||
475 | None | ||
476 | } | ||
477 | |||
478 | fn integer(&mut self, n: &str) -> Self::Literal { | ||
479 | let n: i128 = n.parse().unwrap(); | ||
480 | Literal { text: n.to_string().into(), id: tt::TokenId::unspecified() } | ||
481 | } | ||
482 | |||
483 | fn typed_integer(&mut self, n: &str, kind: &str) -> Self::Literal { | ||
484 | macro_rules! def_suffixed_integer { | ||
485 | ($kind:ident, $($ty:ty),*) => { | ||
486 | match $kind { | ||
487 | $( | ||
488 | stringify!($ty) => { | ||
489 | let n: $ty = n.parse().unwrap(); | ||
490 | format!(concat!("{}", stringify!($ty)), n) | ||
491 | } | ||
492 | )* | ||
493 | _ => unimplemented!("unknown args for typed_integer: n {}, kind {}", n, $kind), | ||
494 | } | ||
495 | } | ||
496 | } | ||
497 | |||
498 | let text = | ||
499 | def_suffixed_integer! {kind, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128}; | ||
500 | |||
501 | Literal { text: text.into(), id: tt::TokenId::unspecified() } | ||
502 | } | ||
503 | |||
504 | fn float(&mut self, n: &str) -> Self::Literal { | ||
505 | let n: f64 = n.parse().unwrap(); | ||
506 | let mut text = f64::to_string(&n); | ||
507 | if !text.contains('.') { | ||
508 | text += ".0" | ||
509 | } | ||
510 | Literal { text: text.into(), id: tt::TokenId::unspecified() } | ||
511 | } | ||
512 | |||
513 | fn f32(&mut self, n: &str) -> Self::Literal { | ||
514 | let n: f32 = n.parse().unwrap(); | ||
515 | let text = format!("{}f32", n); | ||
516 | Literal { text: text.into(), id: tt::TokenId::unspecified() } | ||
517 | } | ||
518 | |||
519 | fn f64(&mut self, n: &str) -> Self::Literal { | ||
520 | let n: f64 = n.parse().unwrap(); | ||
521 | let text = format!("{}f64", n); | ||
522 | Literal { text: text.into(), id: tt::TokenId::unspecified() } | ||
523 | } | ||
524 | |||
525 | fn string(&mut self, string: &str) -> Self::Literal { | ||
526 | let mut escaped = String::new(); | ||
527 | for ch in string.chars() { | ||
528 | escaped.extend(ch.escape_debug()); | ||
529 | } | ||
530 | Literal { text: format!("\"{}\"", escaped).into(), id: tt::TokenId::unspecified() } | ||
531 | } | ||
532 | |||
533 | fn character(&mut self, ch: char) -> Self::Literal { | ||
534 | Literal { text: format!("'{}'", ch).into(), id: tt::TokenId::unspecified() } | ||
535 | } | ||
536 | |||
537 | fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal { | ||
538 | let string = bytes | ||
539 | .iter() | ||
540 | .cloned() | ||
541 | .flat_map(ascii::escape_default) | ||
542 | .map(Into::<char>::into) | ||
543 | .collect::<String>(); | ||
544 | |||
545 | Literal { text: format!("b\"{}\"", string).into(), id: tt::TokenId::unspecified() } | ||
546 | } | ||
547 | |||
548 | fn span(&mut self, literal: &Self::Literal) -> Self::Span { | ||
549 | literal.id | ||
550 | } | ||
551 | |||
552 | fn set_span(&mut self, _literal: &mut Self::Literal, _span: Self::Span) { | ||
553 | // FIXME handle span | ||
554 | } | ||
555 | |||
556 | fn subspan( | ||
557 | &mut self, | ||
558 | _literal: &Self::Literal, | ||
559 | _start: Bound<usize>, | ||
560 | _end: Bound<usize>, | ||
561 | ) -> Option<Self::Span> { | ||
562 | // FIXME handle span | ||
563 | None | ||
564 | } | ||
565 | } | ||
566 | |||
567 | impl server::SourceFile for Rustc { | ||
568 | fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool { | ||
569 | file1.eq(file2) | ||
570 | } | ||
571 | fn path(&mut self, file: &Self::SourceFile) -> String { | ||
572 | String::from( | ||
573 | file.path().to_str().expect("non-UTF8 file path in `proc_macro::SourceFile::path`"), | ||
574 | ) | ||
575 | } | ||
576 | fn is_real(&mut self, file: &Self::SourceFile) -> bool { | ||
577 | file.is_real() | ||
578 | } | ||
579 | } | ||
580 | |||
581 | impl server::Diagnostic for Rustc { | ||
582 | fn new(&mut self, level: Level, msg: &str, spans: Self::MultiSpan) -> Self::Diagnostic { | ||
583 | let mut diag = Diagnostic::new(level, msg); | ||
584 | diag.spans = spans; | ||
585 | diag | ||
586 | } | ||
587 | |||
588 | fn sub( | ||
589 | &mut self, | ||
590 | _diag: &mut Self::Diagnostic, | ||
591 | _level: Level, | ||
592 | _msg: &str, | ||
593 | _spans: Self::MultiSpan, | ||
594 | ) { | ||
595 | // FIXME handle diagnostic | ||
596 | // | ||
597 | } | ||
598 | |||
599 | fn emit(&mut self, _diag: Self::Diagnostic) { | ||
600 | // FIXME handle diagnostic | ||
601 | // diag.emit() | ||
602 | } | ||
603 | } | ||
604 | |||
605 | impl server::Span for Rustc { | ||
606 | fn debug(&mut self, span: Self::Span) -> String { | ||
607 | format!("{:?}", span.0) | ||
608 | } | ||
609 | fn def_site(&mut self) -> Self::Span { | ||
610 | // MySpan(self.span_interner.intern(&MySpanData(Span::def_site()))) | ||
611 | // FIXME handle span | ||
612 | tt::TokenId::unspecified() | ||
613 | } | ||
614 | fn call_site(&mut self) -> Self::Span { | ||
615 | // MySpan(self.span_interner.intern(&MySpanData(Span::call_site()))) | ||
616 | // FIXME handle span | ||
617 | tt::TokenId::unspecified() | ||
618 | } | ||
619 | fn source_file(&mut self, _span: Self::Span) -> Self::SourceFile { | ||
620 | // let MySpanData(span) = self.span_interner.get(span.0); | ||
621 | unimplemented!() | ||
622 | } | ||
623 | |||
624 | /// Recent feature, not yet in the proc_macro | ||
625 | /// | ||
626 | /// See PR: | ||
627 | /// https://github.com/rust-lang/rust/pull/55780 | ||
628 | fn source_text(&mut self, _span: Self::Span) -> Option<String> { | ||
629 | None | ||
630 | } | ||
631 | |||
632 | fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> { | ||
633 | // FIXME handle span | ||
634 | None | ||
635 | } | ||
636 | fn source(&mut self, span: Self::Span) -> Self::Span { | ||
637 | // FIXME handle span | ||
638 | span | ||
639 | } | ||
640 | fn start(&mut self, _span: Self::Span) -> LineColumn { | ||
641 | // FIXME handle span | ||
642 | LineColumn { line: 0, column: 0 } | ||
643 | } | ||
644 | fn end(&mut self, _span: Self::Span) -> LineColumn { | ||
645 | // FIXME handle span | ||
646 | LineColumn { line: 0, column: 0 } | ||
647 | } | ||
648 | fn join(&mut self, _first: Self::Span, _second: Self::Span) -> Option<Self::Span> { | ||
649 | None | ||
650 | } | ||
651 | fn resolved_at(&mut self, _span: Self::Span, _at: Self::Span) -> Self::Span { | ||
652 | // FIXME handle span | ||
653 | tt::TokenId::unspecified() | ||
654 | } | ||
655 | |||
656 | fn mixed_site(&mut self) -> Self::Span { | ||
657 | // FIXME handle span | ||
658 | tt::TokenId::unspecified() | ||
659 | } | ||
660 | } | ||
661 | |||
662 | impl server::MultiSpan for Rustc { | ||
663 | fn new(&mut self) -> Self::MultiSpan { | ||
664 | // FIXME handle span | ||
665 | vec![] | ||
666 | } | ||
667 | |||
668 | fn push(&mut self, other: &mut Self::MultiSpan, span: Self::Span) { | ||
669 | //TODP | ||
670 | other.push(span) | ||
671 | } | ||
672 | } | ||
673 | |||
674 | #[cfg(test)] | ||
675 | mod tests { | ||
676 | use super::*; | ||
677 | use crate::proc_macro::bridge::server::Literal; | ||
678 | |||
679 | #[test] | ||
680 | fn test_rustc_server_literals() { | ||
681 | let mut srv = Rustc { ident_interner: IdentInterner::default() }; | ||
682 | assert_eq!(srv.integer("1234").text, "1234"); | ||
683 | |||
684 | assert_eq!(srv.typed_integer("12", "u8").text, "12u8"); | ||
685 | assert_eq!(srv.typed_integer("255", "u16").text, "255u16"); | ||
686 | assert_eq!(srv.typed_integer("1234", "u32").text, "1234u32"); | ||
687 | assert_eq!(srv.typed_integer("15846685", "u64").text, "15846685u64"); | ||
688 | assert_eq!(srv.typed_integer("15846685258", "u128").text, "15846685258u128"); | ||
689 | assert_eq!(srv.typed_integer("156788984", "usize").text, "156788984usize"); | ||
690 | assert_eq!(srv.typed_integer("127", "i8").text, "127i8"); | ||
691 | assert_eq!(srv.typed_integer("255", "i16").text, "255i16"); | ||
692 | assert_eq!(srv.typed_integer("1234", "i32").text, "1234i32"); | ||
693 | assert_eq!(srv.typed_integer("15846685", "i64").text, "15846685i64"); | ||
694 | assert_eq!(srv.typed_integer("15846685258", "i128").text, "15846685258i128"); | ||
695 | assert_eq!(srv.float("0").text, "0.0"); | ||
696 | assert_eq!(srv.float("15684.5867").text, "15684.5867"); | ||
697 | assert_eq!(srv.f32("15684.58").text, "15684.58f32"); | ||
698 | assert_eq!(srv.f64("15684.58").text, "15684.58f64"); | ||
699 | |||
700 | assert_eq!(srv.string("hello_world").text, "\"hello_world\""); | ||
701 | assert_eq!(srv.character('c').text, "'c'"); | ||
702 | assert_eq!(srv.byte_string(b"1234586\x88").text, "b\"1234586\\x88\""); | ||
703 | } | ||
704 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt deleted file mode 100644 index e6fd21610..000000000 --- a/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt +++ /dev/null | |||
@@ -1,181 +0,0 @@ | |||
1 | SUBTREE $ | ||
2 | PUNCH # [alone] 4294967295 | ||
3 | SUBTREE [] 4294967295 | ||
4 | IDENT doc 4294967295 | ||
5 | SUBTREE () 4294967295 | ||
6 | IDENT hidden 4294967295 | ||
7 | PUNCH # [alone] 4294967295 | ||
8 | SUBTREE [] 4294967295 | ||
9 | IDENT allow 4294967295 | ||
10 | SUBTREE () 4294967295 | ||
11 | IDENT non_upper_case_globals 4294967295 | ||
12 | PUNCH , [alone] 4294967295 | ||
13 | IDENT unused_attributes 4294967295 | ||
14 | PUNCH , [alone] 4294967295 | ||
15 | IDENT unused_qualifications 4294967295 | ||
16 | IDENT const 4294967295 | ||
17 | IDENT _ 4294967295 | ||
18 | PUNCH : [alone] 4294967295 | ||
19 | SUBTREE () 4294967295 | ||
20 | PUNCH = [alone] 4294967295 | ||
21 | SUBTREE {} 4294967295 | ||
22 | PUNCH # [alone] 4294967295 | ||
23 | SUBTREE [] 4294967295 | ||
24 | IDENT allow 4294967295 | ||
25 | SUBTREE () 4294967295 | ||
26 | IDENT rust_2018_idioms 4294967295 | ||
27 | PUNCH , [alone] 4294967295 | ||
28 | IDENT clippy 4294967295 | ||
29 | PUNCH : [joint] 4294967295 | ||
30 | PUNCH : [alone] 4294967295 | ||
31 | IDENT useless_attribute 4294967295 | ||
32 | IDENT extern 4294967295 | ||
33 | IDENT crate 4294967295 | ||
34 | IDENT serde 4294967295 | ||
35 | IDENT as 4294967295 | ||
36 | IDENT _serde 4294967295 | ||
37 | PUNCH ; [alone] 4294967295 | ||
38 | PUNCH # [alone] 4294967295 | ||
39 | SUBTREE [] 4294967295 | ||
40 | IDENT allow 4294967295 | ||
41 | SUBTREE () 4294967295 | ||
42 | IDENT unused_macros 4294967295 | ||
43 | IDENT macro_rules 4294967295 | ||
44 | PUNCH ! [alone] 4294967295 | ||
45 | IDENT try 4294967295 | ||
46 | SUBTREE {} 4294967295 | ||
47 | SUBTREE () 4294967295 | ||
48 | PUNCH $ [alone] 4294967295 | ||
49 | IDENT __expr 4294967295 | ||
50 | PUNCH : [alone] 4294967295 | ||
51 | IDENT expr 4294967295 | ||
52 | PUNCH = [joint] 4294967295 | ||
53 | PUNCH > [alone] 4294967295 | ||
54 | SUBTREE {} 4294967295 | ||
55 | IDENT match 4294967295 | ||
56 | PUNCH $ [alone] 4294967295 | ||
57 | IDENT __expr 4294967295 | ||
58 | SUBTREE {} 4294967295 | ||
59 | IDENT _serde 4294967295 | ||
60 | PUNCH : [joint] 4294967295 | ||
61 | PUNCH : [alone] 4294967295 | ||
62 | IDENT export 4294967295 | ||
63 | PUNCH : [joint] 4294967295 | ||
64 | PUNCH : [alone] 4294967295 | ||
65 | IDENT Ok 4294967295 | ||
66 | SUBTREE () 4294967295 | ||
67 | IDENT __val 4294967295 | ||
68 | PUNCH = [joint] 4294967295 | ||
69 | PUNCH > [alone] 4294967295 | ||
70 | IDENT __val 4294967295 | ||
71 | PUNCH , [alone] 4294967295 | ||
72 | IDENT _serde 4294967295 | ||
73 | PUNCH : [joint] 4294967295 | ||
74 | PUNCH : [alone] 4294967295 | ||
75 | IDENT export 4294967295 | ||
76 | PUNCH : [joint] 4294967295 | ||
77 | PUNCH : [alone] 4294967295 | ||
78 | IDENT Err 4294967295 | ||
79 | SUBTREE () 4294967295 | ||
80 | IDENT __err 4294967295 | ||
81 | PUNCH = [joint] 4294967295 | ||
82 | PUNCH > [alone] 4294967295 | ||
83 | SUBTREE {} 4294967295 | ||
84 | IDENT return 4294967295 | ||
85 | IDENT _serde 4294967295 | ||
86 | PUNCH : [joint] 4294967295 | ||
87 | PUNCH : [alone] 4294967295 | ||
88 | IDENT export 4294967295 | ||
89 | PUNCH : [joint] 4294967295 | ||
90 | PUNCH : [alone] 4294967295 | ||
91 | IDENT Err 4294967295 | ||
92 | SUBTREE () 4294967295 | ||
93 | IDENT __err 4294967295 | ||
94 | PUNCH ; [alone] 4294967295 | ||
95 | PUNCH # [alone] 4294967295 | ||
96 | SUBTREE [] 4294967295 | ||
97 | IDENT automatically_derived 4294967295 | ||
98 | IDENT impl 4294967295 | ||
99 | IDENT _serde 4294967295 | ||
100 | PUNCH : [joint] 4294967295 | ||
101 | PUNCH : [alone] 4294967295 | ||
102 | IDENT Serialize 4294967295 | ||
103 | IDENT for 4294967295 | ||
104 | IDENT Foo 1 | ||
105 | SUBTREE {} 4294967295 | ||
106 | IDENT fn 4294967295 | ||
107 | IDENT serialize 4294967295 | ||
108 | PUNCH < [alone] 4294967295 | ||
109 | IDENT __S 4294967295 | ||
110 | PUNCH > [alone] 4294967295 | ||
111 | SUBTREE () 4294967295 | ||
112 | PUNCH & [alone] 4294967295 | ||
113 | IDENT self 4294967295 | ||
114 | PUNCH , [alone] 4294967295 | ||
115 | IDENT __serializer 4294967295 | ||
116 | PUNCH : [alone] 4294967295 | ||
117 | IDENT __S 4294967295 | ||
118 | PUNCH - [joint] 4294967295 | ||
119 | PUNCH > [alone] 4294967295 | ||
120 | IDENT _serde 4294967295 | ||
121 | PUNCH : [joint] 4294967295 | ||
122 | PUNCH : [alone] 4294967295 | ||
123 | IDENT export 4294967295 | ||
124 | PUNCH : [joint] 4294967295 | ||
125 | PUNCH : [alone] 4294967295 | ||
126 | IDENT Result 4294967295 | ||
127 | PUNCH < [alone] 4294967295 | ||
128 | IDENT __S 4294967295 | ||
129 | PUNCH : [joint] 4294967295 | ||
130 | PUNCH : [alone] 4294967295 | ||
131 | IDENT Ok 4294967295 | ||
132 | PUNCH , [alone] 4294967295 | ||
133 | IDENT __S 4294967295 | ||
134 | PUNCH : [joint] 4294967295 | ||
135 | PUNCH : [alone] 4294967295 | ||
136 | IDENT Error 4294967295 | ||
137 | PUNCH > [alone] 4294967295 | ||
138 | IDENT where 4294967295 | ||
139 | IDENT __S 4294967295 | ||
140 | PUNCH : [alone] 4294967295 | ||
141 | IDENT _serde 4294967295 | ||
142 | PUNCH : [joint] 4294967295 | ||
143 | PUNCH : [alone] 4294967295 | ||
144 | IDENT Serializer 4294967295 | ||
145 | PUNCH , [alone] 4294967295 | ||
146 | SUBTREE {} 4294967295 | ||
147 | IDENT let 4294967295 | ||
148 | IDENT __serde_state 4294967295 | ||
149 | PUNCH = [alone] 4294967295 | ||
150 | IDENT try 4294967295 | ||
151 | PUNCH ! [alone] 4294967295 | ||
152 | SUBTREE () 4294967295 | ||
153 | IDENT _serde 4294967295 | ||
154 | PUNCH : [joint] 4294967295 | ||
155 | PUNCH : [alone] 4294967295 | ||
156 | IDENT Serializer 4294967295 | ||
157 | PUNCH : [joint] 4294967295 | ||
158 | PUNCH : [alone] 4294967295 | ||
159 | IDENT serialize_struct 4294967295 | ||
160 | SUBTREE () 4294967295 | ||
161 | IDENT __serializer 4294967295 | ||
162 | PUNCH , [alone] 4294967295 | ||
163 | LITERAL "Foo" 4294967295 | ||
164 | PUNCH , [alone] 4294967295 | ||
165 | IDENT false 4294967295 | ||
166 | IDENT as 4294967295 | ||
167 | IDENT usize 4294967295 | ||
168 | PUNCH ; [alone] 4294967295 | ||
169 | IDENT _serde 4294967295 | ||
170 | PUNCH : [joint] 4294967295 | ||
171 | PUNCH : [alone] 4294967295 | ||
172 | IDENT ser 4294967295 | ||
173 | PUNCH : [joint] 4294967295 | ||
174 | PUNCH : [alone] 4294967295 | ||
175 | IDENT SerializeStruct 4294967295 | ||
176 | PUNCH : [joint] 4294967295 | ||
177 | PUNCH : [alone] 4294967295 | ||
178 | IDENT end 4294967295 | ||
179 | SUBTREE () 4294967295 | ||
180 | IDENT __serde_state 4294967295 | ||
181 | PUNCH ; [alone] 4294967295 | ||
diff --git a/crates/ra_proc_macro_srv/src/tests/mod.rs b/crates/ra_proc_macro_srv/src/tests/mod.rs deleted file mode 100644 index 8e6f28abd..000000000 --- a/crates/ra_proc_macro_srv/src/tests/mod.rs +++ /dev/null | |||
@@ -1,45 +0,0 @@ | |||
1 | //! proc-macro tests | ||
2 | |||
3 | #[macro_use] | ||
4 | mod utils; | ||
5 | use test_utils::assert_eq_text; | ||
6 | use utils::*; | ||
7 | |||
8 | #[test] | ||
9 | fn test_derive_serialize_proc_macro() { | ||
10 | assert_expand( | ||
11 | "serde_derive", | ||
12 | "Serialize", | ||
13 | "1.0", | ||
14 | r"struct Foo {}", | ||
15 | include_str!("fixtures/test_serialize_proc_macro.txt"), | ||
16 | ); | ||
17 | } | ||
18 | |||
19 | #[test] | ||
20 | fn test_derive_serialize_proc_macro_failed() { | ||
21 | assert_expand( | ||
22 | "serde_derive", | ||
23 | "Serialize", | ||
24 | "1.0", | ||
25 | r"struct {}", | ||
26 | r##" | ||
27 | SUBTREE $ | ||
28 | IDENT compile_error 4294967295 | ||
29 | PUNCH ! [alone] 4294967295 | ||
30 | SUBTREE {} 4294967295 | ||
31 | LITERAL "expected identifier" 4294967295 | ||
32 | "##, | ||
33 | ); | ||
34 | } | ||
35 | |||
36 | #[test] | ||
37 | fn test_derive_proc_macro_list() { | ||
38 | let res = list("serde_derive", "1.0").join("\n"); | ||
39 | |||
40 | assert_eq_text!( | ||
41 | &res, | ||
42 | r#"Serialize [CustomDerive] | ||
43 | Deserialize [CustomDerive]"# | ||
44 | ); | ||
45 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs deleted file mode 100644 index 1b6a0b6fb..000000000 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ /dev/null | |||
@@ -1,64 +0,0 @@ | |||
1 | //! utils used in proc-macro tests | ||
2 | |||
3 | use crate::dylib; | ||
4 | use crate::ProcMacroSrv; | ||
5 | use ra_proc_macro::ListMacrosTask; | ||
6 | use std::str::FromStr; | ||
7 | use test_utils::assert_eq_text; | ||
8 | |||
9 | mod fixtures { | ||
10 | use cargo_metadata::Message; | ||
11 | use std::process::Command; | ||
12 | |||
13 | // Use current project metadata to get the proc-macro dylib path | ||
14 | pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf { | ||
15 | let command = Command::new(toolchain::cargo()) | ||
16 | .args(&["check", "--message-format", "json"]) | ||
17 | .output() | ||
18 | .unwrap() | ||
19 | .stdout; | ||
20 | |||
21 | for message in Message::parse_stream(command.as_slice()) { | ||
22 | match message.unwrap() { | ||
23 | Message::CompilerArtifact(artifact) => { | ||
24 | if artifact.target.kind.contains(&"proc-macro".to_string()) { | ||
25 | let repr = format!("{} {}", crate_name, version); | ||
26 | if artifact.package_id.repr.starts_with(&repr) { | ||
27 | return artifact.filenames[0].clone(); | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | _ => (), // Unknown message | ||
32 | } | ||
33 | } | ||
34 | |||
35 | panic!("No proc-macro dylib for {} found!", crate_name); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> { | ||
40 | Some(crate::rustc_server::TokenStream::from_str(code).unwrap()) | ||
41 | } | ||
42 | |||
43 | pub fn assert_expand( | ||
44 | crate_name: &str, | ||
45 | macro_name: &str, | ||
46 | version: &str, | ||
47 | ra_fixture: &str, | ||
48 | expect: &str, | ||
49 | ) { | ||
50 | let path = fixtures::dylib_path(crate_name, version); | ||
51 | let expander = dylib::Expander::new(&path).unwrap(); | ||
52 | let fixture = parse_string(ra_fixture).unwrap(); | ||
53 | |||
54 | let res = expander.expand(macro_name, &fixture.subtree, None).unwrap(); | ||
55 | assert_eq_text!(&format!("{:?}", res), &expect.trim()); | ||
56 | } | ||
57 | |||
58 | pub fn list(crate_name: &str, version: &str) -> Vec<String> { | ||
59 | let path = fixtures::dylib_path(crate_name, version); | ||
60 | let task = ListMacrosTask { lib: path }; | ||
61 | let mut srv = ProcMacroSrv::default(); | ||
62 | let res = srv.list_macros(&task).unwrap(); | ||
63 | res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect() | ||
64 | } | ||