From 3d4b48e481da35f19366514c0e22ed42fef037a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 14:47:01 +0100 Subject: Fix resolve for field init shorthand --- crates/ra_hir/src/from_source.rs | 16 +++++++++------- crates/ra_hir/src/source_binder.rs | 8 +++++++- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 3b6454a1d..42ca55fe7 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,4 +1,6 @@ -//! FIXME: write short doc here +//! Finds a corresponding hir data structure for a syntax node in a specific +//! file. + use hir_def::{ child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, @@ -11,14 +13,14 @@ use ra_syntax::{ }; use crate::{ - db::{AstDatabase, DefDatabase, HirDatabase}, + db::{DefDatabase, HirDatabase}, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, }; pub trait FromSource: Sized { type Ast; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option; + fn from_source(db: &impl DefDatabase, src: InFile) -> Option; } pub trait FromSourceByContainer: Sized { @@ -32,7 +34,7 @@ where T: From<::Id>, { type Ast = ::Ast; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { + fn from_source(db: &impl DefDatabase, src: InFile) -> Option { analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY] .get(&src) .copied() @@ -64,7 +66,7 @@ from_source_by_container_impls![ impl FromSource for MacroDef { type Ast = ast::MacroCall; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { + fn from_source(db: &impl DefDatabase, src: InFile) -> Option { let kind = MacroDefKind::Declarative; let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); @@ -80,7 +82,7 @@ impl FromSource for MacroDef { impl FromSource for EnumVariant { type Ast = ast::EnumVariant; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { + fn from_source(db: &impl DefDatabase, src: InFile) -> Option { let parent_enum = src.value.parent_enum(); let src_enum = InFile { file_id: src.file_id, value: parent_enum }; let parent_enum = Enum::from_source(db, src_enum)?; @@ -93,7 +95,7 @@ impl FromSource for EnumVariant { impl FromSource for StructField { type Ast = FieldSource; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { + fn from_source(db: &impl DefDatabase, src: InFile) -> Option { let src = src.as_ref(); // FIXME this is buggy diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index d326169b3..b60a6b87e 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -237,7 +237,13 @@ impl SourceAnalyzer { } pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option { - let expr_id = self.expr_id(&field.expr()?)?; + let expr_id = match field.expr() { + Some(it) => self.expr_id(&it)?, + None => { + let src = InFile { file_id: self.file_id, value: field }; + self.body_source_map.as_ref()?.field_init_shorthand_expr(src)? + } + }; self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into()) } -- cgit v1.2.3