diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index aace95be32..d14b11224d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -13,10 +13,10 @@ FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | Selecto AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | CastExpr | EnumVal | Assoc | SizeOf | None | MapInit -pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | -ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | -HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | -LineComment | MultiLineComment | AssertStmt +pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | +ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | +HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | +LineComment | MultiLineComment | AssertStmt | UnsafeStmt pub type Type = StructType | ArrayType @@ -421,6 +421,11 @@ pub: stmts []Stmt } +pub struct UnsafeStmt { +pub: + stmts []Stmt +} + pub struct AssignExpr { pub: op token.Kind diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 1d3dcbb061..6ebc2e98e9 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -191,6 +191,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) { ast.StructDecl { f.struct_decl(it) } + ast.UnsafeStmt { + f.writeln('unsafe {') + f.stmts(it.stmts) + f.writeln('}') + } ast.VarDecl { // type_sym := f.table.get_type_symbol(it.typ) if it.is_mut { diff --git a/vlib/v/fmt/tests/simple_expected.vv b/vlib/v/fmt/tests/simple_expected.vv index f01413ddc2..a00db4400a 100644 --- a/vlib/v/fmt/tests/simple_expected.vv +++ b/vlib/v/fmt/tests/simple_expected.vv @@ -118,3 +118,9 @@ fn fn_with_assign_stmts() { fn fn_with_multi_return() (int, string) { return 0, 'test' } + +fn unsafe_fn() { + unsafe { + malloc(2) + } +} diff --git a/vlib/v/fmt/tests/simple_input.vv b/vlib/v/fmt/tests/simple_input.vv index 91fe2269d7..61a42501ad 100644 --- a/vlib/v/fmt/tests/simple_input.vv +++ b/vlib/v/fmt/tests/simple_input.vv @@ -123,3 +123,6 @@ fn fn_with_multi_return() (int,string) { return 0,'test' } +fn unsafe_fn() { +unsafe { malloc(2) } +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a093fb1aa4..b0c5f7b6f0 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -291,8 +291,10 @@ pub fn (p mut Parser) stmt() ast.Stmt { } .key_unsafe { p.next() - p.parse_block() - return ast.Stmt{} + stmts := p.parse_block() + return ast.UnsafeStmt{ + stmts: stmts + } } .key_defer { p.next()