diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 63c85ca34a8..4d964172854 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -10,10 +10,9 @@ per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, - AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, - LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, - TypeParamId, UnionId, - GenericDefId + AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, + LocalEnumVariantId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, + TraitId, TypeAliasId, TypeParamId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -22,8 +21,7 @@ }; use hir_ty::{ autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, - Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, - Substs + Canonical, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index e4e616519ba..f765e6edc20 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -61,7 +61,7 @@ pub struct WherePredicate { pub enum WherePredicateTarget { TypeRef(TypeRef), /// For desugared where predicates that can directly refer to a type param. - TypeParam(LocalTypeParamId) + TypeParam(LocalTypeParamId), } type SourceMap = ArenaMap>; @@ -197,7 +197,8 @@ fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: Ty return; } let bound = TypeBound::from_ast(bound); - self.where_predicates.push(WherePredicate { target: WherePredicateTarget::TypeRef(type_ref), bound }); + self.where_predicates + .push(WherePredicate { target: WherePredicateTarget::TypeRef(type_ref), bound }); } fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) { @@ -212,7 +213,7 @@ fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) { for bound in bounds { self.where_predicates.push(WherePredicate { target: WherePredicateTarget::TypeParam(param_id), - bound: bound.clone() + bound: bound.clone(), }); } } @@ -226,9 +227,13 @@ pub fn find_by_name(&self, name: &Name) -> Option { } pub fn find_trait_self_param(&self) -> Option { - self.types - .iter() - .find_map(|(id, p)| if p.provenance == TypeParamProvenance::TraitSelf { Some(id) } else { None }) + self.types.iter().find_map(|(id, p)| { + if p.provenance == TypeParamProvenance::TraitSelf { + Some(id) + } else { + None + } + }) } } diff --git a/crates/ra_hir_def/src/type_ref.rs b/crates/ra_hir_def/src/type_ref.rs index 10941477053..102fdb13d72 100644 --- a/crates/ra_hir_def/src/type_ref.rs +++ b/crates/ra_hir_def/src/type_ref.rs @@ -131,9 +131,7 @@ pub fn walk(&self, f: &mut impl FnMut(&TypeRef)) { fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) { f(type_ref); match type_ref { - TypeRef::Fn(types) | TypeRef::Tuple(types) => { - types.iter().for_each(|t| go(t, f)) - } + TypeRef::Fn(types) | TypeRef::Tuple(types) => types.iter().for_each(|t| go(t, f)), TypeRef::RawPtr(type_ref, _) | TypeRef::Reference(type_ref, _) | TypeRef::Array(type_ref) diff --git a/crates/ra_hir_ty/src/db.rs b/crates/ra_hir_ty/src/db.rs index 21ab22fa981..e9bfcfa1768 100644 --- a/crates/ra_hir_ty/src/db.rs +++ b/crates/ra_hir_ty/src/db.rs @@ -3,7 +3,8 @@ use std::sync::Arc; use hir_def::{ - db::DefDatabase, DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId, TypeParamId, + db::DefDatabase, DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, TypeParamId, + VariantId, }; use ra_arena::map::ArenaMap; use ra_db::{impl_intern_key, salsa, CrateId}; @@ -12,8 +13,8 @@ use crate::{ method_resolution::CrateImplBlocks, traits::{chalk, AssocTyValue, Impl}, - CallableDef, PolyFnSig, GenericPredicate, InferenceResult, Substs, TraitRef, Ty, TyDefId, TypeCtor, - ValueTyDefId, Binders, + Binders, CallableDef, GenericPredicate, InferenceResult, PolyFnSig, Substs, TraitRef, Ty, + TyDefId, TypeCtor, ValueTyDefId, }; #[salsa::query_group(HirDatabaseStorage)] diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index dec6bd84c2e..a9d958c8bcd 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs @@ -278,7 +278,8 @@ fn make_ty_with_mode( impl_trait_mode: ImplTraitLoweringMode, ) -> Ty { // FIXME use right resolver for block - let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver).with_impl_trait_mode(impl_trait_mode); + let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver) + .with_impl_trait_mode(impl_trait_mode); let ty = Ty::from_hir(&ctx, type_ref); let ty = self.insert_type_vars(ty); self.normalize_associated_types_in(ty) @@ -455,8 +456,10 @@ fn collect_const(&mut self, data: &ConstData) { fn collect_fn(&mut self, data: &FunctionData) { let body = Arc::clone(&self.body); // avoid borrow checker problem - let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver).with_impl_trait_mode(ImplTraitLoweringMode::Param); - let param_tys = data.params.iter().map(|type_ref| Ty::from_hir(&ctx, type_ref)).collect::>(); + let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver) + .with_impl_trait_mode(ImplTraitLoweringMode::Param); + let param_tys = + data.params.iter().map(|type_ref| Ty::from_hir(&ctx, type_ref)).collect::>(); for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) { let ty = self.insert_type_vars(ty); let ty = self.normalize_associated_types_in(ty); diff --git a/crates/ra_hir_ty/src/infer/coerce.rs b/crates/ra_hir_ty/src/infer/coerce.rs index 2a95678984e..f68a1439f8f 100644 --- a/crates/ra_hir_ty/src/infer/coerce.rs +++ b/crates/ra_hir_ty/src/infer/coerce.rs @@ -66,9 +66,7 @@ pub(super) fn init_coerce_unsized_map( // This works for smart-pointer-like coercion, which covers all impls from std. st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| { match (ty1, ty2) { - (Ty::Bound(idx1), Ty::Bound(idx2)) - if idx1 != idx2 => - { + (Ty::Bound(idx1), Ty::Bound(idx2)) if idx1 != idx2 => { Some(((*ctor1, *ctor2), i)) } _ => None, @@ -277,9 +275,7 @@ fn check_unsize_and_coerce(&mut self, from_ty: &Ty, to_ty: &Ty, depth: usize) -> let mut multiple_used = false; fields.for_each(|(field_id, _data)| { field_tys[field_id].value.walk(&mut |ty| match ty { - &Ty::Bound(idx) if idx == unsize_generic_index => { - multiple_used = true - } + &Ty::Bound(idx) if idx == unsize_generic_index => multiple_used = true, _ => {} }) }); diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 6222bd90e6e..8c360bcadcf 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -19,8 +19,8 @@ method_resolution, op, traits::InEnvironment, utils::{generics, variant_data, Generics}, - ApplicationTy, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, Ty, - TypeCtor, Uncertain, Binders, + ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, + Ty, TypeCtor, Uncertain, }; use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs index fcf13b0b313..686ce7a218b 100644 --- a/crates/ra_hir_ty/src/infer/path.rs +++ b/crates/ra_hir_ty/src/infer/path.rs @@ -9,10 +9,7 @@ }; use hir_expand::name::Name; -use crate::{ - db::HirDatabase, method_resolution, Substs, Ty, - ValueTyDefId -}; +use crate::{db::HirDatabase, method_resolution, Substs, Ty, ValueTyDefId}; use super::{ExprOrPatId, InferenceContext, TraitRef}; diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 314be17b87f..60c7fd0e5d9 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -1033,7 +1033,10 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { write!(f, "impl ")?; let bounds = f.db.generic_predicates_for_param(*id); let substs = Substs::type_params_for_generics(&generics); - write_bounds_like_dyn_trait(&bounds.iter().map(|b| b.clone().subst(&substs)).collect::>(), f)?; + write_bounds_like_dyn_trait( + &bounds.iter().map(|b| b.clone().subst(&substs)).collect::>(), + f, + )?; } } } diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 0d4c075af48..4168e750961 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs @@ -276,7 +276,9 @@ pub(crate) fn from_partly_resolved_hir_path( TypeNs::SelfType(impl_id) => { let generics = generics(ctx.db, impl_id.into()); let substs = match ctx.type_param_mode { - TypeParamLoweringMode::Placeholder => Substs::type_params_for_generics(&generics), + TypeParamLoweringMode::Placeholder => { + Substs::type_params_for_generics(&generics) + } TypeParamLoweringMode::Variable => Substs::bound_vars(&generics), }; ctx.db.impl_self_ty(impl_id).subst(&substs) @@ -284,7 +286,9 @@ pub(crate) fn from_partly_resolved_hir_path( TypeNs::AdtSelfType(adt) => { let generics = generics(ctx.db, adt.into()); let substs = match ctx.type_param_mode { - TypeParamLoweringMode::Placeholder => Substs::type_params_for_generics(&generics), + TypeParamLoweringMode::Placeholder => { + Substs::type_params_for_generics(&generics) + } TypeParamLoweringMode::Variable => Substs::bound_vars(&generics), }; ctx.db.ty(adt.into()).subst(&substs) diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 8260bd15730..4974c565be1 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -144,8 +144,11 @@ fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Ty { } Ty::Param(id) => { let interned_id = db.intern_type_param_id(id); - PlaceholderIndex { ui: UniverseIndex::ROOT, idx: interned_id.as_intern_id().as_usize() } - .to_ty::() + PlaceholderIndex { + ui: UniverseIndex::ROOT, + idx: interned_id.as_intern_id().as_usize(), + } + .to_ty::() } Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx as usize).intern(), Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), @@ -178,7 +181,9 @@ fn from_chalk(db: &impl HirDatabase, chalk: chalk_ir::Ty) -> Self { }, chalk_ir::TyData::Placeholder(idx) => { assert_eq!(idx.ui, UniverseIndex::ROOT); - let interned_id = crate::db::GlobalTypeParamId::from_intern_id(crate::salsa::InternId::from(idx.idx)); + let interned_id = crate::db::GlobalTypeParamId::from_intern_id( + crate::salsa::InternId::from(idx.idx), + ); Ty::Param(db.lookup_intern_type_param_id(interned_id)) } chalk_ir::TyData::Alias(proj) => { diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index 8fa1838bd89..e307d958d83 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs @@ -2,6 +2,7 @@ //! query, but can't be computed directly from `*Data` (ie, which need a `db`). use std::sync::Arc; +use hir_def::generics::WherePredicateTarget; use hir_def::{ adt::VariantData, db::DefDatabase, @@ -12,7 +13,6 @@ AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, }; use hir_expand::name::{name, Name}; -use hir_def::generics::WherePredicateTarget; fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec { let resolver = trait_.resolver(db); @@ -26,8 +26,12 @@ fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec { .where_predicates .iter() .filter_map(|pred| match &pred.target { - WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => pred.bound.as_path(), - WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => pred.bound.as_path(), + WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => { + pred.bound.as_path() + } + WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => { + pred.bound.as_path() + } _ => None, }) .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) { @@ -99,19 +103,35 @@ pub(crate) struct Generics { } impl Generics { - pub(crate) fn iter<'a>(&'a self) -> impl Iterator + 'a { + pub(crate) fn iter<'a>( + &'a self, + ) -> impl Iterator + 'a { self.parent_generics .as_ref() .into_iter() - .flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))) - .chain(self.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p))) + .flat_map(|it| { + it.params + .types + .iter() + .map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p)) + }) + .chain( + self.params + .types + .iter() + .map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p)), + ) } - pub(crate) fn iter_parent<'a>(&'a self) -> impl Iterator + 'a { - self.parent_generics - .as_ref() - .into_iter() - .flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))) + pub(crate) fn iter_parent<'a>( + &'a self, + ) -> impl Iterator + 'a { + self.parent_generics.as_ref().into_iter().flat_map(|it| { + it.params + .types + .iter() + .map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p)) + }) } pub(crate) fn len(&self) -> usize { @@ -127,9 +147,24 @@ pub(crate) fn len_split(&self) -> (usize, usize, usize) { /// (self, type param list, impl trait) pub(crate) fn provenance_split(&self) -> (usize, usize, usize) { - let self_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::TraitSelf).count(); - let list_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::TypeParamList).count(); - let impl_trait_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::ArgumentImplTrait).count(); + let self_params = self + .params + .types + .iter() + .filter(|(_, p)| p.provenance == TypeParamProvenance::TraitSelf) + .count(); + let list_params = self + .params + .types + .iter() + .filter(|(_, p)| p.provenance == TypeParamProvenance::TypeParamList) + .count(); + let impl_trait_params = self + .params + .types + .iter() + .filter(|(_, p)| p.provenance == TypeParamProvenance::ArgumentImplTrait) + .count(); (self_params, list_params, impl_trait_params) }