From 96756f1b1df4729fd00ca96a59971b3997c91934 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Apr 2021 22:08:16 +0200 Subject: Add Lifetime to TyKind::Ref --- crates/hir_ty/src/diagnostics/expr.rs | 2 +- crates/hir_ty/src/display.rs | 2 +- crates/hir_ty/src/infer/coerce.rs | 6 ++++-- crates/hir_ty/src/infer/expr.rs | 22 +++++++++++++++------- crates/hir_ty/src/infer/pat.rs | 20 ++++++++++++-------- crates/hir_ty/src/infer/unify.rs | 2 +- crates/hir_ty/src/lib.rs | 8 ++++---- crates/hir_ty/src/lower.rs | 6 ++++-- crates/hir_ty/src/method_resolution.rs | 18 ++++++++++++++---- crates/hir_ty/src/traits/chalk/mapping.rs | 10 +++++----- crates/hir_ty/src/types.rs | 5 +++-- crates/hir_ty/src/walk.rs | 4 ++-- 12 files changed, 66 insertions(+), 39 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index db278d0db..d7bf9fdf7 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -315,7 +315,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { if pat_ty == match_expr_ty || match_expr_ty .as_reference() - .map(|(match_expr_ty, _)| match_expr_ty == pat_ty) + .map(|(match_expr_ty, ..)| match_expr_ty == pat_ty) .unwrap_or(false) { // If we had a NotUsefulMatchArm diagnostic, we could diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 4ef8024d0..22416c0cf 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -314,7 +314,7 @@ impl HirDisplay for Ty { t.hir_fmt(f)?; write!(f, "; _]")?; } - TyKind::Raw(m, t) | TyKind::Ref(m, t) => { + TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => { let ty_display = t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index 32c273afc..d6c48870a 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -81,7 +81,7 @@ impl<'a> InferenceContext<'a> { // `&T` -> `*const T` // `&mut T` -> `*mut T`/`*const T` (TyKind::Ref(.., substs), &TyKind::Raw(m2 @ Mutability::Not, ..)) - | (TyKind::Ref(Mutability::Mut, substs), &TyKind::Raw(m2, ..)) => { + | (TyKind::Ref(Mutability::Mut, _, substs), &TyKind::Raw(m2, ..)) => { from_ty = TyKind::Raw(m2, substs.clone()).intern(&Interner); } @@ -111,7 +111,9 @@ impl<'a> InferenceContext<'a> { // Auto Deref if cannot coerce match (from_ty.kind(&Interner), to_ty.kind(&Interner)) { // FIXME: DerefMut - (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => self.unify_autoderef_behind_ref(st1, st2), + (TyKind::Ref(.., st1), TyKind::Ref(.., st2)) => { + self.unify_autoderef_behind_ref(st1, st2) + } // Otherwise, normal unify _ => self.unify(&from_ty, to_ty), diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index ff564106b..77fb36332 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -23,7 +23,7 @@ use crate::{ traits::{chalk::from_chalk, FnTrait}, utils::{generics, variant_data, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, - ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, + LifetimeData, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, }; use super::{ @@ -527,7 +527,9 @@ impl<'a> InferenceContext<'a> { let inner_ty = self.infer_expr_inner(*expr, &expectation); match rawness { Rawness::RawPtr => TyKind::Raw(mutability, inner_ty), - Rawness::Ref => TyKind::Ref(mutability, inner_ty), + Rawness::Ref => { + TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), inner_ty) + } } .intern(&Interner) } @@ -730,13 +732,17 @@ impl<'a> InferenceContext<'a> { } Expr::Literal(lit) => match lit { Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), - Literal::String(..) => { - TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner) - } + Literal::String(..) => TyKind::Ref( + Mutability::Not, + LifetimeData::Static.intern(&Interner), + 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, array_type).intern(&Interner) + TyKind::Ref(Mutability::Not, LifetimeData::Static.intern(&Interner), array_type) + .intern(&Interner) } Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner), Literal::Int(_v, ty) => match ty { @@ -872,7 +878,9 @@ impl<'a> InferenceContext<'a> { // Apply autoref so the below unification works correctly // FIXME: return correct autorefs from lookup_method let actual_receiver_ty = match expected_receiver_ty.as_reference() { - Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner), + Some((_, lifetime, mutability)) => { + TyKind::Ref(mutability, lifetime, derefed_receiver_ty).intern(&Interner) + } _ => derefed_receiver_ty, }; self.unify(&expected_receiver_ty, &actual_receiver_ty); diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index 252ae914a..b5e97cc8c 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -13,8 +13,8 @@ use hir_expand::name::Name; use super::{BindingMode, Expectation, InferenceContext}; use crate::{ - lower::lower_to_chalk_mutability, utils::variant_data, Interner, Substitution, Ty, TyBuilder, - TyKind, + lower::lower_to_chalk_mutability, utils::variant_data, Interner, LifetimeData, Substitution, + Ty, TyBuilder, TyKind, }; impl<'a> InferenceContext<'a> { @@ -104,7 +104,7 @@ impl<'a> InferenceContext<'a> { let body = Arc::clone(&self.body); // avoid borrow checker problem if is_non_ref_pat(&body, pat) { - while let Some((inner, mutability)) = expected.as_reference() { + while let Some((inner, _lifetime, mutability)) = expected.as_reference() { expected = inner; default_bm = match default_bm { BindingMode::Move => BindingMode::Ref(mutability), @@ -162,7 +162,7 @@ impl<'a> InferenceContext<'a> { Pat::Ref { pat, mutability } => { let mutability = lower_to_chalk_mutability(*mutability); let expectation = match expected.as_reference() { - Some((inner_ty, exp_mut)) => { + Some((inner_ty, _lifetime, exp_mut)) => { if mutability != exp_mut { // FIXME: emit type error? } @@ -171,7 +171,8 @@ impl<'a> InferenceContext<'a> { _ => self.result.standard_types.unknown.clone(), }; let subty = self.infer_pat(*pat, &expectation, default_bm); - TyKind::Ref(mutability, subty).intern(&Interner) + TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), subty) + .intern(&Interner) } Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( p.as_ref(), @@ -203,9 +204,12 @@ impl<'a> InferenceContext<'a> { let inner_ty = self.insert_type_vars_shallow(inner_ty); let bound_ty = match mode { - BindingMode::Ref(mutability) => { - TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner) - } + BindingMode::Ref(mutability) => TyKind::Ref( + mutability, + LifetimeData::Static.intern(&Interner), + inner_ty.clone(), + ) + .intern(&Interner), BindingMode::Move => inner_ty.clone(), }; let bound_ty = self.resolve_ty_as_possible(bound_ty); diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 2f9523325..c7878ebfd 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -317,7 +317,7 @@ impl InferenceTable { | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => { self.unify_substs(substs1, substs2, depth + 1) } - (TyKind::Ref(_, ty1), TyKind::Ref(_, ty2)) + (TyKind::Ref(_, _, ty1), TyKind::Ref(_, _, ty2)) | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2)) | (TyKind::Array(ty1), TyKind::Array(ty2)) | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1), diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index c3ec12352..1dfe2075c 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -165,16 +165,16 @@ impl CallableSig { } impl Ty { - pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { + pub fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { match self.kind(&Interner) { - TyKind::Ref(mutability, ty) => Some((ty, *mutability)), + TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)), _ => None, } } pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { match self.kind(&Interner) { - TyKind::Ref(mutability, ty) => Some((ty, Rawness::Ref, *mutability)), + TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)), TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)), _ => None, } @@ -183,7 +183,7 @@ impl Ty { pub fn strip_references(&self) -> &Ty { let mut t: &Ty = self; - while let TyKind::Ref(_mutability, ty) = t.kind(&Interner) { + while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) { t = ty; } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 3cbb6ad54..5a769fa6a 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -34,7 +34,7 @@ use crate::{ variant_data, Generics, }, AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, - FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, + FnSubst, ImplTraitId, LifetimeData, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause, }; @@ -174,7 +174,9 @@ impl<'a> TyLoweringContext<'a> { } TypeRef::Reference(inner, _, mutability) => { let inner_ty = self.lower_ty(inner); - TyKind::Ref(lower_to_chalk_mutability(*mutability), inner_ty).intern(&Interner) + let lifetime = LifetimeData::Static.intern(&Interner); + TyKind::Ref(lower_to_chalk_mutability(*mutability), lifetime, inner_ty) + .intern(&Interner) } TypeRef::Placeholder => TyKind::Error.intern(&Interner), TypeRef::Fn(params, is_varargs) => { diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 6d65d3eb9..427844c12 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -21,8 +21,8 @@ use crate::{ primitive::{self, FloatTy, IntTy, UintTy}, utils::all_super_traits, AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, - InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyKind, - TypeWalk, + InEnvironment, Interner, LifetimeData, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, + TyKind, TypeWalk, }; /// This is used as a key for indexing impls. @@ -453,7 +453,12 @@ fn iterate_method_candidates_with_autoref( } let refed = Canonical { binders: deref_chain[0].binders.clone(), - value: TyKind::Ref(Mutability::Not, deref_chain[0].value.clone()).intern(&Interner), + value: TyKind::Ref( + Mutability::Not, + LifetimeData::Static.intern(&Interner), + deref_chain[0].value.clone(), + ) + .intern(&Interner), }; if iterate_method_candidates_by_receiver( &refed, @@ -470,7 +475,12 @@ fn iterate_method_candidates_with_autoref( } let ref_muted = Canonical { binders: deref_chain[0].binders.clone(), - value: TyKind::Ref(Mutability::Mut, deref_chain[0].value.clone()).intern(&Interner), + value: TyKind::Ref( + Mutability::Mut, + LifetimeData::Static.intern(&Interner), + deref_chain[0].value.clone(), + ) + .intern(&Interner), }; if iterate_method_candidates_by_receiver( &ref_muted, diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 3047fbacb..26f8fdbdc 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -11,7 +11,7 @@ use hir_def::{GenericDefId, TypeAliasId}; use crate::{ chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, - Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, + Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, OpaqueTy, ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause, }; @@ -22,7 +22,7 @@ impl ToChalk for Ty { type Chalk = chalk_ir::Ty; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty { match self.into_inner() { - TyKind::Ref(m, ty) => ref_to_chalk(db, m, ty), + TyKind::Ref(m, lt, ty) => ref_to_chalk(db, m, lt, ty), TyKind::Array(ty) => array_to_chalk(db, ty), TyKind::Function(FnPointer { sig, substitution: substs, .. }) => { let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db)); @@ -167,8 +167,8 @@ impl ToChalk for Ty { } chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)), chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)), - chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { - TyKind::Ref(mutability, from_chalk(db, ty)) + chalk_ir::TyKind::Ref(mutability, lifetime, ty) => { + TyKind::Ref(mutability, lifetime, from_chalk(db, ty)) } chalk_ir::TyKind::Str => TyKind::Str, chalk_ir::TyKind::Never => TyKind::Never, @@ -192,10 +192,10 @@ impl ToChalk for Ty { fn ref_to_chalk( db: &dyn HirDatabase, mutability: chalk_ir::Mutability, + lifetime: Lifetime, ty: Ty, ) -> chalk_ir::Ty { let arg = ty.to_chalk(db); - let lifetime = LifetimeData::Static.intern(&Interner); chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner) } diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index d4e07a6b8..c984a31c8 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -11,7 +11,8 @@ use smallvec::SmallVec; use crate::{ AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, FnDefId, FnSig, ForeignDefId, - InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, VariableKinds, + InferenceVar, Interner, Lifetime, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, + VariableKinds, }; #[derive(Clone, PartialEq, Eq, Debug, Hash)] @@ -122,7 +123,7 @@ pub enum TyKind { /// A reference; a pointer with an associated lifetime. Written as /// `&'a mut T` or `&'a T`. - Ref(Mutability, Ty), + Ref(Mutability, Lifetime, Ty), /// This represents a placeholder for an opaque type in situations where we /// don't know the hidden type (i.e. currently almost always). This is diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index e1e77ba37..4cc4e24fd 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -153,7 +153,7 @@ impl TypeWalk for Ty { p.walk(f); } } - TyKind::Slice(ty) | TyKind::Array(ty) | TyKind::Ref(_, ty) | TyKind::Raw(_, ty) => { + TyKind::Slice(ty) | TyKind::Array(ty) | TyKind::Ref(_, _, ty) | TyKind::Raw(_, ty) => { ty.walk(f); } TyKind::Function(fn_pointer) => { @@ -187,7 +187,7 @@ impl TypeWalk for Ty { TyKind::Alias(AliasTy::Opaque(o_ty)) => { o_ty.substitution.walk_mut_binders(f, binders); } - TyKind::Slice(ty) | TyKind::Array(ty) | TyKind::Ref(_, ty) | TyKind::Raw(_, ty) => { + TyKind::Slice(ty) | TyKind::Array(ty) | TyKind::Ref(_, _, ty) | TyKind::Raw(_, ty) => { ty.walk_mut_binders(f, binders); } TyKind::Function(fn_pointer) => { -- cgit v1.2.3 From 9da191c7e07abec9d9f056fd94ee151aef7417a7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Apr 2021 22:12:40 +0200 Subject: Add Lifetime to DynTy --- crates/hir_ty/src/lower.rs | 4 +++- crates/hir_ty/src/traits/chalk/mapping.rs | 1 + crates/hir_ty/src/types.rs | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 5a769fa6a..ff795377a 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -200,7 +200,8 @@ impl<'a> TyLoweringContext<'a> { ) }); let bounds = crate::make_only_type_binders(1, bounds); - TyKind::Dyn(DynTy { bounds }).intern(&Interner) + TyKind::Dyn(DynTy { bounds, lifetime: LifetimeData::Static.intern(&Interner) }) + .intern(&Interner) } TypeRef::ImplTrait(bounds) => { match self.impl_trait_mode { @@ -392,6 +393,7 @@ impl<'a> TyLoweringContext<'a> { ))), ), ), + lifetime: LifetimeData::Static.intern(&Interner), }; TyKind::Dyn(dyn_ty).intern(&Interner) }; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 26f8fdbdc..791f342a1 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -149,6 +149,7 @@ impl ToChalk for Ty { where_clauses.bounds.binders.clone(), crate::QuantifiedWhereClauses::from_iter(&Interner, bounds), ), + lifetime: LifetimeData::Static.intern(&Interner), }) } diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index c984a31c8..844e57896 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -40,6 +40,7 @@ impl ProjectionTy { pub struct DynTy { /// The unknown self type. pub bounds: Binders, + pub lifetime: Lifetime, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] -- cgit v1.2.3 From b98c681cb7ede9fd6a8683b1edd83a5b18502178 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Apr 2021 23:01:34 +0200 Subject: Always use Static lifetimes in chalk mapping --- crates/hir_ty/src/traits/chalk/mapping.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 791f342a1..128ec166a 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -193,10 +193,11 @@ impl ToChalk for Ty { fn ref_to_chalk( db: &dyn HirDatabase, mutability: chalk_ir::Mutability, - lifetime: Lifetime, + _lifetime: Lifetime, ty: Ty, ) -> chalk_ir::Ty { let arg = ty.to_chalk(db); + let lifetime = LifetimeData::Static.intern(&Interner); chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner) } -- cgit v1.2.3 From 08dc69599efb4961319c0118b789d3abb264b7fa Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 6 Apr 2021 10:50:55 +0200 Subject: Use a constructor function for Static lifetimes --- crates/hir_ty/src/infer/expr.rs | 21 ++++++++------------- crates/hir_ty/src/infer/pat.rs | 15 ++++++--------- crates/hir_ty/src/lib.rs | 4 ++++ crates/hir_ty/src/lower.rs | 11 +++++------ crates/hir_ty/src/method_resolution.rs | 21 +++++++-------------- crates/hir_ty/src/traits/chalk/mapping.rs | 14 +++++++------- 6 files changed, 37 insertions(+), 49 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 77fb36332..796487d02 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -19,11 +19,11 @@ use crate::{ 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 @@ impl<'a> InferenceContext<'a> { 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 @@ impl<'a> InferenceContext<'a> { } 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 { diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index b5e97cc8c..2848a393c 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -13,7 +13,7 @@ use hir_expand::name::Name; 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 @@ impl<'a> InferenceContext<'a> { _ => 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 @@ impl<'a> InferenceContext<'a> { 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); diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 1dfe2075c..bccc73449 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -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) +} diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index ff795377a..df6619af3 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -27,14 +27,14 @@ use stdx::impl_from; 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 @@ impl<'a> TyLoweringContext<'a> { } 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 @@ impl<'a> TyLoweringContext<'a> { ) }); 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 @@ impl<'a> TyLoweringContext<'a> { ))), ), ), - lifetime: LifetimeData::Static.intern(&Interner), + lifetime: static_lifetime(), }; TyKind::Dyn(dyn_ty).intern(&Interner) }; diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 427844c12..436dea22b 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -19,10 +19,11 @@ use crate::{ 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, diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 128ec166a..9267e32b5 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -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 @@ impl ToChalk for Ty { ); 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 @@ impl ToChalk for Ty { 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 { 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) } -- cgit v1.2.3