From 0102fb41337ac0442e689d410bb424d215e9a7bd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 12:21:46 +0300 Subject: Decouple Resolver --- crates/ra_hir_def/src/traits.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs index 877d73d66..6c2d5b2a9 100644 --- a/crates/ra_hir_def/src/traits.rs +++ b/crates/ra_hir_def/src/traits.rs @@ -11,7 +11,7 @@ use ra_syntax::ast::{self, NameOwner}; use crate::{ db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId, - TypeAliasLoc, + TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -56,4 +56,11 @@ impl TraitData { }; Arc::new(TraitData { name, items, auto }) } + + pub fn associated_types(&self) -> impl Iterator + '_ { + self.items.iter().filter_map(|item| match item { + AssocItemId::TypeAliasId(t) => Some(*t), + _ => None, + }) + } } -- cgit v1.2.3 From 6d64798a2300858c74b1cc0a22f6d3df578288b3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:39:09 +0300 Subject: Move resolver to hir_def --- crates/ra_hir_def/src/lib.rs | 1 + crates/ra_hir_def/src/resolver.rs | 608 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 609 insertions(+) create mode 100644 crates/ra_hir_def/src/resolver.rs (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 0af41de87..d579f5c7e 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -19,6 +19,7 @@ pub mod expr; pub mod body; pub mod generics; pub mod traits; +pub mod resolver; #[cfg(test)] mod test_db; diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs new file mode 100644 index 000000000..840785baa --- /dev/null +++ b/crates/ra_hir_def/src/resolver.rs @@ -0,0 +1,608 @@ +//! Name resolution façade. +use std::sync::Arc; + +use hir_expand::{ + name::{self, Name}, + MacroDefId, +}; +use ra_db::CrateId; +use rustc_hash::FxHashSet; + +use crate::{ + body::scope::{ExprScopes, ScopeId}, + builtin_type::BuiltinType, + db::DefDatabase2, + expr::{ExprId, PatId}, + generics::GenericParams, + nameres::{per_ns::PerNs, CrateDefMap}, + path::{Path, PathKind}, + AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, + FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, + TypeAliasId, UnionId, +}; + +#[derive(Debug, Clone, Default)] +pub struct Resolver { + scopes: Vec, +} + +// FIXME how to store these best +#[derive(Debug, Clone)] +pub(crate) struct ModuleItemMap { + crate_def_map: Arc, + module_id: CrateModuleId, +} + +#[derive(Debug, Clone)] +pub(crate) struct ExprScope { + owner: DefWithBodyId, + expr_scopes: Arc, + scope_id: ScopeId, +} + +#[derive(Debug, Clone)] +pub(crate) enum Scope { + /// All the items and imported names of a module + ModuleScope(ModuleItemMap), + /// Brings the generic parameters of an item into scope + GenericParams { def: GenericDefId, params: Arc }, + /// Brings `Self` in `impl` block into scope + ImplBlockScope(ImplId), + /// Brings `Self` in enum, struct and union definitions into scope + AdtScope(AdtId), + /// Local bindings + ExprScope(ExprScope), +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum TypeNs { + SelfType(ImplId), + GenericParam(u32), + AdtId(AdtId), + AdtSelfType(AdtId), + EnumVariantId(EnumVariantId), + TypeAliasId(TypeAliasId), + BuiltinType(BuiltinType), + TraitId(TraitId), + // Module belong to type ns, but the resolver is used when all module paths + // are fully resolved. + // ModuleId(ModuleId) +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum ResolveValueResult { + ValueNs(ValueNs), + Partial(TypeNs, usize), +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum ValueNs { + LocalBinding(PatId), + FunctionId(FunctionId), + ConstId(ConstId), + StaticId(StaticId), + StructId(StructId), + EnumVariantId(EnumVariantId), +} + +impl Resolver { + /// Resolve known trait from std, like `std::futures::Future` + pub fn resolve_known_trait(&self, db: &impl DefDatabase2, path: &Path) -> Option { + let res = self.resolve_module_path(db, path).take_types()?; + match res { + ModuleDefId::TraitId(it) => Some(it), + _ => None, + } + } + + /// Resolve known struct from std, like `std::boxed::Box` + pub fn resolve_known_struct(&self, db: &impl DefDatabase2, path: &Path) -> Option { + let res = self.resolve_module_path(db, path).take_types()?; + match res { + ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it), + _ => None, + } + } + + /// Resolve known enum from std, like `std::result::Result` + pub fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option { + let res = self.resolve_module_path(db, path).take_types()?; + match res { + ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it), + _ => None, + } + } + + /// pub only for source-binder + pub fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs { + let (item_map, module) = match self.module() { + Some(it) => it, + None => return PerNs::none(), + }; + let (module_res, segment_index) = item_map.resolve_path(db, module, path); + if segment_index.is_some() { + return PerNs::none(); + } + module_res + } + + pub fn resolve_path_in_type_ns( + &self, + db: &impl DefDatabase2, + path: &Path, + ) -> Option<(TypeNs, Option)> { + if path.is_type_relative() { + return None; + } + let first_name = &path.segments.first()?.name; + let skip_to_mod = path.kind != PathKind::Plain; + for scope in self.scopes.iter().rev() { + match scope { + Scope::ExprScope(_) => continue, + Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue, + + Scope::GenericParams { params, .. } => { + if let Some(param) = params.find_by_name(first_name) { + let idx = if path.segments.len() == 1 { None } else { Some(1) }; + return Some((TypeNs::GenericParam(param.idx), idx)); + } + } + Scope::ImplBlockScope(impl_) => { + if first_name == &name::SELF_TYPE { + let idx = if path.segments.len() == 1 { None } else { Some(1) }; + return Some((TypeNs::SelfType(*impl_), idx)); + } + } + Scope::AdtScope(adt) => { + if first_name == &name::SELF_TYPE { + let idx = if path.segments.len() == 1 { None } else { Some(1) }; + return Some((TypeNs::AdtSelfType(*adt), idx)); + } + } + Scope::ModuleScope(m) => { + let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); + let res = match module_def.take_types()? { + ModuleDefId::AdtId(it) => TypeNs::AdtId(it), + ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it), + + ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), + ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), + + ModuleDefId::TraitId(it) => TypeNs::TraitId(it), + + ModuleDefId::FunctionId(_) + | ModuleDefId::ConstId(_) + | ModuleDefId::StaticId(_) + | ModuleDefId::ModuleId(_) => return None, + }; + return Some((res, idx)); + } + } + } + None + } + + pub fn resolve_path_in_type_ns_fully( + &self, + db: &impl DefDatabase2, + path: &Path, + ) -> Option { + let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?; + if unresolved.is_some() { + return None; + } + Some(res) + } + + pub fn resolve_path_in_value_ns<'p>( + &self, + db: &impl DefDatabase2, + path: &'p Path, + ) -> Option { + if path.is_type_relative() { + return None; + } + let n_segments = path.segments.len(); + let tmp = name::SELF_PARAM; + let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name }; + let skip_to_mod = path.kind != PathKind::Plain && !path.is_self(); + for scope in self.scopes.iter().rev() { + match scope { + Scope::AdtScope(_) + | Scope::ExprScope(_) + | Scope::GenericParams { .. } + | Scope::ImplBlockScope(_) + if skip_to_mod => + { + continue + } + + Scope::ExprScope(scope) if n_segments <= 1 => { + let entry = scope + .expr_scopes + .entries(scope.scope_id) + .iter() + .find(|entry| entry.name() == first_name); + + if let Some(e) = entry { + return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat()))); + } + } + Scope::ExprScope(_) => continue, + + Scope::GenericParams { params, .. } if n_segments > 1 => { + if let Some(param) = params.find_by_name(first_name) { + let ty = TypeNs::GenericParam(param.idx); + return Some(ResolveValueResult::Partial(ty, 1)); + } + } + Scope::GenericParams { .. } => continue, + + Scope::ImplBlockScope(impl_) if n_segments > 1 => { + if first_name == &name::SELF_TYPE { + let ty = TypeNs::SelfType(*impl_); + return Some(ResolveValueResult::Partial(ty, 1)); + } + } + Scope::AdtScope(adt) if n_segments > 1 => { + if first_name == &name::SELF_TYPE { + let ty = TypeNs::AdtSelfType(*adt); + return Some(ResolveValueResult::Partial(ty, 1)); + } + } + Scope::ImplBlockScope(_) | Scope::AdtScope(_) => continue, + + Scope::ModuleScope(m) => { + let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); + return match idx { + None => { + let value = match module_def.take_values()? { + ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it), + ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it), + ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it), + ModuleDefId::ConstId(it) => ValueNs::ConstId(it), + ModuleDefId::StaticId(it) => ValueNs::StaticId(it), + + ModuleDefId::AdtId(AdtId::EnumId(_)) + | ModuleDefId::AdtId(AdtId::UnionId(_)) + | ModuleDefId::TraitId(_) + | ModuleDefId::TypeAliasId(_) + | ModuleDefId::BuiltinType(_) + | ModuleDefId::ModuleId(_) => return None, + }; + Some(ResolveValueResult::ValueNs(value)) + } + Some(idx) => { + let ty = match module_def.take_types()? { + ModuleDefId::AdtId(it) => TypeNs::AdtId(it), + ModuleDefId::TraitId(it) => TypeNs::TraitId(it), + ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), + ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), + + ModuleDefId::ModuleId(_) + | ModuleDefId::FunctionId(_) + | ModuleDefId::EnumVariantId(_) + | ModuleDefId::ConstId(_) + | ModuleDefId::StaticId(_) => return None, + }; + Some(ResolveValueResult::Partial(ty, idx)) + } + }; + } + } + } + None + } + + pub fn resolve_path_in_value_ns_fully( + &self, + db: &impl DefDatabase2, + path: &Path, + ) -> Option { + match self.resolve_path_in_value_ns(db, path)? { + ResolveValueResult::ValueNs(it) => Some(it), + ResolveValueResult::Partial(..) => None, + } + } + + pub fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option { + let (item_map, module) = self.module()?; + item_map.resolve_path(db, module, path).0.get_macros() + } + + pub fn process_all_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { + for scope in self.scopes.iter().rev() { + scope.process_names(db, f); + } + } + + pub fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet { + let mut traits = FxHashSet::default(); + for scope in &self.scopes { + if let Scope::ModuleScope(m) = scope { + if let Some(prelude) = m.crate_def_map.prelude() { + let prelude_def_map = db.crate_def_map(prelude.krate); + traits.extend(prelude_def_map[prelude.module_id].scope.traits()); + } + traits.extend(m.crate_def_map[m.module_id].scope.traits()); + } + } + traits + } + + fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> { + self.scopes.iter().rev().find_map(|scope| match scope { + Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), + + _ => None, + }) + } + + pub fn krate(&self) -> Option { + self.module().map(|t| t.0.krate()) + } + + pub fn where_predicates_in_scope<'a>( + &'a self, + ) -> impl Iterator + 'a { + self.scopes + .iter() + .filter_map(|scope| match scope { + Scope::GenericParams { params, .. } => Some(params), + _ => None, + }) + .flat_map(|params| params.where_predicates.iter()) + } + + pub fn generic_def(&self) -> Option { + self.scopes.iter().find_map(|scope| match scope { + Scope::GenericParams { def, .. } => Some(*def), + _ => None, + }) + } + + pub fn body_owner(&self) -> Option { + self.scopes.iter().find_map(|scope| match scope { + Scope::ExprScope(it) => Some(it.owner), + _ => None, + }) + } +} + +impl Resolver { + pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver { + self.scopes.push(scope); + self + } + + pub(crate) fn push_generic_params_scope( + self, + db: &impl DefDatabase2, + def: GenericDefId, + ) -> Resolver { + let params = db.generic_params(def); + if params.params.is_empty() { + self + } else { + self.push_scope(Scope::GenericParams { def, params }) + } + } + + pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { + self.push_scope(Scope::ImplBlockScope(impl_block)) + } + + pub(crate) fn push_module_scope( + self, + crate_def_map: Arc, + module_id: CrateModuleId, + ) -> Resolver { + self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) + } + + pub(crate) fn push_expr_scope( + self, + owner: DefWithBodyId, + expr_scopes: Arc, + scope_id: ScopeId, + ) -> Resolver { + self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) + } +} + +pub enum ScopeDef { + PerNs(PerNs), + ImplSelfType(ImplId), + AdtSelfType(AdtId), + GenericParam(u32), + Local(PatId), +} + +impl Scope { + fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { + match self { + Scope::ModuleScope(m) => { + // FIXME: should we provide `self` here? + // f( + // Name::self_param(), + // PerNs::types(Resolution::Def { + // def: m.module.into(), + // }), + // ); + m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| { + f(name.clone(), ScopeDef::PerNs(res.def)); + }); + m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { + f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_))); + }); + m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| { + f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into()))); + }); + if let Some(prelude) = m.crate_def_map.prelude() { + let prelude_def_map = db.crate_def_map(prelude.krate); + prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { + f(name.clone(), ScopeDef::PerNs(res.def)); + }); + } + } + Scope::GenericParams { params, .. } => { + for param in params.params.iter() { + f(param.name.clone(), ScopeDef::GenericParam(param.idx)) + } + } + Scope::ImplBlockScope(i) => { + f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into())); + } + Scope::AdtScope(i) => { + f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into())); + } + Scope::ExprScope(scope) => { + scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { + f(e.name().clone(), ScopeDef::Local(e.pat())); + }); + } + } + } +} + +// needs arbitrary_self_types to be a method... or maybe move to the def? +pub fn resolver_for_expr( + db: &impl DefDatabase2, + owner: DefWithBodyId, + expr_id: ExprId, +) -> Resolver { + let scopes = db.expr_scopes(owner); + resolver_for_scope(db, owner, scopes.scope_for(expr_id)) +} + +pub fn resolver_for_scope( + db: &impl DefDatabase2, + owner: DefWithBodyId, + scope_id: Option, +) -> Resolver { + let mut r = owner.resolver(db); + let scopes = db.expr_scopes(owner); + let scope_chain = scopes.scope_chain(scope_id).collect::>(); + for scope in scope_chain.into_iter().rev() { + r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); + } + r +} + +pub trait HasResolver { + /// Builds a resolver for type references inside this def. + fn resolver(self, db: &impl DefDatabase2) -> Resolver; +} + +impl HasResolver for ModuleId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + let def_map = db.crate_def_map(self.krate); + Resolver::default().push_module_scope(def_map, self.module_id) + } +} + +impl HasResolver for TraitId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.module(db).resolver(db).push_generic_params_scope(db, self.into()) + } +} + +impl HasResolver for AdtId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + let module = match self { + AdtId::StructId(it) => it.0.module(db), + AdtId::UnionId(it) => it.0.module(db), + AdtId::EnumId(it) => it.module(db), + }; + + module + .resolver(db) + .push_generic_params_scope(db, self.into()) + .push_scope(Scope::AdtScope(self.into())) + } +} + +impl HasResolver for StructId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + AdtId::from(self).resolver(db) + } +} + +impl HasResolver for UnionId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + AdtId::from(self).resolver(db) + } +} + +impl HasResolver for EnumId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + AdtId::from(self).resolver(db) + } +} + +impl HasResolver for FunctionId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) + } +} + +impl HasResolver for DefWithBodyId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + match self { + DefWithBodyId::ConstId(c) => c.resolver(db), + DefWithBodyId::FunctionId(f) => f.resolver(db), + DefWithBodyId::StaticId(s) => s.resolver(db), + } + } +} + +impl HasResolver for ConstId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.lookup(db).container.resolver(db) + } +} + +impl HasResolver for StaticId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.module(db).resolver(db) + } +} + +impl HasResolver for TypeAliasId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) + } +} + +impl HasResolver for ContainerId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + match self { + ContainerId::TraitId(it) => it.resolver(db), + ContainerId::ImplId(it) => it.resolver(db), + ContainerId::ModuleId(it) => it.resolver(db), + } + } +} + +impl HasResolver for GenericDefId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + match self { + GenericDefId::FunctionId(inner) => inner.resolver(db), + GenericDefId::AdtId(adt) => adt.resolver(db), + GenericDefId::TraitId(inner) => inner.resolver(db), + GenericDefId::TypeAliasId(inner) => inner.resolver(db), + GenericDefId::ImplId(inner) => inner.resolver(db), + GenericDefId::EnumVariantId(inner) => inner.parent.resolver(db), + GenericDefId::ConstId(inner) => inner.resolver(db), + } + } +} + +impl HasResolver for ImplId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.module(db) + .resolver(db) + .push_generic_params_scope(db, self.into()) + .push_impl_block_scope(self) + } +} -- cgit v1.2.3 From c37d1c5b381365ce2d07dfe4b871e43995ccea2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:49:24 +0300 Subject: Restore a mark --- crates/ra_hir_def/src/body/lower.rs | 2 ++ crates/ra_hir_def/src/body/scope.rs | 18 +++++++++++++++++- crates/ra_hir_def/src/marks.rs | 1 + 3 files changed, 20 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 a5bb60e85..77f091288 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -12,6 +12,7 @@ use ra_syntax::{ }, AstNode, AstPtr, }; +use test_utils::tested_by; use crate::{ body::{Body, BodySourceMap, Expander, PatPtr}, @@ -153,6 +154,7 @@ where None => self.collect_expr_opt(condition.expr()), // if let -- desugar to match Some(pat) => { + tested_by!(infer_resolve_while_let); let pat = self.collect_pat(pat); let match_expr = self.collect_expr_opt(condition.expr()); let placeholder_pat = self.missing_pat(); diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index aeb71ff22..5d7d17231 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -174,7 +174,7 @@ mod tests { use hir_expand::{name::AsName, Source}; use ra_db::{fixture::WithFixture, FileId, SourceDatabase}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; - use test_utils::{assert_eq_text, extract_offset}; + use test_utils::{assert_eq_text, covers, extract_offset}; use crate::{db::DefDatabase2, test_db::TestDB, FunctionId, ModuleDefId}; @@ -382,4 +382,20 @@ mod tests { 53, ); } + + #[test] + fn infer_resolve_while_let() { + covers!(infer_resolve_while_let); + do_check_local_name( + r#" +fn test() { + let foo: Option = None; + while let Option::Some(spam) = foo { + spam<|> + } +} +"#, + 75, + ); + } } diff --git a/crates/ra_hir_def/src/marks.rs b/crates/ra_hir_def/src/marks.rs index 0b99eac71..65239ca0a 100644 --- a/crates/ra_hir_def/src/marks.rs +++ b/crates/ra_hir_def/src/marks.rs @@ -11,4 +11,5 @@ test_utils::marks!( prelude_is_macro_use macro_dollar_crate_self macro_dollar_crate_other + infer_resolve_while_let ); -- cgit v1.2.3 From 36758f2767f173863039963c44791ac6c4080ab2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:56:27 +0300 Subject: Resimplify --- crates/ra_hir_def/src/resolver.rs | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 840785baa..1bc9c1760 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -18,7 +18,7 @@ use crate::{ path::{Path, PathKind}, AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, - TypeAliasId, UnionId, + TypeAliasId, }; #[derive(Debug, Clone, Default)] @@ -507,9 +507,10 @@ impl HasResolver for TraitId { } } -impl HasResolver for AdtId { +impl> HasResolver for T { fn resolver(self, db: &impl DefDatabase2) -> Resolver { - let module = match self { + let def = self.into(); + let module = match def { AdtId::StructId(it) => it.0.module(db), AdtId::UnionId(it) => it.0.module(db), AdtId::EnumId(it) => it.module(db), @@ -517,26 +518,8 @@ impl HasResolver for AdtId { module .resolver(db) - .push_generic_params_scope(db, self.into()) - .push_scope(Scope::AdtScope(self.into())) - } -} - -impl HasResolver for StructId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - AdtId::from(self).resolver(db) - } -} - -impl HasResolver for UnionId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - AdtId::from(self).resolver(db) - } -} - -impl HasResolver for EnumId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - AdtId::from(self).resolver(db) + .push_generic_params_scope(db, def.into()) + .push_scope(Scope::AdtScope(def)) } } -- cgit v1.2.3 From 4f8f3393bc3dd9a480319a1b9d66c0778ab30b1e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 16:00:57 +0300 Subject: Reduce visibility --- crates/ra_hir_def/src/resolver.rs | 78 +++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 41 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 1bc9c1760..7b5c3ec06 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -369,47 +369,6 @@ impl Resolver { } } -impl Resolver { - pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver { - self.scopes.push(scope); - self - } - - pub(crate) fn push_generic_params_scope( - self, - db: &impl DefDatabase2, - def: GenericDefId, - ) -> Resolver { - let params = db.generic_params(def); - if params.params.is_empty() { - self - } else { - self.push_scope(Scope::GenericParams { def, params }) - } - } - - pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { - self.push_scope(Scope::ImplBlockScope(impl_block)) - } - - pub(crate) fn push_module_scope( - self, - crate_def_map: Arc, - module_id: CrateModuleId, - ) -> Resolver { - self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) - } - - pub(crate) fn push_expr_scope( - self, - owner: DefWithBodyId, - expr_scopes: Arc, - scope_id: ScopeId, - ) -> Resolver { - self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) - } -} - pub enum ScopeDef { PerNs(PerNs), ImplSelfType(ImplId), @@ -489,6 +448,43 @@ pub fn resolver_for_scope( r } +impl Resolver { + fn push_scope(mut self, scope: Scope) -> Resolver { + self.scopes.push(scope); + self + } + + fn push_generic_params_scope(self, db: &impl DefDatabase2, def: GenericDefId) -> Resolver { + let params = db.generic_params(def); + if params.params.is_empty() { + self + } else { + self.push_scope(Scope::GenericParams { def, params }) + } + } + + fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { + self.push_scope(Scope::ImplBlockScope(impl_block)) + } + + fn push_module_scope( + self, + crate_def_map: Arc, + module_id: CrateModuleId, + ) -> Resolver { + self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) + } + + fn push_expr_scope( + self, + owner: DefWithBodyId, + expr_scopes: Arc, + scope_id: ScopeId, + ) -> Resolver { + self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) + } +} + pub trait HasResolver { /// Builds a resolver for type references inside this def. fn resolver(self, db: &impl DefDatabase2) -> Resolver; -- cgit v1.2.3 From a1346bba5c457d1aa0a35f44231bed8b494b7d60 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 17:09:38 +0300 Subject: Fix test mark placement --- crates/ra_hir_def/src/body/lower.rs | 2 +- crates/ra_hir_def/src/body/scope.rs | 2 +- 2 files changed, 2 insertions(+), 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 77f091288..d8e911aa5 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -154,7 +154,6 @@ where None => self.collect_expr_opt(condition.expr()), // if let -- desugar to match Some(pat) => { - tested_by!(infer_resolve_while_let); let pat = self.collect_pat(pat); let match_expr = self.collect_expr_opt(condition.expr()); let placeholder_pat = self.missing_pat(); @@ -192,6 +191,7 @@ where None => self.collect_expr_opt(condition.expr()), // if let -- desugar to match Some(pat) => { + tested_by!(infer_resolve_while_let); let pat = self.collect_pat(pat); let match_expr = self.collect_expr_opt(condition.expr()); let placeholder_pat = self.missing_pat(); diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 5d7d17231..58740b679 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -384,7 +384,7 @@ mod tests { } #[test] - fn infer_resolve_while_let() { + fn while_let_desugaring() { covers!(infer_resolve_while_let); do_check_local_name( r#" -- cgit v1.2.3