aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/body/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/body/lower.rs')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs53
1 files changed, 13 insertions, 40 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 9d6ee095e..6caa87db4 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -2,9 +2,7 @@
2//! representation. 2//! representation.
3 3
4use either::Either; 4use either::Either;
5
6use hir_expand::{ 5use hir_expand::{
7 hygiene::Hygiene,
8 name::{name, AsName, Name}, 6 name::{name, AsName, Name},
9 MacroDefId, MacroDefKind, 7 MacroDefId, MacroDefKind,
10}; 8};
@@ -18,10 +16,8 @@ use ra_syntax::{
18}; 16};
19use test_utils::tested_by; 17use test_utils::tested_by;
20 18
21use super::{ExprSource, PatSource};
22use crate::{ 19use crate::{
23 adt::StructKind, 20 adt::StructKind,
24 attr::Attrs,
25 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, 21 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax},
26 builtin_type::{BuiltinFloat, BuiltinInt}, 22 builtin_type::{BuiltinFloat, BuiltinInt},
27 db::DefDatabase, 23 db::DefDatabase,
@@ -31,12 +27,13 @@ use crate::{
31 }, 27 },
32 item_scope::BuiltinShadowMode, 28 item_scope::BuiltinShadowMode,
33 path::GenericArgs, 29 path::GenericArgs,
34 path::Path,
35 type_ref::{Mutability, TypeRef}, 30 type_ref::{Mutability, TypeRef},
36 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern, 31 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
37 ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, 32 StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
38}; 33};
39 34
35use super::{ExprSource, PatSource};
36
40pub(super) fn lower( 37pub(super) fn lower(
41 db: &dyn DefDatabase, 38 db: &dyn DefDatabase,
42 def: DefWithBodyId, 39 def: DefWithBodyId,
@@ -104,7 +101,6 @@ impl ExprCollector<'_> {
104 } 101 }
105 102
106 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { 103 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
107 let ptr = Either::Left(ptr);
108 let src = self.expander.to_source(ptr); 104 let src = self.expander.to_source(ptr);
109 let id = self.make_expr(expr, Ok(src.clone())); 105 let id = self.make_expr(expr, Ok(src.clone()));
110 self.source_map.expr_map.insert(src, id); 106 self.source_map.expr_map.insert(src, id);
@@ -115,13 +111,6 @@ impl ExprCollector<'_> {
115 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { 111 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
116 self.make_expr(expr, Err(SyntheticSyntax)) 112 self.make_expr(expr, Err(SyntheticSyntax))
117 } 113 }
118 fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
119 let ptr = Either::Right(ptr);
120 let src = self.expander.to_source(ptr);
121 let id = self.make_expr(expr, Ok(src.clone()));
122 self.source_map.expr_map.insert(src, id);
123 id
124 }
125 fn empty_block(&mut self) -> ExprId { 114 fn empty_block(&mut self) -> ExprId {
126 self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None }) 115 self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None })
127 } 116 }
@@ -291,7 +280,7 @@ impl ExprCollector<'_> {
291 ast::Expr::ParenExpr(e) => { 280 ast::Expr::ParenExpr(e) => {
292 let inner = self.collect_expr_opt(e.expr()); 281 let inner = self.collect_expr_opt(e.expr());
293 // make the paren expr point to the inner expression as well 282 // make the paren expr point to the inner expression as well
294 let src = self.expander.to_source(Either::Left(syntax_ptr)); 283 let src = self.expander.to_source(syntax_ptr);
295 self.source_map.expr_map.insert(src, inner); 284 self.source_map.expr_map.insert(src, inner);
296 inner 285 inner
297 } 286 }
@@ -300,7 +289,6 @@ impl ExprCollector<'_> {
300 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 289 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
301 } 290 }
302 ast::Expr::RecordLit(e) => { 291 ast::Expr::RecordLit(e) => {
303 let crate_graph = self.db.crate_graph();
304 let path = e.path().and_then(|path| self.expander.parse_path(path)); 292 let path = e.path().and_then(|path| self.expander.parse_path(path));
305 let mut field_ptrs = Vec::new(); 293 let mut field_ptrs = Vec::new();
306 let record_lit = if let Some(nfl) = e.record_field_list() { 294 let record_lit = if let Some(nfl) = e.record_field_list() {
@@ -308,31 +296,17 @@ impl ExprCollector<'_> {
308 .fields() 296 .fields()
309 .inspect(|field| field_ptrs.push(AstPtr::new(field))) 297 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
310 .filter_map(|field| { 298 .filter_map(|field| {
311 let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); 299 let attrs = self.expander.parse_attrs(&field);
312 let attrs = Attrs::new( 300 if !self.expander.is_cfg_enabled(&attrs) {
313 &field,
314 &Hygiene::new(self.db.upcast(), self.expander.current_file_id),
315 );
316
317 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) {
318 return None; 301 return None;
319 } 302 }
303 let name = field.field_name()?.as_name();
320 304
321 Some(RecordLitField { 305 Some(RecordLitField {
322 name: field 306 name,
323 .name_ref() 307 expr: match field.expr() {
324 .map(|nr| nr.as_name()) 308 Some(e) => self.collect_expr(e),
325 .unwrap_or_else(Name::missing), 309 None => self.missing_expr(),
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 }, 310 },
337 }) 311 })
338 }) 312 })
@@ -663,7 +637,7 @@ impl ExprCollector<'_> {
663 let iter = record_field_pat_list.record_field_pats().filter_map(|f| { 637 let iter = record_field_pat_list.record_field_pats().filter_map(|f| {
664 let ast_pat = f.pat()?; 638 let ast_pat = f.pat()?;
665 let pat = self.collect_pat(ast_pat); 639 let pat = self.collect_pat(ast_pat);
666 let name = f.name()?.as_name(); 640 let name = f.field_name()?.as_name();
667 Some(RecordFieldPat { name, pat }) 641 Some(RecordFieldPat { name, pat })
668 }); 642 });
669 fields.extend(iter); 643 fields.extend(iter);
@@ -691,7 +665,6 @@ impl ExprCollector<'_> {
691 Pat::Missing 665 Pat::Missing
692 } 666 }
693 } 667 }
694
695 // FIXME: implement 668 // FIXME: implement
696 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, 669 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
697 }; 670 };