From 57750b605154fbebf9833e8dd986d6e7a1a004e9 Mon Sep 17 00:00:00 2001 From: Wodann Date: Sun, 2 Jun 2024 04:39:38 +0000 Subject: [PATCH] test: add test for self field access --- crates/mun_hir/src/code_model/module.rs | 13 +++++++++++-- crates/mun_hir/src/ty/tests.rs | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) 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(