From 2bfb65db93e48d8f9e8ecac0b2ea837c081a4db5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 18:40:43 +0200 Subject: Be consistent about token accesors --- crates/ra_hir_def/src/body/lower.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index b02de5d67..c4a5ec59c 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -572,7 +572,10 @@ impl ExprCollector<'_> { let pattern = match &pat { ast::Pat::BindPat(bp) => { let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); - let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); + let annotation = BindingAnnotation::new( + bp.mut_kw_token().is_some(), + bp.ref_kw_token().is_some(), + ); let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); if annotation == BindingAnnotation::Unannotated && subpat.is_none() { // This could also be a single-segment path pattern. To @@ -613,7 +616,7 @@ impl ExprCollector<'_> { } ast::Pat::RefPat(p) => { let pat = self.collect_pat_opt(p.pat()); - let mutability = Mutability::from_mutable(p.is_mut()); + let mutability = Mutability::from_mutable(p.mut_kw_token().is_some()); Pat::Ref { pat, mutability } } ast::Pat::PathPat(p) => { -- cgit v1.2.3 From 00ec0c10669307bc752812f535f96e121338d688 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 23:19:51 +0200 Subject: use uniform accessor --- crates/ra_hir_def/src/body/lower.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index df560155c..4f23cb5c8 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -372,7 +372,7 @@ impl ExprCollector<'_> { } ast::Expr::RefExpr(e) => { let expr = self.collect_expr_opt(e.expr()); - let mutability = Mutability::from_mutable(e.is_mut()); + let mutability = Mutability::from_mutable(e.mut_kw_token().is_some()); self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) } ast::Expr::PrefixExpr(e) => { -- cgit v1.2.3 From 30084a56a5731343bd4cec727646a6c55900234f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 23:35:05 +0200 Subject: Simpler acessors for keywords --- crates/ra_hir_def/src/body/lower.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 4f23cb5c8..b0d71eb3d 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -372,7 +372,7 @@ impl ExprCollector<'_> { } ast::Expr::RefExpr(e) => { let expr = self.collect_expr_opt(e.expr()); - let mutability = Mutability::from_mutable(e.mut_kw_token().is_some()); + let mutability = Mutability::from_mutable(e.mut_token().is_some()); self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) } ast::Expr::PrefixExpr(e) => { @@ -587,10 +587,8 @@ impl ExprCollector<'_> { let pattern = match &pat { ast::Pat::BindPat(bp) => { let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); - let annotation = BindingAnnotation::new( - bp.mut_kw_token().is_some(), - bp.ref_kw_token().is_some(), - ); + let annotation = + BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some()); let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); if annotation == BindingAnnotation::Unannotated && subpat.is_none() { // This could also be a single-segment path pattern. To @@ -631,7 +629,7 @@ impl ExprCollector<'_> { } ast::Pat::RefPat(p) => { let pat = self.collect_pat_opt(p.pat()); - let mutability = Mutability::from_mutable(p.mut_kw_token().is_some()); + let mutability = Mutability::from_mutable(p.mut_token().is_some()); Pat::Ref { pat, mutability } } ast::Pat::PathPat(p) => { -- cgit v1.2.3 From 730a927c5e6b382690e88f482a03701636242a2c Mon Sep 17 00:00:00 2001 From: Geoffrey Copin Date: Fri, 10 Apr 2020 00:35:43 +0200 Subject: Implement assist "Reorder field names" --- crates/ra_hir_def/src/body/lower.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index b0d71eb3d..80492b733 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -689,9 +689,10 @@ impl ExprCollector<'_> { Pat::Missing } } - // FIXME: implement ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, + // FIXME: implement + ast::Pat::RecordFieldPat(_) => Pat::Missing, }; let ptr = AstPtr::new(&pat); self.alloc_pat(pattern, Either::Left(ptr)) -- cgit v1.2.3 From e63315b8f189396cf556f21d4ca27ae4281d17d7 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Wed, 8 Apr 2020 20:23:51 -0700 Subject: add record pat missing field diagnostic --- crates/ra_hir_def/src/body/lower.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index b0d71eb3d..0855c1d3a 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -668,7 +668,9 @@ impl ExprCollector<'_> { }); fields.extend(iter); - Pat::Record { path, args: fields } + let ellipsis = record_field_pat_list.dotdot_token().is_some(); + + Pat::Record { path, args: fields, ellipsis } } ast::Pat::SlicePat(p) => { let SlicePatComponents { prefix, slice, suffix } = p.components(); -- cgit v1.2.3 From c1244c853c6bdc42bf91a77768963c0d287093ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 00:27:00 +0200 Subject: Forward compat --- crates/ra_hir_def/src/body/lower.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 0855c1d3a..9d6ee095e 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -106,7 +106,7 @@ impl ExprCollector<'_> { fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr) -> ExprId { let ptr = Either::Left(ptr); let src = self.expander.to_source(ptr); - let id = self.make_expr(expr, Ok(src)); + let id = self.make_expr(expr, Ok(src.clone())); self.source_map.expr_map.insert(src, id); id } @@ -118,7 +118,7 @@ impl ExprCollector<'_> { fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr) -> ExprId { let ptr = Either::Right(ptr); let src = self.expander.to_source(ptr); - let id = self.make_expr(expr, Ok(src)); + let id = self.make_expr(expr, Ok(src.clone())); self.source_map.expr_map.insert(src, id); id } @@ -136,7 +136,7 @@ impl ExprCollector<'_> { fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { let src = self.expander.to_source(ptr); - let id = self.make_pat(pat, Ok(src)); + let id = self.make_pat(pat, Ok(src.clone())); self.source_map.pat_map.insert(src, id); id } -- cgit v1.2.3 From 6b0870d12efd868202cfca45da651d21f2441031 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:00:31 +0200 Subject: Simplify --- crates/ra_hir_def/src/body/lower.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 9d6ee095e..06df88a34 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -2,9 +2,7 @@ //! representation. use either::Either; - use hir_expand::{ - hygiene::Hygiene, name::{name, AsName, Name}, MacroDefId, MacroDefKind, }; @@ -18,10 +16,8 @@ use ra_syntax::{ }; use test_utils::tested_by; -use super::{ExprSource, PatSource}; use crate::{ adt::StructKind, - attr::Attrs, body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, builtin_type::{BuiltinFloat, BuiltinInt}, db::DefDatabase, @@ -37,6 +33,8 @@ use crate::{ ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, }; +use super::{ExprSource, PatSource}; + pub(super) fn lower( db: &dyn DefDatabase, def: DefWithBodyId, @@ -309,10 +307,7 @@ impl ExprCollector<'_> { .inspect(|field| field_ptrs.push(AstPtr::new(field))) .filter_map(|field| { let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); - let attrs = Attrs::new( - &field, - &Hygiene::new(self.db.upcast(), self.expander.current_file_id), - ); + let attrs = self.expander.parse_attrs(&field); if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { return None; -- cgit v1.2.3 From e9519e103573e22ea0b461c81edd8cfc001e6a50 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:09:50 +0200 Subject: Pull Expander up --- crates/ra_hir_def/src/body/lower.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 06df88a34..6f56d3d7a 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -29,8 +29,8 @@ use crate::{ path::GenericArgs, path::Path, type_ref::{Mutability, TypeRef}, - AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern, - ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, + AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, + StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, }; use super::{ExprSource, PatSource}; @@ -298,7 +298,6 @@ impl ExprCollector<'_> { self.alloc_expr(Expr::Return { expr }, syntax_ptr) } ast::Expr::RecordLit(e) => { - let crate_graph = self.db.crate_graph(); let path = e.path().and_then(|path| self.expander.parse_path(path)); let mut field_ptrs = Vec::new(); let record_lit = if let Some(nfl) = e.record_field_list() { @@ -306,10 +305,9 @@ impl ExprCollector<'_> { .fields() .inspect(|field| field_ptrs.push(AstPtr::new(field))) .filter_map(|field| { - let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); let attrs = self.expander.parse_attrs(&field); - if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { + if !self.expander.check_cfg(&attrs) { return None; } -- cgit v1.2.3 From deb40d52aaa1989402fc01d65da389dc92fb66fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:20:26 +0200 Subject: Align naming --- crates/ra_hir_def/src/body/lower.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 6f56d3d7a..c1d7eb826 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -306,8 +306,7 @@ impl ExprCollector<'_> { .inspect(|field| field_ptrs.push(AstPtr::new(field))) .filter_map(|field| { let attrs = self.expander.parse_attrs(&field); - - if !self.expander.check_cfg(&attrs) { + if !self.expander.is_cfg_enabled(&attrs) { return None; } -- cgit v1.2.3 From 7a39bc3ba29351feabcd4a16e12568a9e12818ca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 16:42:24 +0200 Subject: Make records grammar more orthogonal We used name [: expr] grammar before, now it is [name :] expr which makes things simpler --- crates/ra_hir_def/src/body/lower.rs | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index c1d7eb826..990761661 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -27,7 +27,6 @@ use crate::{ }, item_scope::BuiltinShadowMode, path::GenericArgs, - path::Path, type_ref::{Mutability, TypeRef}, AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, @@ -113,13 +112,6 @@ impl ExprCollector<'_> { fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { self.make_expr(expr, Err(SyntheticSyntax)) } - fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr) -> ExprId { - let ptr = Either::Right(ptr); - let src = self.expander.to_source(ptr); - let id = self.make_expr(expr, Ok(src.clone())); - self.source_map.expr_map.insert(src, id); - id - } fn empty_block(&mut self) -> ExprId { self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None }) } @@ -309,22 +301,13 @@ impl ExprCollector<'_> { if !self.expander.is_cfg_enabled(&attrs) { return None; } + let name = field.field_name()?.as_name(); Some(RecordLitField { - name: field - .name_ref() - .map(|nr| nr.as_name()) - .unwrap_or_else(Name::missing), - expr: if let Some(e) = field.expr() { - self.collect_expr(e) - } else if let Some(nr) = field.name_ref() { - // field shorthand - self.alloc_expr_field_shorthand( - Expr::Path(Path::from_name_ref(&nr)), - AstPtr::new(&field), - ) - } else { - self.missing_expr() + name, + expr: match field.expr() { + Some(e) => self.collect_expr(e), + None => self.missing_expr(), }, }) }) -- cgit v1.2.3 From 270bcfdfc226bcdfffd7b8aa903073929fcef5f2 Mon Sep 17 00:00:00 2001 From: Geoffrey Copin Date: Sat, 11 Apr 2020 17:04:25 +0200 Subject: Avoid adding a RecordFieldPat variant to the Pat enum --- crates/ra_hir_def/src/body/lower.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 80492b733..e1b08d48f 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -691,8 +691,6 @@ impl ExprCollector<'_> { } // FIXME: implement ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, - // FIXME: implement - ast::Pat::RecordFieldPat(_) => Pat::Missing, }; let ptr = AstPtr::new(&pat); self.alloc_pat(pattern, Either::Left(ptr)) -- cgit v1.2.3 From 0aece75cdd40daa4d48484103cfcd36ba13ba076 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 19:25:33 +0200 Subject: Remove dead code --- crates/ra_hir_def/src/body/lower.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 990761661..cc2532e88 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -101,7 +101,6 @@ impl ExprCollector<'_> { } fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr) -> ExprId { - let ptr = Either::Left(ptr); let src = self.expander.to_source(ptr); let id = self.make_expr(expr, Ok(src.clone())); self.source_map.expr_map.insert(src, id); @@ -281,7 +280,7 @@ impl ExprCollector<'_> { ast::Expr::ParenExpr(e) => { let inner = self.collect_expr_opt(e.expr()); // make the paren expr point to the inner expression as well - let src = self.expander.to_source(Either::Left(syntax_ptr)); + let src = self.expander.to_source(syntax_ptr); self.source_map.expr_map.insert(src, inner); inner } -- cgit v1.2.3