Refactored mutability

This commit is contained in:
2026-05-28 11:45:26 +02:00
parent 3444f93546
commit 7007aa32f1
9 changed files with 3 additions and 20 deletions
-1
View File
@@ -234,7 +234,6 @@ impl GetRust for TypeExprKind {
impl GenRust for Pattern { impl GenRust for Pattern {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
Self::Id(id) => cg.add(&id.get_rust()),
Self::Path(mutable, path) => { Self::Path(mutable, path) => {
if *mutable { if *mutable {
cg.add("mut "); cg.add("mut ");
-1
View File
@@ -158,7 +158,6 @@ impl GenRust for Statement {
} }
Statement::For { Statement::For {
mutable,
pattern, pattern,
iterator, iterator,
body, body,
+1 -2
View File
@@ -212,8 +212,7 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
cg.add_indentedln("}\n"); cg.add_indentedln("}\n");
let mut constructor_params = vec![VarDecl { let mut constructor_params = vec![VarDecl {
mutable: false, name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])),
name: Pattern::Id(Identifier(String::from("self"))),
type_: Some(TypeExpr( type_: Some(TypeExpr(
TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))])), TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))])),
vec![TypePostfix::RefMut], vec![TypePostfix::RefMut],
-1
View File
@@ -37,7 +37,6 @@ pub enum Pattern {
Tuple(Vec<Identifier>), Tuple(Vec<Identifier>),
Literal(Literal), Literal(Literal),
Path(bool, Path), Path(bool, Path),
Id(Identifier),
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
-2
View File
@@ -31,7 +31,6 @@ pub enum Statement {
body: StatementBody, body: StatementBody,
}, },
For { For {
mutable: bool,
pattern: Pattern, pattern: Pattern,
iterator: Expression, iterator: Expression,
body: StatementBody, body: StatementBody,
@@ -46,7 +45,6 @@ pub enum Statement {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct VarDecl { pub struct VarDecl {
pub mutable: bool,
pub name: Pattern, pub name: Pattern,
pub type_: Option<TypeExpr>, pub type_: Option<TypeExpr>,
} }
+1 -5
View File
@@ -2,8 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_expr, ast_expr,
error::{AstError, AstResult, IntoErr}, error::{AstError, IntoErr},
parser::listen_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
@@ -63,13 +62,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
}) })
.transpose(); .transpose();
let mutable: AstResult<'_, bool> = Ok(listen_rule(&mut inner, Rule::mutable));
let name = Pattern::try_from(inner.next().unwrap()); let name = Pattern::try_from(inner.next().unwrap());
ast_expr!(VarDecl { ast_expr!(VarDecl {
type_: type_, type_: type_,
mutable: mutable,
name: name, name: name,
}) })
} }
-2
View File
@@ -74,8 +74,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
Rule::literal => ast_expr!(Pattern::Literal(pair.try_into())), Rule::literal => ast_expr!(Pattern::Literal(pair.try_into())),
Rule::identifier => ast_expr!(Pattern::Id(pair.try_into())),
Rule::path_pattern => ast_expr!(Pattern::Path( Rule::path_pattern => ast_expr!(Pattern::Path(
Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<'_, bool>, Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<'_, bool>,
inner.next().unwrap().try_into() inner.next().unwrap().try_into()
@@ -3,7 +3,6 @@ use crate::{
ast::*, ast::*,
ast_ensure, ast_expr, ast_ensure, ast_expr,
error::{AstError, AstResult, IntoErr, collect_recovered}, error::{AstError, AstResult, IntoErr, collect_recovered},
parser::listen_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
@@ -97,7 +96,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
}), }),
Rule::for_stmt => ast_expr!(Statement::For { Rule::for_stmt => ast_expr!(Statement::For {
mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<'_, bool>,
pattern: inner.next().unwrap().try_into(), pattern: inner.next().unwrap().try_into(),
iterator: inner.next().unwrap().try_into(), iterator: inner.next().unwrap().try_into(),
body: inner.next().unwrap().try_into(), body: inner.next().unwrap().try_into(),
+1 -4
View File
@@ -23,14 +23,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| { let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
let mut param_inner = param.into_inner(); let mut param_inner = param.into_inner();
let name = Pattern::Id(Identifier(String::from("self")));
let mutable = listen_rule(&mut param_inner, Rule::mutable); let mutable = listen_rule(&mut param_inner, Rule::mutable);
let is_ref = listen_rule(&mut param_inner, Rule::deref_px); let is_ref = listen_rule(&mut param_inner, Rule::deref_px);
let name = Pattern::Path(mutable && !is_ref, Path(vec![Identifier(String::from("self"))]));
VarDecl { VarDecl {
mutable: mutable && !is_ref,
name: name.clone(), name: name.clone(),
type_: Some(TypeExpr( type_: Some(TypeExpr(
TypeExprKind::Path(Path(vec![Identifier("Self".to_string())])), TypeExprKind::Path(Path(vec![Identifier("Self".to_string())])),