From 70877428a8d9f17834dee72f03ef80ce5c206e68 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 11 Dec 2020 14:57:50 +0100 Subject: Pass crate environment to proc macros --- crates/hir_expand/src/db.rs | 2 +- crates/hir_expand/src/proc_macro.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'crates/hir_expand/src') diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 077de3727..06f0a3ed9 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -271,7 +271,7 @@ fn expand_proc_macro( _ => unreachable!(), }; - expander.expand(db, lazy_id, ¯o_arg.0) + expander.expand(db, loc.krate, ¯o_arg.0) } fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option { diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs index 38882d2b6..7c77f6ce0 100644 --- a/crates/hir_expand/src/proc_macro.rs +++ b/crates/hir_expand/src/proc_macro.rs @@ -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 { 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), } -- cgit v1.2.3