Skip to content

Commit

Permalink
test: add test for self field access
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Jun 2, 2024
1 parent 7654e6c commit 57750b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
13 changes: 11 additions & 2 deletions crates/mun_hir/src/code_model/module.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions crates/mun_hir/src/ty/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 57750b6

Please sign in to comment.