mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
Replace Substitution::bound_vars and ::type_params_for_generics
This commit is contained in:
parent
715c178f0b
commit
a4d7bdf1c8
7 changed files with 42 additions and 54 deletions
|
@ -19,7 +19,7 @@ use crate::{
|
|||
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
|
||||
to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
|
||||
CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy,
|
||||
ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyExt, TyKind,
|
||||
ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind,
|
||||
WhereClause,
|
||||
};
|
||||
|
||||
|
@ -592,7 +592,7 @@ impl HirDisplay for Ty {
|
|||
write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
|
||||
}
|
||||
TypeParamProvenance::ArgumentImplTrait => {
|
||||
let substs = Substitution::type_params_for_generics(f.db, &generics);
|
||||
let substs = generics.type_params_subst(f.db);
|
||||
let bounds = f
|
||||
.db
|
||||
.generic_predicates(id.parent)
|
||||
|
|
|
@ -38,7 +38,7 @@ use syntax::SmolStr;
|
|||
|
||||
use super::{
|
||||
traits::{DomainGoal, Guidance, Solution},
|
||||
InEnvironment, ProjectionTy, Substitution, TraitEnvironment, TraitRef, Ty, TypeWalk,
|
||||
InEnvironment, ProjectionTy, TraitEnvironment, TraitRef, Ty, TypeWalk,
|
||||
};
|
||||
use crate::{
|
||||
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
|
||||
|
@ -487,7 +487,7 @@ impl<'a> InferenceContext<'a> {
|
|||
}
|
||||
TypeNs::SelfType(impl_id) => {
|
||||
let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
|
||||
let substs = Substitution::type_params_for_generics(self.db, &generics);
|
||||
let substs = generics.type_params_subst(self.db);
|
||||
let ty = self.db.impl_self_ty(impl_id).subst(&substs);
|
||||
match unresolved {
|
||||
None => {
|
||||
|
|
|
@ -80,7 +80,7 @@ impl<'a> InferenceContext<'a> {
|
|||
}
|
||||
ValueNs::ImplSelf(impl_id) => {
|
||||
let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
|
||||
let substs = Substitution::type_params_for_generics(self.db, &generics);
|
||||
let substs = generics.type_params_subst(self.db);
|
||||
let ty = self.db.impl_self_ty(impl_id).subst(&substs);
|
||||
if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() {
|
||||
let ty = self.db.value_ty(struct_id.into()).subst(&substs);
|
||||
|
|
|
@ -41,7 +41,7 @@ use hir_def::{
|
|||
use crate::{
|
||||
db::HirDatabase,
|
||||
display::HirDisplay,
|
||||
utils::{generics, make_mut_slice, Generics},
|
||||
utils::{generics, make_mut_slice},
|
||||
};
|
||||
|
||||
pub use autoderef::autoderef;
|
||||
|
@ -463,34 +463,10 @@ impl Substitution {
|
|||
Substitution(elements.into_iter().casted(interner).collect())
|
||||
}
|
||||
|
||||
/// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
|
||||
pub(crate) fn type_params_for_generics(
|
||||
db: &dyn HirDatabase,
|
||||
generic_params: &Generics,
|
||||
) -> Substitution {
|
||||
Substitution::from_iter(
|
||||
&Interner,
|
||||
generic_params
|
||||
.iter()
|
||||
.map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
|
||||
pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution {
|
||||
let params = generics(db.upcast(), def.into());
|
||||
Substitution::type_params_for_generics(db, ¶ms)
|
||||
}
|
||||
|
||||
/// Return Substs that replace each parameter by a bound variable.
|
||||
pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) -> Substitution {
|
||||
Substitution::from_iter(
|
||||
&Interner,
|
||||
generic_params
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
|
||||
)
|
||||
params.type_params_subst(db)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -234,7 +234,7 @@ impl<'a> TyLoweringContext<'a> {
|
|||
let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx);
|
||||
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
|
||||
let generics = generics(self.db.upcast(), func.into());
|
||||
let parameters = Substitution::bound_vars(&generics, self.in_binders);
|
||||
let parameters = generics.bound_vars_subst(self.in_binders);
|
||||
TyKind::Alias(AliasTy::Opaque(OpaqueTy {
|
||||
opaque_ty_id,
|
||||
substitution: parameters,
|
||||
|
@ -411,24 +411,16 @@ impl<'a> TyLoweringContext<'a> {
|
|||
TypeNs::SelfType(impl_id) => {
|
||||
let generics = generics(self.db.upcast(), impl_id.into());
|
||||
let substs = match self.type_param_mode {
|
||||
TypeParamLoweringMode::Placeholder => {
|
||||
Substitution::type_params_for_generics(self.db, &generics)
|
||||
}
|
||||
TypeParamLoweringMode::Variable => {
|
||||
Substitution::bound_vars(&generics, self.in_binders)
|
||||
}
|
||||
TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db),
|
||||
TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders),
|
||||
};
|
||||
self.db.impl_self_ty(impl_id).subst(&substs)
|
||||
}
|
||||
TypeNs::AdtSelfType(adt) => {
|
||||
let generics = generics(self.db.upcast(), adt.into());
|
||||
let substs = match self.type_param_mode {
|
||||
TypeParamLoweringMode::Placeholder => {
|
||||
Substitution::type_params_for_generics(self.db, &generics)
|
||||
}
|
||||
TypeParamLoweringMode::Variable => {
|
||||
Substitution::bound_vars(&generics, self.in_binders)
|
||||
}
|
||||
TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db),
|
||||
TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders),
|
||||
};
|
||||
self.db.ty(adt.into()).subst(&substs)
|
||||
}
|
||||
|
@ -1060,7 +1052,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
|
|||
/// function body.
|
||||
fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
|
||||
let generics = generics(db.upcast(), def.into());
|
||||
let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
Binders::new(
|
||||
substs.len(&Interner),
|
||||
TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
|
||||
|
@ -1105,7 +1097,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
|
|||
return type_for_adt(db, def.into());
|
||||
}
|
||||
let generics = generics(db.upcast(), def.into());
|
||||
let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
Binders::new(
|
||||
substs.len(&Interner),
|
||||
TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
|
||||
|
@ -1132,7 +1124,7 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
|
|||
return type_for_adt(db, def.parent.into());
|
||||
}
|
||||
let generics = generics(db.upcast(), def.parent.into());
|
||||
let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
Binders::new(
|
||||
substs.len(&Interner),
|
||||
TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
|
||||
|
|
|
@ -387,7 +387,7 @@ pub(crate) fn associated_ty_data_query(
|
|||
// Lower bounds -- we could/should maybe move this to a separate query in `lower`
|
||||
let type_alias_data = db.type_alias_data(type_alias);
|
||||
let generic_params = generics(db.upcast(), type_alias.into());
|
||||
let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
|
||||
let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast());
|
||||
let ctx = crate::TyLoweringContext::new(db, &resolver)
|
||||
.with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable);
|
||||
|
@ -421,7 +421,7 @@ pub(crate) fn trait_datum_query(
|
|||
let trait_data = db.trait_data(trait_);
|
||||
debug!("trait {:?} = {:?}", trait_id, trait_data.name);
|
||||
let generic_params = generics(db.upcast(), trait_.into());
|
||||
let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
|
||||
let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
let flags = rust_ir::TraitFlags {
|
||||
auto: trait_data.is_auto,
|
||||
upstream: trait_.lookup(db.upcast()).container.krate() != krate,
|
||||
|
@ -490,7 +490,7 @@ pub(crate) fn struct_datum_query(
|
|||
let upstream = adt_id.module(db.upcast()).krate() != krate;
|
||||
let where_clauses = {
|
||||
let generic_params = generics(db.upcast(), adt_id.into());
|
||||
let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
|
||||
let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
convert_where_clauses(db, adt_id.into(), &bound_vars)
|
||||
};
|
||||
let flags = rust_ir::AdtFlags {
|
||||
|
@ -539,7 +539,7 @@ fn impl_def_datum(
|
|||
let impl_data = db.impl_data(impl_id);
|
||||
|
||||
let generic_params = generics(db.upcast(), impl_id.into());
|
||||
let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
|
||||
let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
let trait_ = trait_ref.hir_trait_id();
|
||||
let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate {
|
||||
rust_ir::ImplType::Local
|
||||
|
@ -629,7 +629,7 @@ pub(crate) fn fn_def_datum_query(
|
|||
let callable_def: CallableDefId = from_chalk(db, fn_def_id);
|
||||
let generic_params = generics(db.upcast(), callable_def.into());
|
||||
let sig = db.callable_item_signature(callable_def);
|
||||
let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
|
||||
let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
|
||||
let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars);
|
||||
let bound = rust_ir::FnDefDatumBound {
|
||||
// Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
//! query, but can't be computed directly from `*Data` (ie, which need a `db`).
|
||||
use std::sync::Arc;
|
||||
|
||||
use chalk_ir::DebruijnIndex;
|
||||
use chalk_ir::{BoundVar, DebruijnIndex};
|
||||
use hir_def::{
|
||||
adt::VariantData,
|
||||
db::DefDatabase,
|
||||
|
@ -16,7 +16,7 @@ use hir_def::{
|
|||
};
|
||||
use hir_expand::name::{name, Name};
|
||||
|
||||
use crate::{db::HirDatabase, TraitRef, TypeWalk, WhereClause};
|
||||
use crate::{db::HirDatabase, Interner, Substitution, TraitRef, TyKind, TypeWalk, WhereClause};
|
||||
|
||||
fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<TraitId> {
|
||||
let resolver = trait_.resolver(db);
|
||||
|
@ -249,6 +249,26 @@ impl Generics {
|
|||
self.parent_generics.as_ref().and_then(|g| g.find_param(param))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a Substitution that replaces each parameter by a bound variable.
|
||||
pub(crate) fn bound_vars_subst(&self, debruijn: DebruijnIndex) -> Substitution {
|
||||
Substitution::from_iter(
|
||||
&Interner,
|
||||
self.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns a Substitution that replaces each parameter by itself (i.e. `Ty::Param`).
|
||||
pub(crate) fn type_params_subst(&self, db: &dyn HirDatabase) -> Substitution {
|
||||
Substitution::from_iter(
|
||||
&Interner,
|
||||
self.iter().map(|(id, _)| {
|
||||
TyKind::Placeholder(crate::to_placeholder_idx(db, id)).intern(&Interner)
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option<GenericDefId> {
|
||||
|
|
Loading…
Reference in a new issue