From 9faea2364dee4fbc9391ad233c570b70256ef002 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Mar 2020 16:05:46 +0100 Subject: Use `dyn Trait` for working with databse It improves compile time in `--release` mode quite a bit, it doesn't really slow things down and, conceptually, it seems closer to what we want the physical architecture to look like (we don't want to monomorphise EVERYTHING in a single leaf crate). --- crates/ra_hir/src/code_model.rs | 329 +++++++++++++-------------- crates/ra_hir/src/has_source.rs | 64 +++--- crates/ra_hir/src/semantics.rs | 22 +- crates/ra_hir/src/semantics/source_to_def.rs | 23 +- crates/ra_hir/src/source_analyzer.rs | 89 ++++---- 5 files changed, 265 insertions(+), 262 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index ff041150b..45e31095c 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -33,11 +33,7 @@ use ra_syntax::{ }; use rustc_hash::FxHashSet; -use crate::{ - db::{DefDatabase, HirDatabase}, - has_source::HasSource, - CallableDef, HirDisplay, InFile, Name, -}; +use crate::{db::HirDatabase, has_source::HasSource, CallableDef, HirDisplay, InFile, Name}; /// hir::Crate describes a single crate. It's the main interface with which /// a crate's dependencies interact. Mostly, it should be just a proxy for the @@ -54,7 +50,7 @@ pub struct CrateDependency { } impl Crate { - pub fn dependencies(self, db: &impl DefDatabase) -> Vec { + pub fn dependencies(self, db: &dyn HirDatabase) -> Vec { db.crate_graph()[self.id] .dependencies .iter() @@ -67,7 +63,7 @@ impl Crate { } // FIXME: add `transitive_reverse_dependencies`. - pub fn reverse_dependencies(self, db: &impl DefDatabase) -> Vec { + pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec { let crate_graph = db.crate_graph(); crate_graph .iter() @@ -78,20 +74,20 @@ impl Crate { .collect() } - pub fn root_module(self, db: &impl DefDatabase) -> Option { + pub fn root_module(self, db: &dyn HirDatabase) -> Option { let module_id = db.crate_def_map(self.id).root; Some(Module::new(self, module_id)) } - pub fn root_file(self, db: &impl DefDatabase) -> FileId { + pub fn root_file(self, db: &dyn HirDatabase) -> FileId { db.crate_graph()[self.id].root_file_id } - pub fn edition(self, db: &impl DefDatabase) -> Edition { + pub fn edition(self, db: &dyn HirDatabase) -> Edition { db.crate_graph()[self.id].edition } - pub fn all(db: &impl DefDatabase) -> Vec { + pub fn all(db: &dyn HirDatabase) -> Vec { db.crate_graph().iter().map(|id| Crate { id }).collect() } } @@ -128,7 +124,7 @@ impl_froms!( ); impl ModuleDef { - pub fn module(self, db: &impl HirDatabase) -> Option { + pub fn module(self, db: &dyn HirDatabase) -> Option { match self { ModuleDef::Module(it) => it.parent(db), ModuleDef::Function(it) => Some(it.module(db)), @@ -153,7 +149,7 @@ impl Module { } /// Name of this module. - pub fn name(self, db: &impl DefDatabase) -> Option { + pub fn name(self, db: &dyn HirDatabase) -> Option { let def_map = db.crate_def_map(self.id.krate); let parent = def_map[self.id.local_id].parent?; def_map[parent].children.iter().find_map(|(name, module_id)| { @@ -173,13 +169,13 @@ impl Module { /// Topmost parent of this module. Every module has a `crate_root`, but some /// might be missing `krate`. This can happen if a module's file is not included /// in the module tree of any target in `Cargo.toml`. - pub fn crate_root(self, db: &impl DefDatabase) -> Module { + pub fn crate_root(self, db: &dyn HirDatabase) -> Module { let def_map = db.crate_def_map(self.id.krate); self.with_module_id(def_map.root) } /// Iterates over all child modules. - pub fn children(self, db: &impl DefDatabase) -> impl Iterator { + pub fn children(self, db: &dyn HirDatabase) -> impl Iterator { let def_map = db.crate_def_map(self.id.krate); let children = def_map[self.id.local_id] .children @@ -190,13 +186,13 @@ impl Module { } /// Finds a parent module. - pub fn parent(self, db: &impl DefDatabase) -> Option { + pub fn parent(self, db: &dyn HirDatabase) -> Option { let def_map = db.crate_def_map(self.id.krate); let parent_id = def_map[self.id.local_id].parent?; Some(self.with_module_id(parent_id)) } - pub fn path_to_root(self, db: &impl HirDatabase) -> Vec { + pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec { let mut res = vec![self]; let mut curr = self; while let Some(next) = curr.parent(db) { @@ -209,7 +205,7 @@ impl Module { /// Returns a `ModuleScope`: a set of items, visible in this module. pub fn scope( self, - db: &impl HirDatabase, + db: &dyn HirDatabase, visible_from: Option, ) -> Vec<(Name, ScopeDef)> { db.crate_def_map(self.id.krate)[self.id.local_id] @@ -217,7 +213,8 @@ impl Module { .entries() .filter_map(|(name, def)| { if let Some(m) = visible_from { - let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id)); + let filtered = + def.filter_visibility(|vis| vis.is_visible_from(db.upcast(), m.id)); if filtered.is_none() && !def.is_none() { None } else { @@ -233,10 +230,10 @@ impl Module { .collect() } - pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { + pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { let _p = profile("Module::diagnostics"); let crate_def_map = db.crate_def_map(self.id.krate); - crate_def_map.add_diagnostics(db, self.id.local_id, sink); + crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); for decl in self.declarations(db) { match decl { crate::ModuleDef::Function(f) => f.diagnostics(db, sink), @@ -259,12 +256,12 @@ impl Module { } } - pub fn declarations(self, db: &impl DefDatabase) -> Vec { + pub fn declarations(self, db: &dyn HirDatabase) -> Vec { let def_map = db.crate_def_map(self.id.krate); def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() } - pub fn impl_defs(self, db: &impl DefDatabase) -> Vec { + pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec { let def_map = db.crate_def_map(self.id.krate); def_map[self.id.local_id].scope.impls().map(ImplDef::from).collect() } @@ -277,11 +274,11 @@ impl Module { /// this module, if possible. pub fn find_use_path( self, - db: &impl DefDatabase, + db: &dyn HirDatabase, item: ModuleDef, ) -> Option { // FIXME expose namespace choice - hir_def::find_path::find_path(db, determine_item_namespace(item), self.into()) + hir_def::find_path::find_path(db.upcast(), determine_item_namespace(item), self.into()) } } @@ -307,7 +304,7 @@ pub enum FieldSource { } impl StructField { - pub fn name(&self, db: &impl HirDatabase) -> Name { + pub fn name(&self, db: &dyn HirDatabase) -> Name { self.parent.variant_data(db).fields()[self.id].name.clone() } @@ -315,7 +312,7 @@ impl StructField { /// placeholder types for type parameters). This is good for showing /// signature help, but not so good to actually get the type of the field /// when you actually have a variable of the struct. - pub fn signature_ty(&self, db: &impl HirDatabase) -> Type { + pub fn signature_ty(&self, db: &dyn HirDatabase) -> Type { let var_id = self.parent.into(); let generic_def_id: GenericDefId = match self.parent { VariantDef::Struct(it) => it.id.into(), @@ -327,17 +324,17 @@ impl StructField { Type::new(db, self.parent.module(db).id.krate, var_id, ty) } - pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef { + pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef { self.parent } } impl HasVisibility for StructField { - fn visibility(&self, db: &impl HirDatabase) -> Visibility { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { let variant_data = self.parent.variant_data(db); let visibility = &variant_data.fields()[self.id].visibility; let parent_id: hir_def::VariantId = self.parent.into(); - visibility.resolve(db, &parent_id.resolver(db)) + visibility.resolve(db.upcast(), &parent_id.resolver(db.upcast())) } } @@ -347,19 +344,19 @@ pub struct Struct { } impl Struct { - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).container.module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } } - pub fn krate(self, db: &impl DefDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn name(self, db: &impl DefDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.struct_data(self.id).name.clone() } - pub fn fields(self, db: &impl HirDatabase) -> Vec { + pub fn fields(self, db: &dyn HirDatabase) -> Vec { db.struct_data(self.id) .variant_data .fields() @@ -368,11 +365,11 @@ impl Struct { .collect() } - pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) + pub fn ty(self, db: &dyn HirDatabase) -> Type { + Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) } - fn variant_data(self, db: &impl DefDatabase) -> Arc { + fn variant_data(self, db: &dyn HirDatabase) -> Arc { db.struct_data(self.id).variant_data.clone() } } @@ -383,19 +380,19 @@ pub struct Union { } impl Union { - pub fn name(self, db: &impl DefDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.union_data(self.id).name.clone() } - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).container.module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } } - pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) + pub fn ty(self, db: &dyn HirDatabase) -> Type { + Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) } - pub fn fields(self, db: &impl HirDatabase) -> Vec { + pub fn fields(self, db: &dyn HirDatabase) -> Vec { db.union_data(self.id) .variant_data .fields() @@ -404,7 +401,7 @@ impl Union { .collect() } - fn variant_data(self, db: &impl DefDatabase) -> Arc { + fn variant_data(self, db: &dyn HirDatabase) -> Arc { db.union_data(self.id).variant_data.clone() } } @@ -415,19 +412,19 @@ pub struct Enum { } impl Enum { - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).container.module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } } - pub fn krate(self, db: &impl DefDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn name(self, db: &impl DefDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.enum_data(self.id).name.clone() } - pub fn variants(self, db: &impl DefDatabase) -> Vec { + pub fn variants(self, db: &dyn HirDatabase) -> Vec { db.enum_data(self.id) .variants .iter() @@ -435,8 +432,8 @@ impl Enum { .collect() } - pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) + pub fn ty(self, db: &dyn HirDatabase) -> Type { + Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) } } @@ -447,18 +444,18 @@ pub struct EnumVariant { } impl EnumVariant { - pub fn module(self, db: &impl HirDatabase) -> Module { + pub fn module(self, db: &dyn HirDatabase) -> Module { self.parent.module(db) } - pub fn parent_enum(self, _db: &impl DefDatabase) -> Enum { + pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { self.parent } - pub fn name(self, db: &impl DefDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.enum_data(self.parent.id).variants[self.id].name.clone() } - pub fn fields(self, db: &impl HirDatabase) -> Vec { + pub fn fields(self, db: &dyn HirDatabase) -> Vec { self.variant_data(db) .fields() .iter() @@ -466,11 +463,11 @@ impl EnumVariant { .collect() } - pub fn kind(self, db: &impl HirDatabase) -> StructKind { + pub fn kind(self, db: &dyn HirDatabase) -> StructKind { self.variant_data(db).kind() } - pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc { + pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc { db.enum_data(self.parent.id).variants[self.id].variant_data.clone() } } @@ -485,7 +482,7 @@ pub enum Adt { impl_froms!(Adt: Struct, Union, Enum); impl Adt { - pub fn has_non_default_type_params(self, db: &impl HirDatabase) -> bool { + pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { let subst = db.generic_defaults(self.into()); subst.iter().any(|ty| ty == &Ty::Unknown) } @@ -493,12 +490,12 @@ impl Adt { /// Turns this ADT into a type. Any type parameters of the ADT will be /// turned into unknown types, which is good for e.g. finding the most /// general set of completions, but will not look very nice when printed. - pub fn ty(self, db: &impl HirDatabase) -> Type { + pub fn ty(self, db: &dyn HirDatabase) -> Type { let id = AdtId::from(self); - Type::from_def(db, id.module(db).krate, id) + Type::from_def(db, id.module(db.upcast()).krate, id) } - pub fn module(self, db: &impl DefDatabase) -> Module { + pub fn module(self, db: &dyn HirDatabase) -> Module { match self { Adt::Struct(s) => s.module(db), Adt::Union(s) => s.module(db), @@ -506,11 +503,11 @@ impl Adt { } } - pub fn krate(self, db: &impl HirDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn name(&self, db: &impl HirDatabase) -> Name { + pub fn name(&self, db: &dyn HirDatabase) -> Name { match self { Adt::Struct(s) => s.name(db), Adt::Union(u) => u.name(db), @@ -528,7 +525,7 @@ pub enum VariantDef { impl_froms!(VariantDef: Struct, Union, EnumVariant); impl VariantDef { - pub fn fields(self, db: &impl HirDatabase) -> Vec { + pub fn fields(self, db: &dyn HirDatabase) -> Vec { match self { VariantDef::Struct(it) => it.fields(db), VariantDef::Union(it) => it.fields(db), @@ -536,7 +533,7 @@ impl VariantDef { } } - pub fn module(self, db: &impl HirDatabase) -> Module { + pub fn module(self, db: &dyn HirDatabase) -> Module { match self { VariantDef::Struct(it) => it.module(db), VariantDef::Union(it) => it.module(db), @@ -544,7 +541,7 @@ impl VariantDef { } } - pub fn name(&self, db: &impl HirDatabase) -> Name { + pub fn name(&self, db: &dyn HirDatabase) -> Name { match self { VariantDef::Struct(s) => s.name(db), VariantDef::Union(u) => u.name(db), @@ -552,7 +549,7 @@ impl VariantDef { } } - pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc { + pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc { match self { VariantDef::Struct(it) => it.variant_data(db), VariantDef::Union(it) => it.variant_data(db), @@ -572,7 +569,7 @@ pub enum DefWithBody { impl_froms!(DefWithBody: Function, Const, Static); impl DefWithBody { - pub fn module(self, db: &impl HirDatabase) -> Module { + pub fn module(self, db: &dyn HirDatabase) -> Module { match self { DefWithBody::Const(c) => c.module(db), DefWithBody::Function(f) => f.module(db), @@ -580,7 +577,7 @@ impl DefWithBody { } } - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &dyn HirDatabase) -> Option { match self { DefWithBody::Function(f) => Some(f.name(db)), DefWithBody::Static(s) => s.name(db), @@ -595,27 +592,27 @@ pub struct Function { } impl Function { - pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.lookup(db).module(db).into() + pub fn module(self, db: &dyn HirDatabase) -> Module { + self.id.lookup(db.upcast()).module(db.upcast()).into() } - pub fn krate(self, db: &impl DefDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn name(self, db: &impl HirDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.function_data(self.id).name.clone() } - pub fn has_self_param(self, db: &impl HirDatabase) -> bool { + pub fn has_self_param(self, db: &dyn HirDatabase) -> bool { db.function_data(self.id).has_self_param } - pub fn params(self, db: &impl HirDatabase) -> Vec { + pub fn params(self, db: &dyn HirDatabase) -> Vec { db.function_data(self.id).params.clone() } - pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { + pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { let _p = profile("Function::diagnostics"); let infer = db.infer(self.id.into()); infer.add_diagnostics(db, self.id, sink); @@ -625,10 +622,10 @@ impl Function { } impl HasVisibility for Function { - fn visibility(&self, db: &impl HirDatabase) -> Visibility { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { let function_data = db.function_data(self.id); let visibility = &function_data.visibility; - visibility.resolve(db, &self.id.resolver(db)) + visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) } } @@ -638,24 +635,24 @@ pub struct Const { } impl Const { - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } } - pub fn krate(self, db: &impl DefDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &dyn HirDatabase) -> Option { db.const_data(self.id).name.clone() } } impl HasVisibility for Const { - fn visibility(&self, db: &impl HirDatabase) -> Visibility { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { let function_data = db.const_data(self.id); let visibility = &function_data.visibility; - visibility.resolve(db, &self.id.resolver(db)) + visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) } } @@ -665,15 +662,15 @@ pub struct Static { } impl Static { - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } } - pub fn krate(self, db: &impl DefDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &dyn HirDatabase) -> Option { db.static_data(self.id).name.clone() } } @@ -684,19 +681,19 @@ pub struct Trait { } impl Trait { - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).container.module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } } - pub fn name(self, db: &impl DefDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.trait_data(self.id).name.clone() } - pub fn items(self, db: &impl DefDatabase) -> Vec { + pub fn items(self, db: &dyn HirDatabase) -> Vec { db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() } - pub fn is_auto(self, db: &impl DefDatabase) -> bool { + pub fn is_auto(self, db: &dyn HirDatabase) -> bool { db.trait_data(self.id).auto } } @@ -707,37 +704,37 @@ pub struct TypeAlias { } impl TypeAlias { - pub fn has_non_default_type_params(self, db: &impl HirDatabase) -> bool { + pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { let subst = db.generic_defaults(self.id.into()); subst.iter().any(|ty| ty == &Ty::Unknown) } - pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.lookup(db).module(db) } + pub fn module(self, db: &dyn HirDatabase) -> Module { + Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } } - pub fn krate(self, db: &impl DefDatabase) -> Option { + pub fn krate(self, db: &dyn HirDatabase) -> Option { Some(self.module(db).krate()) } - pub fn type_ref(self, db: &impl DefDatabase) -> Option { + pub fn type_ref(self, db: &dyn HirDatabase) -> Option { db.type_alias_data(self.id).type_ref.clone() } - pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.lookup(db).module(db).krate, self.id) + pub fn ty(self, db: &dyn HirDatabase) -> Type { + Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate, self.id) } - pub fn name(self, db: &impl DefDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { db.type_alias_data(self.id).name.clone() } } impl HasVisibility for TypeAlias { - fn visibility(&self, db: &impl HirDatabase) -> Visibility { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { let function_data = db.type_alias_data(self.id); let visibility = &function_data.visibility; - visibility.resolve(db, &self.id.resolver(db)) + visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) } } @@ -750,14 +747,14 @@ impl MacroDef { /// FIXME: right now, this just returns the root module of the crate that /// defines this macro. The reasons for this is that macros are expanded /// early, in `ra_hir_expand`, where modules simply do not exist yet. - pub fn module(self, db: &impl HirDatabase) -> Option { + pub fn module(self, db: &dyn HirDatabase) -> Option { let krate = self.id.krate?; let module_id = db.crate_def_map(krate).root; Some(Module::new(Crate { id: krate }, module_id)) } /// XXX: this parses the file - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &dyn HirDatabase) -> Option { self.source(db).value.name().map(|it| it.as_name()) } } @@ -775,50 +772,50 @@ pub enum AssocItemContainer { ImplDef(ImplDef), } pub trait AsAssocItem { - fn as_assoc_item(self, db: &impl DefDatabase) -> Option; + fn as_assoc_item(self, db: &dyn HirDatabase) -> Option; } impl AsAssocItem for Function { - fn as_assoc_item(self, db: &impl DefDatabase) -> Option { + fn as_assoc_item(self, db: &dyn HirDatabase) -> Option { as_assoc_item(db, AssocItem::Function, self.id) } } impl AsAssocItem for Const { - fn as_assoc_item(self, db: &impl DefDatabase) -> Option { + fn as_assoc_item(self, db: &dyn HirDatabase) -> Option { as_assoc_item(db, AssocItem::Const, self.id) } } impl AsAssocItem for TypeAlias { - fn as_assoc_item(self, db: &impl DefDatabase) -> Option { + fn as_assoc_item(self, db: &dyn HirDatabase) -> Option { as_assoc_item(db, AssocItem::TypeAlias, self.id) } } -fn as_assoc_item(db: &impl DefDatabase, ctor: CTOR, id: ID) -> Option +fn as_assoc_item(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option where ID: Lookup>, DEF: From, CTOR: FnOnce(DEF) -> AssocItem, AST: AstNode, { - match id.lookup(db).container { + match id.lookup(db.upcast()).container { AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))), AssocContainerId::ContainerId(_) => None, } } impl AssocItem { - pub fn module(self, db: &impl DefDatabase) -> Module { + pub fn module(self, db: &dyn HirDatabase) -> Module { match self { AssocItem::Function(f) => f.module(db), AssocItem::Const(c) => c.module(db), AssocItem::TypeAlias(t) => t.module(db), } } - pub fn container(self, db: &impl DefDatabase) -> AssocItemContainer { + pub fn container(self, db: &dyn HirDatabase) -> AssocItemContainer { let container = match self { - AssocItem::Function(it) => it.id.lookup(db).container, - AssocItem::Const(it) => it.id.lookup(db).container, - AssocItem::TypeAlias(it) => it.id.lookup(db).container, + AssocItem::Function(it) => it.id.lookup(db.upcast()).container, + AssocItem::Const(it) => it.id.lookup(db.upcast()).container, + AssocItem::TypeAlias(it) => it.id.lookup(db.upcast()).container, }; match container { AssocContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()), @@ -829,7 +826,7 @@ impl AssocItem { } impl HasVisibility for AssocItem { - fn visibility(&self, db: &impl HirDatabase) -> Visibility { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { match self { AssocItem::Function(f) => f.visibility(db), AssocItem::Const(c) => c.visibility(db), @@ -862,7 +859,7 @@ impl_froms!( ); impl GenericDef { - pub fn params(self, db: &impl HirDatabase) -> Vec { + pub fn params(self, db: &dyn HirDatabase) -> Vec { let generics: Arc = db.generic_params(self.into()); generics .types @@ -880,7 +877,7 @@ pub struct Local { impl Local { // FIXME: why is this an option? It shouldn't be? - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &dyn HirDatabase) -> Option { let body = db.body(self.parent.into()); match &body[self.pat_id] { Pat::Bind { name, .. } => Some(name.clone()), @@ -888,11 +885,11 @@ impl Local { } } - pub fn is_self(self, db: &impl HirDatabase) -> bool { + pub fn is_self(self, db: &dyn HirDatabase) -> bool { self.name(db) == Some(name![self]) } - pub fn is_mut(self, db: &impl HirDatabase) -> bool { + pub fn is_mut(self, db: &dyn HirDatabase) -> bool { let body = db.body(self.parent.into()); match &body[self.pat_id] { Pat::Bind { mode, .. } => match mode { @@ -903,28 +900,28 @@ impl Local { } } - pub fn parent(self, _db: &impl HirDatabase) -> DefWithBody { + pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody { self.parent.into() } - pub fn module(self, db: &impl HirDatabase) -> Module { + pub fn module(self, db: &dyn HirDatabase) -> Module { self.parent(db).module(db) } - pub fn ty(self, db: &impl HirDatabase) -> Type { + pub fn ty(self, db: &dyn HirDatabase) -> Type { let def = DefWithBodyId::from(self.parent); let infer = db.infer(def); let ty = infer[self.pat_id].clone(); - let resolver = def.resolver(db); - let krate = def.module(db).krate; + let resolver = def.resolver(db.upcast()); + let krate = def.module(db.upcast()).krate; let environment = TraitEnvironment::lower(db, &resolver); Type { krate, ty: InEnvironment { value: ty, environment } } } - pub fn source(self, db: &impl HirDatabase) -> InFile> { + pub fn source(self, db: &dyn HirDatabase) -> InFile> { let (_body, source_map) = db.body_with_source_map(self.parent.into()); let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... - let root = src.file_syntax(db); + let root = src.file_syntax(db.upcast()); src.map(|ast| { ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root)) }) @@ -937,13 +934,13 @@ pub struct TypeParam { } impl TypeParam { - pub fn name(self, db: &impl HirDatabase) -> Name { + pub fn name(self, db: &dyn HirDatabase) -> Name { let params = db.generic_params(self.id.parent); params.types[self.id.local_id].name.clone().unwrap_or_else(Name::missing) } - pub fn module(self, db: &impl HirDatabase) -> Module { - self.id.parent.module(db).into() + pub fn module(self, db: &dyn HirDatabase) -> Module { + self.id.parent.module(db.upcast()).into() } } @@ -954,55 +951,55 @@ pub struct ImplDef { } impl ImplDef { - pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec { + pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec { let impls = db.impls_in_crate(krate.id); impls.all_impls().map(Self::from).collect() } - pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec { + pub fn for_trait(db: &dyn HirDatabase, krate: Crate, trait_: Trait) -> Vec { let impls = db.impls_in_crate(krate.id); impls.lookup_impl_defs_for_trait(trait_.id).map(Self::from).collect() } - pub fn target_trait(&self, db: &impl DefDatabase) -> Option { + pub fn target_trait(&self, db: &dyn HirDatabase) -> Option { db.impl_data(self.id).target_trait.clone() } - pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef { + pub fn target_type(&self, db: &dyn HirDatabase) -> TypeRef { db.impl_data(self.id).target_type.clone() } - pub fn target_ty(&self, db: &impl HirDatabase) -> Type { + pub fn target_ty(&self, db: &dyn HirDatabase) -> Type { let impl_data = db.impl_data(self.id); - let resolver = self.id.resolver(db); + let resolver = self.id.resolver(db.upcast()); let ctx = hir_ty::TyLoweringContext::new(db, &resolver); let environment = TraitEnvironment::lower(db, &resolver); let ty = Ty::from_hir(&ctx, &impl_data.target_type); Type { - krate: self.id.lookup(db).container.module(db).krate, + krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, ty: InEnvironment { value: ty, environment }, } } - pub fn items(&self, db: &impl DefDatabase) -> Vec { + pub fn items(&self, db: &dyn HirDatabase) -> Vec { db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect() } - pub fn is_negative(&self, db: &impl DefDatabase) -> bool { + pub fn is_negative(&self, db: &dyn HirDatabase) -> bool { db.impl_data(self.id).is_negative } - pub fn module(&self, db: &impl DefDatabase) -> Module { - self.id.lookup(db).container.module(db).into() + pub fn module(&self, db: &dyn HirDatabase) -> Module { + self.id.lookup(db.upcast()).container.module(db.upcast()).into() } - pub fn krate(&self, db: &impl DefDatabase) -> Crate { + pub fn krate(&self, db: &dyn HirDatabase) -> Crate { Crate { id: self.module(db).id.krate } } - pub fn is_builtin_derive(&self, db: &impl DefDatabase) -> Option> { + pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> Option> { let src = self.source(db); - let item = src.file_id.is_builtin_derive(db)?; - let hygenic = hir_expand::hygiene::Hygiene::new(db, item.file_id); + let item = src.file_id.is_builtin_derive(db.upcast())?; + let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); let attr = item .value @@ -1028,14 +1025,14 @@ pub struct Type { } impl Type { - fn new(db: &impl HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { - let resolver = lexical_env.resolver(db); + fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { + let resolver = lexical_env.resolver(db.upcast()); let environment = TraitEnvironment::lower(db, &resolver); Type { krate, ty: InEnvironment { value: ty, environment } } } fn from_def( - db: &impl HirDatabase, + db: &dyn HirDatabase, krate: CrateId, def: impl HasResolver + Into + Into, ) -> Type { @@ -1073,7 +1070,7 @@ impl Type { /// Checks that particular type `ty` implements `std::future::Future`. /// This function is used in `.await` syntax completion. - pub fn impls_future(&self, db: &impl HirDatabase) -> bool { + pub fn impls_future(&self, db: &dyn HirDatabase) -> bool { let krate = self.krate; let std_future_trait = @@ -1110,7 +1107,7 @@ impl Type { } } - pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> { + pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(StructField, Type)> { if let Ty::Apply(a_ty) = &self.ty.value { if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor { let var_def = s.into(); @@ -1128,7 +1125,7 @@ impl Type { Vec::new() } - pub fn tuple_fields(&self, _db: &impl HirDatabase) -> Vec { + pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec { let mut res = Vec::new(); if let Ty::Apply(a_ty) = &self.ty.value { if let TypeCtor::Tuple { .. } = a_ty.ctor { @@ -1143,7 +1140,7 @@ impl Type { pub fn variant_fields( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, def: VariantDef, ) -> Vec<(StructField, Type)> { // FIXME: check that ty and def match @@ -1162,7 +1159,7 @@ impl Type { } } - pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator + 'a { + pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator + 'a { // There should be no inference vars in types passed here // FIXME check that? let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; @@ -1177,7 +1174,7 @@ impl Type { // lifetime problems, because we need to borrow temp `CrateImplDefs`. pub fn iterate_impl_items( self, - db: &impl HirDatabase, + db: &dyn HirDatabase, krate: Crate, mut callback: impl FnMut(AssocItem) -> Option, ) -> Option { @@ -1197,7 +1194,7 @@ impl Type { pub fn iterate_method_candidates( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, krate: Crate, traits_in_scope: &FxHashSet, name: Option<&Name>, @@ -1228,7 +1225,7 @@ impl Type { pub fn iterate_path_candidates( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, krate: Crate, traits_in_scope: &FxHashSet, name: Option<&Name>, @@ -1283,7 +1280,7 @@ impl Type { } impl HirDisplay for Type { - fn hir_fmt(&self, f: &mut HirFormatter) -> std::fmt::Result { + fn hir_fmt(&self, f: &mut HirFormatter) -> std::fmt::Result { self.ty.value.hir_fmt(f) } } @@ -1360,30 +1357,30 @@ impl_froms!( ); pub trait HasAttrs { - fn attrs(self, db: &impl DefDatabase) -> Attrs; + fn attrs(self, db: &dyn HirDatabase) -> Attrs; } impl> HasAttrs for T { - fn attrs(self, db: &impl DefDatabase) -> Attrs { + fn attrs(self, db: &dyn HirDatabase) -> Attrs { let def: AttrDef = self.into(); db.attrs(def.into()) } } pub trait Docs { - fn docs(&self, db: &impl HirDatabase) -> Option; + fn docs(&self, db: &dyn HirDatabase) -> Option; } impl + Copy> Docs for T { - fn docs(&self, db: &impl HirDatabase) -> Option { + fn docs(&self, db: &dyn HirDatabase) -> Option { let def: AttrDef = (*self).into(); db.documentation(def.into()) } } pub trait HasVisibility { - fn visibility(&self, db: &impl HirDatabase) -> Visibility; - fn is_visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility; + fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool { let vis = self.visibility(db); - vis.is_visible_from(db, module.id) + vis.is_visible_from(db.upcast(), module.id) } } diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index f121e1eff..129764e0a 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -9,7 +9,7 @@ use hir_def::{ use ra_syntax::ast; use crate::{ - db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module, + db::HirDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, }; @@ -17,31 +17,31 @@ pub use hir_expand::InFile; pub trait HasSource { type Ast; - fn source(self, db: &impl DefDatabase) -> InFile; + fn source(self, db: &dyn HirDatabase) -> InFile; } /// NB: Module is !HasSource, because it has two source nodes at the same time: /// definition and declaration. impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. - pub fn definition_source(self, db: &impl DefDatabase) -> InFile { + pub fn definition_source(self, db: &dyn HirDatabase) -> InFile { let def_map = db.crate_def_map(self.id.krate); - def_map[self.id.local_id].definition_source(db) + def_map[self.id.local_id].definition_source(db.upcast()) } /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. /// `None` for the crate root. - pub fn declaration_source(self, db: &impl DefDatabase) -> Option> { + pub fn declaration_source(self, db: &dyn HirDatabase) -> Option> { let def_map = db.crate_def_map(self.id.krate); - def_map[self.id.local_id].declaration_source(db) + def_map[self.id.local_id].declaration_source(db.upcast()) } } impl HasSource for StructField { type Ast = FieldSource; - fn source(self, db: &impl DefDatabase) -> InFile { + fn source(self, db: &dyn HirDatabase) -> InFile { let var = VariantId::from(self.parent); - let src = var.child_source(db); + 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), @@ -50,78 +50,78 @@ impl HasSource for StructField { } impl HasSource for Struct { type Ast = ast::StructDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Union { type Ast = ast::UnionDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Enum { type Ast = ast::EnumDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for EnumVariant { type Ast = ast::EnumVariant; - fn source(self, db: &impl DefDatabase) -> InFile { - self.parent.id.child_source(db).map(|map| map[self.id].clone()) + fn source(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::FnDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Const { type Ast = ast::ConstDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Static { type Ast = ast::StaticDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Trait { type Ast = ast::TraitDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for TypeAlias { type Ast = ast::TypeAliasDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for MacroDef { type Ast = ast::MacroCall; - fn source(self, db: &impl DefDatabase) -> InFile { + fn source(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), + value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()), } } } impl HasSource for ImplDef { type Ast = ast::ImplDef; - fn source(self, db: &impl DefDatabase) -> InFile { - self.id.lookup(db).source(db) + fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for TypeParam { type Ast = Either; - fn source(self, db: &impl DefDatabase) -> InFile { - let child_source = self.id.parent.child_source(db); + fn source(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()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 788bb3eb7..55e634528 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -190,7 +190,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { T::to_def(self, src) } - fn with_ctx) -> T, T>(&self, f: F) -> T { + fn with_ctx T, T>(&self, f: F) -> T { let mut cache = self.s2d_cache.borrow_mut(); let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache }; f(&mut ctx) @@ -369,35 +369,35 @@ impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> { } // FIXME: Change `HasSource` trait to work with `Semantics` and remove this? -pub fn original_range(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> FileRange { +pub fn original_range(db: &dyn HirDatabase, node: InFile<&SyntaxNode>) -> FileRange { if let Some(range) = original_range_opt(db, node) { - let original_file = range.file_id.original_file(db); + let original_file = range.file_id.original_file(db.upcast()); if range.file_id == original_file.into() { return FileRange { file_id: original_file, range: range.value }; } log::error!("Fail to mapping up more for {:?}", range); - return FileRange { file_id: range.file_id.original_file(db), range: range.value }; + return FileRange { file_id: range.file_id.original_file(db.upcast()), range: range.value }; } // Fall back to whole macro call - if let Some(expansion) = node.file_id.expansion_info(db) { + if let Some(expansion) = node.file_id.expansion_info(db.upcast()) { if let Some(call_node) = expansion.call_node() { return FileRange { - file_id: call_node.file_id.original_file(db), + file_id: call_node.file_id.original_file(db.upcast()), range: call_node.value.text_range(), }; } } - FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } + FileRange { file_id: node.file_id.original_file(db.upcast()), range: node.value.text_range() } } fn original_range_opt( - db: &impl HirDatabase, + db: &dyn HirDatabase, node: InFile<&SyntaxNode>, ) -> Option> { - let expansion = node.file_id.expansion_info(db)?; + let expansion = node.file_id.expansion_info(db.upcast())?; // the input node has only one token ? let single = skip_trivia_token(node.value.first_token()?, Direction::Next)? @@ -419,7 +419,7 @@ fn original_range_opt( } fn ascend_call_token( - db: &impl HirDatabase, + db: &dyn HirDatabase, expansion: &ExpansionInfo, token: InFile, ) -> Option> { @@ -427,7 +427,7 @@ fn ascend_call_token( if origin != Origin::Call { return None; } - if let Some(info) = mapped.file_id.expansion_info(db) { + if let Some(info) = mapped.file_id.expansion_info(db.upcast()) { return ascend_call_token(db, &info, mapped); } Some(mapped) diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 67b243222..8843f2835 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -21,12 +21,12 @@ use crate::{db::HirDatabase, InFile, MacroDefId}; pub(super) type SourceToDefCache = FxHashMap; -pub(super) struct SourceToDefCtx<'a, DB> { - pub(super) db: DB, +pub(super) struct SourceToDefCtx<'a, 'b> { + pub(super) db: &'b dyn HirDatabase, pub(super) cache: &'a mut SourceToDefCache, } -impl SourceToDefCtx<'_, &'_ DB> { +impl SourceToDefCtx<'_, '_> { pub(super) fn file_to_def(&mut self, file: FileId) -> Option { let _p = profile("SourceBinder::to_module_def"); let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { @@ -43,7 +43,7 @@ impl SourceToDefCtx<'_, &'_ DB> { .as_ref() .map(|it| it.syntax()) .cloned() - .ancestors_with_macros(self.db) + .ancestors_with_macros(self.db.upcast()) .skip(1) .find_map(|it| { let m = ast::Module::cast(it.value.clone())?; @@ -53,7 +53,7 @@ impl SourceToDefCtx<'_, &'_ DB> { let parent_module = match parent_declaration { Some(parent_declaration) => self.module_to_def(parent_declaration), None => { - let file_id = src.file_id.original_file(self.db); + let file_id = src.file_id.original_file(self.db.upcast()); self.file_to_def(file_id) } }?; @@ -147,7 +147,7 @@ impl SourceToDefCtx<'_, &'_ DB> { // FIXME: use DynMap as well? pub(super) fn macro_call_to_def(&mut self, src: InFile) -> Option { let kind = MacroDefKind::Declarative; - let file_id = src.file_id.original_file(self.db); + let file_id = src.file_id.original_file(self.db.upcast()); let krate = self.file_to_def(file_id)?.krate; let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); let ast_id = Some(AstId::new(src.file_id, file_ast_id)); @@ -155,7 +155,7 @@ impl SourceToDefCtx<'_, &'_ DB> { } pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option { - for container in src.cloned().ancestors_with_macros(self.db).skip(1) { + for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { let res: ChildContainer = match_ast! { match (container.value) { ast::Module(it) => { @@ -200,12 +200,12 @@ impl SourceToDefCtx<'_, &'_ DB> { return Some(res); } - let def = self.file_to_def(src.file_id.original_file(self.db))?; + let def = self.file_to_def(src.file_id.original_file(self.db.upcast()))?; Some(def.into()) } fn find_type_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option { - for container in src.cloned().ancestors_with_macros(self.db).skip(1) { + for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { let res: GenericDefId = match_ast! { match (container.value) { ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() }, @@ -223,7 +223,7 @@ impl SourceToDefCtx<'_, &'_ DB> { } fn find_pat_container(&mut self, src: InFile<&SyntaxNode>) -> Option { - for container in src.cloned().ancestors_with_macros(self.db).skip(1) { + for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { let res: DefWithBodyId = match_ast! { match (container.value) { ast::ConstDef(it) => { self.const_to_def(container.with_value(it))?.into() }, @@ -262,7 +262,8 @@ impl_froms! { } impl ChildContainer { - fn child_by_source(self, db: &impl HirDatabase) -> DynMap { + fn child_by_source(self, db: &dyn HirDatabase) -> DynMap { + let db = db.upcast(); match self { ChildContainer::DefWithBodyId(it) => it.child_by_source(db), ChildContainer::ModuleId(it) => it.child_by_source(db), diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 331ecdd9c..e8afef328 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -42,7 +42,7 @@ pub(crate) struct SourceAnalyzer { impl SourceAnalyzer { pub(crate) fn new_for_body( - db: &impl HirDatabase, + db: &dyn HirDatabase, def: DefWithBodyId, node: InFile<&SyntaxNode>, offset: Option, @@ -53,7 +53,7 @@ impl SourceAnalyzer { None => scope_for(&scopes, &source_map, node), Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), }; - let resolver = resolver_for_scope(db, def, scope); + let resolver = resolver_for_scope(db.upcast(), def, scope); SourceAnalyzer { resolver, body: Some(body), @@ -90,7 +90,7 @@ impl SourceAnalyzer { fn expand_expr( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, expr: InFile, ) -> Option> { let macro_file = self.body_source_map.as_ref()?.node_macro_file(expr.as_ref())?; @@ -103,11 +103,11 @@ impl SourceAnalyzer { Some(res) } - fn trait_env(&self, db: &impl HirDatabase) -> Arc { + fn trait_env(&self, db: &dyn HirDatabase) -> Arc { TraitEnvironment::lower(db, &self.resolver) } - pub(crate) fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option { + pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option { let expr_id = match expr { ast::Expr::MacroCall(call) => { let expr = self.expand_expr(db, InFile::new(self.file_id, call.clone()))?; @@ -121,7 +121,7 @@ impl SourceAnalyzer { Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) } - pub(crate) fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option { + pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option { let pat_id = self.pat_id(pat)?; let ty = self.infer.as_ref()?[pat_id].clone(); let environment = self.trait_env(db); @@ -140,7 +140,7 @@ impl SourceAnalyzer { pub(crate) fn resolve_record_field( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, field: &ast::RecordField, ) -> Option<(crate::StructField, Option)> { let (expr_id, local) = match field.expr() { @@ -150,7 +150,7 @@ impl SourceAnalyzer { let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?; let local_name = field.name_ref()?.as_name(); let path = ModPath::from_segments(PathKind::Plain, once(local_name)); - let local = match self.resolver.resolve_path_in_value_ns_fully(db, &path) { + let local = match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { Some(ValueNs::LocalBinding(pat_id)) => { Some(Local { pat_id, parent: self.resolver.body_owner()? }) } @@ -181,17 +181,17 @@ impl SourceAnalyzer { pub(crate) fn resolve_macro_call( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, macro_call: InFile<&ast::MacroCall>, ) -> Option { - let hygiene = Hygiene::new(db, macro_call.file_id); + let hygiene = Hygiene::new(db.upcast(), macro_call.file_id); let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?; - self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into()) + self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()).map(|it| it.into()) } pub(crate) fn resolve_bind_pat_to_const( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, pat: &ast::BindPat, ) -> Option { let pat_id = self.pat_id(&pat.clone().into())?; @@ -209,7 +209,7 @@ impl SourceAnalyzer { pub(crate) fn resolve_path( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, path: &ast::Path, ) -> Option { if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { @@ -231,11 +231,12 @@ impl SourceAnalyzer { pub(crate) fn expand( &self, - db: &impl HirDatabase, + db: &dyn HirDatabase, macro_call: InFile<&ast::MacroCall>, ) -> Option { - let macro_call_id = - macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?; + let macro_call_id = macro_call.as_call_id(db.upcast(), |path| { + self.resolver.resolve_path_as_macro(db.upcast(), &path) + })?; Some(macro_call_id.as_file()) } } @@ -283,42 +284,46 @@ fn scope_for_offset( } pub(crate) fn resolve_hir_path( - db: &impl HirDatabase, + db: &dyn HirDatabase, resolver: &Resolver, path: &crate::Path, ) -> Option { - let types = resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty { - TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), - TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), - TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()), - TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), - TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), - TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), - }); - let body_owner = resolver.body_owner(); - let values = resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| { - let res = match val { - ValueNs::LocalBinding(pat_id) => { - let var = Local { parent: body_owner?.into(), pat_id }; - PathResolution::Local(var) + let types = + resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty { + TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), + TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), + TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { + PathResolution::Def(Adt::from(it).into()) } - ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()), - ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()), - ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()), - ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), - ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), - }; - Some(res) - }); + TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), + TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), + TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), + TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), + }); + let body_owner = resolver.body_owner(); + let values = + resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| { + let res = match val { + ValueNs::LocalBinding(pat_id) => { + let var = Local { parent: body_owner?.into(), pat_id }; + PathResolution::Local(var) + } + ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()), + ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()), + ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()), + ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), + ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), + }; + Some(res) + }); let items = resolver - .resolve_module_path_in_items(db, path.mod_path()) + .resolve_module_path_in_items(db.upcast(), path.mod_path()) .take_types() .map(|it| PathResolution::Def(it.into())); types.or(values).or(items).or_else(|| { resolver - .resolve_path_as_macro(db, path.mod_path()) + .resolve_path_as_macro(db.upcast(), path.mod_path()) .map(|def| PathResolution::Macro(def.into())) }) } -- cgit v1.2.3