From e6d22187a67e762bb950de244a6ca15f3a0b0731 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 18:25:36 +0200 Subject: Add _token suffix to token accessors I think this makes is more clear which things are : AstNode and which are : AstToken --- crates/ra_hir_def/src/path/lower.rs | 2 +- crates/ra_hir_def/src/path/lower/lower_use.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/path/lower.rs b/crates/ra_hir_def/src/path/lower.rs index 3c13cb2c7..0f806d6fb 100644 --- a/crates/ra_hir_def/src/path/lower.rs +++ b/crates/ra_hir_def/src/path/lower.rs @@ -28,7 +28,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option loop { let segment = path.segment()?; - if segment.coloncolon().is_some() { + if segment.coloncolon_token().is_some() { kind = PathKind::Abs; } diff --git a/crates/ra_hir_def/src/path/lower/lower_use.rs b/crates/ra_hir_def/src/path/lower/lower_use.rs index 6ec944228..5b6854b0f 100644 --- a/crates/ra_hir_def/src/path/lower/lower_use.rs +++ b/crates/ra_hir_def/src/path/lower/lower_use.rs @@ -34,7 +34,7 @@ pub(crate) fn lower_use_tree( let alias = tree.alias().map(|a| { a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias) }); - let is_glob = tree.star().is_some(); + let is_glob = tree.star_token().is_some(); if let Some(ast_path) = tree.path() { // Handle self in a path. // E.g. `use something::{self, <...>}` -- cgit v1.2.3 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 +++++-- crates/ra_hir_def/src/data.rs | 6 +++--- crates/ra_hir_def/src/generics.rs | 2 +- crates/ra_hir_def/src/nameres/raw.rs | 2 +- crates/ra_hir_def/src/type_ref.rs | 4 ++-- 5 files changed, 12 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir_def') 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) => { diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 606ec48b0..689bb6c5c 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -75,7 +75,7 @@ impl FunctionData { TypeRef::unit() }; - let ret_type = if src.value.is_async() { + let ret_type = if src.value.async_kw_token().is_some() { let future_impl = desugar_future_path(ret_type); let ty_bound = TypeBound::Path(future_impl); TypeRef::ImplTrait(vec![ty_bound]) @@ -136,7 +136,7 @@ impl TraitData { pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc { let src = tr.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); - let auto = src.value.is_auto(); + let auto = src.value.auto_kw_token().is_some(); let ast_id_map = db.ast_id_map(src.file_id); let container = AssocContainerId::TraitId(tr); @@ -213,7 +213,7 @@ impl ImplData { let target_trait = src.value.target_trait().map(TypeRef::from_ast); let target_type = TypeRef::from_ast_opt(src.value.target_type()); - let is_negative = src.value.is_negative(); + let is_negative = src.value.excl_token().is_some(); let module_id = impl_loc.container.module(db); let mut items = Vec::new(); diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index b687ce2b2..d850244c4 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -194,7 +194,7 @@ impl GenericParams { } fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) { - if bound.has_question_mark() { + if bound.question_token().is_some() { // FIXME: remove this bound return; } diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index a9dff3a5d..e72ba52cf 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -287,7 +287,7 @@ impl RawItemsCollector { let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene); let ast_id = self.source_ast_id_map.ast_id(&module); - if module.has_semi() { + if module.semi_token().is_some() { let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id }); self.push_item(current_module, attrs, RawItemKind::Module(item)); diff --git a/crates/ra_hir_def/src/type_ref.rs b/crates/ra_hir_def/src/type_ref.rs index 01cc392db..7a8338937 100644 --- a/crates/ra_hir_def/src/type_ref.rs +++ b/crates/ra_hir_def/src/type_ref.rs @@ -77,7 +77,7 @@ impl TypeRef { } ast::TypeRef::PointerType(inner) => { let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); - let mutability = Mutability::from_mutable(inner.is_mut()); + let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some()); TypeRef::RawPtr(Box::new(inner_ty), mutability) } ast::TypeRef::ArrayType(inner) => { @@ -88,7 +88,7 @@ impl TypeRef { } ast::TypeRef::ReferenceType(inner) => { let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); - let mutability = Mutability::from_mutable(inner.is_mut()); + let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some()); TypeRef::Reference(Box::new(inner_ty), mutability) } ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder, -- 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') 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 ++++------ crates/ra_hir_def/src/data.rs | 4 ++-- crates/ra_hir_def/src/type_ref.rs | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def') 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) => { diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 419d62c28..b8fbf0ed4 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -74,7 +74,7 @@ impl FunctionData { TypeRef::unit() }; - let ret_type = if src.value.async_kw_token().is_some() { + let ret_type = if src.value.async_token().is_some() { let future_impl = desugar_future_path(ret_type); let ty_bound = TypeBound::Path(future_impl); TypeRef::ImplTrait(vec![ty_bound]) @@ -135,7 +135,7 @@ impl TraitData { pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc { let src = tr.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); - let auto = src.value.auto_kw_token().is_some(); + let auto = src.value.auto_token().is_some(); let ast_id_map = db.ast_id_map(src.file_id); let container = AssocContainerId::TraitId(tr); diff --git a/crates/ra_hir_def/src/type_ref.rs b/crates/ra_hir_def/src/type_ref.rs index 7a8338937..ea29c4176 100644 --- a/crates/ra_hir_def/src/type_ref.rs +++ b/crates/ra_hir_def/src/type_ref.rs @@ -77,7 +77,7 @@ impl TypeRef { } ast::TypeRef::PointerType(inner) => { let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); - let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some()); + let mutability = Mutability::from_mutable(inner.mut_token().is_some()); TypeRef::RawPtr(Box::new(inner_ty), mutability) } ast::TypeRef::ArrayType(inner) => { @@ -88,7 +88,7 @@ impl TypeRef { } ast::TypeRef::ReferenceType(inner) => { let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); - let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some()); + let mutability = Mutability::from_mutable(inner.mut_token().is_some()); TypeRef::Reference(Box::new(inner_ty), mutability) } ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder, -- 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') 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 +++- crates/ra_hir_def/src/expr.rs | 33 ++++++--------------------------- 2 files changed, 9 insertions(+), 28 deletions(-) (limited to 'crates/ra_hir_def') 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(); diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index 197bbe9bd..e11bdf3ec 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs @@ -376,35 +376,14 @@ pub enum Pat { Wild, Tuple(Vec), Or(Vec), - Record { - path: Option, - args: Vec, - // FIXME: 'ellipsis' option - }, - Range { - start: ExprId, - end: ExprId, - }, - Slice { - prefix: Vec, - slice: Option, - suffix: Vec, - }, + Record { path: Option, args: Vec, ellipsis: bool }, + Range { start: ExprId, end: ExprId }, + Slice { prefix: Vec, slice: Option, suffix: Vec }, Path(Path), Lit(ExprId), - Bind { - mode: BindingAnnotation, - name: Name, - subpat: Option, - }, - TupleStruct { - path: Option, - args: Vec, - }, - Ref { - pat: PatId, - mutability: Mutability, - }, + Bind { mode: BindingAnnotation, name: Name, subpat: Option }, + TupleStruct { path: Option, args: Vec }, + Ref { pat: PatId, mutability: Mutability }, } impl Pat { -- cgit v1.2.3 From c8b4c36f8161d34c8145a49965efee4514275989 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Apr 2020 10:11:05 +0200 Subject: Semicolon token --- crates/ra_hir_def/src/nameres/raw.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index e72ba52cf..afd538e4a 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -287,7 +287,7 @@ impl RawItemsCollector { let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene); let ast_id = self.source_ast_id_map.ast_id(&module); - if module.semi_token().is_some() { + if module.semicolon_token().is_some() { let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id }); self.push_item(current_module, attrs, RawItemKind::Module(item)); -- cgit v1.2.3 From 7995c916fe69eb4fbf7efdbf5cc25869e0111d75 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Apr 2020 20:25:55 +0200 Subject: profile queries --- crates/ra_hir_def/src/lang_item.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 01b367278..d96ac8c0a 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -4,6 +4,7 @@ //! features, such as Fn family of traits. use std::sync::Arc; +use ra_prof::profile; use ra_syntax::SmolStr; use rustc_hash::FxHashMap; @@ -78,6 +79,8 @@ impl LangItems { /// Salsa query. This will look for lang items in a specific crate. pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc { + let _p = profile("crate_lang_items_query"); + let mut lang_items = LangItems::default(); let crate_def_map = db.crate_def_map(krate); @@ -95,6 +98,7 @@ impl LangItems { db: &dyn DefDatabase, module: ModuleId, ) -> Option> { + let _p = profile("module_lang_items_query"); let mut lang_items = LangItems::default(); lang_items.collect_lang_items(db, module); if lang_items.items.is_empty() { @@ -111,6 +115,7 @@ impl LangItems { start_crate: CrateId, item: SmolStr, ) -> Option { + let _p = profile("lang_item_query"); let lang_items = db.crate_lang_items(start_crate); let start_crate_target = lang_items.items.get(&item); if let Some(target) = start_crate_target { -- 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.rs | 6 +++--- crates/ra_hir_def/src/body/lower.rs | 6 +++--- crates/ra_hir_def/src/diagnostics.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 5f9d53ecb..e09996c6f 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -236,7 +236,7 @@ impl Index for Body { impl BodySourceMap { pub fn expr_syntax(&self, expr: ExprId) -> Result { - self.expr_map_back[expr] + self.expr_map_back[expr].clone() } pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option { @@ -255,7 +255,7 @@ impl BodySourceMap { } pub fn pat_syntax(&self, pat: PatId) -> Result { - self.pat_map_back[pat] + self.pat_map_back[pat].clone() } pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option { @@ -264,6 +264,6 @@ impl BodySourceMap { } pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr { - self.field_map[&(expr, field)] + self.field_map[&(expr, field)].clone() } } 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 } diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index 095498429..cfa0f2f76 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs @@ -20,7 +20,7 @@ impl Diagnostic for UnresolvedModule { "unresolved module".to_string() } fn source(&self) -> InFile { - InFile { file_id: self.file, value: self.decl.into() } + InFile { file_id: self.file, value: self.decl.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { self -- 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.rs | 5 +++++ crates/ra_hir_def/src/body/lower.rs | 11 +++-------- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index e09996c6f..ff0758da0 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -14,6 +14,7 @@ use ra_syntax::{ast, AstNode, AstPtr}; use rustc_hash::FxHashMap; use crate::{ + attr::Attrs, db::DefDatabase, expr::{Expr, ExprId, Pat, PatId}, item_scope::BuiltinShadowMode, @@ -102,6 +103,10 @@ impl Expander { InFile { file_id: self.current_file_id, value } } + pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { + Attrs::new(owner, &self.hygiene) + } + fn parse_path(&mut self, path: ast::Path) -> Option { Path::from_src(path, &self.hygiene) } 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.rs | 17 ++++++++++++++++- crates/ra_hir_def/src/body/lower.rs | 8 +++----- crates/ra_hir_def/src/data.rs | 10 +++++----- 3 files changed, 24 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index ff0758da0..48b797dd6 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -24,9 +24,11 @@ use crate::{ src::HasSource, AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, }; +use ra_cfg::CfgOptions; pub(crate) struct Expander { crate_def_map: Arc, + cfg_options: CfgOptions, current_file_id: HirFileId, hygiene: Hygiene, ast_id_map: Arc, @@ -43,7 +45,16 @@ impl Expander { let crate_def_map = db.crate_def_map(module.krate); let hygiene = Hygiene::new(db.upcast(), current_file_id); let ast_id_map = db.ast_id_map(current_file_id); - Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 } + let cfg_options = db.crate_graph()[module.krate].cfg_options.clone(); + Expander { + crate_def_map, + cfg_options, + current_file_id, + hygiene, + ast_id_map, + module, + recursive_limit: 0, + } } pub(crate) fn enter_expand( @@ -107,6 +118,10 @@ impl Expander { Attrs::new(owner, &self.hygiene) } + pub(crate) fn check_cfg(&self, attrs: &Attrs) -> bool { + attrs.is_cfg_enabled(&self.cfg_options) + } + fn parse_path(&mut self, path: ast::Path) -> Option { Path::from_src(path, &self.hygiene) } 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; } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index b8fbf0ed4..dd0e679e8 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -20,7 +20,7 @@ use crate::{ type_ref::{Mutability, TypeBound, TypeRef}, visibility::RawVisibility, AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, - ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, + ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -218,10 +218,11 @@ impl ImplData { let mut items = Vec::new(); if let Some(item_list) = src.value.item_list() { + let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); items.extend(collect_impl_items_in_macros( db, - module_id, + &mut expander, &src.with_value(item_list), id, )); @@ -268,18 +269,17 @@ impl ConstData { fn collect_impl_items_in_macros( db: &dyn DefDatabase, - module_id: ModuleId, + expander: &mut Expander, impl_def: &InFile, id: ImplId, ) -> Vec { - let mut expander = Expander::new(db, impl_def.file_id, module_id); let mut res = Vec::new(); // We set a limit to protect against infinite recursion let limit = 100; for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { - res.extend(collect_impl_items_in_macro(db, &mut expander, m, id, limit)) + res.extend(collect_impl_items_in_macro(db, expander, m, id, limit)) } res -- cgit v1.2.3 From ac21100f32be00c2b80982fa6faf31c0bfaf2c5f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:12:17 +0200 Subject: Use Expander for cfg handling in impls --- crates/ra_hir_def/src/data.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index dd0e679e8..446af9d9b 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -219,7 +219,13 @@ impl ImplData { if let Some(item_list) = src.value.item_list() { let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); - items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); + items.extend(collect_impl_items( + db, + &mut expander, + item_list.impl_items(), + src.file_id, + id, + )); items.extend(collect_impl_items_in_macros( db, &mut expander, @@ -300,6 +306,7 @@ fn collect_impl_items_in_macro( let items: InFile = expander.to_source(items); let mut res = collect_impl_items( db, + expander, items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())), items.file_id, id, @@ -319,32 +326,26 @@ fn collect_impl_items_in_macro( fn collect_impl_items( db: &dyn DefDatabase, + expander: &mut Expander, impl_items: impl Iterator, file_id: crate::HirFileId, id: ImplId, ) -> Vec { let items = db.ast_id_map(file_id); - let crate_graph = db.crate_graph(); - let module_id = id.lookup(db).container.module(db); impl_items .filter_map(|item_node| match item_node { ast::ImplItem::FnDef(it) => { + let attrs = expander.parse_attrs(&it); + if !expander.check_cfg(&attrs) { + return None; + } let def = FunctionLoc { container: AssocContainerId::ImplId(id), ast_id: AstId::new(file_id, items.ast_id(&it)), } .intern(db); - - if !db - .function_data(def) - .attrs - .is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) - { - None - } else { - Some(def.into()) - } + Some(def.into()) } ast::ImplItem::ConstDef(it) => { let def = ConstLoc { -- cgit v1.2.3 From b8eb1597c9a950a31b06d5625f2793d5237d756c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:17:12 +0200 Subject: Use Expander for cfg handling in structs --- crates/ra_hir_def/src/adt.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index be4b0accb..2619d106d 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -4,7 +4,6 @@ use std::sync::Arc; use either::Either; use hir_expand::{ - hygiene::Hygiene, name::{AsName, Name}, InFile, }; @@ -13,7 +12,7 @@ use ra_prof::profile; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; use crate::{ - attr::Attrs, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, + body::Expander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId, LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId, }; @@ -125,8 +124,9 @@ fn lower_enum( impl VariantData { fn new(db: &dyn DefDatabase, flavor: InFile, module_id: ModuleId) -> Self { + let mut expander = Expander::new(db, flavor.file_id, module_id); let mut trace = Trace::new_for_arena(); - match lower_struct(db, &mut trace, &flavor, module_id) { + match lower_struct(db, &mut expander, &mut trace, &flavor) { StructKind::Tuple => VariantData::Tuple(trace.into_arena()), StructKind::Record => VariantData::Record(trace.into_arena()), StructKind::Unit => VariantData::Unit, @@ -178,8 +178,9 @@ impl HasChildSource for VariantId { it.lookup(db).container.module(db), ), }; + let mut expander = Expander::new(db, src.file_id, module_id); let mut trace = Trace::new_for_map(); - lower_struct(db, &mut trace, &src, module_id); + lower_struct(db, &mut expander, &mut trace, &src); src.with_value(trace.into_map()) } } @@ -193,16 +194,15 @@ pub enum StructKind { fn lower_struct( db: &dyn DefDatabase, + expander: &mut Expander, trace: &mut Trace>, ast: &InFile, - module_id: ModuleId, ) -> StructKind { - let crate_graph = db.crate_graph(); match &ast.value { ast::StructKind::Tuple(fl) => { for (i, fd) in fl.fields().enumerate() { - let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)); - if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { + let attrs = expander.parse_attrs(&fd); + if !expander.check_cfg(&attrs) { continue; } @@ -219,8 +219,8 @@ fn lower_struct( } ast::StructKind::Record(fl) => { for fd in fl.fields() { - let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)); - if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { + let attrs = expander.parse_attrs(&fd); + if !expander.check_cfg(&attrs) { continue; } -- cgit v1.2.3 From 55356332bfd22db159fef044b9e611b5b82b417c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:18:42 +0200 Subject: Remove code duplication --- crates/ra_hir_def/src/attr.rs | 1 + crates/ra_hir_def/src/nameres/collector.rs | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 7b0c506b1..2f2e3e5ba 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -93,6 +93,7 @@ impl Attrs { } pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { + // FIXME: handle cfg_attr :-) self.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false)) } } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 8fe3f8617..1c33a6520 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -977,11 +977,7 @@ impl ModCollector<'_, '_> { } fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { - // FIXME: handle cfg_attr :-) - attrs - .by_key("cfg") - .tt_values() - .all(|tt| self.def_collector.cfg_options.is_cfg_enabled(tt) != Some(false)) + attrs.is_cfg_enabled(self.def_collector.cfg_options) } } -- 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/adt.rs | 4 ++-- crates/ra_hir_def/src/body.rs | 2 +- crates/ra_hir_def/src/body/lower.rs | 3 +-- crates/ra_hir_def/src/data.rs | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 2619d106d..c7c3c0bc7 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -202,7 +202,7 @@ fn lower_struct( ast::StructKind::Tuple(fl) => { for (i, fd) in fl.fields().enumerate() { let attrs = expander.parse_attrs(&fd); - if !expander.check_cfg(&attrs) { + if !expander.is_cfg_enabled(&attrs) { continue; } @@ -220,7 +220,7 @@ fn lower_struct( ast::StructKind::Record(fl) => { for fd in fl.fields() { let attrs = expander.parse_attrs(&fd); - if !expander.check_cfg(&attrs) { + if !expander.is_cfg_enabled(&attrs) { continue; } diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 48b797dd6..7fe5403c0 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -118,7 +118,7 @@ impl Expander { Attrs::new(owner, &self.hygiene) } - pub(crate) fn check_cfg(&self, attrs: &Attrs) -> bool { + pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { attrs.is_cfg_enabled(&self.cfg_options) } 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; } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 446af9d9b..56a20c5bd 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -337,7 +337,7 @@ fn collect_impl_items( .filter_map(|item_node| match item_node { ast::ImplItem::FnDef(it) => { let attrs = expander.parse_attrs(&it); - if !expander.check_cfg(&attrs) { + if !expander.is_cfg_enabled(&attrs) { return None; } let def = FunctionLoc { -- cgit v1.2.3 From 38c67e5c0d4297f45ab2b78a00b59f737796d160 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:52:26 +0200 Subject: Avoid cyclic queries in name resolution when processing enums --- crates/ra_hir_def/src/adt.rs | 8 ++--- crates/ra_hir_def/src/body.rs | 47 +++++++++++++++++++++++------- crates/ra_hir_def/src/nameres/collector.rs | 8 +++++ 3 files changed, 48 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index c7c3c0bc7..7c0d93691 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -12,7 +12,7 @@ use ra_prof::profile; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; use crate::{ - body::Expander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, + body::CfgExpander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId, LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId, }; @@ -124,7 +124,7 @@ fn lower_enum( impl VariantData { fn new(db: &dyn DefDatabase, flavor: InFile, module_id: ModuleId) -> Self { - let mut expander = Expander::new(db, flavor.file_id, module_id); + let mut expander = CfgExpander::new(db, flavor.file_id, module_id.krate); let mut trace = Trace::new_for_arena(); match lower_struct(db, &mut expander, &mut trace, &flavor) { StructKind::Tuple => VariantData::Tuple(trace.into_arena()), @@ -178,7 +178,7 @@ impl HasChildSource for VariantId { it.lookup(db).container.module(db), ), }; - let mut expander = Expander::new(db, src.file_id, module_id); + let mut expander = CfgExpander::new(db, src.file_id, module_id.krate); let mut trace = Trace::new_for_map(); lower_struct(db, &mut expander, &mut trace, &src); src.with_value(trace.into_map()) @@ -194,7 +194,7 @@ pub enum StructKind { fn lower_struct( db: &dyn DefDatabase, - expander: &mut Expander, + expander: &mut CfgExpander, trace: &mut Trace>, ast: &InFile, ) -> StructKind { diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 7fe5403c0..7fac6ce66 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -25,32 +25,57 @@ use crate::{ AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, }; use ra_cfg::CfgOptions; +use ra_db::CrateId; + +/// A subser of Exander that only deals with cfg attributes. We only need it to +/// avoid cyclic queries in crate def map during enum processing. +pub(crate) struct CfgExpander { + cfg_options: CfgOptions, + hygiene: Hygiene, +} pub(crate) struct Expander { + cfg_expander: CfgExpander, crate_def_map: Arc, - cfg_options: CfgOptions, current_file_id: HirFileId, - hygiene: Hygiene, ast_id_map: Arc, module: ModuleId, recursive_limit: usize, } +impl CfgExpander { + pub(crate) fn new( + db: &dyn DefDatabase, + current_file_id: HirFileId, + krate: CrateId, + ) -> CfgExpander { + let hygiene = Hygiene::new(db.upcast(), current_file_id); + let cfg_options = db.crate_graph()[krate].cfg_options.clone(); + CfgExpander { cfg_options, hygiene } + } + + pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { + Attrs::new(owner, &self.hygiene) + } + + pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { + attrs.is_cfg_enabled(&self.cfg_options) + } +} + impl Expander { pub(crate) fn new( db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId, ) -> Expander { + let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); let crate_def_map = db.crate_def_map(module.krate); - let hygiene = Hygiene::new(db.upcast(), current_file_id); let ast_id_map = db.ast_id_map(current_file_id); - let cfg_options = db.crate_graph()[module.krate].cfg_options.clone(); Expander { + cfg_expander, crate_def_map, - cfg_options, current_file_id, - hygiene, ast_id_map, module, recursive_limit: 0, @@ -87,7 +112,7 @@ impl Expander { ast_id_map: mem::take(&mut self.ast_id_map), bomb: DropBomb::new("expansion mark dropped"), }; - self.hygiene = Hygiene::new(db.upcast(), file_id); + self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id); self.current_file_id = file_id; self.ast_id_map = db.ast_id_map(file_id); self.recursive_limit += 1; @@ -103,7 +128,7 @@ impl Expander { } pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { - self.hygiene = Hygiene::new(db.upcast(), mark.file_id); + self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id); self.current_file_id = mark.file_id; self.ast_id_map = mem::take(&mut mark.ast_id_map); self.recursive_limit -= 1; @@ -115,15 +140,15 @@ impl Expander { } pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { - Attrs::new(owner, &self.hygiene) + self.cfg_expander.parse_attrs(owner) } pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { - attrs.is_cfg_enabled(&self.cfg_options) + self.cfg_expander.is_cfg_enabled(attrs) } fn parse_path(&mut self, path: ast::Path) -> Option { - Path::from_src(path, &self.hygiene) + Path::from_src(path, &self.cfg_expander.hygiene) } fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option { diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 1c33a6520..98c74fe25 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -462,6 +462,14 @@ impl DefCollector<'_> { Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => { tested_by!(glob_enum); // glob import from enum => just import all the variants + + // XXX: urgh, so this works by accident! Here, we look at + // the enum data, and, in theory, this might require us to + // look back at the crate_def_map, creating a cycle. For + // example, `enum E { crate::some_macro!(); }`. Luckely, the + // only kind of macro that is allowed inside enum is a + // `cfg_macro`, and we don't need to run name resolution for + // it, but this is sheer luck! let enum_data = self.db.enum_data(e); let resolutions = enum_data .variants -- cgit v1.2.3 From f980b0752994a1b823499f6edb10fd9c4c285c42 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 17:54:46 +0200 Subject: Make incremental nameres test harder --- crates/ra_hir_def/src/nameres/tests/incremental.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs index 496fc6b08..87165ac33 100644 --- a/crates/ra_hir_def/src/nameres/tests/incremental.rs +++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs @@ -32,6 +32,9 @@ fn typing_inside_a_function_should_not_invalidate_def_map() { use crate::foo::bar::Baz; + enum E { A, B } + use E::*; + fn foo() -> i32 { 1 + 1 } @@ -46,6 +49,9 @@ fn typing_inside_a_function_should_not_invalidate_def_map() { use crate::foo::bar::Baz; + enum E { A, B } + use E::*; + fn foo() -> i32 { 92 } ", ); -- 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.rs | 5 ----- crates/ra_hir_def/src/body/lower.rs | 27 +++++---------------------- crates/ra_hir_def/src/path.rs | 5 ----- 3 files changed, 5 insertions(+), 32 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 7fac6ce66..67d623714 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -294,11 +294,6 @@ impl BodySourceMap { self.expansions.get(&src).cloned() } - pub fn field_init_shorthand_expr(&self, node: InFile<&ast::RecordField>) -> Option { - let src = node.map(|it| Either::Right(AstPtr::new(it))); - self.expr_map.get(&src).cloned() - } - pub fn pat_syntax(&self, pat: PatId) -> Result { self.pat_map_back[pat].clone() } 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(), }, }) }) diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 904080341..91c7b3e09 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -134,11 +134,6 @@ impl Path { lower::lower_path(path, hygiene) } - /// Converts an `ast::NameRef` into a single-identifier `Path`. - pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path { - Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } - } - /// Converts a known mod path to `Path`. pub(crate) fn from_known_path( path: ModPath, -- 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') 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.rs | 8 ++++---- crates/ra_hir_def/src/body/lower.rs | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 67d623714..eafaf48c1 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -9,6 +9,8 @@ use drop_bomb::DropBomb; use either::Either; use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId}; use ra_arena::{map::ArenaMap, Arena}; +use ra_cfg::CfgOptions; +use ra_db::CrateId; use ra_prof::profile; use ra_syntax::{ast, AstNode, AstPtr}; use rustc_hash::FxHashMap; @@ -24,8 +26,6 @@ use crate::{ src::HasSource, AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, }; -use ra_cfg::CfgOptions; -use ra_db::CrateId; /// A subser of Exander that only deals with cfg attributes. We only need it to /// avoid cyclic queries in crate def map during enum processing. @@ -187,7 +187,7 @@ pub struct Body { pub item_scope: ItemScope, } -pub type ExprPtr = Either, AstPtr>; +pub type ExprPtr = AstPtr; pub type ExprSource = InFile; pub type PatPtr = Either, AstPtr>; @@ -285,7 +285,7 @@ impl BodySourceMap { } pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option { - let src = node.map(|it| Either::Left(AstPtr::new(it))); + let src = node.map(|it| AstPtr::new(it)); self.expr_map.get(&src).cloned() } 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