Use a constructor function for Static lifetimes

This commit is contained in:
Lukas Wirth 2021-04-06 10:50:55 +02:00
parent b98c681cb7
commit 08dc69599e
6 changed files with 37 additions and 49 deletions

View File

@ -19,11 +19,11 @@
lower::lower_to_chalk_mutability,
method_resolution, op,
primitive::{self, UintTy},
to_chalk_trait_id,
static_lifetime, to_chalk_trait_id,
traits::{chalk::from_chalk, FnTrait},
utils::{generics, variant_data, Generics},
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
LifetimeData, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk,
};
use super::{
@ -527,9 +527,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
let inner_ty = self.infer_expr_inner(*expr, &expectation);
match rawness {
Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
Rawness::Ref => {
TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), inner_ty)
}
Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty),
}
.intern(&Interner)
}
@ -732,17 +730,14 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
}
Expr::Literal(lit) => match lit {
Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
Literal::String(..) => TyKind::Ref(
Mutability::Not,
LifetimeData::Static.intern(&Interner),
TyKind::Str.intern(&Interner),
)
.intern(&Interner),
Literal::String(..) => {
TyKind::Ref(Mutability::Not, static_lifetime(), TyKind::Str.intern(&Interner))
.intern(&Interner)
}
Literal::ByteString(..) => {
let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
let array_type = TyKind::Array(byte_type).intern(&Interner);
TyKind::Ref(Mutability::Not, LifetimeData::Static.intern(&Interner), array_type)
.intern(&Interner)
TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner)
}
Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
Literal::Int(_v, ty) => match ty {

View File

@ -13,7 +13,7 @@
use super::{BindingMode, Expectation, InferenceContext};
use crate::{
lower::lower_to_chalk_mutability, utils::variant_data, Interner, LifetimeData, Substitution,
lower::lower_to_chalk_mutability, static_lifetime, utils::variant_data, Interner, Substitution,
Ty, TyBuilder, TyKind,
};
@ -171,8 +171,7 @@ pub(super) fn infer_pat(
_ => self.result.standard_types.unknown.clone(),
};
let subty = self.infer_pat(*pat, &expectation, default_bm);
TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), subty)
.intern(&Interner)
TyKind::Ref(mutability, static_lifetime(), subty).intern(&Interner)
}
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
p.as_ref(),
@ -204,12 +203,10 @@ pub(super) fn infer_pat(
let inner_ty = self.insert_type_vars_shallow(inner_ty);
let bound_ty = match mode {
BindingMode::Ref(mutability) => TyKind::Ref(
mutability,
LifetimeData::Static.intern(&Interner),
inner_ty.clone(),
)
.intern(&Interner),
BindingMode::Ref(mutability) => {
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone())
.intern(&Interner)
}
BindingMode::Move => inner_ty.clone(),
};
let bound_ty = self.resolve_ty_as_possible(bound_ty);

View File

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

View File

@ -27,14 +27,14 @@
use crate::{
db::HirDatabase,
to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
traits::chalk::{Interner, ToChalk},
utils::{
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
variant_data, Generics,
},
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
FnSubst, ImplTraitId, LifetimeData, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause,
};
@ -174,7 +174,7 @@ pub fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) {
}
TypeRef::Reference(inner, _, mutability) => {
let inner_ty = self.lower_ty(inner);
let lifetime = LifetimeData::Static.intern(&Interner);
let lifetime = static_lifetime();
TyKind::Ref(lower_to_chalk_mutability(*mutability), lifetime, inner_ty)
.intern(&Interner)
}
@ -200,8 +200,7 @@ pub fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) {
)
});
let bounds = crate::make_only_type_binders(1, bounds);
TyKind::Dyn(DynTy { bounds, lifetime: LifetimeData::Static.intern(&Interner) })
.intern(&Interner)
TyKind::Dyn(DynTy { bounds, lifetime: static_lifetime() }).intern(&Interner)
}
TypeRef::ImplTrait(bounds) => {
match self.impl_trait_mode {
@ -393,7 +392,7 @@ pub(crate) fn lower_partly_resolved_path(
))),
),
),
lifetime: LifetimeData::Static.intern(&Interner),
lifetime: static_lifetime(),
};
TyKind::Dyn(dyn_ty).intern(&Interner)
};

View File

@ -19,10 +19,11 @@
db::HirDatabase,
from_foreign_def_id,
primitive::{self, FloatTy, IntTy, UintTy},
static_lifetime,
utils::all_super_traits,
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
InEnvironment, Interner, LifetimeData, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder,
TyKind, TypeWalk,
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyKind,
TypeWalk,
};
/// This is used as a key for indexing impls.
@ -453,12 +454,8 @@ fn iterate_method_candidates_with_autoref(
}
let refed = Canonical {
binders: deref_chain[0].binders.clone(),
value: TyKind::Ref(
Mutability::Not,
LifetimeData::Static.intern(&Interner),
deref_chain[0].value.clone(),
)
.intern(&Interner),
value: TyKind::Ref(Mutability::Not, static_lifetime(), deref_chain[0].value.clone())
.intern(&Interner),
};
if iterate_method_candidates_by_receiver(
&refed,
@ -475,12 +472,8 @@ fn iterate_method_candidates_with_autoref(
}
let ref_muted = Canonical {
binders: deref_chain[0].binders.clone(),
value: TyKind::Ref(
Mutability::Mut,
LifetimeData::Static.intern(&Interner),
deref_chain[0].value.clone(),
)
.intern(&Interner),
value: TyKind::Ref(Mutability::Mut, static_lifetime(), deref_chain[0].value.clone())
.intern(&Interner),
};
if iterate_method_candidates_by_receiver(
&ref_muted,

View File

@ -3,16 +3,16 @@
//! Chalk (in both directions); plus some helper functions for more specialized
//! conversions.
use chalk_ir::{cast::Cast, interner::HasInterner, LifetimeData};
use chalk_ir::{cast::Cast, interner::HasInterner};
use chalk_solve::rust_ir;
use base_db::salsa::InternKey;
use hir_def::{GenericDefId, TypeAliasId};
use crate::{
chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId,
Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, OpaqueTy, ProjectionTy,
QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, static_lifetime, AliasTy,
CallableDefId, Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, OpaqueTy,
ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
};
use super::interner::*;
@ -100,7 +100,7 @@ fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
);
let bounded_ty = chalk_ir::DynTy {
bounds: chalk_ir::Binders::new(binders, where_clauses),
lifetime: LifetimeData::Static.intern(&Interner),
lifetime: static_lifetime(),
};
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
}
@ -149,7 +149,7 @@ fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
where_clauses.bounds.binders.clone(),
crate::QuantifiedWhereClauses::from_iter(&Interner, bounds),
),
lifetime: LifetimeData::Static.intern(&Interner),
lifetime: static_lifetime(),
})
}
@ -197,7 +197,7 @@ fn ref_to_chalk(
ty: Ty,
) -> chalk_ir::Ty<Interner> {
let arg = ty.to_chalk(db);
let lifetime = LifetimeData::Static.intern(&Interner);
let lifetime = static_lifetime();
chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner)
}