Skip to content

Commit 0927f86

Browse files
committed
Replace static_lifetime usages with error_lifetime, lower outlives goals
1 parent 3691380 commit 0927f86

File tree

15 files changed

+139
-98
lines changed

15 files changed

+139
-98
lines changed

crates/hir-def/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ impl ModuleId {
422422
}
423423
}
424424

425+
pub fn crate_def_map(self, db: &dyn DefDatabase) -> Arc<DefMap> {
426+
db.crate_def_map(self.krate)
427+
}
428+
425429
pub fn krate(self) -> CrateId {
426430
self.krate
427431
}
@@ -438,6 +442,8 @@ impl ModuleId {
438442
})
439443
}
440444

445+
/// Returns the module containing `self`, either the parent `mod`, or the module (or block) containing
446+
/// the block, if `self` corresponds to a block expression.
441447
pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
442448
self.def_map(db).containing_module(self.local_id)
443449
}
@@ -929,6 +935,18 @@ impl GenericDefId {
929935
GenericDefId::EnumVariantId(_) => (FileId::BOGUS.into(), None),
930936
}
931937
}
938+
939+
pub fn assoc_trait_container(self, db: &dyn DefDatabase) -> Option<TraitId> {
940+
match match self {
941+
GenericDefId::FunctionId(f) => f.lookup(db).container,
942+
GenericDefId::TypeAliasId(t) => t.lookup(db).container,
943+
GenericDefId::ConstId(c) => c.lookup(db).container,
944+
_ => return None,
945+
} {
946+
ItemContainerId::TraitId(trait_) => Some(trait_),
947+
_ => None,
948+
}
949+
}
932950
}
933951

