Skip to content

Commit 08dc695

Browse files
committed
Use a constructor function for Static lifetimes
1 parent b98c681 commit 08dc695

File tree

6 files changed

+37
-49
lines changed

6 files changed

+37
-49
lines changed

crates/hir_ty/src/infer/expr.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ 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,
26-
LifetimeData, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
26+
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk,
2727
};
2828

2929
use super::{
@@ -527,9 +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 => {
531-
TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), inner_ty)
532-
}
530+
Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty),
533531
}
534532
.intern(&Interner)
535533
}
@@ -732,17 +730,14 @@ impl<'a> InferenceContext<'a> {
732730
}
733731
Expr::Literal(lit) => match lit {
734732
Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
735-
Literal::String(..) => TyKind::Ref(
736-
Mutability::Not,
737-
LifetimeData::Static.intern(&Interner),
738-
TyKind::Str.intern(&Interner),
739-
)
740-
.intern(&Interner),
733+
Literal::String(..) => {
734+
TyKind::Ref(Mutability::Not, static_lifetime(), TyKind::Str.intern(&Interner))
735+
.intern(&Interner)
736+
}
741737
Literal::ByteString(..) => {
742738
let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
743739
let array_type = TyKind::Array(byte_type).intern(&Interner);
744-
TyKind::Ref(Mutability::Not, LifetimeData::Static.intern(&Interner), array_type)
745-
.intern(&Interner)
740+
TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner)
746741
}
747742
Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
748743
Literal::Int(_v, ty) => match ty {

crates/hir_ty/src/infer/pat.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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, LifetimeData, Substitution,
16+
lower::lower_to_chalk_mutability, static_lifetime, utils::variant_data, Interner, Substitution,
1717
Ty, TyBuilder, TyKind,
1818
};
1919

@@ -171,8 +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, LifetimeData::Static.intern(&Interner), subty)
175-
.intern(&Interner)
174+
TyKind::Ref(mutability, static_lifetime(), subty).intern(&Interner)
176175
}
177176
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
178177
p.as_ref(),
@@ -204,12 +203,10 @@ impl<'a> InferenceContext<'a> {
204203
let inner_ty = self.insert_type_vars_shallow(inner_ty);
205204

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

crates/hir_ty/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ 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,
3434
variant_data, Generics,
3535
},
3636
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
37-
FnSubst, ImplTraitId, LifetimeData, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
37+
FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
3838
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
3939
TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause,
4040
};
@@ -174,7 +174,7 @@ impl<'a> TyLoweringContext<'a> {
174174
}
175175
TypeRef::Reference(inner, _, mutability) => {
176176
let inner_ty = self.lower_ty(inner);
177-
let lifetime = LifetimeData::Static.intern(&Interner);
177+
let lifetime = static_lifetime();
178178
TyKind::Ref(lower_to_chalk_mutability(*mutability), lifetime, inner_ty)
179179
.intern(&Interner)
180180
}
@@ -200,8 +200,7 @@ impl<'a> TyLoweringContext<'a> {
200200
)
201201
});
202202
let bounds = crate::make_only_type_binders(1, bounds);
203-
TyKind::Dyn(DynTy { bounds, lifetime: LifetimeData::Static.intern(&Interner) })
204-
.intern(&Interner)
203+
TyKind::Dyn(DynTy { bounds, lifetime: static_lifetime() }).intern(&Interner)
205204
}
206205
TypeRef::ImplTrait(bounds) => {
207206
match self.impl_trait_mode {
@@ -393,7 +392,7 @@ impl<'a> TyLoweringContext<'a> {
393392
))),
394393
),
395394
),
396-
lifetime: LifetimeData::Static.intern(&Interner),
395+
lifetime: static_lifetime(),
397396
};
398397
TyKind::Dyn(dyn_ty).intern(&Interner)
399398
};

crates/hir_ty/src/method_resolution.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ 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,
24-
InEnvironment, Interner, LifetimeData, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder,
25-
TyKind, TypeWalk,
25+
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyKind,
26+
TypeWalk,
2627
};
2728

2829
/// This is used as a key for indexing impls.
@@ -453,12 +454,8 @@ fn iterate_method_candidates_with_autoref(
453454
}
454455
let refed = Canonical {
455456
binders: deref_chain[0].binders.clone(),
456-
value: TyKind::Ref(
457-
Mutability::Not,
458-
LifetimeData::Static.intern(&Interner),
459-
deref_chain[0].value.clone(),
460-
)
461-
.intern(&Interner),
457+
value: TyKind::Ref(Mutability::Not, static_lifetime(), deref_chain[0].value.clone())
458+
.intern(&Interner),
462459
};
463460
if iterate_method_candidates_by_receiver(
464461
&refed,
@@ -475,12 +472,8 @@ fn iterate_method_candidates_with_autoref(
475472
}
476473
let ref_muted = Canonical {
477474
binders: deref_chain[0].binders.clone(),
478-
value: TyKind::Ref(
479-
Mutability::Mut,
480-
LifetimeData::Static.intern(&Interner),
481-
deref_chain[0].value.clone(),
482-
)
483-
.intern(&Interner),
475+
value: TyKind::Ref(Mutability::Mut, static_lifetime(), deref_chain[0].value.clone())
476+
.intern(&Interner),
484477
};
485478
if iterate_method_candidates_by_receiver(
486479
&ref_muted,

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
//! Chalk (in both directions); plus some helper functions for more specialized
44
//! conversions.
55
6-
use chalk_ir::{cast::Cast, interner::HasInterner, LifetimeData};
6+
use chalk_ir::{cast::Cast, interner::HasInterner};
77
use chalk_solve::rust_ir;
88

99
use base_db::salsa::InternKey;
1010
use hir_def::{GenericDefId, TypeAliasId};
1111

1212
use crate::{
13-
chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId,
14-
Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, OpaqueTy, ProjectionTy,
15-
QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
13+
chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, static_lifetime, AliasTy,
14+
CallableDefId, Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, OpaqueTy,
15+
ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
1616
};
1717

1818
use super::interner::*;
@@ -100,7 +100,7 @@ impl ToChalk for Ty {
100100
);
101101
let bounded_ty = chalk_ir::DynTy {
102102
bounds: chalk_ir::Binders::new(binders, where_clauses),
103-
lifetime: LifetimeData::Static.intern(&Interner),
103+
lifetime: static_lifetime(),
104104
};
105105
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
106106
}
@@ -149,7 +149,7 @@ impl ToChalk for Ty {
149149
where_clauses.bounds.binders.clone(),
150150
crate::QuantifiedWhereClauses::from_iter(&Interner, bounds),
151151
),
152-
lifetime: LifetimeData::Static.intern(&Interner),
152+
lifetime: static_lifetime(),
153153
})
154154
}
155155

@@ -197,7 +197,7 @@ fn ref_to_chalk(
197197
ty: Ty,
198198
) -> chalk_ir::Ty<Interner> {
199199
let arg = ty.to_chalk(db);
200-
let lifetime = LifetimeData::Static.intern(&Interner);
200+
let lifetime = static_lifetime();
201201
chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner)
202202
}
203203

0 commit comments

Comments
 (0)