Skip to content

Commit b357bca

Browse files
committed
modify insert_type_vars for lifetimes
1 parent 13301e7 commit b357bca

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

crates/hir-ty/src/infer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use hir_def::{
4242
layout::Integer,
4343
path::{ModPath, Path},
4444
resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs},
45-
type_ref::TypeRef,
45+
type_ref::{LifetimeRef, TypeRef},
4646
AdtId, AssocItemId, DefWithBodyId, FieldId, FunctionId, ItemContainerId, Lookup, TraitId,
4747
TupleFieldId, TupleId, TypeAliasId, VariantId,
4848
};
@@ -1037,6 +1037,12 @@ impl<'a> InferenceContext<'a> {
10371037
self.result.standard_types.unknown.clone()
10381038
}
10391039

1040+
fn make_lifetime(&mut self, lifetime_ref: &LifetimeRef) -> Lifetime {
1041+
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver, self.owner.into());
1042+
let lt = ctx.lower_lifetime(lifetime_ref);
1043+
self.insert_type_vars(lt)
1044+
}
1045+
10401046
/// Replaces `Ty::Error` by a new type var, so we can maybe still infer it.
10411047
fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
10421048
self.table.insert_type_vars_shallow(ty)

crates/hir-ty/src/infer/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,8 +1855,7 @@ impl InferenceContext<'_> {
18551855
DebruijnIndex::INNERMOST,
18561856
)
18571857
},
1858-
// FIXME: create make_lifetimes and infer lifetimes
1859-
|_, _| static_lifetime(),
1858+
|this, lt_ref| this.make_lifetime(lt_ref),
18601859
) {
18611860
substs.push(g);
18621861
}

crates/hir-ty/src/infer/unify.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use triomphe::Arc;
1616

1717
use super::{InferOk, InferResult, InferenceContext, TypeError};
1818
use crate::{
19-
consteval::unknown_const, db::HirDatabase, fold_tys_and_consts, static_lifetime,
20-
to_chalk_trait_id, traits::FnTrait, AliasEq, AliasTy, BoundVar, Canonical, Const, ConstValue,
21-
DebruijnIndex, DomainGoal, GenericArg, GenericArgData, Goal, GoalData, Guidance, InEnvironment,
22-
InferenceVar, Interner, Lifetime, OpaqueTyId, ParamKind, ProjectionTy, ProjectionTyExt, Scalar,
23-
Solution, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind, VariableKind,
24-
WhereClause,
19+
consteval::unknown_const, db::HirDatabase, fold_generic_args, fold_tys_and_consts,
20+
static_lifetime, to_chalk_trait_id, traits::FnTrait, AliasEq, AliasTy, BoundVar, Canonical,
21+
Const, ConstValue, DebruijnIndex, DomainGoal, GenericArg, GenericArgData, Goal, GoalData,
22+
Guidance, InEnvironment, InferenceVar, Interner, Lifetime, OpaqueTyId, ParamKind, ProjectionTy,
23+
ProjectionTyExt, Scalar, Solution, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
24+
TyKind, VariableKind, WhereClause,
2525
};
2626

2727
impl InferenceContext<'_> {
@@ -862,11 +862,16 @@ impl<'a> InferenceTable<'a> {
862862
where
863863
T: HasInterner<Interner = Interner> + TypeFoldable<Interner>,
864864
{
865-
fold_tys_and_consts(
865+
fold_generic_args(
866866
ty,
867-
|it, _| match it {
868-
Either::Left(ty) => Either::Left(self.insert_type_vars_shallow(ty)),
869-
Either::Right(c) => Either::Right(self.insert_const_vars_shallow(c)),
867+
|arg, _| match arg {
868+
GenericArgData::Ty(ty) => GenericArgData::Ty(self.insert_type_vars_shallow(ty)),
869+
// FIXME: insert lifetime vars once LifetimeData::InferenceVar
870+
// and specific error variant for lifetimes start being constructed
871+
GenericArgData::Lifetime(lt) => GenericArgData::Lifetime(lt),
872+
GenericArgData::Const(c) => {
873+
GenericArgData::Const(self.insert_const_vars_shallow(c))
874+
}
870875
},
871876
DebruijnIndex::INNERMOST,
872877
)

crates/hir-ty/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,55 @@ pub(crate) fn fold_tys_and_consts<T: HasInterner<Interner = Interner> + TypeFold
716716
t.fold_with(&mut TyFolder(f), binders)
717717
}
718718

719+
pub(crate) fn fold_generic_args<T: HasInterner<Interner = Interner> + TypeFoldable<Interner>>(
720+
t: T,
721+
f: impl FnMut(GenericArgData, DebruijnIndex) -> GenericArgData,
722+
binders: DebruijnIndex,
723+
) -> T {
724+
use chalk_ir::fold::{TypeFolder, TypeSuperFoldable};
725+
#[derive(chalk_derive::FallibleTypeFolder)]
726+
#[has_interner(Interner)]
727+
struct TyFolder<F: FnMut(GenericArgData, DebruijnIndex) -> GenericArgData>(F);
728+
impl<F: FnMut(GenericArgData, DebruijnIndex) -> GenericArgData> TypeFolder<Interner>
729+
for TyFolder<F>
730+
{
731+
fn as_dyn(&mut self) -> &mut dyn TypeFolder<Interner> {
732+
self
733+
}
734+
735+
fn interner(&self) -> Interner {
736+
Interner
737+
}
738+
739+
fn fold_ty(&mut self, ty: Ty, outer_binder: DebruijnIndex) -> Ty {
740+
let ty = ty.super_fold_with(self.as_dyn(), outer_binder);
741+
self.0(GenericArgData::Ty(ty), outer_binder)
742+
.intern(Interner)
743+
.ty(Interner)
744+
.unwrap()
745+
.clone()
746+
}
747+
748+
fn fold_const(&mut self, c: Const, outer_binder: DebruijnIndex) -> Const {
749+
self.0(GenericArgData::Const(c), outer_binder)
750+
.intern(Interner)
751+
.constant(Interner)
752+
.unwrap()
753+
.clone()
754+
}
755+
756+
fn fold_lifetime(&mut self, lt: Lifetime, outer_binder: DebruijnIndex) -> Lifetime {
757+
let lt = lt.super_fold_with(self.as_dyn(), outer_binder);
758+
self.0(GenericArgData::Lifetime(lt), outer_binder)
759+
.intern(Interner)
760+
.lifetime(Interner)
761+
.unwrap()
762+
.clone()
763+
}
764+
}
765+
t.fold_with(&mut TyFolder(f), binders)
766+
}
767+
719768
/// 'Canonicalizes' the `t` by replacing any errors with new variables. Also
720769
/// ensures there are no unbound variables or inference variables anywhere in
721770
/// the `t`.

crates/hir-ty/src/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ impl<'a> TyLoweringContext<'a> {
13621362
ImplTrait { bounds: crate::make_single_type_binders(predicates) }
13631363
}
13641364

1365-
fn lower_lifetime(&self, lifetime: &LifetimeRef) -> Lifetime {
1365+
pub fn lower_lifetime(&self, lifetime: &LifetimeRef) -> Lifetime {
13661366
match self.resolver.resolve_lifetime(lifetime) {
13671367
Some(resolution) => match resolution {
13681368
LifetimeNs::Static => static_lifetime(),

0 commit comments

Comments
 (0)