diff --git a/crates/mun_hir/src/code_model/module.rs b/crates/mun_hir/src/code_model/module.rs index 195c2a57..f2f31e91 100644 --- a/crates/mun_hir/src/code_model/module.rs +++ b/crates/mun_hir/src/code_model/module.rs @@ -1,4 +1,4 @@ -use super::{r#impl::Impl, Function, Package, Struct, TypeAlias}; +use super::{r#impl::Impl, AssocItem, Function, Package, Struct, TypeAlias}; use crate::{ ids::{ItemDefinitionId, ModuleId}, primitive_type::PrimitiveType, @@ -81,7 +81,7 @@ impl Module { let package_defs = db.package_defs(self.id.package); package_defs.add_diagnostics(db.upcast(), self.id.local_id, sink); - // Add diagnostics from impls + // Add diagnostics from inherent impls let inherent_impls = db.inherent_impls_in_package(self.id.package); inherent_impls.add_module_diagnostics(db, self.id.local_id, sink); @@ -102,6 +102,15 @@ impl Module { _ => (), } } + + // Add diagnostics from impls + for item in self.impls(db) { + for associated_item in item.items(db) { + let AssocItem::Function(fun) = associated_item; + + fun.diagnostics(db, sink); + } + } } /// Returns all the child modules of this module diff --git a/crates/mun_hir/src/ty/tests.rs b/crates/mun_hir/src/ty/tests.rs index 4d0cba0c..5b123b9f 100644 --- a/crates/mun_hir/src/ty/tests.rs +++ b/crates/mun_hir/src/ty/tests.rs @@ -673,6 +673,27 @@ fn infer_self_param() { )); } +#[test] +fn infer_self_field() { + insta::assert_snapshot!(infer( + r#" + struct Foo { + a: i32 + } + + impl Foo { + fn self_a(self) -> i32 { + self.a + } + + fn self_b(self) -> i32 { + self.b // error: attempted to access a non-existent field in a struct. + } + } + "# + )); +} + #[test] fn infer_basics() { insta::assert_snapshot!(infer(