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/src/ty/infer.rs | 23 ++++++++++++----------- crates/ra_hir/src/ty/infer/path.rs | 4 ++-- crates/ra_hir/src/ty/lower.rs | 31 ++++++++++++++++++------------- crates/ra_hir/src/ty/method_resolution.rs | 4 +++- 4 files changed, 35 insertions(+), 27 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 7f9e81d64..684d66946 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -24,6 +24,7 @@ use rustc_hash::FxHashMap; use hir_def::{ path::known, type_ref::{Mutability, TypeRef}, + AdtId, }; use hir_expand::{diagnostics::DiagnosticSink, name}; use ra_arena::map::ArenaMap; @@ -43,7 +44,7 @@ use crate::{ resolve::{HasResolver, Resolver, TypeNs}, ty::infer::diagnostics::InferenceDiagnostic, Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, - StructField, VariantDef, + StructField, Trait, VariantDef, }; macro_rules! ty_app { @@ -518,17 +519,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // FIXME: this should resolve assoc items as well, see this example: // https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521 match resolver.resolve_path_in_type_ns_fully(self.db, &path) { - Some(TypeNs::Adt(Adt::Struct(it))) => it.into(), - Some(TypeNs::Adt(Adt::Union(it))) => it.into(), + Some(TypeNs::AdtId(AdtId::StructId(it))) => it.into(), + Some(TypeNs::AdtId(AdtId::UnionId(it))) => it.into(), Some(TypeNs::AdtSelfType(adt)) => adt.into(), - Some(TypeNs::EnumVariant(it)) => it.into(), - Some(TypeNs::TypeAlias(it)) => it.into(), + Some(TypeNs::EnumVariantId(it)) => it.into(), + Some(TypeNs::TypeAliasId(it)) => it.into(), Some(TypeNs::SelfType(_)) | Some(TypeNs::GenericParam(_)) | Some(TypeNs::BuiltinType(_)) | - Some(TypeNs::Trait(_)) | - Some(TypeNs::Adt(Adt::Enum(_))) | + Some(TypeNs::TraitId(_)) | + Some(TypeNs::AdtId(AdtId::EnumId(_))) | None => { return (Ty::Unknown, None) } @@ -576,26 +577,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn resolve_into_iter_item(&self) -> Option { let path = known::std_iter_into_iterator(); - let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; + let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); trait_.associated_type_by_name(self.db, &name::ITEM_TYPE) } fn resolve_ops_try_ok(&self) -> Option { let path = known::std_ops_try(); - let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; + let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); trait_.associated_type_by_name(self.db, &name::OK_TYPE) } fn resolve_future_future_output(&self) -> Option { let path = known::std_future_future(); - let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; + let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE) } fn resolve_boxed_box(&self) -> Option { let path = known::std_boxed_box(); let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; - Some(Adt::Struct(struct_)) + Some(Adt::Struct(struct_.into())) } } diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 31ca675aa..f36a27929 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -94,13 +94,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let is_before_last = remaining_segments.len() == 1; match (def, is_before_last) { - (TypeNs::Trait(trait_), true) => { + (TypeNs::TraitId(trait_), true) => { let segment = remaining_segments.last().expect("there should be at least one segment here"); let trait_ref = TraitRef::from_resolved_path( self.db, &self.resolver, - trait_, + trait_.into(), resolved_segment, None, ); diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 397ee7d5f..d4fbddac0 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -28,8 +28,8 @@ use crate::{ Adt, }, util::make_mut_slice, - Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, - TypeAlias, Union, VariantDef, + Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField, + Trait, TypeAlias, Union, VariantDef, }; // FIXME: this is only really used in `type_for_def`, which contains a bunch of @@ -156,9 +156,14 @@ impl Ty { remaining_segments: &[PathSegment], ) -> Ty { let ty = match resolution { - TypeNs::Trait(trait_) => { - let trait_ref = - TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None); + TypeNs::TraitId(trait_) => { + let trait_ref = TraitRef::from_resolved_path( + db, + resolver, + trait_.into(), + resolved_segment, + None, + ); return if remaining_segments.len() == 1 { let segment = &remaining_segments[0]; match trait_ref @@ -189,18 +194,18 @@ impl Ty { let name = resolved_segment.name.clone(); Ty::Param { idx, name } } - TypeNs::SelfType(impl_block) => impl_block.target_ty(db), - TypeNs::AdtSelfType(adt) => adt.ty(db), + TypeNs::SelfType(impl_block) => ImplBlock::from(impl_block).target_ty(db), + TypeNs::AdtSelfType(adt) => Adt::from(adt).ty(db), - TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), + TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), TypeNs::BuiltinType(it) => { Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) } - TypeNs::TypeAlias(it) => { + TypeNs::TypeAliasId(it) => { Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) } // FIXME: report error - TypeNs::EnumVariant(_) => return Ty::Unknown, + TypeNs::EnumVariantId(_) => return Ty::Unknown, }; Ty::from_type_relative_path(db, resolver, ty, remaining_segments) @@ -247,7 +252,7 @@ impl Ty { Some(def) => def, None => return Ty::Unknown, // this can't actually happen }; - let predicates = db.generic_predicates_for_param(def, param_idx); + let predicates = db.generic_predicates_for_param(def.into(), param_idx); let traits_from_env = predicates.iter().filter_map(|pred| match pred { GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_), _ => None, @@ -391,11 +396,11 @@ impl TraitRef { explicit_self_ty: Option, ) -> Option { let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? { - TypeNs::Trait(tr) => tr, + TypeNs::TraitId(tr) => tr, _ => return None, }; let segment = path.segments.last().expect("path should have at least one segment"); - Some(TraitRef::from_resolved_path(db, resolver, resolved, segment, explicit_self_ty)) + Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty)) } pub(super) fn from_resolved_path( diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index f377fca48..26dd06171 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -224,7 +224,9 @@ fn iterate_trait_method_candidates( .trait_predicates_for_self_ty(&ty.value) .map(|tr| tr.trait_) .flat_map(|t| t.all_super_traits(db)); - let traits = inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db)); + let traits = inherent_trait + .chain(traits_from_env) + .chain(resolver.traits_in_scope(db).into_iter().map(Trait::from)); 'traits: for t in traits { let data = t.trait_data(db); -- cgit v1.2.3 From 24964ca58e057b3f32d5cbb17d84e46f2a236ff6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 13:28:04 +0300 Subject: Move expr resolver to resolve --- crates/ra_hir/src/ty/infer/expr.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 5e68a1678..414b06ba1 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -12,8 +12,9 @@ use hir_expand::name; use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; use crate::{ db::HirDatabase, - expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, + expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, generics::{GenericParams, HasGenericParams}, + resolve::resolver_for_expr, ty::{ autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, @@ -186,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Expr::Path(p) => { // FIXME this could be more efficient... - let resolver = expr::resolver_for_expr(self.db, self.owner, tgt_expr); + let resolver = resolver_for_expr(self.db, self.owner, tgt_expr); self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) } Expr::Continue => Ty::simple(TypeCtor::Never), -- cgit v1.2.3 From 621cf06156975f8bd75e35af46da034f72e11fad Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 13:32:03 +0300 Subject: Decouple --- crates/ra_hir/src/ty/infer/expr.rs | 2 +- crates/ra_hir/src/ty/infer/path.rs | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 414b06ba1..1ac2709f5 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -187,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Expr::Path(p) => { // FIXME this could be more efficient... - let resolver = resolver_for_expr(self.db, self.owner, tgt_expr); + let resolver = resolver_for_expr(self.db, self.owner.into(), tgt_expr); self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) } Expr::Continue => Ty::simple(TypeCtor::Never), diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index f36a27929..55a5dbec7 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -8,7 +8,7 @@ use crate::{ generics::HasGenericParams, resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, - AssocItem, Container, Name, Path, + AssocItem, Container, Function, Name, Path, }; impl<'a, D: HirDatabase> InferenceContext<'a, D> { @@ -60,11 +60,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = self.resolve_ty_as_possible(&mut vec![], ty); return Some(ty); } - ValueNs::Function(it) => it.into(), - ValueNs::Const(it) => it.into(), - ValueNs::Static(it) => it.into(), - ValueNs::Struct(it) => it.into(), - ValueNs::EnumVariant(it) => it.into(), + ValueNs::FunctionId(it) => it.into(), + ValueNs::ConstId(it) => it.into(), + ValueNs::StaticId(it) => it.into(), + ValueNs::StructId(it) => it.into(), + ValueNs::EnumVariantId(it) => it.into(), }; let mut ty = self.db.type_for_def(typable, Namespace::Values); @@ -160,8 +160,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { AssocItem::TypeAlias(_) => None, })?; let def = match item { - AssocItem::Function(f) => ValueNs::Function(f), - AssocItem::Const(c) => ValueNs::Const(c), + AssocItem::Function(f) => ValueNs::FunctionId(f.id), + AssocItem::Const(c) => ValueNs::ConstId(c.id), AssocItem::TypeAlias(_) => unreachable!(), }; let substs = Substs::build_for_def(self.db, item) @@ -193,8 +193,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { method_resolution::LookupMode::Path, move |_ty, item| { let def = match item { - AssocItem::Function(f) => ValueNs::Function(f), - AssocItem::Const(c) => ValueNs::Const(c), + AssocItem::Function(f) => ValueNs::FunctionId(f.id), + AssocItem::Const(c) => ValueNs::ConstId(c.id), AssocItem::TypeAlias(_) => unreachable!(), }; let substs = match item.container(self.db) { @@ -224,7 +224,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option { - if let ValueNs::Function(func) = def { + if let ValueNs::FunctionId(func) = def { + let func = Function::from(*func); // We only do the infer if parent has generic params let gen = func.generic_params(self.db); if gen.count_parent_params() == 0 { -- cgit v1.2.3 From 061e6c77b5fdb63aa1cad63f1420a7fc810fa17d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 14:13:49 +0300 Subject: Decouple --- crates/ra_hir/src/ty/lower.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index d4fbddac0..8ea2806dd 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -553,8 +553,8 @@ pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSi pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { let parent_def = field.parent_def(db); let resolver = match parent_def { - VariantDef::Struct(it) => it.resolver(db), - VariantDef::EnumVariant(it) => it.parent_enum(db).resolver(db), + VariantDef::Struct(it) => it.id.resolver(db), + VariantDef::EnumVariant(it) => it.parent.id.resolver(db), }; let var_data = parent_def.variant_data(db); let type_ref = &var_data.fields().unwrap()[field.id].type_ref; @@ -623,7 +623,7 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { let data = def.data(db); - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); let ret = Ty::from_hir(db, &resolver, data.ret_type()); FnSig::from_params_and_return(params, ret) @@ -640,7 +640,7 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { /// Build the declared type of a const. fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { let data = def.data(db); - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); Ty::from_hir(db, &resolver, data.type_ref()) } @@ -648,7 +648,7 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { /// Build the declared type of a static. fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { let data = def.data(db); - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); Ty::from_hir(db, &resolver, data.type_ref()) } @@ -700,7 +700,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { Some(fields) => fields, None => panic!("fn_sig_for_struct_constructor called on unit struct"), }; - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); let params = fields .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) @@ -726,7 +726,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) Some(fields) => fields, None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), }; - let resolver = def.parent_enum(db).resolver(db); + let resolver = def.parent.id.resolver(db); let params = fields .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) @@ -755,7 +755,7 @@ fn type_for_adt(db: &impl HirDatabase, adt: impl Into + HasGenericParams) - fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { let generics = t.generic_params(db); - let resolver = t.resolver(db); + let resolver = t.id.resolver(db); let type_ref = t.type_ref(db); let substs = Substs::identity(&generics); let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error)); -- cgit v1.2.3 From 00684d708b64fe81a0264795f27594d450a8d08d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:24:51 +0300 Subject: Decouple --- crates/ra_hir/src/ty/autoderef.rs | 4 ++-- crates/ra_hir/src/ty/infer.rs | 9 +++++---- crates/ra_hir/src/ty/infer/coerce.rs | 4 ++-- crates/ra_hir/src/ty/lower.rs | 7 ++++--- crates/ra_hir/src/ty/method_resolution.rs | 15 ++++++++++----- 5 files changed, 23 insertions(+), 16 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 872a4517d..f77492170 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -39,7 +39,7 @@ fn deref_by_trait( ty: &Canonical, ) -> Option> { let krate = resolver.krate()?; - let deref_trait = match db.lang_item(krate, "deref".into())? { + let deref_trait = match db.lang_item(krate.into(), "deref".into())? { crate::lang_item::LangItemTarget::Trait(t) => t, _ => return None, }; @@ -71,7 +71,7 @@ fn deref_by_trait( let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env }; - let solution = db.trait_solve(krate, canonical)?; + let solution = db.trait_solve(krate.into(), canonical)?; match &solution { Solution::Unique(vars) => { diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 684d66946..c3d65afa6 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -24,7 +24,7 @@ use rustc_hash::FxHashMap; use hir_def::{ path::known, type_ref::{Mutability, TypeRef}, - AdtId, + AdtId, DefWithBodyId, }; use hir_expand::{diagnostics::DiagnosticSink, name}; use ra_arena::map::ArenaMap; @@ -65,7 +65,7 @@ mod coerce; /// The entry point of type inference. pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { let _p = profile("infer_query"); - let resolver = def.resolver(db); + let resolver = DefWithBodyId::from(def).resolver(db); let mut ctx = InferenceContext::new(db, def, resolver); match def { @@ -378,8 +378,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { for obligation in obligations { let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone()); let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); - let solution = - self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone()); + let solution = self + .db + .trait_solve(self.resolver.krate().unwrap().into(), canonicalized.value.clone()); match solution { Some(Solution::Unique(substs)) => { diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs index 6ea135126..6d297c268 100644 --- a/crates/ra_hir/src/ty/infer/coerce.rs +++ b/crates/ra_hir/src/ty/infer/coerce.rs @@ -49,8 +49,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { resolver: &Resolver, ) -> FxHashMap<(TypeCtor, TypeCtor), usize> { let krate = resolver.krate().unwrap(); - let impls = match db.lang_item(krate, "coerce_unsized".into()) { - Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_), + let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) { + Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_), _ => return FxHashMap::default(), }; diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 8ea2806dd..e477b2439 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -12,6 +12,7 @@ use hir_def::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, path::{GenericArg, PathSegment}, type_ref::{TypeBound, TypeRef}, + GenericDefId, }; use super::{ @@ -574,7 +575,7 @@ pub(crate) fn generic_predicates_for_param_query( def: GenericDef, param_idx: u32, ) -> Arc<[GenericPredicate]> { - let resolver = def.resolver(db); + let resolver = GenericDefId::from(def).resolver(db); resolver .where_predicates_in_scope() // we have to filter out all other predicates *first*, before attempting to lower them @@ -600,7 +601,7 @@ pub(crate) fn generic_predicates_query( db: &impl HirDatabase, def: GenericDef, ) -> Arc<[GenericPredicate]> { - let resolver = def.resolver(db); + let resolver = GenericDefId::from(def).resolver(db); resolver .where_predicates_in_scope() .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) @@ -609,7 +610,7 @@ pub(crate) fn generic_predicates_query( /// Resolve the default type params from generics pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { - let resolver = def.resolver(db); + let resolver = GenericDefId::from(def).resolver(db); let generic_params = def.generic_params(db); let defaults = generic_params diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 26dd06171..5ad72ef9f 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -172,9 +172,14 @@ pub(crate) fn iterate_method_candidates( // rustc does an autoderef and then autoref again). for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) { - if let Some(result) = - iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback) - { + if let Some(result) = iterate_inherent_methods( + &derefed_ty, + db, + name, + mode, + krate.into(), + &mut callback, + ) { return Some(result); } if let Some(result) = iterate_trait_method_candidates( @@ -192,7 +197,7 @@ pub(crate) fn iterate_method_candidates( LookupMode::Path => { // No autoderef for path lookups if let Some(result) = - iterate_inherent_methods(&ty, db, name, mode, krate, &mut callback) + iterate_inherent_methods(&ty, db, name, mode, krate.into(), &mut callback) { return Some(result); } @@ -240,7 +245,7 @@ fn iterate_trait_method_candidates( } if !known_implemented { let goal = generic_implements_goal(db, env.clone(), t, ty.clone()); - if db.trait_solve(krate, goal).is_none() { + if db.trait_solve(krate.into(), goal).is_none() { continue 'traits; } } -- 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/src/ty/autoderef.rs | 3 ++- crates/ra_hir/src/ty/infer.rs | 2 +- crates/ra_hir/src/ty/infer/coerce.rs | 6 +++--- crates/ra_hir/src/ty/infer/expr.rs | 5 +++-- crates/ra_hir/src/ty/infer/path.rs | 9 ++++++--- crates/ra_hir/src/ty/lower.rs | 2 +- crates/ra_hir/src/ty/method_resolution.rs | 2 +- 7 files changed, 17 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index f77492170..5d8518041 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -5,11 +5,12 @@ use std::iter::successors; +use hir_def::resolver::Resolver; use hir_expand::name; use log::{info, warn}; use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; -use crate::{db::HirDatabase, generics::HasGenericParams, Resolver}; +use crate::{db::HirDatabase, generics::HasGenericParams}; const AUTODEREF_RECURSION_LIMIT: usize = 10; diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index c3d65afa6..69b13baef 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -23,6 +23,7 @@ use rustc_hash::FxHashMap; use hir_def::{ path::known, + resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, AdtId, DefWithBodyId, }; @@ -41,7 +42,6 @@ use crate::{ code_model::TypeAlias, db::HirDatabase, expr::{BindingAnnotation, Body, ExprId, PatId}, - resolve::{HasResolver, Resolver, TypeNs}, ty::infer::diagnostics::InferenceDiagnostic, Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, StructField, Trait, VariantDef, diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs index 6d297c268..0772b9df5 100644 --- a/crates/ra_hir/src/ty/infer/coerce.rs +++ b/crates/ra_hir/src/ty/infer/coerce.rs @@ -4,19 +4,19 @@ //! //! See: https://doc.rust-lang.org/nomicon/coercions.html +use hir_def::resolver::Resolver; use rustc_hash::FxHashMap; - use test_utils::tested_by; -use super::{InferTy, InferenceContext, TypeVarValue}; use crate::{ db::HirDatabase, lang_item::LangItemTarget, - resolve::Resolver, ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk}, Adt, Mutability, }; +use super::{InferTy, InferenceContext, TypeVarValue}; + impl<'a, D: HirDatabase> InferenceContext<'a, D> { /// Unify two types, but may coerce the first one to the second one /// using "implicit coercion rules" if needed. diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 1ac2709f5..ac570075f 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -6,15 +6,14 @@ use std::sync::Arc; use hir_def::{ builtin_type::Signedness, path::{GenericArg, GenericArgs}, + resolver::resolver_for_expr, }; use hir_expand::name; -use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; use crate::{ db::HirDatabase, expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, generics::{GenericParams, HasGenericParams}, - resolve::resolver_for_expr, ty::{ autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, @@ -23,6 +22,8 @@ use crate::{ Adt, Name, }; +use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; + impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { let ty = self.infer_expr_inner(tgt_expr, expected); diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 55a5dbec7..70136e514 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -1,16 +1,19 @@ //! Path expression resolution. -use hir_def::path::PathSegment; +use hir_def::{ + path::PathSegment, + resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, +}; -use super::{ExprOrPatId, InferenceContext, TraitRef}; use crate::{ db::HirDatabase, generics::HasGenericParams, - resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, AssocItem, Container, Function, Name, Path, }; +use super::{ExprOrPatId, InferenceContext, TraitRef}; + impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_path( &mut self, diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index e477b2439..c6ad0811b 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use hir_def::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, path::{GenericArg, PathSegment}, + resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, GenericDefId, }; @@ -23,7 +24,6 @@ use crate::{ db::HirDatabase, generics::HasGenericParams, generics::{GenericDef, WherePredicate}, - resolve::{HasResolver, Resolver, TypeNs}, ty::{ primitive::{FloatTy, IntTy, Uncertain}, Adt, diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 5ad72ef9f..64adb814d 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -5,11 +5,11 @@ use std::sync::Arc; use arrayvec::ArrayVec; +use hir_def::resolver::Resolver; use rustc_hash::FxHashMap; use crate::{ db::HirDatabase, - resolve::Resolver, ty::primitive::{FloatBitness, Uncertain}, ty::{Ty, TypeCtor}, AssocItem, Crate, Function, ImplBlock, Module, Mutability, Name, Trait, -- cgit v1.2.3 From 4daf931111029cee9bb43691f88c32f0aa47a802 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 16:23:02 +0300 Subject: Remove old hir::generics module --- crates/ra_hir/src/ty/autoderef.rs | 4 ++-- crates/ra_hir/src/ty/infer/expr.rs | 8 +++++--- crates/ra_hir/src/ty/infer/path.rs | 3 +-- crates/ra_hir/src/ty/lower.rs | 33 +++++++++++++++++---------------- crates/ra_hir/src/ty/traits/chalk.rs | 13 ++++++------- 5 files changed, 31 insertions(+), 30 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 5d8518041..33ac782fe 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -10,7 +10,7 @@ use hir_expand::name; use log::{info, warn}; use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; -use crate::{db::HirDatabase, generics::HasGenericParams}; +use crate::{db::HirDatabase}; const AUTODEREF_RECURSION_LIMIT: usize = 10; @@ -46,7 +46,7 @@ fn deref_by_trait( }; let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?; - let generic_params = target.generic_params(db); + let generic_params = db.generic_params(target.id.into()); if generic_params.count_params_including_parent() != 1 { // the Target type + Deref trait should only have one generic parameter, // namely Deref's Self type diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index ac570075f..20a7e9352 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use hir_def::{ builtin_type::Signedness, + generics::GenericParams, path::{GenericArg, GenericArgs}, resolver::resolver_for_expr, }; @@ -13,7 +14,6 @@ use hir_expand::name; use crate::{ db::HirDatabase, expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, - generics::{GenericParams, HasGenericParams}, ty::{ autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, @@ -534,7 +534,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { ( ty, self.db.type_for_def(func.into(), Namespace::Values), - Some(func.generic_params(self.db)), + Some(self.db.generic_params(func.id.into())), ) } None => (receiver_ty, Ty::Unknown, None), @@ -645,7 +645,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { if let Some(trait_) = f.parent_trait(self.db) { // construct a TraitDef let substs = a_ty.parameters.prefix( - trait_.generic_params(self.db).count_params_including_parent(), + self.db + .generic_params(trait_.id.into()) + .count_params_including_parent(), ); self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); } diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 70136e514..ee54d8217 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -7,7 +7,6 @@ use hir_def::{ use crate::{ db::HirDatabase, - generics::HasGenericParams, ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, AssocItem, Container, Function, Name, Path, }; @@ -230,7 +229,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { if let ValueNs::FunctionId(func) = def { let func = Function::from(*func); // We only do the infer if parent has generic params - let gen = func.generic_params(self.db); + let gen = self.db.generic_params(func.id.into()); if gen.count_parent_params() == 0 { return None; } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index c6ad0811b..75c552569 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -10,10 +10,11 @@ use std::sync::Arc; use hir_def::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, + generics::WherePredicate, path::{GenericArg, PathSegment}, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, - GenericDefId, + AdtId, GenericDefId, }; use super::{ @@ -22,15 +23,13 @@ use super::{ }; use crate::{ db::HirDatabase, - generics::HasGenericParams, - generics::{GenericDef, WherePredicate}, ty::{ primitive::{FloatTy, IntTy, Uncertain}, Adt, }, util::make_mut_slice, - Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField, - Trait, TypeAlias, Union, VariantDef, + Const, Enum, EnumVariant, Function, GenericDef, ImplBlock, ModuleDef, Path, Static, Struct, + StructField, Trait, TypeAlias, Union, VariantDef, }; // FIXME: this is only really used in `type_for_def`, which contains a bunch of @@ -342,7 +341,7 @@ pub(super) fn substs_from_path_segment( add_self_param: bool, ) -> Substs { let mut substs = Vec::new(); - let def_generics = def_generic.map(|def| def.generic_params(db)); + let def_generics = def_generic.map(|def| db.generic_params(def.into())); let (parent_param_count, param_count) = def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); @@ -443,7 +442,7 @@ impl TraitRef { } pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef { - let substs = Substs::identity(&trait_.generic_params(db)); + let substs = Substs::identity(&db.generic_params(trait_.id.into())); TraitRef { trait_, substs } } @@ -611,7 +610,7 @@ pub(crate) fn generic_predicates_query( /// Resolve the default type params from generics pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { let resolver = GenericDefId::from(def).resolver(db); - let generic_params = def.generic_params(db); + let generic_params = db.generic_params(def.into()); let defaults = generic_params .params_including_parent() @@ -633,7 +632,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { /// Build the declared type of a function. This should not need to look at the /// function body. fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { - let generics = def.generic_params(db); + let generics = db.generic_params(def.id.into()); let substs = Substs::identity(&generics); Ty::apply(TypeCtor::FnDef(def.into()), substs) } @@ -716,7 +715,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { if struct_data.variant_data.fields().is_none() { return type_for_adt(db, def); // Unit struct } - let generics = def.generic_params(db); + let generics = db.generic_params(def.id.into()); let substs = Substs::identity(&generics); Ty::apply(TypeCtor::FnDef(def.into()), substs) } @@ -732,7 +731,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) .collect::>(); - let generics = def.parent_enum(db).generic_params(db); + let generics = db.generic_params(def.parent_enum(db).id.into()); let substs = Substs::identity(&generics); let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs); FnSig::from_params_and_return(params, ret) @@ -744,18 +743,20 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> if var_data.fields().is_none() { return type_for_adt(db, def.parent_enum(db)); // Unit variant } - let generics = def.parent_enum(db).generic_params(db); + let generics = db.generic_params(def.parent_enum(db).id.into()); let substs = Substs::identity(&generics); Ty::apply(TypeCtor::FnDef(def.into()), substs) } -fn type_for_adt(db: &impl HirDatabase, adt: impl Into + HasGenericParams) -> Ty { - let generics = adt.generic_params(db); - Ty::apply(TypeCtor::Adt(adt.into()), Substs::identity(&generics)) +fn type_for_adt(db: &impl HirDatabase, adt: impl Into) -> Ty { + let adt = adt.into(); + let adt_id: AdtId = adt.into(); + let generics = db.generic_params(adt_id.into()); + Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics)) } fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { - let generics = t.generic_params(db); + let generics = db.generic_params(t.id.into()); let resolver = t.id.resolver(db); let type_ref = t.type_ref(db); let substs = Substs::identity(&generics); diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 9bf93981a..88785f305 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -16,10 +16,9 @@ use ra_db::salsa::{InternId, InternKey}; use super::{AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; use crate::{ db::HirDatabase, - generics::{GenericDef, HasGenericParams}, ty::display::HirDisplay, ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, - Crate, HasBody, ImplBlock, Trait, TypeAlias, + Crate, GenericDef, HasBody, ImplBlock, Trait, TypeAlias, }; /// This represents a trait whose name we could not resolve. @@ -509,7 +508,7 @@ pub(crate) fn associated_ty_data_query( Some(crate::Container::Trait(t)) => t, _ => panic!("associated type not in trait"), }; - let generic_params = type_alias.generic_params(db); + let generic_params = db.generic_params(type_alias.id.into()); let bound_data = chalk_rust_ir::AssociatedTyDatumBound { // FIXME add bounds and where clauses bounds: vec![], @@ -550,7 +549,7 @@ pub(crate) fn trait_datum_query( } let trait_: Trait = from_chalk(db, trait_id); debug!("trait {:?} = {:?}", trait_id, trait_.name(db)); - let generic_params = trait_.generic_params(db); + let generic_params = db.generic_params(trait_.id.into()); let bound_vars = Substs::bound_vars(&generic_params); let flags = chalk_rust_ir::TraitFlags { auto: trait_.is_auto(db), @@ -594,7 +593,7 @@ pub(crate) fn struct_datum_query( let where_clauses = type_ctor .as_generic_def() .map(|generic_def| { - let generic_params = generic_def.generic_params(db); + let generic_params = db.generic_params(generic_def.into()); let bound_vars = Substs::bound_vars(&generic_params); convert_where_clauses(db, generic_def, &bound_vars) }) @@ -634,7 +633,7 @@ fn impl_block_datum( impl_id: ImplId, impl_block: ImplBlock, ) -> Option>> { - let generic_params = impl_block.generic_params(db); + let generic_params = db.generic_params(impl_block.id.into()); let bound_vars = Substs::bound_vars(&generic_params); let trait_ref = impl_block.target_trait_ref(db)?.subst(&bound_vars); let trait_ = trait_ref.trait_; @@ -786,7 +785,7 @@ fn type_alias_associated_ty_value( let assoc_ty = trait_ .associated_type_by_name(db, &type_alias.name(db)) .expect("assoc ty value should not exist"); // validated when building the impl data as well - let generic_params = impl_block.generic_params(db); + let generic_params = db.generic_params(impl_block.id.into()); let bound_vars = Substs::bound_vars(&generic_params); let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars); let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; -- cgit v1.2.3 From 952405cb4180993af0f5c939d5276dbdf2a19152 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 16:31:48 +0300 Subject: reformat --- crates/ra_hir/src/ty/autoderef.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 33ac782fe..b60e4bb31 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -10,7 +10,7 @@ use hir_expand::name; use log::{info, warn}; use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; -use crate::{db::HirDatabase}; +use crate::db::HirDatabase; const AUTODEREF_RECURSION_LIMIT: usize = 10; -- cgit v1.2.3