Skip to content

Commit 96756f1

Browse files
committed
Add Lifetime to TyKind::Ref
1 parent 4bc8a01 commit 96756f1

File tree

13 files changed

+74
-43
lines changed

13 files changed

+74
-43
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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
traits::{chalk::from_chalk, FnTrait},
2424
utils::{generics, variant_data, Generics},
2525
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
26-
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk,
26+
LifetimeData, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
2727
};
2828

2929
use super::{
@@ -527,7 +527,9 @@ 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 => {
531+
TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), inner_ty)
532+
}
531533
}
532534
.intern(&Interner)
533535
}
@@ -730,13 +732,17 @@ impl<'a> InferenceContext<'a> {
730732
}
731733
Expr::Literal(lit) => match lit {
732734
Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
733-
Literal::String(..) => {
734-
TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner)
735-
}
735+
Literal::String(..) => TyKind::Ref(
736+
Mutability::Not,
737+
LifetimeData::Static.intern(&Interner),
738+
TyKind::Str.intern(&Interner),
739+
)
740+
.intern(&Interner),
736741
Literal::ByteString(..) => {
737742
let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
738743
let array_type = TyKind::Array(byte_type).intern(&Interner);
739-
TyKind::Ref(Mutability::Not, array_type).intern(&Interner)
744+
TyKind::Ref(Mutability::Not, LifetimeData::Static.intern(&Interner), array_type)
745+
.intern(&Interner)
740746
}
741747
Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
742748
Literal::Int(_v, ty) => match ty {
@@ -872,7 +878,9 @@ impl<'a> InferenceContext<'a> {
872878
// Apply autoref so the below unification works correctly
873879
// FIXME: return correct autorefs from lookup_method
874880
let actual_receiver_ty = match expected_receiver_ty.as_reference() {
875-
Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner),
881+
Some((_, lifetime, mutability)) => {
882+
TyKind::Ref(mutability, lifetime, derefed_receiver_ty).intern(&Interner)
883+
}
876884
_ => derefed_receiver_ty,
877885
};
878886
self.unify(&expected_receiver_ty, &actual_receiver_ty);

crates/hir_ty/src/infer/pat.rs

Lines changed: 12 additions & 8 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, utils::variant_data, Interner, LifetimeData, 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,8 @@ 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, LifetimeData::Static.intern(&Interner), subty)
175+
.intern(&Interner)
175176
}
176177
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
177178
p.as_ref(),
@@ -203,9 +204,12 @@ impl<'a> InferenceContext<'a> {
203204
let inner_ty = self.insert_type_vars_shallow(inner_ty);
204205

205206
let bound_ty = match mode {
206-
BindingMode::Ref(mutability) => {
207-
TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner)
208-
}
207+
BindingMode::Ref(mutability) => TyKind::Ref(
208+
mutability,
209+
LifetimeData::Static.intern(&Interner),
210+
inner_ty.clone(),
211+
)
212+
.intern(&Interner),
209213
BindingMode::Move => inner_ty.clone(),
210214
};
211215
let bound_ty = self.resolve_ty_as_possible(bound_ty);

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: 4 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

crates/hir_ty/src/lower.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
variant_data, Generics,
3535
},
3636
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
37-
FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
37+
FnSubst, ImplTraitId, LifetimeData, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
3838
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
3939
TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause,
4040
};
@@ -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 = LifetimeData::Static.intern(&Interner);
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) => {

crates/hir_ty/src/method_resolution.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::{
2121
primitive::{self, FloatTy, IntTy, UintTy},
2222
utils::all_super_traits,
2323
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
24-
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyKind,
25-
TypeWalk,
24+
InEnvironment, Interner, LifetimeData, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder,
25+
TyKind, TypeWalk,
2626
};
2727

2828
/// This is used as a key for indexing impls.
@@ -453,7 +453,12 @@ fn iterate_method_candidates_with_autoref(
453453
}
454454
let refed = Canonical {
455455
binders: deref_chain[0].binders.clone(),
456-
value: TyKind::Ref(Mutability::Not, deref_chain[0].value.clone()).intern(&Interner),
456+
value: TyKind::Ref(
457+
Mutability::Not,
458+
LifetimeData::Static.intern(&Interner),
459+
deref_chain[0].value.clone(),
460+
)
461+
.intern(&Interner),
457462
};
458463
if iterate_method_candidates_by_receiver(
459464
&refed,
@@ -470,7 +475,12 @@ fn iterate_method_candidates_with_autoref(
470475
}
471476
let ref_muted = Canonical {
472477
binders: deref_chain[0].binders.clone(),
473-
value: TyKind::Ref(Mutability::Mut, deref_chain[0].value.clone()).intern(&Interner),
478+
value: TyKind::Ref(
479+
Mutability::Mut,
480+
LifetimeData::Static.intern(&Interner),
481+
deref_chain[0].value.clone(),
482+
)
483+
.intern(&Interner),
474484
};
475485
if iterate_method_candidates_by_receiver(
476486
&ref_muted,

0 commit comments

Comments
 (0)