mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
Add TyBuilder::adt
This commit is contained in:
parent
e6f007d9a8
commit
620769f322
4 changed files with 104 additions and 45 deletions
|
@ -539,17 +539,10 @@ impl<'a> InferenceContext<'a> {
|
|||
Expr::Box { expr } => {
|
||||
let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
|
||||
if let Some(box_) = self.resolve_boxed_box() {
|
||||
let mut sb =
|
||||
Substitution::build_for_generics(&generics(self.db.upcast(), box_.into()));
|
||||
sb = sb.push(inner_ty);
|
||||
match self.db.generic_defaults(box_.into()).get(1) {
|
||||
Some(alloc_ty) if !alloc_ty.value.is_unknown() && sb.remaining() > 0 => {
|
||||
sb = sb.push(alloc_ty.value.clone());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
sb = sb.fill(repeat_with(|| self.table.new_type_var()));
|
||||
Ty::adt_ty(box_, sb.build())
|
||||
TyBuilder::adt(self.db, box_)
|
||||
.push(inner_ty)
|
||||
.fill_with_defaults(self.db, || self.table.new_type_var())
|
||||
.build()
|
||||
} else {
|
||||
self.err_ty()
|
||||
}
|
||||
|
@ -639,31 +632,31 @@ impl<'a> InferenceContext<'a> {
|
|||
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
|
||||
match (range_type, lhs_ty, rhs_ty) {
|
||||
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
|
||||
Some(adt) => Ty::adt_ty(adt, Substitution::empty(&Interner)),
|
||||
Some(adt) => TyBuilder::adt(self.db, adt).build(),
|
||||
None => self.err_ty(),
|
||||
},
|
||||
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
|
||||
Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)),
|
||||
Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(),
|
||||
None => self.err_ty(),
|
||||
},
|
||||
(RangeOp::Inclusive, None, Some(ty)) => {
|
||||
match self.resolve_range_to_inclusive() {
|
||||
Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)),
|
||||
Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(),
|
||||
None => self.err_ty(),
|
||||
}
|
||||
}
|
||||
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
|
||||
Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)),
|
||||
Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(),
|
||||
None => self.err_ty(),
|
||||
},
|
||||
(RangeOp::Inclusive, Some(_), Some(ty)) => {
|
||||
match self.resolve_range_inclusive() {
|
||||
Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)),
|
||||
Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(),
|
||||
None => self.err_ty(),
|
||||
}
|
||||
}
|
||||
(RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
|
||||
Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)),
|
||||
Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(),
|
||||
None => self.err_ty(),
|
||||
},
|
||||
(RangeOp::Inclusive, _, None) => self.err_ty(),
|
||||
|
|
|
@ -13,9 +13,8 @@ use hir_expand::name::Name;
|
|||
|
||||
use super::{BindingMode, Expectation, InferenceContext};
|
||||
use crate::{
|
||||
lower::lower_to_chalk_mutability,
|
||||
utils::{generics, variant_data},
|
||||
Interner, Substitution, Ty, TyKind,
|
||||
lower::lower_to_chalk_mutability, utils::variant_data, Interner, Substitution, Ty, TyBuilder,
|
||||
TyKind,
|
||||
};
|
||||
|
||||
impl<'a> InferenceContext<'a> {
|
||||
|
@ -246,23 +245,12 @@ impl<'a> InferenceContext<'a> {
|
|||
};
|
||||
|
||||
let inner_ty = self.infer_pat(*inner, &inner_ty, default_bm);
|
||||
let mut sb = Substitution::build_for_generics(&generics(
|
||||
self.db.upcast(),
|
||||
box_adt.into(),
|
||||
));
|
||||
sb = sb.push(inner_ty);
|
||||
if sb.remaining() == 1 {
|
||||
sb = sb.push(match alloc_ty {
|
||||
Some(alloc_ty) if !alloc_ty.is_unknown() => alloc_ty,
|
||||
_ => match self.db.generic_defaults(box_adt.into()).get(1) {
|
||||
Some(alloc_ty) if !alloc_ty.value.is_unknown() => {
|
||||
alloc_ty.value.clone()
|
||||
}
|
||||
_ => self.table.new_type_var(),
|
||||
},
|
||||
});
|
||||
let mut b = TyBuilder::adt(self.db, box_adt).push(inner_ty);
|
||||
|
||||
if let Some(alloc_ty) = alloc_ty {
|
||||
b = b.push(alloc_ty);
|
||||
}
|
||||
Ty::adt_ty(box_adt, sb.build())
|
||||
b.fill_with_defaults(self.db, || self.table.new_type_var()).build()
|
||||
}
|
||||
None => self.err_ty(),
|
||||
},
|
||||
|
|
|
@ -812,9 +812,59 @@ impl TypeWalk for CallableSig {
|
|||
}
|
||||
}
|
||||
|
||||
struct TyBuilder {}
|
||||
pub struct TyBuilder<D> {
|
||||
data: D,
|
||||
vec: SmallVec<[GenericArg; 2]>,
|
||||
param_count: usize,
|
||||
}
|
||||
|
||||
impl TyBuilder {
|
||||
impl<D> TyBuilder<D> {
|
||||
fn new(data: D, param_count: usize) -> TyBuilder<D> {
|
||||
TyBuilder { data, param_count, vec: SmallVec::with_capacity(param_count) }
|
||||
}
|
||||
|
||||
fn build_internal(self) -> (D, Substitution) {
|
||||
assert_eq!(self.vec.len(), self.param_count);
|
||||
// FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form
|
||||
let subst = Substitution(self.vec);
|
||||
(self.data, subst)
|
||||
}
|
||||
|
||||
pub fn push(mut self, arg: impl CastTo<GenericArg>) -> Self {
|
||||
self.vec.push(arg.cast(&Interner));
|
||||
self
|
||||
}
|
||||
|
||||
fn remaining(&self) -> usize {
|
||||
self.param_count - self.vec.len()
|
||||
}
|
||||
|
||||
pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
|
||||
self.fill(
|
||||
(starting_from..)
|
||||
.map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn fill_with_unknown(self) -> Self {
|
||||
self.fill(iter::repeat(TyKind::Unknown.intern(&Interner)))
|
||||
}
|
||||
|
||||
pub fn fill(mut self, filler: impl Iterator<Item = impl CastTo<GenericArg>>) -> Self {
|
||||
self.vec.extend(filler.take(self.remaining()).casted(&Interner));
|
||||
assert_eq!(self.remaining(), 0);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self {
|
||||
assert!(self.vec.is_empty());
|
||||
assert!(parent_substs.len(&Interner) <= self.param_count);
|
||||
self.vec.extend(parent_substs.iter(&Interner).cloned());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl TyBuilder<()> {
|
||||
pub fn unit() -> Ty {
|
||||
TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner)
|
||||
}
|
||||
|
@ -829,11 +879,38 @@ impl TyBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
impl Ty {
|
||||
pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty {
|
||||
TyKind::Adt(AdtId(adt), substs).intern(&Interner)
|
||||
impl TyBuilder<hir_def::AdtId> {
|
||||
pub fn adt(db: &dyn HirDatabase, adt: hir_def::AdtId) -> TyBuilder<hir_def::AdtId> {
|
||||
let generics = generics(db.upcast(), adt.into());
|
||||
let param_count = generics.len();
|
||||
TyBuilder::new(adt, param_count)
|
||||
}
|
||||
|
||||
pub fn fill_with_defaults(
|
||||
mut self,
|
||||
db: &dyn HirDatabase,
|
||||
mut fallback: impl FnMut() -> Ty,
|
||||
) -> Self {
|
||||
let defaults = db.generic_defaults(self.data.into());
|
||||
for default_ty in defaults.iter().skip(self.vec.len()) {
|
||||
if default_ty.skip_binders().is_unknown() {
|
||||
self.vec.push(fallback().cast(&Interner));
|
||||
} else {
|
||||
// each default can depend on the previous parameters
|
||||
let subst_so_far = Substitution(self.vec.clone());
|
||||
self.vec.push(default_ty.clone().subst(&subst_so_far).cast(&Interner));
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Ty {
|
||||
let (adt, subst) = self.build_internal();
|
||||
TyKind::Adt(AdtId(adt), subst).intern(&Interner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ty {
|
||||
pub fn builtin(builtin: BuiltinType) -> Self {
|
||||
match builtin {
|
||||
BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner),
|
||||
|
|
|
@ -36,7 +36,7 @@ use crate::{
|
|||
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
|
||||
ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
|
||||
ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty,
|
||||
TyKind, TypeWalk, WhereClause,
|
||||
TyBuilder, TyKind, TypeWalk, WhereClause,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -1141,9 +1141,10 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
|
|||
}
|
||||
|
||||
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
|
||||
let generics = generics(db.upcast(), adt.into());
|
||||
let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(&Interner), Ty::adt_ty(adt, substs))
|
||||
let b = TyBuilder::adt(db, adt);
|
||||
let num_binders = b.remaining();
|
||||
let ty = b.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build();
|
||||
Binders::new(num_binders, ty)
|
||||
}
|
||||
|
||||
fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
|
||||
|
|
Loading…
Reference in a new issue