Skip to content

Commit caf158d

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

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

crates/hir_expand/src/proc_macro.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Proc Macro Expander stub
22
33
use crate::db::AstDatabase;
4-
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId};
4+
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind};
55
use mbe::ExpandResult;
66

77
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
@@ -36,20 +36,26 @@ impl ProcMacroExpander {
3636
let krate_graph = db.crate_graph();
3737
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
3838
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()),
4040
};
4141

4242
// Proc macros have access to the environment variables of the invoking crate.
4343
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+
)
5359
}
5460
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
5561
}

0 commit comments

Comments
 (0)