Skip to content

Commit 047b531

Browse files
bors[bot]Veykril
andauthored
Merge #8359
8359: Add Lifetime to TyKind::Ref and DynTy r=flodiebold a=Veykril CC #8313 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 4bc8a01 + 08dc695 commit 047b531

File tree

13 files changed

+66
-42
lines changed

13 files changed

+66
-42
lines changed

crates/hir/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,9 +1888,10 @@ impl Type {
18881888
substs.iter(&Interner).filter_map(|a| a.ty(&Interner)).any(go)
18891889
}
18901890

1891-
TyKind::Array(ty) | TyKind::Slice(ty) | TyKind::Raw(_, ty) | TyKind::Ref(_, ty) => {
1892-
go(ty)
1893-
}
1891+
TyKind::Array(ty)
1892+
| TyKind::Slice(ty)
1893+
| TyKind::Raw(_, ty)
1894+
| TyKind::Ref(_, _, ty) => go(ty),
18941895

18951896
TyKind::Scalar(_)
18961897
| TyKind::Str
@@ -2148,7 +2149,10 @@ impl Type {
21482149
);
21492150
}
21502151

2151-
TyKind::Ref(_, ty) | TyKind::Raw(_, ty) | TyKind::Array(ty) | TyKind::Slice(ty) => {
2152+
TyKind::Ref(_, _, ty)
2153+
| TyKind::Raw(_, ty)
2154+
| TyKind::Array(ty)
2155+
| TyKind::Slice(ty) => {
21522156
walk_type(db, &type_.derived(ty.clone()), cb);
21532157
}
21542158

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
315315
if pat_ty == match_expr_ty
316316
|| match_expr_ty
317317
.as_reference()
318-
.map(|(match_expr_ty, _)| match_expr_ty == pat_ty)
318+
.map(|(match_expr_ty, ..)| match_expr_ty == pat_ty)
319319
.unwrap_or(false)
320320
{
321321
// If we had a NotUsefulMatchArm diagnostic, we could

crates/hir_ty/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl HirDisplay for Ty {
314314
t.hir_fmt(f)?;
315315
write!(f, "; _]")?;
316316
}
317-
TyKind::Raw(m, t) | TyKind::Ref(m, t) => {
317+
TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => {
318318
let ty_display =
319319
t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
320320

crates/hir_ty/src/infer/coerce.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'a> InferenceContext<'a> {
8181
// `&T` -> `*const T`
8282
// `&mut T` -> `*mut T`/`*const T`
8383
(TyKind::Ref(.., substs), &TyKind::Raw(m2 @ Mutability::Not, ..))
84-
| (TyKind::Ref(Mutability::Mut, substs), &TyKind::Raw(m2, ..)) => {
84+
| (TyKind::Ref(Mutability::Mut, _, substs), &TyKind::Raw(m2, ..)) => {
8585
from_ty = TyKind::Raw(m2, substs.clone()).intern(&Interner);
8686
}
8787

@@ -111,7 +111,9 @@ impl<'a> InferenceContext<'a> {
111111
// Auto Deref if cannot coerce
112112
match (from_ty.kind(&Interner), to_ty.kind(&Interner)) {
113113
// FIXME: DerefMut
114-
(TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => self.unify_autoderef_behind_ref(st1, st2),
114+
(TyKind::Ref(.., st1), TyKind::Ref(.., st2)) => {
115+
self.unify_autoderef_behind_ref(st1, st2)
116+
}
115117

116118
// Otherwise, normal unify
117119
_ => self.unify(&from_ty, to_ty),

crates/hir_ty/src/infer/expr.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
lower::lower_to_chalk_mutability,
2020
method_resolution, op,
2121
primitive::{self, UintTy},
22-
to_chalk_trait_id,
22+
static_lifetime, to_chalk_trait_id,
2323
traits::{chalk::from_chalk, FnTrait},
2424
utils::{generics, variant_data, Generics},
2525
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
@@ -527,7 +527,7 @@ impl<'a> InferenceContext<'a> {
527527
let inner_ty = self.infer_expr_inner(*expr, &expectation);
528528
match rawness {
529529
Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
530-
Rawness::Ref => TyKind::Ref(mutability, inner_ty),
530+
Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty),
531531
}
532532
.intern(&Interner)
533533
}
@@ -731,12 +731,13 @@ impl<'a> InferenceContext<'a> {
731731
Expr::Literal(lit) => match lit {
732732
Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
733733
Literal::String(..) => {
734-
TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner)
734+
TyKind::Ref(Mutability::Not, static_lifetime(), TyKind::Str.intern(&Interner))
735+
.intern(&Interner)
735736
}
736737
Literal::ByteString(..) => {
737738
let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
738739
let array_type = TyKind::Array(byte_type).intern(&Interner);
739-
TyKind::Ref(Mutability::Not, array_type).intern(&Interner)
740+
TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner)
740741
}
741742
Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
742743
Literal::Int(_v, ty) => match ty {
@@ -872,7 +873,9 @@ impl<'a> InferenceContext<'a> {
872873
// Apply autoref so the below unification works correctly
873874
// FIXME: return correct autorefs from lookup_method
874875
let actual_receiver_ty = match expected_receiver_ty.as_reference() {
875-
Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner),
876+
Some((_, lifetime, mutability)) => {
877+
TyKind::Ref(mutability, lifetime, derefed_receiver_ty).intern(&Interner)
878+
}
876879
_ => derefed_receiver_ty,
877880
};
878881
self.unify(&expected_receiver_ty, &actual_receiver_ty);

crates/hir_ty/src/infer/pat.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use hir_expand::name::Name;
1313

1414
use super::{BindingMode, Expectation, InferenceContext};
1515
use crate::{
16-
lower::lower_to_chalk_mutability, utils::variant_data, Interner, Substitution, Ty, TyBuilder,
17-
TyKind,
16+
lower::lower_to_chalk_mutability, static_lifetime, utils::variant_data, Interner, Substitution,
17+
Ty, TyBuilder, TyKind,
1818
};
1919

2020
impl<'a> InferenceContext<'a> {
@@ -104,7 +104,7 @@ impl<'a> InferenceContext<'a> {
104104
let body = Arc::clone(&self.body); // avoid borrow checker problem
105105

106106
if is_non_ref_pat(&body, pat) {
107-
while let Some((inner, mutability)) = expected.as_reference() {
107+
while let Some((inner, _lifetime, mutability)) = expected.as_reference() {
108108
expected = inner;
109109
default_bm = match default_bm {
110110
BindingMode::Move => BindingMode::Ref(mutability),
@@ -162,7 +162,7 @@ impl<'a> InferenceContext<'a> {
162162
Pat::Ref { pat, mutability } => {
163163
let mutability = lower_to_chalk_mutability(*mutability);
164164
let expectation = match expected.as_reference() {
165-
Some((inner_ty, exp_mut)) => {
165+
Some((inner_ty, _lifetime, exp_mut)) => {
166166
if mutability != exp_mut {
167167
// FIXME: emit type error?
168168
}
@@ -171,7 +171,7 @@ impl<'a> InferenceContext<'a> {
171171
_ => self.result.standard_types.unknown.clone(),
172172
};
173173
let subty = self.infer_pat(*pat, &expectation, default_bm);
174-
TyKind::Ref(mutability, subty).intern(&Interner)
174+
TyKind::Ref(mutability, static_lifetime(), subty).intern(&Interner)
175175
}
176176
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
177177
p.as_ref(),
@@ -204,7 +204,8 @@ impl<'a> InferenceContext<'a> {
204204

205205
let bound_ty = match mode {
206206
BindingMode::Ref(mutability) => {
207-
TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner)
207+
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone())
208+
.intern(&Interner)
208209
}
209210
BindingMode::Move => inner_ty.clone(),
210211
};

crates/hir_ty/src/infer/unify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl InferenceTable {
317317
| (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => {
318318
self.unify_substs(substs1, substs2, depth + 1)
319319
}
320-
(TyKind::Ref(_, ty1), TyKind::Ref(_, ty2))
320+
(TyKind::Ref(_, _, ty1), TyKind::Ref(_, _, ty2))
321321
| (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2))
322322
| (TyKind::Array(ty1), TyKind::Array(ty2))
323323
| (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1),

crates/hir_ty/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,16 @@ impl CallableSig {
165165
}
166166

167167
impl Ty {
168-
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
168+
pub fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {
169169
match self.kind(&Interner) {
170-
TyKind::Ref(mutability, ty) => Some((ty, *mutability)),
170+
TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)),
171171
_ => None,
172172
}
173173
}
174174

175175
pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
176176
match self.kind(&Interner) {
177-
TyKind::Ref(mutability, ty) => Some((ty, Rawness::Ref, *mutability)),
177+
TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)),
178178
TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)),
179179
_ => None,
180180
}
@@ -183,7 +183,7 @@ impl Ty {
183183
pub fn strip_references(&self) -> &Ty {
184184
let mut t: &Ty = self;
185185

186-
while let TyKind::Ref(_mutability, ty) = t.kind(&Interner) {
186+
while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) {
187187
t = ty;
188188
}
189189

@@ -495,3 +495,7 @@ pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
495495
pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
496496
salsa::InternKey::from_intern_id(id.0)
497497
}
498+
499+
pub fn static_lifetime() -> Lifetime {
500+
LifetimeData::Static.intern(&Interner)
501+
}

crates/hir_ty/src/lower.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use stdx::impl_from;
2727

2828
use crate::{
2929
db::HirDatabase,
30-
to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
30+
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
3131
traits::chalk::{Interner, ToChalk},
3232
utils::{
3333
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
@@ -174,7 +174,9 @@ impl<'a> TyLoweringContext<'a> {
174174
}
175175
TypeRef::Reference(inner, _, mutability) => {
176176
let inner_ty = self.lower_ty(inner);
177-
TyKind::Ref(lower_to_chalk_mutability(*mutability), inner_ty).intern(&Interner)
177+
let lifetime = static_lifetime();
178+
TyKind::Ref(lower_to_chalk_mutability(*mutability), lifetime, inner_ty)
179+
.intern(&Interner)
178180
}
179181
TypeRef::Placeholder => TyKind::Error.intern(&Interner),
180182
TypeRef::Fn(params, is_varargs) => {
@@ -198,7 +200,7 @@ impl<'a> TyLoweringContext<'a> {
198200
)
199201
});
200202
let bounds = crate::make_only_type_binders(1, bounds);
201-
TyKind::Dyn(DynTy { bounds }).intern(&Interner)
203+
TyKind::Dyn(DynTy { bounds, lifetime: static_lifetime() }).intern(&Interner)
202204
}
203205
TypeRef::ImplTrait(bounds) => {
204206
match self.impl_trait_mode {
@@ -390,6 +392,7 @@ impl<'a> TyLoweringContext<'a> {
390392
))),
391393
),
392394
),
395+
lifetime: static_lifetime(),
393396
};
394397
TyKind::Dyn(dyn_ty).intern(&Interner)
395398
};

crates/hir_ty/src/method_resolution.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
db::HirDatabase,
2020
from_foreign_def_id,
2121
primitive::{self, FloatTy, IntTy, UintTy},
22+
static_lifetime,
2223
utils::all_super_traits,
2324
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
2425
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyKind,
@@ -453,7 +454,8 @@ fn iterate_method_candidates_with_autoref(
453454
}
454455
let refed = Canonical {
455456
binders: deref_chain[0].binders.clone(),
456-
value: TyKind::Ref(Mutability::Not, deref_chain[0].value.clone()).intern(&Interner),
457+
value: TyKind::Ref(Mutability::Not, static_lifetime(), deref_chain[0].value.clone())
458+
.intern(&Interner),
457459
};
458460
if iterate_method_candidates_by_receiver(
459461
&refed,
@@ -470,7 +472,8 @@ fn iterate_method_candidates_with_autoref(
470472
}
471473
let ref_muted = Canonical {
472474
binders: deref_chain[0].binders.clone(),
473-
value: TyKind::Ref(Mutability::Mut, deref_chain[0].value.clone()).intern(&Interner),
475+
value: TyKind::Ref(Mutability::Mut, static_lifetime(), deref_chain[0].value.clone())
476+
.intern(&Interner),
474477
};
475478
if iterate_method_candidates_by_receiver(
476479
&ref_muted,

0 commit comments

Comments
 (0)