From 262b9c39824b58068d89d6c5cf53d8fea782b11c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 23 Dec 2020 16:34:30 +0100 Subject: Track labels in the HIR --- crates/hir/src/semantics/source_to_def.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'crates/hir/src/semantics/source_to_def.rs') diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 3efca5baa..424e6e8a9 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -4,7 +4,7 @@ use base_db::FileId; use hir_def::{ child_by_source::ChildBySource, dyn_map::DynMap, - expr::PatId, + expr::{LabelId, PatId}, keys::{self, Key}, ConstId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, @@ -108,12 +108,21 @@ impl SourceToDefCtx<'_, '_> { &mut self, src: InFile, ) -> Option<(DefWithBodyId, PatId)> { - let container = self.find_pat_container(src.as_ref().map(|it| it.syntax()))?; + let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?; let (_body, source_map) = self.db.body_with_source_map(container); let src = src.map(ast::Pat::from); let pat_id = source_map.node_pat(src.as_ref())?; Some((container, pat_id)) } + pub(super) fn label_to_def( + &mut self, + src: InFile, + ) -> Option<(DefWithBodyId, LabelId)> { + let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?; + let (_body, source_map) = self.db.body_with_source_map(container); + let label_id = source_map.node_label(src.as_ref())?; + Some((container, label_id)) + } fn to_def( &mut self, @@ -237,7 +246,7 @@ impl SourceToDefCtx<'_, '_> { None } - fn find_pat_container(&mut self, src: InFile<&SyntaxNode>) -> Option { + fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option { for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { let res: DefWithBodyId = match_ast! { match (container.value) { -- cgit v1.2.3