aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/body
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-04-09 20:27:06 +0100
committerGitHub <[email protected]>2020-04-09 20:27:06 +0100
commit33df20868da38ca47f22f8bfab36dd4c965cf333 (patch)
treed1651cf0e2cda2b50e13a631c11a5d94e92ea306 /crates/ra_hir_def/src/body
parent9635d8bc44cf58875e97e9e77c3426f746ab7007 (diff)
parentc1317d692321ba5ba8f138067ebefbb9559d098d (diff)
Merge #3918
3918: Add support for feature attributes in struct literal r=matklad a=bnjjj As promised here is the next PR to solve 2 different scenarios with feature flag on struct literal. close #3870 Co-authored-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/body')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs51
1 files changed, 33 insertions, 18 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index c4a5ec59c..df560155c 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -4,6 +4,7 @@
4use either::Either; 4use either::Either;
5 5
6use hir_expand::{ 6use hir_expand::{
7 hygiene::Hygiene,
7 name::{name, AsName, Name}, 8 name::{name, AsName, Name},
8 MacroDefId, MacroDefKind, 9 MacroDefId, MacroDefKind,
9}; 10};
@@ -20,6 +21,7 @@ use test_utils::tested_by;
20use super::{ExprSource, PatSource}; 21use super::{ExprSource, PatSource};
21use crate::{ 22use crate::{
22 adt::StructKind, 23 adt::StructKind,
24 attr::Attrs,
23 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, 25 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax},
24 builtin_type::{BuiltinFloat, BuiltinInt}, 26 builtin_type::{BuiltinFloat, BuiltinInt},
25 db::DefDatabase, 27 db::DefDatabase,
@@ -31,8 +33,8 @@ use crate::{
31 path::GenericArgs, 33 path::GenericArgs,
32 path::Path, 34 path::Path,
33 type_ref::{Mutability, TypeRef}, 35 type_ref::{Mutability, TypeRef},
34 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, 36 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern,
35 StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, 37 ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
36}; 38};
37 39
38pub(super) fn lower( 40pub(super) fn lower(
@@ -298,28 +300,41 @@ impl ExprCollector<'_> {
298 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 300 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
299 } 301 }
300 ast::Expr::RecordLit(e) => { 302 ast::Expr::RecordLit(e) => {
303 let crate_graph = self.db.crate_graph();
301 let path = e.path().and_then(|path| self.expander.parse_path(path)); 304 let path = e.path().and_then(|path| self.expander.parse_path(path));
302 let mut field_ptrs = Vec::new(); 305 let mut field_ptrs = Vec::new();
303 let record_lit = if let Some(nfl) = e.record_field_list() { 306 let record_lit = if let Some(nfl) = e.record_field_list() {
304 let fields = nfl 307 let fields = nfl
305 .fields() 308 .fields()
306 .inspect(|field| field_ptrs.push(AstPtr::new(field))) 309 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
307 .map(|field| RecordLitField { 310 .filter_map(|field| {
308 name: field 311 let module_id = ContainerId::DefWithBodyId(self.def).module(self.db);
309 .name_ref() 312 let attrs = Attrs::new(
310 .map(|nr| nr.as_name()) 313 &field,
311 .unwrap_or_else(Name::missing), 314 &Hygiene::new(self.db.upcast(), self.expander.current_file_id),
312 expr: if let Some(e) = field.expr() { 315 );
313 self.collect_expr(e) 316
314 } else if let Some(nr) = field.name_ref() { 317 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) {
315 // field shorthand 318 return None;
316 self.alloc_expr_field_shorthand( 319 }
317 Expr::Path(Path::from_name_ref(&nr)), 320
318 AstPtr::new(&field), 321 Some(RecordLitField {
319 ) 322 name: field
320 } else { 323 .name_ref()
321 self.missing_expr() 324 .map(|nr| nr.as_name())
322 }, 325 .unwrap_or_else(Name::missing),
326 expr: if let Some(e) = field.expr() {
327 self.collect_expr(e)
328 } else if let Some(nr) = field.name_ref() {
329 // field shorthand
330 self.alloc_expr_field_shorthand(
331 Expr::Path(Path::from_name_ref(&nr)),
332 AstPtr::new(&field),
333 )
334 } else {
335 self.missing_expr()
336 },
337 })
323 }) 338 })
324 .collect(); 339 .collect();
325 let spread = nfl.spread().map(|s| self.collect_expr(s)); 340 let spread = nfl.spread().map(|s| self.collect_expr(s));