refactor: use attrsOwner directly in is_cfg_enabled

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
Benjamin Coenen 2020-05-05 09:34:07 +02:00
parent 15de338703
commit 831bb1cf91
4 changed files with 15 additions and 22 deletions

View file

@ -118,11 +118,12 @@ fn lower_enum(
module_id: ModuleId, module_id: ModuleId,
) { ) {
let expander = CfgExpander::new(db, ast.file_id, module_id.krate); let expander = CfgExpander::new(db, ast.file_id, module_id.krate);
let variants = let variants = ast
ast.value.variant_list().into_iter().flat_map(|it| it.variants()).filter(|var| { .value
let attrs = expander.parse_attrs(var); .variant_list()
expander.is_cfg_enabled(&attrs) .into_iter()
}); .flat_map(|it| it.variants())
.filter(|var| expander.is_cfg_enabled(var));
for var in variants { for var in variants {
trace.alloc( trace.alloc(
|| var.clone(), || var.clone(),
@ -215,8 +216,7 @@ fn lower_struct(
match &ast.value { match &ast.value {
ast::StructKind::Tuple(fl) => { ast::StructKind::Tuple(fl) => {
for (i, fd) in fl.fields().enumerate() { for (i, fd) in fl.fields().enumerate() {
let attrs = expander.parse_attrs(&fd); if !expander.is_cfg_enabled(&fd) {
if !expander.is_cfg_enabled(&attrs) {
continue; continue;
} }
@ -233,8 +233,7 @@ fn lower_struct(
} }
ast::StructKind::Record(fl) => { ast::StructKind::Record(fl) => {
for fd in fl.fields() { for fd in fl.fields() {
let attrs = expander.parse_attrs(&fd); if !expander.is_cfg_enabled(&fd) {
if !expander.is_cfg_enabled(&attrs) {
continue; continue;
} }

View file

@ -60,7 +60,8 @@ impl CfgExpander {
Attrs::new(owner, &self.hygiene) Attrs::new(owner, &self.hygiene)
} }
pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
let attrs = self.parse_attrs(owner);
attrs.is_cfg_enabled(&self.cfg_options) attrs.is_cfg_enabled(&self.cfg_options)
} }
} }
@ -141,12 +142,8 @@ impl Expander {
InFile { file_id: self.current_file_id, value } InFile { file_id: self.current_file_id, value }
} }
pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
self.cfg_expander.parse_attrs(owner) self.cfg_expander.is_cfg_enabled(owner)
}
pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
self.cfg_expander.is_cfg_enabled(attrs)
} }
fn parse_path(&mut self, path: ast::Path) -> Option<Path> { fn parse_path(&mut self, path: ast::Path) -> Option<Path> {

View file

@ -162,8 +162,7 @@ impl ExprCollector<'_> {
fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
let syntax_ptr = AstPtr::new(&expr); let syntax_ptr = AstPtr::new(&expr);
let attrs = self.expander.parse_attrs(&expr); if !self.expander.is_cfg_enabled(&expr) {
if !self.expander.is_cfg_enabled(&attrs) {
return self.missing_expr(); return self.missing_expr();
} }
match expr { match expr {
@ -329,8 +328,7 @@ impl ExprCollector<'_> {
.fields() .fields()
.inspect(|field| field_ptrs.push(AstPtr::new(field))) .inspect(|field| field_ptrs.push(AstPtr::new(field)))
.filter_map(|field| { .filter_map(|field| {
let attrs = self.expander.parse_attrs(&field); if !self.expander.is_cfg_enabled(&field) {
if !self.expander.is_cfg_enabled(&attrs) {
return None; return None;
} }
let name = field.field_name()?.as_name(); let name = field.field_name()?.as_name();

View file

@ -335,8 +335,7 @@ fn collect_items(
.filter_map(|item_node| match item_node { .filter_map(|item_node| match item_node {
ast::ImplItem::FnDef(it) => { ast::ImplItem::FnDef(it) => {
let name = it.name().map_or_else(Name::missing, |it| it.as_name()); let name = it.name().map_or_else(Name::missing, |it| it.as_name());
let attrs = expander.parse_attrs(&it); if !expander.is_cfg_enabled(&it) {
if !expander.is_cfg_enabled(&attrs) {
return None; return None;
} }
let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }