From 6cc881593142c928193e69bf23c8c646cebf221a Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 8 Aug 2020 15:24:05 +0100 Subject: [PATCH] parser: support [unsafe] instead of [unsafe_fn] (#6066) --- .../unsafe_pointer_arithmetic_should_be_checked.vv | 2 +- vlib/v/checker/tests/unsafe_required.vv | 2 +- vlib/v/parser/fn.v | 2 +- vlib/v/parser/parser.v | 10 ++++++++++ vlib/v/table/attr.v | 2 +- vlib/v/tests/unsafe_test.v | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/tests/unsafe_pointer_arithmetic_should_be_checked.vv b/vlib/v/checker/tests/unsafe_pointer_arithmetic_should_be_checked.vv index 40903c5079..4e42943fef 100644 --- a/vlib/v/checker/tests/unsafe_pointer_arithmetic_should_be_checked.vv +++ b/vlib/v/checker/tests/unsafe_pointer_arithmetic_should_be_checked.vv @@ -16,7 +16,7 @@ fn test_ptr_infix() { struct S1 {} -[unsafe_fn] +[unsafe] fn (s S1) f(){} fn test_funcs() { diff --git a/vlib/v/checker/tests/unsafe_required.vv b/vlib/v/checker/tests/unsafe_required.vv index ebefff36ad..4f665dd080 100644 --- a/vlib/v/checker/tests/unsafe_required.vv +++ b/vlib/v/checker/tests/unsafe_required.vv @@ -16,7 +16,7 @@ fn test_ptr_infix() { struct S1 {} -[unsafe_fn] +[unsafe] fn (s S1) f(){} fn test_funcs() { diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index f90ebde7dc..781317ed6f 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -132,7 +132,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.top_level_statement_start() start_pos := p.tok.position() is_deprecated := p.attrs.contains('deprecated') - mut is_unsafe := p.attrs.contains('unsafe_fn') + mut is_unsafe := p.attrs.contains('unsafe') is_pub := p.tok.kind == .key_pub if is_pub { p.next() diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 238c665ad5..e4b5cc18d8 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -725,6 +725,12 @@ fn (mut p Parser) attributes() { } fn (mut p Parser) parse_attr() table.Attr { + if p.tok.kind == .key_unsafe { + p.next() + return table.Attr{ + name: 'unsafe' + } + } mut is_ctdefine := false if p.tok.kind == .key_if { p.next() @@ -737,6 +743,10 @@ fn (mut p Parser) parse_attr() table.Attr { p.next() } else { mut name = p.check_name() + if name == 'unsafe_fn' { + // p.error_with_pos('please use `[unsafe]` instead', p.tok.position()) + name = 'unsafe' + } if p.tok.kind == .colon { name += ':' p.next() diff --git a/vlib/v/table/attr.v b/vlib/v/table/attr.v index ad97d5d0c5..134430ed29 100644 --- a/vlib/v/table/attr.v +++ b/vlib/v/table/attr.v @@ -3,7 +3,7 @@ // that can be found in the LICENSE file. module table -// e.g. `[unsafe_fn]` +// e.g. `[unsafe]` pub struct Attr { pub: name string diff --git a/vlib/v/tests/unsafe_test.v b/vlib/v/tests/unsafe_test.v index d0b672c067..f61399fda8 100644 --- a/vlib/v/tests/unsafe_test.v +++ b/vlib/v/tests/unsafe_test.v @@ -33,7 +33,7 @@ fn test_ptr_infix() { struct S1 { } -[unsafe_fn] +[unsafe] fn (s S1) f() { }