Formatting
This commit is contained in:
parent
6787f124b5
commit
dded90a748
@ -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;
|
||||
|
@ -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<LocalTypeParamId, Either<ast::TraitDef, ast::TypeParam>>;
|
||||
@ -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<LocalTypeParamId> {
|
||||
}
|
||||
|
||||
pub fn find_trait_self_param(&self) -> Option<LocalTypeParamId> {
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)]
|
||||
|
@ -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::<Vec<_>>();
|
||||
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::<Vec<_>>();
|
||||
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);
|
||||
|
@ -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,
|
||||
_ => {}
|
||||
})
|
||||
});
|
||||
|
@ -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};
|
||||
|
@ -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};
|
||||
|
||||
|
@ -1033,7 +1033,10 @@ fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> 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::<Vec<_>>(), f)?;
|
||||
write_bounds_like_dyn_trait(
|
||||
&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(),
|
||||
f,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -144,8 +144,11 @@ fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Ty<TypeFamily> {
|
||||
}
|
||||
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::<TypeFamily>()
|
||||
PlaceholderIndex {
|
||||
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::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
|
||||
@ -178,7 +181,9 @@ fn from_chalk(db: &impl HirDatabase, chalk: chalk_ir::Ty<TypeFamily>) -> 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) => {
|
||||
|
@ -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<TraitId> {
|
||||
let resolver = trait_.resolver(db);
|
||||
@ -26,8 +26,12 @@ fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
|
||||
.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<Item = (TypeParamId, &'a TypeParamData)> + 'a {
|
||||
pub(crate) fn iter<'a>(
|
||||
&'a self,
|
||||
) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + '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<Item = (TypeParamId, &'a TypeParamData)> + '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<Item = (TypeParamId, &'a TypeParamData)> + '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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user