From 0613fc5c535d345198ce8710b126ef169e5ca97e Mon Sep 17 00:00:00 2001 From: sofia Date: Sun, 27 Jul 2025 18:41:51 +0300 Subject: [PATCH] Allow associated functions to take self as owned --- examples/associated_functions.reid | 5 ++--- reid/src/ast/mod.rs | 1 + reid/src/ast/parse.rs | 26 +++++++++++++++++++------- reid/src/ast/process.rs | 1 + 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/associated_functions.reid b/examples/associated_functions.reid index d67b852..46c3274 100644 --- a/examples/associated_functions.reid +++ b/examples/associated_functions.reid @@ -13,16 +13,15 @@ impl Otus { } impl i32 { - fn test(&self) -> u32 { + fn test(self) -> u32 { 43 } } fn main() -> u32 { let otus = Otus { field: 17 }; - let num = 54; print(from_str("otus: ") + Otus::test(&otus) as u64); - print(from_str("i32: ") + i32::test(&num) as u64); + print(from_str("i32: ") + i32::test(54) as u64); return Otus::test(&otus); } diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 4ad0281..caeb2af 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -181,6 +181,7 @@ pub struct FunctionSignature { #[derive(Debug, Clone)] pub enum SelfKind { + Owned(TypeKind), Borrow(TypeKind), MutBorrow(TypeKind), None, diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index fb601d5..9430dc2 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -599,23 +599,34 @@ impl Parse for FunctionParam { #[derive(Debug)] pub struct SelfParam(SelfKind); +pub enum SelfParamKind { + Owned, + Borrow, + BorrowMut, +} + impl Parse for SelfParam { fn parse(mut stream: TokenStream) -> Result { - stream.expect(Token::Et)?; - let mutable = if let Some(Token::MutKeyword) = stream.peek() { + let kind = if let Some(Token::Et) = stream.peek() { stream.next(); - true + if let Some(Token::MutKeyword) = stream.peek() { + stream.next(); + SelfParamKind::BorrowMut + } else { + SelfParamKind::Borrow + } } else { - false + SelfParamKind::Owned }; let Some(Token::Identifier(name)) = stream.next() else { return Err(stream.expected_err("parameter name")?); }; if name == "self" { - match mutable { - true => Ok(SelfParam(SelfKind::MutBorrow(TypeKind::Unknown))), - false => Ok(SelfParam(SelfKind::Borrow(TypeKind::Unknown))), + match kind { + SelfParamKind::BorrowMut => Ok(SelfParam(SelfKind::MutBorrow(TypeKind::Unknown))), + SelfParamKind::Borrow => Ok(SelfParam(SelfKind::Borrow(TypeKind::Unknown))), + SelfParamKind::Owned => Ok(SelfParam(SelfKind::Owned(TypeKind::Unknown))), } } else { Err(stream.expected_err("self parameter")?) @@ -984,6 +995,7 @@ impl Parse for AssociatedFunctionBlock { while let Some(Token::FnKeyword) = stream.peek() { let mut fun: FunctionDefinition = stream.parse()?; fun.0.self_kind = match fun.0.self_kind { + SelfKind::Owned(_) => SelfKind::Owned(ty.0.clone()), SelfKind::Borrow(_) => SelfKind::Borrow(ty.0.clone()), SelfKind::MutBorrow(_) => SelfKind::MutBorrow(ty.0.clone()), SelfKind::None => SelfKind::None, diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 8c60cc3..29ef2e4 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -136,6 +136,7 @@ impl ast::FunctionDefinition { "self".to_owned(), mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true), )), + ast::SelfKind::Owned(type_kind) => params.push(("self".to_owned(), type_kind.into_mir(module_id))), ast::SelfKind::None => {} }