Substitution::single -> from1

This commit is contained in:
Florian Diebold 2021-04-05 21:17:35 +02:00
parent 788533d380
commit 2f5a77658b
4 changed files with 17 additions and 14 deletions

View file

@ -180,7 +180,8 @@ impl<'a> InferenceContext<'a> {
let inner_ty = self.infer_expr(*body, &Expectation::none()); let inner_ty = self.infer_expr(*body, &Expectation::none());
let impl_trait_id = crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body); let impl_trait_id = crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body);
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
TyKind::OpaqueType(opaque_ty_id, Substitution::single(inner_ty)).intern(&Interner) TyKind::OpaqueType(opaque_ty_id, Substitution::from1(&Interner, inner_ty))
.intern(&Interner)
} }
Expr::Loop { body, label } => { Expr::Loop { body, label } => {
self.breakables.push(BreakableContext { self.breakables.push(BreakableContext {
@ -266,7 +267,8 @@ impl<'a> InferenceContext<'a> {
.intern(&Interner); .intern(&Interner);
let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into(); let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into();
let closure_ty = let closure_ty =
TyKind::Closure(closure_id, Substitution::single(sig_ty)).intern(&Interner); TyKind::Closure(closure_id, Substitution::from1(&Interner, sig_ty))
.intern(&Interner);
// Eagerly try to relate the closure type with the expected // Eagerly try to relate the closure type with the expected
// type, otherwise we often won't have enough information to // type, otherwise we often won't have enough information to

View file

@ -31,7 +31,6 @@ mod test_db;
use std::sync::Arc; use std::sync::Arc;
use itertools::Itertools; use itertools::Itertools;
use smallvec::SmallVec;
use base_db::salsa; use base_db::salsa;
use hir_def::{ use hir_def::{
@ -77,14 +76,6 @@ pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
pub type FnSig = chalk_ir::FnSig<Interner>; pub type FnSig = chalk_ir::FnSig<Interner>;
impl Substitution { impl Substitution {
pub fn single(ty: Ty) -> Substitution {
Substitution::intern({
let mut v = SmallVec::new();
v.push(ty.cast(&Interner));
v
})
}
pub fn prefix(&self, n: usize) -> Substitution { pub fn prefix(&self, n: usize) -> Substitution {
Substitution::intern(self.interned()[..std::cmp::min(self.len(&Interner), n)].into()) Substitution::intern(self.interned()[..std::cmp::min(self.len(&Interner), n)].into())
} }

View file

@ -220,7 +220,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
let impl_bound = WhereClause::Implemented(TraitRef { let impl_bound = WhereClause::Implemented(TraitRef {
trait_id: to_chalk_trait_id(future_trait), trait_id: to_chalk_trait_id(future_trait),
// Self type as the first parameter. // Self type as the first parameter.
substitution: Substitution::single( substitution: Substitution::from1(
&Interner,
TyKind::BoundVar(BoundVar { TyKind::BoundVar(BoundVar {
debruijn: DebruijnIndex::INNERMOST, debruijn: DebruijnIndex::INNERMOST,
index: 0, index: 0,
@ -232,7 +233,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
alias: AliasTy::Projection(ProjectionTy { alias: AliasTy::Projection(ProjectionTy {
associated_ty_id: to_assoc_type_id(future_output), associated_ty_id: to_assoc_type_id(future_output),
// Self type as the first parameter. // Self type as the first parameter.
substitution: Substitution::single( substitution: Substitution::from1(
&Interner,
TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
.intern(&Interner), .intern(&Interner),
), ),

View file

@ -4,7 +4,7 @@
use std::sync::Arc; use std::sync::Arc;
use chalk_ir::{ use chalk_ir::{
cast::{CastTo, Caster}, cast::{Cast, CastTo, Caster},
BoundVar, Mutability, Scalar, TyVariableKind, BoundVar, Mutability, Scalar, TyVariableKind,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
@ -278,6 +278,14 @@ impl Substitution {
self.0.iter() self.0.iter()
} }
pub fn from1(_interner: &Interner, ty: Ty) -> Substitution {
Substitution::intern({
let mut v = SmallVec::new();
v.push(ty.cast(&Interner));
v
})
}
pub fn from_iter( pub fn from_iter(
interner: &Interner, interner: &Interner,
elements: impl IntoIterator<Item = impl CastTo<GenericArg>>, elements: impl IntoIterator<Item = impl CastTo<GenericArg>>,