aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-05 09:24:27 +0100
committerGitHub <[email protected]>2020-05-05 09:24:27 +0100
commit87af7c9bd71892028ac91c4e514c548a66e4e8a8 (patch)
tree433165dceea40f9b530a343dd4cf3725ec6406c5 /crates/ra_hir_def
parent15de338703fd9c789c4cf8041a59d8730f12bc78 (diff)
parent831bb1cf91d69e7e325ecbdab11e6fc80bcae4a4 (diff)
Merge #4313
4313: refactor: use attrsOwner directly in is_cfg_enabled r=edwin0cheng a=bnjjj As promised @edwin0cheng here is the PR to resolve the comment [here](https://github.com/rust-analyzer/rust-analyzer/pull/4285#discussion_r419200375). Co-authored-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/adt.rs17
-rw-r--r--crates/ra_hir_def/src/body.rs11
-rw-r--r--crates/ra_hir_def/src/body/lower.rs6
-rw-r--r--crates/ra_hir_def/src/data.rs3
4 files changed, 15 insertions, 22 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index d0912ddaa..2bc34d449 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -118,11 +118,12 @@ fn lower_enum(
118 module_id: ModuleId, 118 module_id: ModuleId,
119) { 119) {
120 let expander = CfgExpander::new(db, ast.file_id, module_id.krate); 120 let expander = CfgExpander::new(db, ast.file_id, module_id.krate);
121 let variants = 121 let variants = ast
122 ast.value.variant_list().into_iter().flat_map(|it| it.variants()).filter(|var| { 122 .value
123 let attrs = expander.parse_attrs(var); 123 .variant_list()
124 expander.is_cfg_enabled(&attrs) 124 .into_iter()
125 }); 125 .flat_map(|it| it.variants())
126 .filter(|var| expander.is_cfg_enabled(var));
126 for var in variants { 127 for var in variants {
127 trace.alloc( 128 trace.alloc(
128 || var.clone(), 129 || var.clone(),
@@ -215,8 +216,7 @@ fn lower_struct(
215 match &ast.value { 216 match &ast.value {
216 ast::StructKind::Tuple(fl) => { 217 ast::StructKind::Tuple(fl) => {
217 for (i, fd) in fl.fields().enumerate() { 218 for (i, fd) in fl.fields().enumerate() {
218 let attrs = expander.parse_attrs(&fd); 219 if !expander.is_cfg_enabled(&fd) {
219 if !expander.is_cfg_enabled(&attrs) {
220 continue; 220 continue;
221 } 221 }
222 222
@@ -233,8 +233,7 @@ fn lower_struct(
233 } 233 }
234 ast::StructKind::Record(fl) => { 234 ast::StructKind::Record(fl) => {
235 for fd in fl.fields() { 235 for fd in fl.fields() {
236 let attrs = expander.parse_attrs(&fd); 236 if !expander.is_cfg_enabled(&fd) {
237 if !expander.is_cfg_enabled(&attrs) {
238 continue; 237 continue;
239 } 238 }
240 239
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 4edaad960..f5a7305dc 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -60,7 +60,8 @@ impl CfgExpander {
60 Attrs::new(owner, &self.hygiene) 60 Attrs::new(owner, &self.hygiene)
61 } 61 }
62 62
63 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { 63 pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
64 let attrs = self.parse_attrs(owner);
64 attrs.is_cfg_enabled(&self.cfg_options) 65 attrs.is_cfg_enabled(&self.cfg_options)
65 } 66 }
66} 67}
@@ -141,12 +142,8 @@ impl Expander {
141 InFile { file_id: self.current_file_id, value } 142 InFile { file_id: self.current_file_id, value }
142 } 143 }
143 144
144 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { 145 pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
145 self.cfg_expander.parse_attrs(owner) 146 self.cfg_expander.is_cfg_enabled(owner)
146 }
147
148 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
149 self.cfg_expander.is_cfg_enabled(attrs)
150 } 147 }
151 148
152 fn parse_path(&mut self, path: ast::Path) -> Option<Path> { 149 fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 687216dc3..8f5fa1b55 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -162,8 +162,7 @@ impl ExprCollector<'_> {
162 162
163 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { 163 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
164 let syntax_ptr = AstPtr::new(&expr); 164 let syntax_ptr = AstPtr::new(&expr);
165 let attrs = self.expander.parse_attrs(&expr); 165 if !self.expander.is_cfg_enabled(&expr) {
166 if !self.expander.is_cfg_enabled(&attrs) {
167 return self.missing_expr(); 166 return self.missing_expr();
168 } 167 }
169 match expr { 168 match expr {
@@ -329,8 +328,7 @@ impl ExprCollector<'_> {
329 .fields() 328 .fields()
330 .inspect(|field| field_ptrs.push(AstPtr::new(field))) 329 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
331 .filter_map(|field| { 330 .filter_map(|field| {
332 let attrs = self.expander.parse_attrs(&field); 331 if !self.expander.is_cfg_enabled(&field) {
333 if !self.expander.is_cfg_enabled(&attrs) {
334 return None; 332 return None;
335 } 333 }
336 let name = field.field_name()?.as_name(); 334 let name = field.field_name()?.as_name();
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index d4cba4d05..2dbae04d3 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -335,8 +335,7 @@ fn collect_items(
335 .filter_map(|item_node| match item_node { 335 .filter_map(|item_node| match item_node {
336 ast::ImplItem::FnDef(it) => { 336 ast::ImplItem::FnDef(it) => {
337 let name = it.name().map_or_else(Name::missing, |it| it.as_name()); 337 let name = it.name().map_or_else(Name::missing, |it| it.as_name());
338 let attrs = expander.parse_attrs(&it); 338 if !expander.is_cfg_enabled(&it) {
339 if !expander.is_cfg_enabled(&attrs) {
340 return None; 339 return None;
341 } 340 }
342 let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } 341 let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }