|
1 | 1 | //! Proc Macro Expander stub
|
2 | 2 |
|
3 | 3 | use crate::db::AstDatabase;
|
4 |
| -use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId}; |
| 4 | +use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind}; |
5 | 5 | use mbe::ExpandResult;
|
6 | 6 |
|
7 | 7 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
@@ -36,20 +36,26 @@ impl ProcMacroExpander {
|
36 | 36 | let krate_graph = db.crate_graph();
|
37 | 37 | let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
|
38 | 38 | Some(proc_macro) => proc_macro,
|
39 |
| - None => return ExpandResult::str_err("No derive macro found.".to_string()), |
| 39 | + None => return ExpandResult::str_err("No proc-macro found.".to_string()), |
40 | 40 | };
|
41 | 41 |
|
42 | 42 | // Proc macros have access to the environment variables of the invoking crate.
|
43 | 43 | let env = &krate_graph[calling_crate].env;
|
44 |
| - |
45 |
| - proc_macro |
46 |
| - .expander |
47 |
| - .expand(tt, attr_arg, env) |
48 |
| - .map_err(|err| match err { |
49 |
| - ProcMacroExpansionError::Panic(text) => mbe::ExpandError::Other(text), |
50 |
| - ProcMacroExpansionError::System(text) => mbe::ExpandError::Other(text), |
51 |
| - }) |
52 |
| - .into() |
| 44 | + proc_macro.expander.expand(tt, attr_arg, env).map_or_else( |
| 45 | + |err| ExpandResult { |
| 46 | + // Don't discard the item in case something unexpected happened while expanding attributes |
| 47 | + value: if proc_macro.kind == ProcMacroKind::Attr { |
| 48 | + tt.clone() |
| 49 | + } else { |
| 50 | + Default::default() |
| 51 | + }, |
| 52 | + err: Some(match err { |
| 53 | + ProcMacroExpansionError::Panic(text) => mbe::ExpandError::Other(text), |
| 54 | + ProcMacroExpansionError::System(text) => mbe::ExpandError::Other(text), |
| 55 | + }), |
| 56 | + }, |
| 57 | + ExpandResult::ok, |
| 58 | + ) |
53 | 59 | }
|
54 | 60 | None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
|
55 | 61 | }
|
|
0 commit comments