From 9b4699a9be2c45ec96647c2079a32706ec65f222 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 6 Apr 2021 12:36:12 +0200 Subject: Move Ty accessors to TyExt --- crates/hir/src/lib.rs | 2 +- crates/hir_ty/src/builder.rs | 2 +- crates/hir_ty/src/chalk_ext.rs | 235 ++++++++++++++++++++++++- crates/hir_ty/src/diagnostics/match_check.rs | 2 +- crates/hir_ty/src/diagnostics/unsafe_check.rs | 4 +- crates/hir_ty/src/infer.rs | 2 +- crates/hir_ty/src/infer/coerce.rs | 2 +- crates/hir_ty/src/infer/expr.rs | 3 +- crates/hir_ty/src/infer/pat.rs | 2 +- crates/hir_ty/src/infer/path.rs | 4 +- crates/hir_ty/src/lib.rs | 214 +--------------------- crates/hir_ty/src/method_resolution.rs | 2 +- crates/hir_ty/src/traits/chalk.rs | 2 +- crates/rust-analyzer/src/cli/analysis_stats.rs | 2 +- 14 files changed, 252 insertions(+), 226 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index dfc1d8a0c..8d00f7401 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -59,7 +59,7 @@ use hir_ty::{ traits::FnTrait, AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, - SolutionVariables, Substitution, TraitEnvironment, Ty, TyBuilder, TyDefId, TyKind, + SolutionVariables, Substitution, TraitEnvironment, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, WhereClause, }; use itertools::Itertools; diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs index 791915fe0..09512d1ce 100644 --- a/crates/hir_ty/src/builder.rs +++ b/crates/hir_ty/src/builder.rs @@ -13,7 +13,7 @@ use smallvec::SmallVec; use crate::{ db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution, - TraitRef, Ty, TyDefId, TyKind, TypeWalk, ValueTyDefId, + TraitRef, Ty, TyDefId, TyExt, TyKind, TypeWalk, ValueTyDefId, }; /// This is a builder for `Ty` or anything that needs a `Substitution`. diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 0f4cb43e9..b101e904f 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -1,20 +1,249 @@ //! Various extensions traits for Chalk types. -use hir_def::{AssocContainerId, Lookup, TraitId}; +use chalk_ir::Mutability; +use hir_def::{ + type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId, +}; use crate::{ - db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty, - TyKind, + db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, + from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId, + CallableSig, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause, + Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause, }; pub trait TyExt { fn is_unit(&self) -> bool; + fn is_never(&self) -> bool; + fn is_unknown(&self) -> bool; + + fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>; + fn as_tuple(&self) -> Option<&Substitution>; + fn as_fn_def(&self, db: &dyn HirDatabase) -> Option; + fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>; + fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)>; + fn as_generic_def(&self, db: &dyn HirDatabase) -> Option; + + fn callable_def(&self, db: &dyn HirDatabase) -> Option; + fn callable_sig(&self, db: &dyn HirDatabase) -> Option; + + fn strip_references(&self) -> &Ty; + + /// If this is a `dyn Trait` type, this returns the `Trait` part. + fn dyn_trait_ref(&self) -> Option<&TraitRef>; + + /// If this is a `dyn Trait`, returns that trait. + fn dyn_trait(&self) -> Option; + + fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option>; + fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option; } impl TyExt for Ty { fn is_unit(&self) -> bool { matches!(self.kind(&Interner), TyKind::Tuple(0, _)) } + + fn is_never(&self) -> bool { + matches!(self.kind(&Interner), TyKind::Never) + } + + fn is_unknown(&self) -> bool { + matches!(self.kind(&Interner), TyKind::Error) + } + + fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> { + match self.kind(&Interner) { + TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), + _ => None, + } + } + + fn as_tuple(&self) -> Option<&Substitution> { + match self.kind(&Interner) { + TyKind::Tuple(_, substs) => Some(substs), + _ => None, + } + } + + fn as_fn_def(&self, db: &dyn HirDatabase) -> Option { + if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { + Some(func) + } else { + None + } + } + fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { + match self.kind(&Interner) { + TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)), + _ => None, + } + } + + fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { + match self.kind(&Interner) { + TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)), + TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)), + _ => None, + } + } + + fn as_generic_def(&self, db: &dyn HirDatabase) -> Option { + match *self.kind(&Interner) { + TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), + TyKind::FnDef(callable, ..) => { + Some(db.lookup_intern_callable_def(callable.into()).into()) + } + TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), + TyKind::Foreign(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), + _ => None, + } + } + + fn callable_def(&self, db: &dyn HirDatabase) -> Option { + match self.kind(&Interner) { + &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())), + _ => None, + } + } + + fn callable_sig(&self, db: &dyn HirDatabase) -> Option { + match self.kind(&Interner) { + TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), + TyKind::FnDef(def, parameters) => { + let callable_def = db.lookup_intern_callable_def((*def).into()); + let sig = db.callable_item_signature(callable_def); + Some(sig.substitute(&Interner, ¶meters)) + } + TyKind::Closure(.., substs) => { + let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner); + sig_param.callable_sig(db) + } + _ => None, + } + } + + fn dyn_trait_ref(&self) -> Option<&TraitRef> { + match self.kind(&Interner) { + TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| { + match b.skip_binders() { + WhereClause::Implemented(trait_ref) => Some(trait_ref), + _ => None, + } + }), + _ => None, + } + } + + fn dyn_trait(&self) -> Option { + self.dyn_trait_ref().map(|it| it.trait_id).map(from_chalk_trait_id) + } + + fn strip_references(&self) -> &Ty { + let mut t: &Ty = self; + while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) { + t = ty; + } + t + } + + fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option> { + match self.kind(&Interner) { + TyKind::OpaqueType(opaque_ty_id, ..) => { + match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) { + ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => { + let krate = def.module(db.upcast()).krate(); + if let Some(future_trait) = db + .lang_item(krate, "future_trait".into()) + .and_then(|item| item.as_trait()) + { + // This is only used by type walking. + // Parameters will be walked outside, and projection predicate is not used. + // So just provide the Future trait. + let impl_bound = Binders::empty( + &Interner, + WhereClause::Implemented(TraitRef { + trait_id: to_chalk_trait_id(future_trait), + substitution: Substitution::empty(&Interner), + }), + ); + Some(vec![impl_bound]) + } else { + None + } + } + ImplTraitId::ReturnTypeImplTrait(..) => None, + } + } + TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { + let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into()) + { + ImplTraitId::ReturnTypeImplTrait(func, idx) => { + db.return_type_impl_traits(func).map(|it| { + let data = (*it) + .as_ref() + .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); + data.substitute(&Interner, &opaque_ty.substitution) + }) + } + // It always has an parameter for Future::Output type. + ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(), + }; + + predicates.map(|it| it.into_value_and_skipped_binders().0) + } + TyKind::Placeholder(idx) => { + let id = from_placeholder_idx(db, *idx); + let generic_params = db.generic_params(id.parent); + let param_data = &generic_params.types[id.local_id]; + match param_data.provenance { + hir_def::generics::TypeParamProvenance::ArgumentImplTrait => { + let substs = TyBuilder::type_params_subst(db, id.parent); + let predicates = db + .generic_predicates(id.parent) + .into_iter() + .map(|pred| pred.clone().substitute(&Interner, &substs)) + .filter(|wc| match &wc.skip_binders() { + WhereClause::Implemented(tr) => { + tr.self_type_parameter(&Interner) == self + } + WhereClause::AliasEq(AliasEq { + alias: AliasTy::Projection(proj), + ty: _, + }) => proj.self_type_parameter(&Interner) == self, + _ => false, + }) + .collect::>(); + + Some(predicates) + } + _ => None, + } + } + _ => None, + } + } + + fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option { + match self.kind(&Interner) { + TyKind::AssociatedType(id, ..) => { + match from_assoc_type_id(*id).lookup(db.upcast()).container { + AssocContainerId::TraitId(trait_id) => Some(trait_id), + _ => None, + } + } + TyKind::Alias(AliasTy::Projection(projection_ty)) => { + match from_assoc_type_id(projection_ty.associated_ty_id) + .lookup(db.upcast()) + .container + { + AssocContainerId::TraitId(trait_id) => Some(trait_id), + _ => None, + } + } + _ => None, + } + } } pub trait ProjectionTyExt { diff --git a/crates/hir_ty/src/diagnostics/match_check.rs b/crates/hir_ty/src/diagnostics/match_check.rs index 34291578a..e9762622f 100644 --- a/crates/hir_ty/src/diagnostics/match_check.rs +++ b/crates/hir_ty/src/diagnostics/match_check.rs @@ -227,7 +227,7 @@ use hir_def::{ use la_arena::Idx; use smallvec::{smallvec, SmallVec}; -use crate::{db::HirDatabase, AdtId, InferenceResult, Interner, TyKind}; +use crate::{db::HirDatabase, AdtId, InferenceResult, Interner, TyExt, TyKind}; #[derive(Debug, Clone, Copy)] /// Either a pattern from the source code being analyzed, represented as diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs index b5efe9df5..ed97dc0e3 100644 --- a/crates/hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs @@ -11,7 +11,9 @@ use hir_def::{ }; use hir_expand::diagnostics::DiagnosticSink; -use crate::{db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Interner, TyKind}; +use crate::{ + db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Interner, TyExt, TyKind, +}; pub(super) struct UnsafeValidator<'a, 'b: 'a> { owner: DefWithBodyId, diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 1c3faf5cb..5146d873b 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -42,7 +42,7 @@ use super::{ }; use crate::{ db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, - to_assoc_type_id, AliasEq, AliasTy, Interner, TyBuilder, TyKind, + to_assoc_type_id, AliasEq, AliasTy, Interner, TyBuilder, TyExt, TyKind, }; // This lint has a false positive here. See the link below for details. diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index d6c48870a..159a53a63 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -7,7 +7,7 @@ use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; use hir_def::lang_item::LangItemTarget; -use crate::{autoderef, Interner, Solution, Ty, TyBuilder, TyKind}; +use crate::{autoderef, Interner, Solution, Ty, TyBuilder, TyExt, TyKind}; use super::{InEnvironment, InferenceContext}; diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 53d94fd0d..dab137ae1 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -23,7 +23,8 @@ use crate::{ traits::{chalk::from_chalk, FnTrait}, utils::{generics, variant_data, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, - ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, + ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, + TypeWalk, }; use super::{ diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index c1d7a6b76..fc2bc3ef8 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -14,7 +14,7 @@ use hir_expand::name::Name; use super::{BindingMode, Expectation, InferenceContext}; use crate::{ lower::lower_to_chalk_mutability, static_lifetime, utils::variant_data, Interner, Substitution, - Ty, TyBuilder, TyKind, + Ty, TyBuilder, TyExt, TyKind, }; impl<'a> InferenceContext<'a> { diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index 14f705173..b19d67bb1 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -10,7 +10,9 @@ use hir_def::{ }; use hir_expand::name::Name; -use crate::{method_resolution, Interner, Substitution, Ty, TyBuilder, TyKind, ValueTyDefId}; +use crate::{ + method_resolution, Interner, Substitution, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId, +}; use super::{ExprOrPatId, InferenceContext, TraitRef}; diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index a2a5bcc07..f5b658cba 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -30,13 +30,11 @@ mod test_db; use std::sync::Arc; -use chalk_ir::UintTy; -use itertools::Itertools; - use base_db::salsa; +use chalk_ir::UintTy; use hir_def::{ - expr::ExprId, type_ref::Rawness, AssocContainerId, ConstParamId, FunctionId, GenericDefId, - HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, + expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId, + TypeParamId, }; use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; @@ -171,65 +169,6 @@ impl CallableSig { } impl Ty { - pub fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { - match self.kind(&Interner) { - TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)), - _ => None, - } - } - - pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { - match self.kind(&Interner) { - TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)), - TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)), - _ => None, - } - } - - pub fn strip_references(&self) -> &Ty { - let mut t: &Ty = self; - - while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) { - t = ty; - } - - t - } - - pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> { - match self.kind(&Interner) { - TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), - _ => None, - } - } - - pub fn as_tuple(&self) -> Option<&Substitution> { - match self.kind(&Interner) { - TyKind::Tuple(_, substs) => Some(substs), - _ => None, - } - } - - pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option { - match *self.kind(&Interner) { - TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), - TyKind::FnDef(callable, ..) => { - Some(db.lookup_intern_callable_def(callable.into()).into()) - } - TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), - TyKind::Foreign(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), - _ => None, - } - } - - pub fn is_never(&self) -> bool { - matches!(self.kind(&Interner), TyKind::Never) - } - - pub fn is_unknown(&self) -> bool { - matches!(self.kind(&Interner), TyKind::Error) - } - pub fn equals_ctor(&self, other: &Ty) -> bool { match (self.kind(&Interner), other.kind(&Interner)) { (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2, @@ -260,24 +199,6 @@ impl Ty { } } - /// If this is a `dyn Trait` type, this returns the `Trait` part. - fn dyn_trait_ref(&self) -> Option<&TraitRef> { - match self.kind(&Interner) { - TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| { - match b.skip_binders() { - WhereClause::Implemented(trait_ref) => Some(trait_ref), - _ => None, - } - }), - _ => None, - } - } - - /// If this is a `dyn Trait`, returns that trait. - pub fn dyn_trait(&self) -> Option { - self.dyn_trait_ref().map(|it| it.trait_id).map(from_chalk_trait_id) - } - fn builtin_deref(&self) -> Option { match self.kind(&Interner) { TyKind::Ref(.., ty) => Some(ty.clone()), @@ -286,37 +207,6 @@ impl Ty { } } - pub fn callable_def(&self, db: &dyn HirDatabase) -> Option { - match self.kind(&Interner) { - &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())), - _ => None, - } - } - - pub fn as_fn_def(&self, db: &dyn HirDatabase) -> Option { - if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { - Some(func) - } else { - None - } - } - - pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option { - match self.kind(&Interner) { - TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), - TyKind::FnDef(def, parameters) => { - let callable_def = db.lookup_intern_callable_def((*def).into()); - let sig = db.callable_item_signature(callable_def); - Some(sig.substitute(&Interner, ¶meters)) - } - TyKind::Closure(.., substs) => { - let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner); - sig_param.callable_sig(db) - } - _ => None, - } - } - /// Returns the type parameters of this type if it has some (i.e. is an ADT /// or function); so if `self` is `Option`, this returns the `u32`. pub fn substs(&self) -> Option<&Substitution> { @@ -344,104 +234,6 @@ impl Ty { _ => None, } } - - pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option> { - match self.kind(&Interner) { - TyKind::OpaqueType(opaque_ty_id, ..) => { - match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) { - ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => { - let krate = def.module(db.upcast()).krate(); - if let Some(future_trait) = db - .lang_item(krate, "future_trait".into()) - .and_then(|item| item.as_trait()) - { - // This is only used by type walking. - // Parameters will be walked outside, and projection predicate is not used. - // So just provide the Future trait. - let impl_bound = Binders::empty( - &Interner, - WhereClause::Implemented(TraitRef { - trait_id: to_chalk_trait_id(future_trait), - substitution: Substitution::empty(&Interner), - }), - ); - Some(vec![impl_bound]) - } else { - None - } - } - ImplTraitId::ReturnTypeImplTrait(..) => None, - } - } - TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { - let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into()) - { - ImplTraitId::ReturnTypeImplTrait(func, idx) => { - db.return_type_impl_traits(func).map(|it| { - let data = (*it) - .as_ref() - .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - data.substitute(&Interner, &opaque_ty.substitution) - }) - } - // It always has an parameter for Future::Output type. - ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(), - }; - - predicates.map(|it| it.into_value_and_skipped_binders().0) - } - TyKind::Placeholder(idx) => { - let id = from_placeholder_idx(db, *idx); - let generic_params = db.generic_params(id.parent); - let param_data = &generic_params.types[id.local_id]; - match param_data.provenance { - hir_def::generics::TypeParamProvenance::ArgumentImplTrait => { - let substs = TyBuilder::type_params_subst(db, id.parent); - let predicates = db - .generic_predicates(id.parent) - .into_iter() - .map(|pred| pred.clone().substitute(&Interner, &substs)) - .filter(|wc| match &wc.skip_binders() { - WhereClause::Implemented(tr) => { - tr.self_type_parameter(&Interner) == self - } - WhereClause::AliasEq(AliasEq { - alias: AliasTy::Projection(proj), - ty: _, - }) => proj.self_type_parameter(&Interner) == self, - _ => false, - }) - .collect_vec(); - - Some(predicates) - } - _ => None, - } - } - _ => None, - } - } - - pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option { - match self.kind(&Interner) { - TyKind::AssociatedType(id, ..) => { - match from_assoc_type_id(*id).lookup(db.upcast()).container { - AssocContainerId::TraitId(trait_id) => Some(trait_id), - _ => None, - } - } - TyKind::Alias(AliasTy::Projection(projection_ty)) => { - match from_assoc_type_id(projection_ty.associated_ty_id) - .lookup(db.upcast()) - .container - { - AssocContainerId::TraitId(trait_id) => Some(trait_id), - _ => None, - } - } - _ => None, - } - } } #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 5042bfbca..ee725fd46 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -22,7 +22,7 @@ use crate::{ static_lifetime, utils::all_super_traits, AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, - InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyKind, + InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind, TypeWalk, }; diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 5a8b5cd86..f03b92422 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -22,7 +22,7 @@ use crate::{ to_assoc_type_id, to_chalk_trait_id, utils::generics, AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, - TraitRef, Ty, TyBuilder, TyKind, WhereClause, + TraitRef, Ty, TyBuilder, TyExt, TyKind, WhereClause, }; use mapping::{ convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 18fd7ea74..fe9f273b0 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -12,7 +12,7 @@ use hir::{ AssocItem, Crate, Function, HasSource, HirDisplay, ModuleDef, }; use hir_def::FunctionId; -use hir_ty::TypeWalk; +use hir_ty::{TyExt, TypeWalk}; use ide::{AnalysisHost, RootDatabase}; use ide_db::base_db::{ salsa::{self, ParallelDatabase}, -- cgit v1.2.3 From bc993bbe85a231a7aa0414c6bea65d31d7375277 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 6 Apr 2021 12:40:11 +0200 Subject: Remove TyExt::dyn_trait_ref --- crates/hir_ty/src/chalk_ext.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index b101e904f..8e8a1aa48 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -29,9 +29,6 @@ pub trait TyExt { fn strip_references(&self) -> &Ty; - /// If this is a `dyn Trait` type, this returns the `Trait` part. - fn dyn_trait_ref(&self) -> Option<&TraitRef>; - /// If this is a `dyn Trait`, returns that trait. fn dyn_trait(&self) -> Option; @@ -123,8 +120,8 @@ impl TyExt for Ty { } } - fn dyn_trait_ref(&self) -> Option<&TraitRef> { - match self.kind(&Interner) { + fn dyn_trait(&self) -> Option { + let trait_ref = match self.kind(&Interner) { TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| { match b.skip_binders() { WhereClause::Implemented(trait_ref) => Some(trait_ref), @@ -132,11 +129,8 @@ impl TyExt for Ty { } }), _ => None, - } - } - - fn dyn_trait(&self) -> Option { - self.dyn_trait_ref().map(|it| it.trait_id).map(from_chalk_trait_id) + }?; + Some(from_chalk_trait_id(trait_ref.trait_id)) } fn strip_references(&self) -> &Ty { -- cgit v1.2.3