Skip to content

Commit d587ca2

Browse files
committed
Replace unused hir_ty::Lifetime with chalk equivalents
1 parent 87e56eb commit d587ca2

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

crates/hir_ty/src/db.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use base_db::{impl_intern_key, salsa, CrateId, Upcast};
66
use hir_def::{
77
db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId,
8-
LocalFieldId, TypeParamId, VariantId,
8+
LifetimeParamId, LocalFieldId, TypeParamId, VariantId,
99
};
1010
use la_arena::ArenaMap;
1111

@@ -86,6 +86,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
8686
#[salsa::interned]
8787
fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId;
8888
#[salsa::interned]
89+
fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId;
90+
#[salsa::interned]
8991
fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
9092
#[salsa::interned]
9193
fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
@@ -155,6 +157,10 @@ fn hir_database_is_object_safe() {
155157
pub struct InternedTypeParamId(salsa::InternId);
156158
impl_intern_key!(InternedTypeParamId);
157159

160+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
161+
pub struct InternedLifetimeParamId(salsa::InternId);
162+
impl_intern_key!(InternedLifetimeParamId);
163+
158164
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
159165
pub struct InternedOpaqueTyId(salsa::InternId);
160166
impl_intern_key!(InternedOpaqueTyId);

crates/hir_ty/src/display.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! FIXME: write short doc here
22
3-
use std::{array, fmt};
3+
use std::{
4+
array,
5+
fmt::{self, Debug},
6+
};
47

5-
use chalk_ir::Mutability;
68
use hir_def::{
79
db::DefDatabase,
810
find_path,
@@ -16,9 +18,10 @@ use hir_def::{
1618
use hir_expand::name::Name;
1719

1820
use crate::{
19-
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
20-
to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
21-
CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy,
21+
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
22+
lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk,
23+
utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg,
24+
ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy,
2225
ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause,
2326
};
2427

@@ -827,15 +830,35 @@ impl HirDisplay for WhereClause {
827830
}
828831
}
829832

833+
impl HirDisplay for LifetimeOutlives {
834+
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
835+
self.a.hir_fmt(f)?;
836+
write!(f, ": ")?;
837+
self.b.hir_fmt(f)
838+
}
839+
}
840+
830841
impl HirDisplay for Lifetime {
842+
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
843+
self.interned().hir_fmt(f)
844+
}
845+
}
846+
847+
impl HirDisplay for LifetimeData {
831848
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
832849
match self {
833-
Lifetime::Parameter(id) => {
850+
LifetimeData::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index),
851+
LifetimeData::InferenceVar(_) => write!(f, "_"),
852+
LifetimeData::Placeholder(idx) => {
853+
let id = lt_from_placeholder_idx(f.db, *idx);
834854
let generics = generics(f.db.upcast(), id.parent);
835855
let param_data = &generics.params.lifetimes[id.local_id];
836-
write!(f, "{}", &param_data.name)
856+
write!(f, "{}", param_data.name)
837857
}
838-
Lifetime::Static => write!(f, "'static"),
858+
LifetimeData::Static => write!(f, "'static"),
859+
LifetimeData::Empty(_) => Ok(()),
860+
LifetimeData::Erased => Ok(()),
861+
LifetimeData::Phantom(_, _) => Ok(()),
839862
}
840863
}
841864
}

crates/hir_ty/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use smallvec::SmallVec;
3535

3636
use base_db::salsa;
3737
use hir_def::{
38-
expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup,
39-
TraitId, TypeAliasId, TypeParamId,
38+
expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule,
39+
LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId,
4040
};
4141

4242
use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
@@ -70,6 +70,10 @@ pub type VariableKind = chalk_ir::VariableKind<Interner>;
7070
pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
7171
pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
7272

73+
pub type Lifetime = chalk_ir::Lifetime<Interner>;
74+
pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
75+
pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
76+
7377
pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
7478

7579
impl ProjectionTy {
@@ -546,6 +550,12 @@ pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderI
546550
}
547551
}
548552

553+
pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
554+
assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
555+
let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
556+
db.lookup_intern_lifetime_param_id(interned_id)
557+
}
558+
549559
pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
550560
chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
551561
}

crates/hir_ty/src/types.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@ use chalk_ir::{
77
cast::{CastTo, Caster},
88
BoundVar, Mutability, Scalar, TyVariableKind,
99
};
10-
use hir_def::LifetimeParamId;
1110
use smallvec::SmallVec;
1211

1312
use crate::{
1413
AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, FnDefId, FnSig, ForeignDefId,
1514
InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKinds,
1615
};
1716

18-
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
19-
pub enum Lifetime {
20-
Parameter(LifetimeParamId),
21-
Static,
22-
}
23-
2417
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
2518
pub struct OpaqueTy {
2619
pub opaque_ty_id: OpaqueTyId,

0 commit comments

Comments
 (0)