diff --git a/crates/parser/src/semantics.rs b/crates/parser/src/semantics.rs index ac9f95b..a90706c 100644 --- a/crates/parser/src/semantics.rs +++ b/crates/parser/src/semantics.rs @@ -54,20 +54,25 @@ pub fn check_class_semantics<'a>(top_level: &TopLevel) -> Result<(), Vec Option Option { + match initial { + Expression::Path(v) => { + if v.0.first()?.ident.0 == "self" { + if let Some(Postfix::FieldAccess(method, _)) = postfixes.first() { + if matches!(postfixes.get(1), Some(Postfix::Call(_))) { + return Some(method.clone()); + } + } + } + } + _ => {} + } + None +} + impl Expression { fn get_self_ref(&self) -> Option { match self { @@ -265,21 +286,46 @@ impl GetMutability for Expression { } => { let mut result = Vec::new(); + // &mut self.field pattern if prefixes.iter().any(|p| matches!(p, Prefix::RefMut)) { if let Some(self_ref) = get_self_ref(initial, postfixes) { result.push(self_ref); } } + // Recurse into all postfix sub-expressions for postfix in postfixes { - if let Postfix::Assign(_, rhs) = postfix { - if let Some(self_ref) = get_self_ref(initial, postfixes) { - result.push(self_ref); + match postfix { + Postfix::Assign(_, rhs) => { + if let Some(self_ref) = get_self_ref(initial, postfixes) { + result.push(self_ref); + } + result.append(&mut rhs.get_mutability()); } - result.append(&mut rhs.get_mutability()); + Postfix::Call(args) => { + for arg in args { + result.append(&mut arg.get_mutability()); + } + } + Postfix::StructCall(fields) => { + for (_, expr) in fields { + if let Some(expr) = expr { + result.append(&mut expr.get_mutability()); + } + } + } + Postfix::Index(expr) => { + result.append(&mut expr.get_mutability()); + } + _ => {} } } + // Method calls on self: self.method_name(args) + if let Some(method) = get_self_method_call(initial, postfixes) { + result.push(method); + } + result } Self::Closure { body, .. } => body.get_mutability(), @@ -289,5 +335,5 @@ impl GetMutability for Expression { } pub fn mutates_lhs(op: &str) -> bool { - matches!(op, "=" | "->" | "+=" | "-=" | "*=" | "/=") + matches!(op, "=" | "->") } diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index 65310fa..3cb41b9 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -2,16 +2,9 @@ pub module class_test; pub class Animal { pub name String; - pub val i32; constructor() { self.name = "Rex".to_string(); - - self.init_v(); - } - - pub fn void init_v(*mut self) { - self.val = 0; } pub fn String speak(*self) {