Pass crate environment to proc macros

This commit is contained in:
Jonas Schievink 2020-12-11 14:57:50 +01:00
parent 798968e1e3
commit 70877428a8
6 changed files with 42 additions and 9 deletions

View file

@ -1,6 +1,6 @@
//! Proc Macro Expander stub
use crate::{db::AstDatabase, LazyMacroId};
use crate::db::AstDatabase;
use base_db::{CrateId, ProcMacroId};
use tt::buffer::{Cursor, TokenBuffer};
@ -32,7 +32,7 @@ impl ProcMacroExpander {
pub fn expand(
self,
db: &dyn AstDatabase,
_id: LazyMacroId,
calling_crate: CrateId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
match self.proc_macro_id {
@ -47,7 +47,10 @@ impl ProcMacroExpander {
let tt = remove_derive_attrs(tt)
.ok_or_else(|| err!("Fail to remove derive for custom derive"))?;
proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from)
// Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env;
proc_macro.expander.expand(&tt, None, &env).map_err(mbe::ExpandError::from)
}
None => Err(mbe::ExpandError::UnresolvedProcMacro),
}