Formatting

This commit is contained in:
Florian Diebold 2020-02-07 15:13:15 +01:00
parent 6787f124b5
commit dded90a748
12 changed files with 98 additions and 53 deletions

View File

@ -10,10 +10,9 @@ use hir_def::{
per_ns::PerNs, per_ns::PerNs,
resolver::HasResolver, resolver::HasResolver,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId,
LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, LocalEnumVariantId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId,
TypeParamId, UnionId, TraitId, TypeAliasId, TypeParamId, UnionId,
GenericDefId
}; };
use hir_expand::{ use hir_expand::{
diagnostics::DiagnosticSink, diagnostics::DiagnosticSink,
@ -22,8 +21,7 @@ use hir_expand::{
}; };
use hir_ty::{ use hir_ty::{
autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy,
Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, Canonical, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor,
Substs
}; };
use ra_db::{CrateId, Edition, FileId}; use ra_db::{CrateId, Edition, FileId};
use ra_prof::profile; use ra_prof::profile;

View File

@ -61,7 +61,7 @@ pub struct WherePredicate {
pub enum WherePredicateTarget { pub enum WherePredicateTarget {
TypeRef(TypeRef), TypeRef(TypeRef),
/// For desugared where predicates that can directly refer to a type param. /// For desugared where predicates that can directly refer to a type param.
TypeParam(LocalTypeParamId) TypeParam(LocalTypeParamId),
} }
type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::TraitDef, ast::TypeParam>>; type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::TraitDef, ast::TypeParam>>;
@ -197,7 +197,8 @@ impl GenericParams {
return; return;
} }
let bound = TypeBound::from_ast(bound); 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) { fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) {
@ -212,7 +213,7 @@ impl GenericParams {
for bound in bounds { for bound in bounds {
self.where_predicates.push(WherePredicate { self.where_predicates.push(WherePredicate {
target: WherePredicateTarget::TypeParam(param_id), target: WherePredicateTarget::TypeParam(param_id),
bound: bound.clone() bound: bound.clone(),
}); });
} }
} }
@ -226,9 +227,13 @@ impl GenericParams {
} }
pub fn find_trait_self_param(&self) -> Option<LocalTypeParamId> { pub fn find_trait_self_param(&self) -> Option<LocalTypeParamId> {
self.types self.types.iter().find_map(|(id, p)| {
.iter() if p.provenance == TypeParamProvenance::TraitSelf {
.find_map(|(id, p)| if p.provenance == TypeParamProvenance::TraitSelf { Some(id) } else { None }) Some(id)
} else {
None
}
})
} }
} }

View File

