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.rs66
1 files changed, 21 insertions, 45 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 032037c8c..c057dc8f2 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,9 +101,8 @@ 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)); 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);
111 id 107 id
112 } 108 }
@@ -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));
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 }
@@ -136,7 +125,7 @@ impl ExprCollector<'_> {
136 125
137 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { 126 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
138 let src = self.expander.to_source(ptr); 127 let src = self.expander.to_source(ptr);
139 let id = self.make_pat(pat, Ok(src)); 128 let id = self.make_pat(pat, Ok(src.clone()));
140 self.source_map.pat_map.insert(src, id); 129 self.source_map.pat_map.insert(src, id);
141 id 130 id
142 } 131 }
@@ -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 })
@@ -372,7 +346,7 @@ impl ExprCollector<'_> {
372 } 346 }
373 ast::Expr::RefExpr(e) => { 347 ast::Expr::RefExpr(e) => {
374 let expr = self.collect_expr_opt(e.expr()); 348 let expr = self.collect_expr_opt(e.expr());
375 let mutability = Mutability::from_mutable(e.is_mut()); 349 let mutability = Mutability::from_mutable(e.mut_token().is_some());
376 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) 350 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
377 } 351 }
378 ast::Expr::PrefixExpr(e) => { 352 ast::Expr::PrefixExpr(e) => {
@@ -587,7 +561,8 @@ impl ExprCollector<'_> {
587 let pattern = match &pat { 561 let pattern = match &pat {
588 ast::Pat::BindPat(bp) => { 562 ast::Pat::BindPat(bp) => {
589 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); 563 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
590 let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); 564 let annotation =
565 BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
591 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); 566 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
592 if annotation == BindingAnnotation::Unannotated && subpat.is_none() { 567 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
593 // This could also be a single-segment path pattern. To 568 // This could also be a single-segment path pattern. To
@@ -628,7 +603,7 @@ impl ExprCollector<'_> {
628 } 603 }
629 ast::Pat::RefPat(p) => { 604 ast::Pat::RefPat(p) => {
630 let pat = self.collect_pat_opt(p.pat()); 605 let pat = self.collect_pat_opt(p.pat());
631 let mutability = Mutability::from_mutable(p.is_mut()); 606 let mutability = Mutability::from_mutable(p.mut_token().is_some());
632 Pat::Ref { pat, mutability } 607 Pat::Ref { pat, mutability }
633 } 608 }
634 ast::Pat::PathPat(p) => { 609 ast::Pat::PathPat(p) => {
@@ -667,7 +642,9 @@ impl ExprCollector<'_> {
667 }); 642 });
668 fields.extend(iter); 643 fields.extend(iter);
669 644
670 Pat::Record { path, args: fields } 645 let ellipsis = record_field_pat_list.dotdot_token().is_some();
646
647 Pat::Record { path, args: fields, ellipsis }
671 } 648 }
672 ast::Pat::SlicePat(p) => { 649 ast::Pat::SlicePat(p) => {
673 let SlicePatComponents { prefix, slice, suffix } = p.components(); 650 let SlicePatComponents { prefix, slice, suffix } = p.components();
@@ -688,7 +665,6 @@ impl ExprCollector<'_> {
688 Pat::Missing 665 Pat::Missing
689 } 666 }
690 } 667 }
691
692 // FIXME: implement 668 // FIXME: implement
693 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, 669 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
694 }; 670 };