7021: Track labels in the HIR r=matklad a=Veykril

Groundwork for #6966

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-12-24 12:04:28 +00:00 committed by GitHub
commit 06320015af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 167 additions and 68 deletions

View file

@ -9,7 +9,7 @@ use hir_def::{
adt::StructKind,
adt::VariantData,
builtin_type::BuiltinType,
expr::{BindingAnnotation, Pat, PatId},
expr::{BindingAnnotation, LabelId, Pat, PatId},
import_map,
item_tree::ItemTreeNode,
lang_item::LangItemTarget,
@ -1205,6 +1205,34 @@ impl Local {
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Label {
pub(crate) parent: DefWithBodyId,
pub(crate) label_id: LabelId,
}
impl Label {
pub fn module(self, db: &dyn HirDatabase) -> Module {
self.parent(db).module(db)
}
pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
self.parent.into()
}
pub fn name(self, db: &dyn HirDatabase) -> Name {
let body = db.body(self.parent.into());
body[self.label_id].name.clone()
}
pub fn source(self, db: &dyn HirDatabase) -> InFile<ast::Label> {
let (_body, source_map) = db.body_with_source_map(self.parent.into());
let src = source_map.label_syntax(self.label_id);
let root = src.file_syntax(db.upcast());
src.map(|ast| ast.to_node(&root))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GenericParam {
TypeParam(TypeParam),