From eaeabbb815b30b9169f06eaf30d8225055662196 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 17 Dec 2020 14:29:29 +0100 Subject: Add `AttrDefId::krate` Access to the containing crate will be needed to handle `cfg_attr` --- crates/hir_def/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'crates') diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 7e2199a9c..ba09a9126 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -425,6 +425,16 @@ impl HasModule for AdtId { } } +impl HasModule for VariantId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { + match self { + VariantId::EnumVariantId(it) => it.parent.lookup(db).container.module(db), + VariantId::StructId(it) => it.lookup(db).container.module(db), + VariantId::UnionId(it) => it.lookup(db).container.module(db), + } + } +} + impl HasModule for DefWithBodyId { fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { match self { @@ -465,6 +475,26 @@ impl HasModule for StaticLoc { } } +impl AttrDefId { + pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId { + match self { + AttrDefId::ModuleId(it) => it.krate, + AttrDefId::FieldId(it) => it.parent.module(db).krate, + AttrDefId::AdtId(it) => it.module(db).krate, + AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate, + AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db).krate, + AttrDefId::StaticId(it) => it.lookup(db).module(db).krate, + AttrDefId::ConstId(it) => it.lookup(db).module(db).krate, + AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate, + AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate, + AttrDefId::ImplId(it) => it.lookup(db).container.module(db).krate, + // FIXME: `MacroDefId` should store the defining module, then this can implement + // `HasModule` + AttrDefId::MacroDefId(it) => it.krate, + } + } +} + /// A helper trait for converting to MacroCallId pub trait AsMacroCall { fn as_call_id( -- cgit v1.2.3