diff options
author | Benjamin Coenen <[email protected]> | 2020-04-21 13:32:02 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-04-21 13:32:02 +0100 |
commit | 7f143b154e7c47b1f5bdc558bb0b5d1f2bf74f8d (patch) | |
tree | 571b7d5bdfc84bdaeaa8235221fd96a87453fe05 /crates/ra_proc_macro_srv/src/lib.rs | |
parent | 1c3a1385a587f0713908c0ae888ffad31f13de11 (diff) | |
parent | a88887df0726cc3d390db4bfbbc1274195d87f91 (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer
Diffstat (limited to 'crates/ra_proc_macro_srv/src/lib.rs')
-rw-r--r-- | crates/ra_proc_macro_srv/src/lib.rs | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index c62b0ed89..3aca859db 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs | |||
@@ -3,10 +3,10 @@ | |||
3 | //! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code. | 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. | 4 | //! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander. |
5 | //! | 5 | //! |
6 | //! But we change some several design for fitting RA needs: | 6 | //! But we adapt it to better fit RA needs: |
7 | //! | 7 | //! |
8 | //! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with | 8 | //! * We use `ra_tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with |
9 | //! RA then proc-macro2 token stream. | 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` | 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) | 11 | //! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue) |
12 | 12 | ||
@@ -21,36 +21,28 @@ mod dylib; | |||
21 | 21 | ||
22 | use proc_macro::bridge::client::TokenStream; | 22 | use proc_macro::bridge::client::TokenStream; |
23 | use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; | 23 | use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; |
24 | use std::path::Path; | ||
24 | 25 | ||
25 | pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> { | 26 | pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> { |
26 | let expander = dylib::Expander::new(&task.lib) | 27 | let expander = create_expander(&task.lib); |
27 | .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); | ||
28 | 28 | ||
29 | match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { | 29 | match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { |
30 | Ok(expansion) => Ok(ExpansionResult { expansion }), | 30 | Ok(expansion) => Ok(ExpansionResult { expansion }), |
31 | Err(msg) => { | 31 | Err(msg) => { |
32 | let reason = format!( | 32 | Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) |
33 | "Cannot perform expansion for {}: error {:?}!", | ||
34 | &task.macro_name, | ||
35 | msg.as_str() | ||
36 | ); | ||
37 | Err(reason) | ||
38 | } | 33 | } |
39 | } | 34 | } |
40 | } | 35 | } |
41 | 36 | ||
42 | pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> { | 37 | pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult { |
43 | let expander = dylib::Expander::new(&task.lib) | 38 | let expander = create_expander(&task.lib); |
44 | .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); | ||
45 | 39 | ||
46 | match expander.list_macros() { | 40 | ListMacrosResult { macros: expander.list_macros() } |
47 | Ok(macros) => Ok(ListMacrosResult { macros }), | 41 | } |
48 | Err(msg) => { | 42 | |
49 | let reason = | 43 | fn create_expander(lib: &Path) -> dylib::Expander { |
50 | format!("Cannot perform expansion for {:?}: error {:?}!", &task.lib, msg.as_str()); | 44 | dylib::Expander::new(lib) |
51 | Err(reason) | 45 | .unwrap_or_else(|err| panic!("Cannot create expander for {}: {:?}", lib.display(), err)) |
52 | } | ||
53 | } | ||
54 | } | 46 | } |
55 | 47 | ||
56 | pub mod cli; | 48 | pub mod cli; |