Replace unused hir_ty::Lifetime with chalk equivalents

This commit is contained in:
Lukas Wirth 2021-04-05 20:46:15 +02:00
parent 87e56eb94c
commit d587ca2991
4 changed files with 50 additions and 18 deletions

View file

@ -5,7 +5,7 @@ use std::sync::Arc;
use base_db::{impl_intern_key, salsa, CrateId, Upcast}; use base_db::{impl_intern_key, salsa, CrateId, Upcast};
use hir_def::{ use hir_def::{
db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId, db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId,
LocalFieldId, TypeParamId, VariantId, LifetimeParamId, LocalFieldId, TypeParamId, VariantId,
}; };
use la_arena::ArenaMap; use la_arena::ArenaMap;
@ -86,6 +86,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::interned] #[salsa::interned]
fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId; fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId;
#[salsa::interned] #[salsa::interned]
fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId;
#[salsa::interned]
fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId; fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
#[salsa::interned] #[salsa::interned]
fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId; fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
@ -155,6 +157,10 @@ fn hir_database_is_object_safe() {
pub struct InternedTypeParamId(salsa::InternId); pub struct InternedTypeParamId(salsa::InternId);
impl_intern_key!(InternedTypeParamId); 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedOpaqueTyId(salsa::InternId); pub struct InternedOpaqueTyId(salsa::InternId);
impl_intern_key!(InternedOpaqueTyId); impl_intern_key!(InternedOpaqueTyId);

View file

@ -1,8 +1,10 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use std::{array, fmt}; use std::{
array,
fmt::{self, Debug},
};
use chalk_ir::Mutability;
use hir_def::{ use hir_def::{
db::DefDatabase, db::DefDatabase,
find_path, find_path,
@ -16,9 +18,10 @@ use hir_def::{
use hir_expand::name::Name; use hir_expand::name::Name;
use crate::{ use crate::{
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk,
CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg,
ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy,
ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause, 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 { 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> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self { 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 generics = generics(f.db.upcast(), id.parent);
let param_data = &generics.params.lifetimes[id.local_id]; let param_data = &generics.params.lifetimes[id.local_id];
write!(f, "{}", &param_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(()),
} }
} }
} }

View file

@ -35,8 +35,8 @@ use smallvec::SmallVec;
use base_db::salsa; use base_db::salsa;
use hir_def::{ use hir_def::{
expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule,
TraitId, TypeAliasId, TypeParamId, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId,
}; };
use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
@ -70,6 +70,10 @@ pub type VariableKind = chalk_ir::VariableKind<Interner>;
pub type VariableKinds = chalk_ir::VariableKinds<Interner>; pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>; pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
pub type Lifetime = chalk_ir::Lifetime<Interner>;
pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
pub type ChalkTraitId = chalk_ir::TraitId<Interner>; pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
impl ProjectionTy { 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 { pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
} }

View file

@ -7,7 +7,6 @@ use chalk_ir::{
cast::{CastTo, Caster}, cast::{CastTo, Caster},
BoundVar, Mutability, Scalar, TyVariableKind, BoundVar, Mutability, Scalar, TyVariableKind,
}; };
use hir_def::LifetimeParamId;
use smallvec::SmallVec; use smallvec::SmallVec;
use crate::{ use crate::{
@ -15,12 +14,6 @@ use crate::{
InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKinds, InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKinds,
}; };
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Lifetime {
Parameter(LifetimeParamId),
Static,
}
#[derive(Clone, PartialEq, Eq, Debug, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct OpaqueTy { pub struct OpaqueTy {
pub opaque_ty_id: OpaqueTyId, pub opaque_ty_id: OpaqueTyId,