Skip to content

Commit b9fa37f

Browse files
bors[bot]Veykril
andauthored
Merge #10648
10648: fix: Don't discard attributed items when a proc-macro unexpectedly fails to expand r=Veykril a=Veykril Fixes #9205 Co-authored-by: Lukas Wirth <[email protected]>
2 parents 7810823 + 98cff65 commit b9fa37f

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

crates/hir_expand/src/proc_macro.rs

Lines changed: 22 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,29 @@ 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+
match proc_macro.expander.expand(tt, attr_arg, env) {
46+
Ok(t) => ExpandResult::ok(t),
47+
Err(err) => match err {
48+
// Don't discard the item in case something unexpected happened while expanding attributes
49+
ProcMacroExpansionError::System(text)
50+
if proc_macro.kind == ProcMacroKind::Attr =>
51+
{
52+
ExpandResult {
53+
value: tt.clone(),
54+
err: Some(mbe::ExpandError::Other(text)),
55+
}
56+
}
57+
ProcMacroExpansionError::System(text)
58+
| ProcMacroExpansionError::Panic(text) => {
59+
ExpandResult::only_err(mbe::ExpandError::Other(text))
60+
}
61+
},
62+
}
5363
}
5464
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
5565
}

crates/proc_macro_api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl ProcMacro {
167167
let request = msg::Request::ExpandMacro(task);
168168
let response = self.process.lock().unwrap_or_else(|e| e.into_inner()).send_task(request)?;
169169
match response {
170-
msg::Response::ExpandMacro(it) => Ok(it.map(|it| it.to_subtree())),
170+
msg::Response::ExpandMacro(it) => Ok(it.map(FlatTree::to_subtree)),
171171
msg::Response::ListMacros { .. } => {
172172
Err(ServerError { message: "unexpected response".to_string(), io: None })
173173
}

0 commit comments

Comments
 (0)