From e9519e103573e22ea0b461c81edd8cfc001e6a50 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:09:50 +0200 Subject: Pull Expander up --- crates/ra_hir_def/src/body.rs | 17 ++++++++++++++++- crates/ra_hir_def/src/body/lower.rs | 8 +++----- crates/ra_hir_def/src/data.rs | 10 +++++----- 3 files changed, 24 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index ff0758da0..48b797dd6 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -24,9 +24,11 @@ use crate::{ src::HasSource, AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, }; +use ra_cfg::CfgOptions; pub(crate) struct Expander { crate_def_map: Arc, + cfg_options: CfgOptions, current_file_id: HirFileId, hygiene: Hygiene, ast_id_map: Arc, @@ -43,7 +45,16 @@ impl Expander { let crate_def_map = db.crate_def_map(module.krate); let hygiene = Hygiene::new(db.upcast(), current_file_id); let ast_id_map = db.ast_id_map(current_file_id); - Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 } + let cfg_options = db.crate_graph()[module.krate].cfg_options.clone(); + Expander { + crate_def_map, + cfg_options, + current_file_id, + hygiene, + ast_id_map, + module, + recursive_limit: 0, + } } pub(crate) fn enter_expand( @@ -107,6 +118,10 @@ impl Expander { Attrs::new(owner, &self.hygiene) } + pub(crate) fn check_cfg(&self, attrs: &Attrs) -> bool { + attrs.is_cfg_enabled(&self.cfg_options) + } + fn parse_path(&mut self, path: ast::Path) -> Option { Path::from_src(path, &self.hygiene) } diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 06df88a34..6f56d3d7a 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -29,8 +29,8 @@ use crate::{ path::GenericArgs, path::Path, type_ref::{Mutability, TypeRef}, - AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern, - ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, + AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, + StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, }; use super::{ExprSource, PatSource}; @@ -298,7 +298,6 @@ impl ExprCollector<'_> { self.alloc_expr(Expr::Return { expr }, syntax_ptr) } ast::Expr::RecordLit(e) => { - let crate_graph = self.db.crate_graph(); let path = e.path().and_then(|path| self.expander.parse_path(path)); let mut field_ptrs = Vec::new(); let record_lit = if let Some(nfl) = e.record_field_list() { @@ -306,10 +305,9 @@ impl ExprCollector<'_> { .fields() .inspect(|field| field_ptrs.push(AstPtr::new(field))) .filter_map(|field| { - let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); let attrs = self.expander.parse_attrs(&field); - if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { + if !self.expander.check_cfg(&attrs) { return None; } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index b8fbf0ed4..dd0e679e8 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -20,7 +20,7 @@ use crate::{ type_ref::{Mutability, TypeBound, TypeRef}, visibility::RawVisibility, AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, - ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, + ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -218,10 +218,11 @@ impl ImplData { let mut items = Vec::new(); if let Some(item_list) = src.value.item_list() { + let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); items.extend(collect_impl_items_in_macros( db, - module_id, + &mut expander, &src.with_value(item_list), id, )); @@ -268,18 +269,17 @@ impl ConstData { fn collect_impl_items_in_macros( db: &dyn DefDatabase, - module_id: ModuleId, + expander: &mut Expander, impl_def: &InFile, id: ImplId, ) -> Vec { - let mut expander = Expander::new(db, impl_def.file_id, module_id); let mut res = Vec::new(); // We set a limit to protect against infinite recursion let limit = 100; for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { - res.extend(collect_impl_items_in_macro(db, &mut expander, m, id, limit)) + res.extend(collect_impl_items_in_macro(db, expander, m, id, limit)) } res -- cgit v1.2.3