From 27cadcd531c017aa7c78c6f7a36f2b7f2ce8a196 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:05:28 +1100 Subject: HasSource::source -> HasSource::source_old To start migrating HasSource::source to return an Option. --- crates/hir/src/code_model.rs | 4 ++-- crates/hir/src/has_source.rs | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 97b7a8b5f..5020aa196 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -989,7 +989,7 @@ impl MacroDef { if self.is_proc_macro() { return None; } - self.source(db).value.name().map(|it| it.as_name()) + self.source_old(db).value.name().map(|it| it.as_name()) } /// Indicate it is a proc-macro @@ -1378,7 +1378,7 @@ impl Impl { } pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option> { - let src = self.source(db); + let src = self.source_old(db); let item = src.file_id.is_builtin_derive(db.upcast())?; let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index dd7c0c570..a8256c181 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -16,7 +16,7 @@ use crate::{ pub trait HasSource { type Ast; - fn source(self, db: &dyn HirDatabase) -> InFile; + fn source_old(self, db: &dyn HirDatabase) -> InFile; } /// NB: Module is !HasSource, because it has two source nodes at the same time: @@ -46,7 +46,7 @@ impl Module { impl HasSource for Field { type Ast = FieldSource; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { let var = VariantId::from(self.parent); let src = var.child_source(db.upcast()); src.map(|it| match it[self.id].clone() { @@ -57,61 +57,61 @@ impl HasSource for Field { } impl HasSource for Struct { type Ast = ast::Struct; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Union { type Ast = ast::Union; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Enum { type Ast = ast::Enum; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Variant { type Ast = ast::Variant; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()) } } impl HasSource for Function { type Ast = ast::Fn; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Const { type Ast = ast::Const; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Static { type Ast = ast::Static; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Trait { type Ast = ast::Trait; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for TypeAlias { type Ast = ast::TypeAlias; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for MacroDef { type Ast = ast::Macro; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { InFile { file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id, value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()), @@ -120,14 +120,14 @@ impl HasSource for MacroDef { } impl HasSource for Impl { type Ast = ast::Impl; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for TypeParam { type Ast = Either; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) } @@ -135,7 +135,7 @@ impl HasSource for TypeParam { impl HasSource for LifetimeParam { type Ast = ast::LifetimeParam; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source_old(self, db: &dyn HirDatabase) -> InFile { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) } -- cgit v1.2.3 From 2de2b1eca3c3a3a74c0374f4de0b0c3ff25e66a9 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:27:38 +1100 Subject: Implement new HasSource::source for all implementors of HasSource --- crates/hir/src/has_source.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'crates/hir') diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index a8256c181..84fbeca75 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -17,6 +17,7 @@ use crate::{ pub trait HasSource { type Ast; fn source_old(self, db: &dyn HirDatabase) -> InFile; + fn source(self, db: &dyn HirDatabase) -> Option>; } /// NB: Module is !HasSource, because it has two source nodes at the same time: @@ -54,60 +55,106 @@ impl HasSource for Field { Either::Right(it) => FieldSource::Named(it), }) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let var = VariantId::from(self.parent); + let src = var.child_source(db.upcast()); + let field_source = src.map(|it| match it[self.id].clone() { + Either::Left(it) => FieldSource::Pos(it), + Either::Right(it) => FieldSource::Named(it), + }); + Some(field_source) + } } impl HasSource for Struct { type Ast = ast::Struct; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Union { type Ast = ast::Union; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Enum { type Ast = ast::Enum; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Variant { type Ast = ast::Variant; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())) + } } impl HasSource for Function { type Ast = ast::Fn; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Const { type Ast = ast::Const; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Static { type Ast = ast::Static; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Trait { type Ast = ast::Trait; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for TypeAlias { type Ast = ast::TypeAlias; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for MacroDef { type Ast = ast::Macro; @@ -117,12 +164,21 @@ impl HasSource for MacroDef { value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()), } } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let ast_id = self.id.ast_id?; + Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) }) + } } impl HasSource for Impl { type Ast = ast::Impl; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for TypeParam { @@ -131,6 +187,11 @@ impl HasSource for TypeParam { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let child_source = self.id.parent.child_source(db.upcast()); + Some(child_source.map(|it| it[self.id.local_id].clone())) + } } impl HasSource for LifetimeParam { @@ -139,6 +200,11 @@ impl HasSource for LifetimeParam { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let child_source = self.id.parent.child_source(db.upcast()); + Some(child_source.map(|it| it[self.id.local_id].clone())) + } } impl HasSource for ConstParam { -- cgit v1.2.3 From ea4708c444509449b86c50b7b1b23f9ff5af4e97 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:50:50 +1100 Subject: Mark HasSource::source_old as deprecated but allow at all call sites --- crates/hir/src/code_model.rs | 2 ++ crates/hir/src/has_source.rs | 1 + 2 files changed, 3 insertions(+) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 5020aa196..285905e96 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -989,6 +989,7 @@ impl MacroDef { if self.is_proc_macro() { return None; } + #[allow(deprecated)] self.source_old(db).value.name().map(|it| it.as_name()) } @@ -1378,6 +1379,7 @@ impl Impl { } pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option> { + #[allow(deprecated)] let src = self.source_old(db); let item = src.file_id.is_builtin_derive(db.upcast())?; let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 84fbeca75..8a7306def 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -16,6 +16,7 @@ use crate::{ pub trait HasSource { type Ast; + #[deprecated = "migrating to source() method that returns an Option"] fn source_old(self, db: &dyn HirDatabase) -> InFile; fn source(self, db: &dyn HirDatabase) -> Option>; } -- cgit v1.2.3 From 14d0db0759c5b8e1d085ebab03a8b944a8921f2e Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 14:14:09 +1100 Subject: HasSource::source_old -> HasSource::source for places where proc-macros were special cased In #6901 some special case handling for proc-macros was introduced to prevent panicing as they have no AST. Now the new HasSource::source method is used that returns an option. Generally this was a pretty trivial change, the only thing of much interest is that `hir::MacroDef` now implements `TryToNav` not `ToNav` as this allows us to handle `HasSource::source` now returning an option. --- crates/hir/src/code_model.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 285905e96..62237f481 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -983,14 +983,7 @@ impl MacroDef { /// XXX: this parses the file pub fn name(self, db: &dyn HirDatabase) -> Option { - // FIXME: Currently proc-macro do not have ast-node, - // such that it does not have source - // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913 - if self.is_proc_macro() { - return None; - } - #[allow(deprecated)] - self.source_old(db).value.name().map(|it| it.as_name()) + self.source(db)?.value.name().map(|it| it.as_name()) } /// Indicate it is a proc-macro -- cgit v1.2.3 From c936e4b86fd5de8e9709cd01547a69054cdec91b Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 15:02:39 +1100 Subject: source_old -> source for cases that can be handled by simple bubbling --- crates/hir/src/code_model.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 62237f481..3c83231cf 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -1372,8 +1372,7 @@ impl Impl { } pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option> { - #[allow(deprecated)] - let src = self.source_old(db); + let src = self.source(db)?; let item = src.file_id.is_builtin_derive(db.upcast())?; let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); -- cgit v1.2.3 From 887028fcf52bf7f3af55114f112123c902989bed Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Sat, 2 Jan 2021 16:25:36 +1100 Subject: Remove old_source now we've fully migrated Fixes #6913 --- crates/hir/src/has_source.rs | 68 -------------------------------------------- 1 file changed, 68 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 8a7306def..57baeb3cf 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -16,8 +16,6 @@ use crate::{ pub trait HasSource { type Ast; - #[deprecated = "migrating to source() method that returns an Option"] - fn source_old(self, db: &dyn HirDatabase) -> InFile; fn source(self, db: &dyn HirDatabase) -> Option>; } @@ -48,15 +46,6 @@ impl Module { impl HasSource for Field { type Ast = FieldSource; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - let var = VariantId::from(self.parent); - let src = var.child_source(db.upcast()); - src.map(|it| match it[self.id].clone() { - Either::Left(it) => FieldSource::Pos(it), - Either::Right(it) => FieldSource::Named(it), - }) - } - fn source(self, db: &dyn HirDatabase) -> Option> { let var = VariantId::from(self.parent); let src = var.child_source(db.upcast()); @@ -69,103 +58,60 @@ impl HasSource for Field { } impl HasSource for Struct { type Ast = ast::Struct; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for Union { type Ast = ast::Union; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for Enum { type Ast = ast::Enum; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for Variant { type Ast = ast::Variant; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())) } } impl HasSource for Function { type Ast = ast::Fn; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for Const { type Ast = ast::Const; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for Static { type Ast = ast::Static; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for Trait { type Ast = ast::Trait; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for TypeAlias { type Ast = ast::TypeAlias; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } } impl HasSource for MacroDef { type Ast = ast::Macro; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - InFile { - file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id, - value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()), - } - } - fn source(self, db: &dyn HirDatabase) -> Option> { let ast_id = self.id.ast_id?; Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) }) @@ -173,10 +119,6 @@ impl HasSource for MacroDef { } impl HasSource for Impl { type Ast = ast::Impl; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - self.id.lookup(db.upcast()).source(db.upcast()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { Some(self.id.lookup(db.upcast()).source(db.upcast())) } @@ -184,11 +126,6 @@ impl HasSource for Impl { impl HasSource for TypeParam { type Ast = Either; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - let child_source = self.id.parent.child_source(db.upcast()); - child_source.map(|it| it[self.id.local_id].clone()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { let child_source = self.id.parent.child_source(db.upcast()); Some(child_source.map(|it| it[self.id.local_id].clone())) @@ -197,11 +134,6 @@ impl HasSource for TypeParam { impl HasSource for LifetimeParam { type Ast = ast::LifetimeParam; - fn source_old(self, db: &dyn HirDatabase) -> InFile { - let child_source = self.id.parent.child_source(db.upcast()); - child_source.map(|it| it[self.id.local_id].clone()) - } - fn source(self, db: &dyn HirDatabase) -> Option> { let child_source = self.id.parent.child_source(db.upcast()); Some(child_source.map(|it| it[self.id.local_id].clone())) -- cgit v1.2.3 From 40cd6cdf67dcfad89a80ff3a662bec2dfd983d67 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Sat, 2 Jan 2021 22:11:25 +1100 Subject: Fix ConstParam HasSource impl and implement TryToNav not Nav --- crates/hir/src/has_source.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 57baeb3cf..7c57d8378 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -142,8 +142,8 @@ impl HasSource for LifetimeParam { impl HasSource for ConstParam { type Ast = ast::ConstParam; - fn source(self, db: &dyn HirDatabase) -> InFile { + fn source(self, db: &dyn HirDatabase) -> Option> { let child_source = self.id.parent.child_source(db.upcast()); - child_source.map(|it| it[self.id.local_id].clone()) + Some(child_source.map(|it| it[self.id.local_id].clone())) } } -- cgit v1.2.3