10648: fix: Don't discard attributed items when a proc-macro unexpectedly fails to expand r=Veykril a=Veykril

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/9205

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-10-29 13:13:12 +00:00 committed by GitHub
commit b9fa37f5b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 13 deletions

View file

@ -1,9 +1,10 @@
//! Proc Macro Expander stub //! Proc Macro Expander stub
use crate::db::AstDatabase; use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind};
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId};
use mbe::ExpandResult; use mbe::ExpandResult;
use crate::db::AstDatabase;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ProcMacroExpander { pub struct ProcMacroExpander {
krate: CrateId, krate: CrateId,
@ -36,20 +37,29 @@ impl ProcMacroExpander {
let krate_graph = db.crate_graph(); let krate_graph = db.crate_graph();
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) { let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
Some(proc_macro) => proc_macro, Some(proc_macro) => proc_macro,
None => return ExpandResult::str_err("No derive macro found.".to_string()), None => return ExpandResult::str_err("No proc-macro found.".to_string()),
}; };
// Proc macros have access to the environment variables of the invoking crate. // Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env; let env = &krate_graph[calling_crate].env;
match proc_macro.expander.expand(tt, attr_arg, env) {
proc_macro Ok(t) => ExpandResult::ok(t),
.expander Err(err) => match err {
.expand(tt, attr_arg, env) // Don't discard the item in case something unexpected happened while expanding attributes
.map_err(|err| match err { ProcMacroExpansionError::System(text)
ProcMacroExpansionError::Panic(text) => mbe::ExpandError::Other(text), if proc_macro.kind == ProcMacroKind::Attr =>
ProcMacroExpansionError::System(text) => mbe::ExpandError::Other(text), {
}) ExpandResult {
.into() value: tt.clone(),
err: Some(mbe::ExpandError::Other(text)),
}
}
ProcMacroExpansionError::System(text)
| ProcMacroExpansionError::Panic(text) => {
ExpandResult::only_err(mbe::ExpandError::Other(text))
}
},
}
} }
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro), None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
} }

View file

@ -167,7 +167,7 @@ impl ProcMacro {
let request = msg::Request::ExpandMacro(task); let request = msg::Request::ExpandMacro(task);
let response = self.process.lock().unwrap_or_else(|e| e.into_inner()).send_task(request)?; let response = self.process.lock().unwrap_or_else(|e| e.into_inner()).send_task(request)?;
match response { match response {
msg::Response::ExpandMacro(it) => Ok(it.map(|it| it.to_subtree())), msg::Response::ExpandMacro(it) => Ok(it.map(FlatTree::to_subtree)),
msg::Response::ListMacros { .. } => { msg::Response::ListMacros { .. } => {
Err(ServerError { message: "unexpected response".to_string(), io: None }) Err(ServerError { message: "unexpected response".to_string(), io: None })
} }