Pull Expander up

This commit is contained in:
Aleksey Kladov 2020-04-11 17:09:50 +02:00
parent 6b0870d12e
commit e9519e1035
3 changed files with 24 additions and 11 deletions

View file

@ -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<CrateDefMap>,
cfg_options: CfgOptions,
current_file_id: HirFileId,
hygiene: Hygiene,
ast_id_map: Arc<AstIdMap>,
@ -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<T: ast::AstNode>(
@ -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> {
Path::from_src(path, &self.hygiene)
}

View file

@ -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;
}

View file

@ -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<ast::ItemList>,
id: ImplId,
) -> Vec<AssocItemId> {
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