Skip to content

Commit a880a3a

Browse files
committed
Don't discard attributed items when a proc-macro unexpectedly fails to expand
1 parent 54e6583 commit a880a3a

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

crates/hir_expand/src/proc_macro.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Proc Macro Expander stub
22
3-
use crate::db::AstDatabase;
4-
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId};
3+
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind};
54
use mbe::ExpandResult;
65

6+
use crate::db::AstDatabase;
7+
78
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
89
pub struct ProcMacroExpander {
910
krate: CrateId,
@@ -36,20 +37,26 @@ impl ProcMacroExpander {
3637
let krate_graph = db.crate_graph();
3738
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
3839
Some(proc_macro) => proc_macro,
39-
None => return ExpandResult::str_err("No derive macro found.".to_string()),
40+
None => return ExpandResult::str_err("No proc-macro found.".to_string()),
4041
};
4142

4243
// Proc macros have access to the environment variables of the invoking crate.
4344
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()
45+
proc_macro.expander.expand(tt, attr_arg, env).map_or_else(
46+
|err| ExpandResult {
47+
// Don't discard the item in case something unexpected happened while expanding attributes
48+
value: if proc_macro.kind == ProcMacroKind::Attr {
49+
tt.clone()
50+
} else {
51+
Default::default()
52+
},
53+
err: Some(match err {
54+
ProcMacroExpansionError::Panic(text) => mbe::ExpandError::Other(text),
55+
ProcMacroExpansionError::System(text) => mbe::ExpandError::Other(text),
56+
}),
57+
},
58+
ExpandResult::ok,
59+
)
5360
}
5461
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
5562
}

0 commit comments

Comments
 (0)