6915: Add `AttrDefId::krate` r=jonas-schievink a=jonas-schievink

Access to the containing crate will be needed to handle `cfg_attr`.

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2020-12-17 13:30:09 +00:00 committed by GitHub
commit 3f6077ffe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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(