Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/proc_macro.rs')
-rw-r--r--crates/hir-expand/src/proc_macro.rs44
1 files changed, 23 insertions, 21 deletions
diff --git a/crates/hir-expand/src/proc_macro.rs b/crates/hir-expand/src/proc_macro.rs
index d758e9302c..c9539210ab 100644
--- a/crates/hir-expand/src/proc_macro.rs
+++ b/crates/hir-expand/src/proc_macro.rs
@@ -7,20 +7,23 @@ use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ProcMacroExpander {
- proc_macro_id: Option<ProcMacroId>,
+ proc_macro_id: ProcMacroId,
}
+const DUMMY_ID: u32 = !0;
+
impl ProcMacroExpander {
pub fn new(proc_macro_id: ProcMacroId) -> Self {
- Self { proc_macro_id: Some(proc_macro_id) }
+ assert_ne!(proc_macro_id.0, DUMMY_ID);
+ Self { proc_macro_id }
}
pub fn dummy() -> Self {
- Self { proc_macro_id: None }
+ Self { proc_macro_id: ProcMacroId(DUMMY_ID) }
}
pub fn is_dummy(&self) -> bool {
- self.proc_macro_id.is_none()
+ self.proc_macro_id.0 == DUMMY_ID
}
pub fn expand(
@@ -32,33 +35,37 @@ impl ProcMacroExpander {
attr_arg: Option<&tt::Subtree>,
) -> ExpandResult<tt::Subtree> {
match self.proc_macro_id {
- Some(id) => {
- let krate_graph = db.crate_graph();
- let proc_macros = match &krate_graph[def_crate].proc_macro {
- Ok(proc_macros) => proc_macros,
- Err(_) => {
+ ProcMacroId(DUMMY_ID) => {
+ ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
+ }
+ ProcMacroId(id) => {
+ let proc_macros = db.proc_macros();
+ let proc_macros = match proc_macros.get(&def_crate) {
+ Some(Ok(proc_macros)) => proc_macros,
+ Some(Err(_)) | None => {
never!("Non-dummy expander even though there are no proc macros");
- return ExpandResult::with_err(
+ return ExpandResult::new(
tt::Subtree::empty(),
ExpandError::Other("Internal error".into()),
);
}
};
- let proc_macro = match proc_macros.get(id.0 as usize) {
+ let proc_macro = match proc_macros.get(id as usize) {
Some(proc_macro) => proc_macro,
None => {
never!(
"Proc macro index out of bounds: the length is {} but the index is {}",
proc_macros.len(),
- id.0
+ id
);
- return ExpandResult::with_err(
+ return ExpandResult::new(
tt::Subtree::empty(),
ExpandError::Other("Internal error".into()),
);
}
};
+ let krate_graph = db.crate_graph();
// Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env;
match proc_macro.expander.expand(tt, attr_arg, env) {
@@ -74,17 +81,12 @@ impl ProcMacroExpander {
}
}
ProcMacroExpansionError::System(text)
- | ProcMacroExpansionError::Panic(text) => ExpandResult::with_err(
- tt::Subtree::empty(),
- ExpandError::Other(text.into()),
- ),
+ | ProcMacroExpansionError::Panic(text) => {
+ ExpandResult::new(tt::Subtree::empty(), ExpandError::Other(text.into()))
+ }
},
}
}
- None => ExpandResult::with_err(
- tt::Subtree::empty(),
- ExpandError::UnresolvedProcMacro(def_crate),
- ),
}
}
}