934952
impl From<AssocItemId> for GenericDefId {

crates/hir-ty/src/chalk_ext.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Various extensions traits for Chalk types.
22
3-
use chalk_ir::{cast::Cast, FloatTy, IntTy, Mutability, Scalar, TyVariableKind, UintTy};
3+
use chalk_ir::{
4+
cast::Cast, FloatTy, IntTy, Mutability, Scalar, TyVariableKind, TypeOutlives, UintTy,
5+
};
46
use hir_def::{
57
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
68
generics::TypeOrConstParamData,
@@ -312,14 +314,17 @@ impl TyExt for Ty {
312314
.generic_predicates(id.parent)
313315
.iter()
314316
.map(|pred| pred.clone().substitute(Interner, &substs))
315-
.filter(|wc| match &wc.skip_binders() {
317+
.filter(|wc| match wc.skip_binders() {
316318
WhereClause::Implemented(tr) => {
317319
&tr.self_type_parameter(Interner) == self
318320
}
319321
WhereClause::AliasEq(AliasEq {
320322
alias: AliasTy::Projection(proj),
321323
ty: _,
322324
}) => &proj.self_type_parameter(db) == self,
325+
WhereClause::TypeOutlives(TypeOutlives { ty, lifetime: _ }) => {
326+
ty == self
327+
}
323328
_ => false,
324329
})
325330
.collect::<Vec<_>>();

crates/hir-ty/src/infer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ use triomphe::Arc;
5555

5656
use crate::{
5757
db::HirDatabase,
58-
fold_tys,
58+
error_lifetime, fold_tys,
5959
infer::{coerce::CoerceMany, unify::InferenceTable},
6060
lower::ImplTraitLoweringMode,
61-
static_lifetime, to_assoc_type_id,
61+
to_assoc_type_id,
6262
traits::FnTrait,
6363
utils::{InTypeConstIdMetadata, UnevaluatedConstEvaluatorFolder},
6464
AliasEq, AliasTy, Binders, ClosureId, Const, DomainGoal, GenericArg, Goal, ImplTraitId,
@@ -326,7 +326,7 @@ pub struct Adjustment {
326326

327327
impl Adjustment {
328328
pub fn borrow(m: Mutability, ty: Ty) -> Self {
329-
let ty = TyKind::Ref(m, static_lifetime(), ty).intern(Interner);
329+
let ty = TyKind::Ref(m, error_lifetime(), ty).intern(Interner);
330330
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(m)), target: ty }
331331
}
332332
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use stdx::never;
2222

2323
use crate::{
2424
db::{HirDatabase, InternedClosure},
25-
from_chalk_trait_id, from_placeholder_idx, make_binders,
25+
error_lifetime, from_chalk_trait_id, from_placeholder_idx, make_binders,
2626
mir::{BorrowKind, MirSpan, MutBorrowKind, ProjectionElem},
27-
static_lifetime, to_chalk_trait_id,
27+
to_chalk_trait_id,
2828
traits::FnTrait,
2929
utils::{self, elaborate_clause_supertraits, generics, Generics},
3030
Adjust, Adjustment, AliasEq, AliasTy, Binders, BindingMode, ChalkTraitId, ClosureId, DynTy,
@@ -324,7 +324,7 @@ impl CapturedItemWithoutTy {
324324
BorrowKind::Mut { .. } => Mutability::Mut,
325325
_ => Mutability::Not,
326326
};
327-
TyKind::Ref(m, static_lifetime(), ty).intern(Interner)
327+
TyKind::Ref(m, error_lifetime(), ty).intern(Interner)
328328
}
329329
};
330330
return CapturedItem {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use triomphe::Arc;
1818
use crate::{
1919
autoderef::{Autoderef, AutoderefKind},
2020
db::HirDatabase,
21+
error_lifetime,
2122
infer::{
2223
Adjust, Adjustment, AutoBorrow, InferOk, InferenceContext, OverloadedDeref, PointerCast,
2324
TypeError, TypeMismatch,
2425
},
25-
static_lifetime,
2626
utils::ClosureSubst,
2727
Canonical, DomainGoal, FnAbi, FnPointer, FnSig, Guidance, InEnvironment, Interner, Solution,
2828
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
@@ -427,7 +427,7 @@ impl InferenceTable<'_> {
427427
// compare those. Note that this means we use the target
428428
// mutability [1], since it may be that we are coercing
429429
// from `&mut T` to `&U`.
430-
let lt = static_lifetime(); // FIXME: handle lifetimes correctly, see rustc
430+
let lt = error_lifetime(); // FIXME: handle lifetimes correctly, see rustc
431431
let derefd_from_ty = TyKind::Ref(to_mt, lt, referent_ty).intern(Interner);
432432
match autoderef.table.try_unify(&derefd_from_ty, to_ty) {
433433
Ok(result) => {
@@ -621,7 +621,7 @@ impl InferenceTable<'_> {
621621
(TyKind::Ref(from_mt, _, from_inner), &TyKind::Ref(to_mt, _, _)) => {
622622
coerce_mutabilities(*from_mt, to_mt)?;
623623

624-
let lt = static_lifetime();
624+
let lt = error_lifetime();
625625
Some((
626626
Adjustment { kind: Adjust::Deref(None), target: from_inner.clone() },
627627
Adjustment {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{
2323
autoderef::{builtin_deref, deref_by_trait, Autoderef},
2424
consteval,
2525
db::{InternedClosure, InternedCoroutine},
26+
error_lifetime,
2627
infer::{
2728
coerce::{CoerceMany, CoercionCause},
2829
find_continuable,
@@ -630,7 +631,7 @@ impl InferenceContext<'_> {
630631
let inner_ty = self.infer_expr_inner(*expr, &expectation);
631632
match rawness {
632633
Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
633-
Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty),
634+
Rawness::Ref => TyKind::Ref(mutability, error_lifetime(), inner_ty),
634635
}
635636
.intern(Interner)
636637
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use hir_expand::name::Name;
1212

1313
use crate::{
1414
consteval::{try_const_usize, usize_const},
15+
error_lifetime,
1516
infer::{BindingMode, Expectation, InferenceContext, TypeMismatch},
1617
lower::lower_to_chalk_mutability,
1718
primitive::UintTy,
@@ -396,14 +397,14 @@ impl InferenceContext<'_> {
396397
None => {
397398
let inner_ty = self.table.new_type_var();
398399
let ref_ty =
399-
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone()).intern(Interner);
400+
TyKind::Ref(mutability, error_lifetime(), inner_ty.clone()).intern(Interner);
400401
// Unification failure will be reported by the caller.
401402
self.unify(&ref_ty, expected);
402403
inner_ty
403404
}
404405
};
405406
let subty = self.infer_pat(inner_pat, &expectation, default_bm);
406-
TyKind::Ref(mutability, static_lifetime(), subty).intern(Interner)
407+
TyKind::Ref(mutability, error_lifetime(), subty).intern(Interner)
407408
}
408409

409410
fn infer_bind_pat(
@@ -430,7 +431,7 @@ impl InferenceContext<'_> {
430431

431432
let bound_ty = match mode {
432433
BindingMode::Ref(mutability) => {
433-
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone()).intern(Interner)
434+
TyKind::Ref(mutability, error_lifetime(), inner_ty.clone()).intern(Interner)
434435
}
435436
BindingMode::Move => inner_ty.clone(),
436437
};

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

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use triomphe::Arc;
1616

1717
use super::{InferOk, InferResult, InferenceContext, TypeError};
1818
use crate::{
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,
19+
consteval::unknown_const, db::HirDatabase, error_lifetime, fold_generic_args,
20+
fold_tys_and_consts, to_chalk_trait_id, traits::FnTrait, AliasEq, AliasTy, BoundVar, Canonical,
2121
Const, ConstValue, DebruijnIndex, DomainGoal, GenericArg, GenericArgData, Goal, GoalData,
2222
Guidance, InEnvironment, InferenceVar, Interner, Lifetime, OpaqueTyId, ParamKind, ProjectionTy,
2323
ProjectionTyExt, Scalar, Solution, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
@@ -43,40 +43,21 @@ impl InferenceContext<'_> {
4343
let obligations = pending_obligations
4444
.iter()
4545
.filter_map(|obligation| match obligation.value.value.goal.data(Interner) {
46-
GoalData::DomainGoal(DomainGoal::Holds(
47-
clause @ WhereClause::AliasEq(AliasEq {
48-
alias: AliasTy::Projection(projection),
49-
..
50-
}),
51-
)) => {
52-
let projection_self = projection.self_type_parameter(self.db);
53-
let uncanonical = chalk_ir::Substitute::apply(
54-
&obligation.free_vars,
55-
projection_self,
56-
Interner,
57-
);
58-
if matches!(
59-
self.resolve_ty_shallow(&uncanonical).kind(Interner),
60-
TyKind::InferenceVar(iv, TyVariableKind::General) if *iv == root,
61-
) {
62-
Some(chalk_ir::Substitute::apply(
63-
&obligation.free_vars,
64-
clause.clone(),
65-
Interner,
66-
))
67-
} else {
68-
None
69-
}
70-
}
71-
GoalData::DomainGoal(DomainGoal::Holds(
72-
clause @ WhereClause::Implemented(trait_ref),
73-
)) => {
74-
let trait_ref_self = trait_ref.self_type_parameter(Interner);
75-
let uncanonical = chalk_ir::Substitute::apply(
76-
&obligation.free_vars,
77-
trait_ref_self,
78-
Interner,
79-
);
46+
GoalData::DomainGoal(DomainGoal::Holds(clause)) => {
47+
let ty = match clause {
48+
WhereClause::AliasEq(AliasEq {
49+
alias: AliasTy::Projection(projection),
50+
..
51+
}) => projection.self_type_parameter(self.db),
52+
WhereClause::Implemented(trait_ref) => {
53+
trait_ref.self_type_parameter(Interner)
54+
}
55+
WhereClause::TypeOutlives(to) => to.ty.clone(),
56+
_ => return None,
57+
};
58+
59+
let uncanonical =
60+
chalk_ir::Substitute::apply(&obligation.free_vars, ty, Interner);
8061
if matches!(
8162
self.resolve_ty_shallow(&uncanonical).kind(Interner),
8263
TyKind::InferenceVar(iv, TyVariableKind::General) if *iv == root,
@@ -121,8 +102,9 @@ impl<T: HasInterner<Interner = Interner>> Canonicalized<T> {
121102
VariableKind::Ty(TyVariableKind::General) => ctx.new_type_var().cast(Interner),
122103
VariableKind::Ty(TyVariableKind::Integer) => ctx.new_integer_var().cast(Interner),
123104
VariableKind::Ty(TyVariableKind::Float) => ctx.new_float_var().cast(Interner),
124-
// Chalk can sometimes return new lifetime variables. We just use the static lifetime everywhere
125-
VariableKind::Lifetime => static_lifetime().cast(Interner),
105+
// Chalk can sometimes return new lifetime variables. We just replace them by errors
106+
// for now.
107+
VariableKind::Lifetime => error_lifetime().cast(Interner),
126108
VariableKind::Const(ty) => ctx.new_const_var(ty.clone()).cast(Interner),
127109
}),
128110
);
@@ -1020,11 +1002,11 @@ mod resolve {
10201002
_var: InferenceVar,
10211003
_outer_binder: DebruijnIndex,
10221004
) -> Lifetime {
1023-
// fall back all lifetimes to 'static -- currently we don't deal
1005+
// fall back all lifetimes to 'error -- currently we don't deal
10241006
// with any lifetimes, but we can sometimes get some lifetime
10251007
// variables through Chalk's unification, and this at least makes
10261008
// sure we don't leak them outside of inference
1027-
crate::static_lifetime()
1009+
crate::error_lifetime()
10281010
}
10291011
}
10301012
}

crates/hir-ty/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ pub fn static_lifetime() -> Lifetime {
622622
}
623623

624624
pub fn error_lifetime() -> Lifetime {
625-
static_lifetime()
625+
LifetimeData::Static.intern(Interner)
626626
}
627627

628628
pub(crate) fn fold_free_vars<T: HasInterner<Interner = Interner> + TypeFoldable<Interner>>(
@@ -861,7 +861,7 @@ where
861861
if cfg!(debug_assertions) {
862862
Err(NoSolution)
863863
} else {
864-
Ok(static_lifetime())
864+
Ok(error_lifetime())
865865
}
866866
}
867867

@@ -873,7 +873,7 @@ where
873873
if cfg!(debug_assertions) {
874874
Err(NoSolution)
875875
} else {
876-
Ok(static_lifetime())
876+
Ok(error_lifetime())
877877
}
878878
}
879879
}

0 commit comments

Comments
 (0)