@ -131,9 +131,7 @@ impl TypeRef {
fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) { fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) {
f(type_ref); f(type_ref);
match type_ref { match type_ref {
TypeRef::Fn(types) | TypeRef::Tuple(types) => { TypeRef::Fn(types) | TypeRef::Tuple(types) => types.iter().for_each(|t| go(t, f)),
types.iter().for_each(|t| go(t, f))
}
TypeRef::RawPtr(type_ref, _) TypeRef::RawPtr(type_ref, _)
| TypeRef::Reference(type_ref, _) | TypeRef::Reference(type_ref, _)
| TypeRef::Array(type_ref) | TypeRef::Array(type_ref)

View File

@ -3,7 +3,8 @@
use std::sync::Arc; use std::sync::Arc;
use hir_def::{ 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_arena::map::ArenaMap;
use ra_db::{impl_intern_key, salsa, CrateId}; use ra_db::{impl_intern_key, salsa, CrateId};
@ -12,8 +13,8 @@ use ra_prof::profile;
use crate::{ use crate::{
method_resolution::CrateImplBlocks, method_resolution::CrateImplBlocks,
traits::{chalk, AssocTyValue, Impl}, traits::{chalk, AssocTyValue, Impl},
CallableDef, PolyFnSig, GenericPredicate, InferenceResult, Substs, TraitRef, Ty, TyDefId, TypeCtor, Binders, CallableDef, GenericPredicate, InferenceResult, PolyFnSig, Substs, TraitRef, Ty,
ValueTyDefId, Binders, TyDefId, TypeCtor, ValueTyDefId,
}; };
#[salsa::query_group(HirDatabaseStorage)] #[salsa::query_group(HirDatabaseStorage)]

View File

@ -278,7 +278,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
impl_trait_mode: ImplTraitLoweringMode, impl_trait_mode: ImplTraitLoweringMode,
) -> Ty { ) -> Ty {
// FIXME use right resolver for block // 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 = Ty::from_hir(&ctx, type_ref);
let ty = self.insert_type_vars(ty); let ty = self.insert_type_vars(ty);
self.normalize_associated_types_in(ty) self.normalize_associated_types_in(ty)
@ -455,8 +456,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
fn collect_fn(&mut self, data: &FunctionData) { fn collect_fn(&mut self, data: &FunctionData) {
let body = Arc::clone(&self.body); // avoid borrow checker problem 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 ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
let param_tys = data.params.iter().map(|type_ref| Ty::from_hir(&ctx, type_ref)).collect::<Vec<_>>(); .with_impl_trait_mode(ImplTraitLoweringMode::Param);
let param_tys =
data.params.iter().map(|type_ref| Ty::from_hir(&ctx, type_ref)).collect::<Vec<_>>();
for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) { for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) {
let ty = self.insert_type_vars(ty); let ty = self.insert_type_vars(ty);
let ty = self.normalize_associated_types_in(ty); let ty = self.normalize_associated_types_in(ty);

View File

@ -66,9 +66,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
// This works for smart-pointer-like coercion, which covers all impls from std. // This works for smart-pointer-like coercion, which covers all impls from std.
st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| { st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| {
match (ty1, ty2) { match (ty1, ty2) {
(Ty::Bound(idx1), Ty::Bound(idx2)) (Ty::Bound(idx1), Ty::Bound(idx2)) if idx1 != idx2 => {
if idx1 != idx2 =>
{
Some(((*ctor1, *ctor2), i)) Some(((*ctor1, *ctor2), i))
} }
_ => None, _ => None,
@ -277,9 +275,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let mut multiple_used = false; let mut multiple_used = false;
fields.for_each(|(field_id, _data)| { fields.for_each(|(field_id, _data)| {
field_tys[field_id].value.walk(&mut |ty| match ty { field_tys[field_id].value.walk(&mut |ty| match ty {
&Ty::Bound(idx) if idx == unsize_generic_index => { &Ty::Bound(idx) if idx == unsize_generic_index => multiple_used = true,
multiple_used = true
}
_ => {} _ => {}
}) })
}); });

View File

@ -19,8 +19,8 @@ use crate::{
method_resolution, op, method_resolution, op,
traits::InEnvironment, traits::InEnvironment,
utils::{generics, variant_data, Generics}, utils::{generics, variant_data, Generics},
ApplicationTy, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, Ty, ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef,
TypeCtor, Uncertain, Binders, Ty, TypeCtor, Uncertain,
}; };
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};

View File

@ -9,10 +9,7 @@ use hir_def::{
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
use crate::{ use crate::{db::HirDatabase, method_resolution, Substs, Ty, ValueTyDefId};
db::HirDatabase, method_resolution, Substs, Ty,
ValueTyDefId
};
use super::{ExprOrPatId, InferenceContext, TraitRef}; use super::{ExprOrPatId, InferenceContext, TraitRef};

View File

@ -1033,7 +1033,10 @@ impl HirDisplay for Ty {
write!(f, "impl ")?; write!(f, "impl ")?;
let bounds = f.db.generic_predicates_for_param(*id); let bounds = f.db.generic_predicates_for_param(*id);
let substs = Substs::type_params_for_generics(&generics); let substs = Substs::type_params_for_generics(&generics);
write_bounds_like_dyn_trait(&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(), f)?; write_bounds_like_dyn_trait(
&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(),
f,
)?;
} }
} }
} }

View File

@ -276,7 +276,9 @@ impl Ty {
TypeNs::SelfType(impl_id) => { TypeNs::SelfType(impl_id) => {
let generics = generics(ctx.db, impl_id.into()); let generics = generics(ctx.db, impl_id.into());
let substs = match ctx.type_param_mode { 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), TypeParamLoweringMode::Variable => Substs::bound_vars(&generics),
}; };
ctx.db.impl_self_ty(impl_id).subst(&substs) ctx.db.impl_self_ty(impl_id).subst(&substs)
@ -284,7 +286,9 @@ impl Ty {
TypeNs::AdtSelfType(adt) => { TypeNs::AdtSelfType(adt) => {
let generics = generics(ctx.db, adt.into()); let generics = generics(ctx.db, adt.into());
let substs = match ctx.type_param_mode { 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), TypeParamLoweringMode::Variable => Substs::bound_vars(&generics),
}; };
ctx.db.ty(adt.into()).subst(&substs) ctx.db.ty(adt.into()).subst(&substs)

View File

@ -144,8 +144,11 @@ impl ToChalk for Ty {
} }
Ty::Param(id) => { Ty::Param(id) => {
let interned_id = db.intern_type_param_id(id); let interned_id = db.intern_type_param_id(id);
PlaceholderIndex { ui: UniverseIndex::ROOT, idx: interned_id.as_intern_id().as_usize() } PlaceholderIndex {
.to_ty::<TypeFamily>() ui: UniverseIndex::ROOT,
idx: interned_id.as_intern_id().as_usize(),
}
.to_ty::<TypeFamily>()
} }
Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx as usize).intern(), Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx as usize).intern(),
Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
@ -178,7 +181,9 @@ impl ToChalk for Ty {
}, },
chalk_ir::TyData::Placeholder(idx) => { chalk_ir::TyData::Placeholder(idx) => {
assert_eq!(idx.ui, UniverseIndex::ROOT); 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)) Ty::Param(db.lookup_intern_type_param_id(interned_id))
} }
chalk_ir::TyData::Alias(proj) => { chalk_ir::TyData::Alias(proj) => {

View File

@ -2,6 +2,7 @@
//! query, but can't be computed directly from `*Data` (ie, which need a `db`). //! query, but can't be computed directly from `*Data` (ie, which need a `db`).
use std::sync::Arc; use std::sync::Arc;
use hir_def::generics::WherePredicateTarget;
use hir_def::{ use hir_def::{
adt::VariantData, adt::VariantData,
db::DefDatabase, db::DefDatabase,
@ -12,7 +13,6 @@ use hir_def::{
AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId,
}; };
use hir_expand::name::{name, Name}; use hir_expand::name::{name, Name};
use hir_def::generics::WherePredicateTarget;
fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> { fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
let resolver = trait_.resolver(db); let resolver = trait_.resolver(db);
@ -26,8 +26,12 @@ fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
.where_predicates .where_predicates
.iter() .iter()
.filter_map(|pred| match &pred.target { .filter_map(|pred| match &pred.target {
WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => pred.bound.as_path(), WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => {
WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => pred.bound.as_path(), pred.bound.as_path()
}
WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => {
pred.bound.as_path()
}
_ => None, _ => None,
}) })
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) { .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 { impl Generics {
pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a { pub(crate) fn iter<'a>(
&'a self,
) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
self.parent_generics self.parent_generics
.as_ref() .as_ref()
.into_iter() .into_iter()
.flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))) .flat_map(|it| {
.chain(self.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p))) 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<Item = (TypeParamId, &'a TypeParamData)> + 'a { pub(crate) fn iter_parent<'a>(
self.parent_generics &'a self,
.as_ref() ) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
.into_iter() self.parent_generics.as_ref().into_iter().flat_map(|it| {
.flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))) it.params
.types
.iter()
.map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))
})
} }
pub(crate) fn len(&self) -> usize { pub(crate) fn len(&self) -> usize {
@ -127,9 +147,24 @@ impl Generics {
/// (self, type param list, impl trait) /// (self, type param list, impl trait)
pub(crate) fn provenance_split(&self) -> (usize, usize, usize) { pub(crate) fn provenance_split(&self) -> (usize, usize, usize) {
let self_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::TraitSelf).count(); let self_params = self
let list_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::TypeParamList).count(); .params
let impl_trait_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::ArgumentImplTrait).count(); .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) (self_params, list_params, impl_trait_params)
} }