Skip to content

Commit e83b567

Browse files
committed
Flatten the TypeOwnerId
1 parent f8594f7 commit e83b567

File tree

3 files changed

+89
-14
lines changed

3 files changed

+89
-14
lines changed

crates/hir-def/src/lib.rs

+76-9
Original file line numberDiff line numberDiff line change
@@ -487,22 +487,81 @@ impl_intern_key!(AnonymousConstId);
487487

488488
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
489489
pub enum TypeOwnerId {
490+
FunctionId(FunctionId),
491+
StaticId(StaticId),
492+
ConstId(ConstId),
493+
InTypeConstId(InTypeConstId),
494+
AdtId(AdtId),
495+
TraitId(TraitId),
496+
TraitAliasId(TraitAliasId),
497+
TypeAliasId(TypeAliasId),
498+
ImplId(ImplId),
499+
EnumVariantId(EnumVariantId),
500+
// FIXME(const-generic-body): ModuleId should not be a type owner. This needs to be fixed to make `TypeOwnerId` actually
501+
// useful for assigning ids to in type consts.
490502
ModuleId(ModuleId),
491-
DefWithBodyId(DefWithBodyId),
492-
GenericDefId(GenericDefId),
493503
}
494504

495505
impl TypeOwnerId {
496506
fn as_generic_def_id(self) -> Option<GenericDefId> {
497-
match self {
498-
TypeOwnerId::ModuleId(_) => None,
499-
TypeOwnerId::DefWithBodyId(x) => x.as_generic_def_id(),
500-
TypeOwnerId::GenericDefId(x) => Some(x),
507+
Some(match self {
508+
TypeOwnerId::FunctionId(x) => GenericDefId::FunctionId(x),
509+
TypeOwnerId::ConstId(x) => GenericDefId::ConstId(x),
510+
TypeOwnerId::AdtId(x) => GenericDefId::AdtId(x),
511+
TypeOwnerId::TraitId(x) => GenericDefId::TraitId(x),
512+
TypeOwnerId::TraitAliasId(x) => GenericDefId::TraitAliasId(x),
513+
TypeOwnerId::TypeAliasId(x) => GenericDefId::TypeAliasId(x),
514+
TypeOwnerId::ImplId(x) => GenericDefId::ImplId(x),
515+
TypeOwnerId::EnumVariantId(x) => GenericDefId::EnumVariantId(x),
516+
TypeOwnerId::InTypeConstId(_) | TypeOwnerId::ModuleId(_) | TypeOwnerId::StaticId(_) => {
517+
return None
518+
}
519+
})
520+
}
521+
}
522+
523+
impl_from!(
524+
FunctionId,
525+
StaticId,
526+
ConstId,
527+
InTypeConstId,
528+
AdtId,
529+
TraitId,
530+
TraitAliasId,
531+
TypeAliasId,
532+
ImplId,
533+
EnumVariantId,
534+
ModuleId
535+
for TypeOwnerId
536+
);
537+
538+
// Every `DefWithBodyId` is a type owner, since bodies can contain type (e.g. `{ let x: Type = _; }`)
539+
impl From<DefWithBodyId> for TypeOwnerId {
540+
fn from(value: DefWithBodyId) -> Self {
541+
match value {
542+
DefWithBodyId::FunctionId(x) => x.into(),
543+
DefWithBodyId::StaticId(x) => x.into(),
544+
DefWithBodyId::ConstId(x) => x.into(),
545+
DefWithBodyId::InTypeConstId(x) => x.into(),
546+
DefWithBodyId::VariantId(x) => x.into(),
501547
}
502548
}
503549
}
504550

505-
impl_from!(ModuleId, DefWithBodyId(FunctionId, ConstId, StaticId), GenericDefId(AdtId, TypeAliasId, ImplId) for TypeOwnerId);
551+
impl From<GenericDefId> for TypeOwnerId {
552+
fn from(value: GenericDefId) -> Self {
553+
match value {
554+
GenericDefId::FunctionId(x) => x.into(),
555+
GenericDefId::AdtId(x) => x.into(),
556+
GenericDefId::TraitId(x) => x.into(),
557+
GenericDefId::TraitAliasId(x) => x.into(),
558+
GenericDefId::TypeAliasId(x) => x.into(),
559+
GenericDefId::ImplId(x) => x.into(),
560+
GenericDefId::EnumVariantId(x) => x.into(),
561+
GenericDefId::ConstId(x) => x.into(),
562+
}
563+
}
564+
}
506565

507566
/// A thing that we want to store in interned ids, but we don't know its type in `hir-def`
508567
pub trait OpaqueInternableThing:
@@ -815,9 +874,17 @@ impl HasModule for MacroId {
815874
impl HasModule for TypeOwnerId {
816875
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
817876
match self {
877+
TypeOwnerId::FunctionId(x) => x.lookup(db).module(db),
878+
TypeOwnerId::StaticId(x) => x.lookup(db).module(db),
879+
TypeOwnerId::ConstId(x) => x.lookup(db).module(db),
880+
TypeOwnerId::InTypeConstId(x) => x.lookup(db).1.module(db),
881+
TypeOwnerId::AdtId(x) => x.module(db),
882+
TypeOwnerId::TraitId(x) => x.lookup(db).container,
883+
TypeOwnerId::TraitAliasId(x) => x.lookup(db).container,
884+
TypeOwnerId::TypeAliasId(x) => x.lookup(db).module(db),
885+
TypeOwnerId::ImplId(x) => x.lookup(db).container,
886+
TypeOwnerId::EnumVariantId(x) => x.parent.lookup(db).container,
818887
TypeOwnerId::ModuleId(x) => *x,
819-
TypeOwnerId::DefWithBodyId(x) => x.module(db),
820-
TypeOwnerId::GenericDefId(x) => x.module(db),
821888
}
822889
}
823890
}

crates/hir-def/src/resolver.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1012,9 +1012,17 @@ impl HasResolver for ExternBlockId {
10121012
impl HasResolver for TypeOwnerId {
10131013
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
10141014
match self {
1015-
TypeOwnerId::ModuleId(it) => it.resolver(db),
1016-
TypeOwnerId::DefWithBodyId(it) => it.resolver(db),
1017-
TypeOwnerId::GenericDefId(it) => it.resolver(db),
1015+
TypeOwnerId::FunctionId(x) => x.resolver(db),
1016+
TypeOwnerId::StaticId(x) => x.resolver(db),
1017+
TypeOwnerId::ConstId(x) => x.resolver(db),
1018+
TypeOwnerId::InTypeConstId(x) => x.lookup(db).1.resolver(db),
1019+
TypeOwnerId::AdtId(x) => x.resolver(db),
1020+
TypeOwnerId::TraitId(x) => x.resolver(db),
1021+
TypeOwnerId::TraitAliasId(x) => x.resolver(db),
1022+
TypeOwnerId::TypeAliasId(x) => x.resolver(db),
1023+
TypeOwnerId::ImplId(x) => x.resolver(db),
1024+
TypeOwnerId::EnumVariantId(x) => x.resolver(db),
1025+
TypeOwnerId::ModuleId(x) => x.resolver(db),
10181026
}
10191027
}
10201028
}

crates/hir/src/source_analyzer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ use hir_ty::{
3838
UnsafeExpr,
3939
},
4040
lang_items::lang_items_for_bin_op,
41-
method_resolution::{self},
42-
Adjustment, InferenceResult, Interner, Substitution, Ty, TyExt, TyKind, TyLoweringContext,
41+
method_resolution, Adjustment, InferenceResult, Interner, Substitution, Ty, TyExt, TyKind,
42+
TyLoweringContext,
4343
};
4444
use itertools::Itertools;
4545
use smallvec::SmallVec;

0 commit comments

Comments
 (0)