From e3ef968033cc92e9e5c0a6434956869e78cb55e7 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 10 Jun 2026 04:43:36 +0200 Subject: [PATCH] Fixed unsafe --- crates/codegen/src/statement.rs | 9 +++++---- crates/parser/src/ast/statement.rs | 3 +-- crates/parser/src/grammar.pest | 9 ++++++--- crates/parser/src/parser/common/expr.rs | 2 +- crates/parser/src/parser/common/statement.rs | 6 +++--- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index 5bf7d58..4d0229f 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -6,10 +6,6 @@ use crate::{GenRust, GetRust, RustCodegen}; impl GenRust for Block { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { - if self.is_unsafe { - cg.add("unsafe "); - } - cg.addln("{"); cg.indent += 1; @@ -33,6 +29,11 @@ impl GenRust for Block { impl GenRust for Statement { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { match self { + Statement::UnsafeBlock(block) => { + cg.add("unsafe "); + block.gen_rust(ctx, cg); + } + Statement::Block(block) => block.gen_rust(ctx, cg), Statement::VarDecl(VarDeclStmt { decl, init }) => { diff --git a/crates/parser/src/ast/statement.rs b/crates/parser/src/ast/statement.rs index 70abc75..4eb581f 100644 --- a/crates/parser/src/ast/statement.rs +++ b/crates/parser/src/ast/statement.rs @@ -4,14 +4,13 @@ use super::*; #[derive(Debug, Clone, Serialize, Default)] pub struct Block { - pub is_unsafe: bool, - pub statements: Vec>, pub soft_return: Option>, } #[derive(Debug, Clone, Serialize)] pub enum Statement { + UnsafeBlock(Block), Block(Block), If { initial: StatementBranch, diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index ac01f75..127d298 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -64,8 +64,6 @@ static_path = { identifier ~ ("::" ~ identifier)* } -unsafe_kw = { "unsafe" } - // ====================================================== // PROGRAM // ====================================================== @@ -345,7 +343,11 @@ top_level = { // ====================================================== block = { - unsafe_kw? ~ "{" ~ statement_list ~ expr? ~ "}" + "{" ~ statement_list ~ expr? ~ "}" +} + +unsafe_block = { + "unsafe" ~ block } statement_list = { @@ -396,6 +398,7 @@ control_flow = { | match_stmt | loop_stmt | block + | unsafe_block } statement_branch = { expr ~ block } diff --git a/crates/parser/src/parser/common/expr.rs b/crates/parser/src/parser/common/expr.rs index ce98a79..c6a67d1 100644 --- a/crates/parser/src/parser/common/expr.rs +++ b/crates/parser/src/parser/common/expr.rs @@ -100,7 +100,7 @@ impl<'a> TryFrom> for Expression { Rule::static_path => ast_expr!(Expression::Path(pair.try_into())), Rule::literal => ast_expr!(Expression::Literal(pair.try_into())), Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())), - Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::block => ast_expr!( + Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::block | Rule::unsafe_block => ast_expr!( Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new)) ), diff --git a/crates/parser/src/parser/common/statement.rs b/crates/parser/src/parser/common/statement.rs index 18c6b0e..0a470b9 100644 --- a/crates/parser/src/parser/common/statement.rs +++ b/crates/parser/src/parser/common/statement.rs @@ -2,8 +2,7 @@ use crate::{ Rule, ast::*, ast_ensure, ast_expr, - error::{AstError, AstResult, IntoErr, collect_recovered}, - parser::listen_rule, + error::{AstError, IntoErr, collect_recovered}, }; impl<'a> TryFrom> for Block { @@ -14,7 +13,6 @@ impl<'a> TryFrom> for Block { ast_ensure!(pair, Rule::block => { ast_expr!(Block { - is_unsafe: Ok(listen_rule(&mut inner, Rule::unsafe_kw)) as AstResult, statements: collect_recovered(inner.next().unwrap().into_inner()), soft_return: inner.next().map(Spanned::try_from).transpose(), }) @@ -49,6 +47,8 @@ impl<'a> TryFrom> for Statement { Statement::try_from(inner.next().unwrap()) } + Rule::unsafe_block => ast_expr!(Statement::UnsafeBlock(inner.next().unwrap().try_into())), + Rule::block => ast_expr!(Statement::Block(pair.try_into())), Rule::var_decl_statement => ast_expr!(Statement::VarDecl(pair.try_into())),