From d587ca2991406bd348768b2912c3cb66c869e7e8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Apr 2021 20:46:15 +0200 Subject: Replace unused hir_ty::Lifetime with chalk equivalents --- crates/hir_ty/src/db.rs | 8 +++++++- crates/hir_ty/src/display.rs | 39 +++++++++++++++++++++++++++++++-------- crates/hir_ty/src/lib.rs | 14 ++++++++++++-- crates/hir_ty/src/types.rs | 7 ------- 4 files changed, 50 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index 4300680d9..07510ae02 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use base_db::{impl_intern_key, salsa, CrateId, Upcast}; use hir_def::{ db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId, - LocalFieldId, TypeParamId, VariantId, + LifetimeParamId, LocalFieldId, TypeParamId, VariantId, }; use la_arena::ArenaMap; @@ -86,6 +86,8 @@ pub trait HirDatabase: DefDatabase + Upcast { #[salsa::interned] fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId; #[salsa::interned] + fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId; + #[salsa::interned] fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId; #[salsa::interned] fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId; @@ -155,6 +157,10 @@ fn hir_database_is_object_safe() { pub struct InternedTypeParamId(salsa::InternId); impl_intern_key!(InternedTypeParamId); +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct InternedLifetimeParamId(salsa::InternId); +impl_intern_key!(InternedLifetimeParamId); + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct InternedOpaqueTyId(salsa::InternId); impl_intern_key!(InternedOpaqueTyId); diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index f31e6b108..01c7ef91f 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1,8 +1,10 @@ //! FIXME: write short doc here -use std::{array, fmt}; +use std::{ + array, + fmt::{self, Debug}, +}; -use chalk_ir::Mutability; use hir_def::{ db::DefDatabase, find_path, @@ -16,9 +18,10 @@ use hir_def::{ use hir_expand::name::Name; use crate::{ - db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, - to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, - CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy, + db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, + lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk, + utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg, + ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause, }; @@ -827,15 +830,35 @@ impl HirDisplay for WhereClause { } } +impl HirDisplay for LifetimeOutlives { + fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { + self.a.hir_fmt(f)?; + write!(f, ": ")?; + self.b.hir_fmt(f) + } +} + impl HirDisplay for Lifetime { + fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { + self.interned().hir_fmt(f) + } +} + +impl HirDisplay for LifetimeData { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { match self { - Lifetime::Parameter(id) => { + LifetimeData::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index), + LifetimeData::InferenceVar(_) => write!(f, "_"), + LifetimeData::Placeholder(idx) => { + let id = lt_from_placeholder_idx(f.db, *idx); let generics = generics(f.db.upcast(), id.parent); let param_data = &generics.params.lifetimes[id.local_id]; - write!(f, "{}", ¶m_data.name) + write!(f, "{}", param_data.name) } - Lifetime::Static => write!(f, "'static"), + LifetimeData::Static => write!(f, "'static"), + LifetimeData::Empty(_) => Ok(()), + LifetimeData::Erased => Ok(()), + LifetimeData::Phantom(_, _) => Ok(()), } } } diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index daf379ef8..a3addc8e9 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -35,8 +35,8 @@ use smallvec::SmallVec; use base_db::salsa; use hir_def::{ - expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, - TraitId, TypeAliasId, TypeParamId, + expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, + LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, }; use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; @@ -70,6 +70,10 @@ pub type VariableKind = chalk_ir::VariableKind; pub type VariableKinds = chalk_ir::VariableKinds; pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds; +pub type Lifetime = chalk_ir::Lifetime; +pub type LifetimeData = chalk_ir::LifetimeData; +pub type LifetimeOutlives = chalk_ir::LifetimeOutlives; + pub type ChalkTraitId = chalk_ir::TraitId; impl ProjectionTy { @@ -546,6 +550,12 @@ pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderI } } +pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId { + assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); + let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); + db.lookup_intern_lifetime_param_id(interned_id) +} + pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId { chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) } diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index 46c705a76..dc64e6e2b 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -7,7 +7,6 @@ use chalk_ir::{ cast::{CastTo, Caster}, BoundVar, Mutability, Scalar, TyVariableKind, }; -use hir_def::LifetimeParamId; use smallvec::SmallVec; use crate::{ @@ -15,12 +14,6 @@ use crate::{ InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKinds, }; -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub enum Lifetime { - Parameter(LifetimeParamId), - Static, -} - #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct OpaqueTy { pub opaque_ty_id: OpaqueTyId, -- cgit v1.2.3