Fix compilation of other crates

This commit is contained in:
Florian Diebold 2020-02-04 21:33:03 +01:00 committed by Florian Diebold
parent a3d8cffde3
commit 0718682cff
3 changed files with 25 additions and 14 deletions

View file

@ -13,6 +13,7 @@ use hir_def::{
AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
TypeParamId, UnionId, TypeParamId, UnionId,
GenericDefId
}; };
use hir_expand::{ use hir_expand::{
diagnostics::DiagnosticSink, diagnostics::DiagnosticSink,
@ -21,7 +22,8 @@ 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, TypeWalk, Canonical, InEnvironment, 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;
@ -270,7 +272,13 @@ impl StructField {
pub fn ty(&self, db: &impl HirDatabase) -> Type { pub fn ty(&self, db: &impl HirDatabase) -> Type {
let var_id = self.parent.into(); let var_id = self.parent.into();
let ty = db.field_types(var_id)[self.id].clone(); let generic_def_id: GenericDefId = match self.parent {
VariantDef::Struct(it) => it.id.into(),
VariantDef::Union(it) => it.id.into(),
VariantDef::EnumVariant(it) => it.parent.id.into(),
};
let substs = Substs::type_params(db, generic_def_id);
let ty = db.field_types(var_id)[self.id].clone().subst(&substs);
Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty) Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty)
} }
@ -789,11 +797,7 @@ impl ImplBlock {
pub fn target_ty(&self, db: &impl HirDatabase) -> Type { pub fn target_ty(&self, db: &impl HirDatabase) -> Type {
let impl_data = db.impl_data(self.id); let impl_data = db.impl_data(self.id);
let resolver = self.id.resolver(db); let resolver = self.id.resolver(db);
let ctx = hir_ty::TyLoweringContext { let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
db,
resolver: &resolver,
impl_trait_mode: hir_ty::ImplTraitLoweringMode::Disallowed,
};
let environment = TraitEnvironment::lower(db, &resolver); let environment = TraitEnvironment::lower(db, &resolver);
let ty = Ty::from_hir(&ctx, &impl_data.target_type); let ty = Ty::from_hir(&ctx, &impl_data.target_type);
Type { Type {
@ -856,9 +860,10 @@ impl Type {
fn from_def( fn from_def(
db: &impl HirDatabase, db: &impl HirDatabase,
krate: CrateId, krate: CrateId,
def: impl HasResolver + Into<TyDefId>, def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>,
) -> Type { ) -> Type {
let ty = db.ty(def.into()); let substs = Substs::type_params(db, def);
let ty = db.ty(def.into()).subst(&substs);
Type::new(db, krate, def, ty) Type::new(db, krate, def, ty)
} }
@ -955,7 +960,7 @@ impl Type {
match a_ty.ctor { match a_ty.ctor {
TypeCtor::Tuple { .. } => { TypeCtor::Tuple { .. } => {
for ty in a_ty.parameters.iter() { for ty in a_ty.parameters.iter() {
let ty = ty.clone().subst(&a_ty.parameters); let ty = ty.clone();
res.push(self.derived(ty)); res.push(self.derived(ty));
} }
} }

View file

@ -361,10 +361,16 @@ impl Substs {
} }
/// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
pub(crate) fn type_params(generic_params: &Generics) -> Substs { pub(crate) fn type_params_for_generics(generic_params: &Generics) -> Substs {
Substs(generic_params.iter().map(|(id, _)| Ty::Param(id)).collect()) Substs(generic_params.iter().map(|(id, _)| Ty::Param(id)).collect())
} }
/// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
pub fn type_params(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> Substs {
let params = generics(db, def.into());
Substs::type_params_for_generics(&params)
}
/// Return Substs that replace each parameter by a bound variable. /// Return Substs that replace each parameter by a bound variable.
pub(crate) fn bound_vars(generic_params: &Generics) -> Substs { pub(crate) fn bound_vars(generic_params: &Generics) -> Substs {
Substs(generic_params.iter().enumerate().map(|(idx, _)| Ty::Bound(idx as u32)).collect()) Substs(generic_params.iter().enumerate().map(|(idx, _)| Ty::Bound(idx as u32)).collect())
@ -1026,7 +1032,7 @@ impl HirDisplay for Ty {
TypeParamProvenance::ArgumentImplTrait => { TypeParamProvenance::ArgumentImplTrait => {
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(&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,7 @@ 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(&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 +284,7 @@ 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(&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)