From 2dd90de993abe69f7f85d4b67acbe4f325a7e471 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 9 Aug 2020 10:22:11 +0100 Subject: [PATCH] parser: error on `[unsafe_fn]` (#6090) --- vlib/builtin/array.v | 6 +++--- vlib/builtin/bare/mm_bare.v | 4 ++-- vlib/builtin/builtin.v | 10 +++++----- vlib/builtin/cfns.c.v | 6 +++--- vlib/builtin/map.v | 8 ++++---- vlib/builtin/string.v | 8 ++++---- vlib/os/os.v | 2 +- vlib/picohttpparser/misc.v | 2 +- vlib/v/builder/compile.v | 2 +- vlib/v/parser/fn.v | 2 +- vlib/v/parser/parser.v | 7 ++++--- vlib/v/tests/unsafe_test.v | 1 + 12 files changed, 30 insertions(+), 28 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 3154dbda53..bc5701b81c 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -219,7 +219,7 @@ pub fn (mut a array) trim(index int) { } // we manually inline this for single operations for performance without -prod -[inline] [unsafe_fn] +[inline] [unsafe] fn (a array) get_unsafe(i int) voidptr { unsafe { return byteptr(a.data) + i * a.element_size @@ -379,7 +379,7 @@ fn (a &array) slice_clone(start, _end int) array { } // we manually inline this for single operations for performance without -prod -[inline] [unsafe_fn] +[inline] [unsafe] fn (mut a array) set_unsafe(i int, val voidptr) { unsafe { C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size) @@ -462,7 +462,7 @@ pub fn (a array) reverse() array { } // pub fn (a []int) free() { -[unsafe_fn] +[unsafe] pub fn (a &array) free() { $if prealloc { return diff --git a/vlib/builtin/bare/mm_bare.v b/vlib/builtin/bare/mm_bare.v index 0fe6c655e8..890358221e 100644 --- a/vlib/builtin/bare/mm_bare.v +++ b/vlib/builtin/bare/mm_bare.v @@ -40,7 +40,7 @@ pub fn mem_copy(dest0 voidptr, src0 voidptr, n int) voidptr { return dest0 } -[unsafe_fn] +[unsafe] pub fn malloc(n int) byteptr { if n < 0 { panic('malloc(<0)') @@ -52,7 +52,7 @@ pub fn malloc(n int) byteptr { return ptr } -[unsafe_fn] +[unsafe] pub fn free(ptr voidptr) { assert mm_free(ptr) == .enoerror } diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 3e564862bb..363ab020f9 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -136,7 +136,7 @@ __global nr_mallocs int=0 fn looo(){} // TODO remove, [ pratt -[unsafe_fn] +[unsafe] pub fn malloc(n int) byteptr { if n <= 0 { panic('malloc(<=0)') @@ -174,7 +174,7 @@ TODO //#include //fn malloc_size(b byteptr) int -[unsafe_fn] +[unsafe] pub fn v_realloc(b byteptr, n u32) byteptr { $if prealloc { unsafe { @@ -192,12 +192,12 @@ pub fn v_realloc(b byteptr, n u32) byteptr { } } -[unsafe_fn] +[unsafe] pub fn v_calloc(n int) byteptr { return C.calloc(1, n) } -[unsafe_fn] +[unsafe] pub fn vcalloc(n int) byteptr { if n < 0 { panic('calloc(<=0)') @@ -207,7 +207,7 @@ pub fn vcalloc(n int) byteptr { return C.calloc(1, n) } -[unsafe_fn] +[unsafe] pub fn free(ptr voidptr) { $if prealloc { return diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index 34fd045655..0777cbe5f9 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -125,7 +125,7 @@ fn C.mktime() int fn C.gettimeofday() int -[trusted_fn] +[trusted] fn C.sleep(int) int @@ -153,11 +153,11 @@ fn C.tolower() int fn C.toupper() int -[trusted_fn] +[trusted] fn C.getchar() int -[trusted_fn] +[trusted] fn C.strerror(int) charptr diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 896c3c2e38..27d7a62b1a 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -105,7 +105,7 @@ mut: } [inline] -[unsafe_fn] +[unsafe] fn new_dense_array(value_bytes int) DenseArray { return DenseArray{ value_bytes: value_bytes @@ -501,7 +501,7 @@ pub fn (m &map) keys() []string { return keys } -[unsafe_fn] +[unsafe] pub fn (d DenseArray) clone() DenseArray { res := DenseArray { value_bytes: d.value_bytes @@ -518,7 +518,7 @@ pub fn (d DenseArray) clone() DenseArray { return res } -[unsafe_fn] +[unsafe] pub fn (m map) clone() map { metas_size := sizeof(u32) * (m.cap + 2 + m.extra_metas) res := map{ @@ -537,7 +537,7 @@ pub fn (m map) clone() map { return res } -[unsafe_fn] +[unsafe] pub fn (m &map) free() { unsafe { free(m.metas) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index c495795981..ec96bb673f 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -66,14 +66,14 @@ pub mut: len int } -[unsafe_fn] +[unsafe] pub fn vstrlen(s byteptr) int { return unsafe {C.strlen(charptr(s))} } // Converts a C string to a V string. // String data is reused, not copied. -[unsafe_fn] +[unsafe] pub fn tos(s byteptr, len int) string { // This should never happen. if s == 0 { @@ -147,7 +147,7 @@ pub fn (s string) cstr() byteptr { */ // cstring_to_vstring creates a copy of cstr and turns it into a v string -[unsafe_fn] +[unsafe] pub fn cstring_to_vstring(cstr byteptr) string { return tos_clone(cstr) } @@ -1217,7 +1217,7 @@ pub fn (u ustring) at(idx int) string { return u.substr(idx, idx + 1) } -[unsafe_fn] +[unsafe] fn (u &ustring) free() { $if prealloc { return diff --git a/vlib/os/os.v b/vlib/os/os.v index cb37ba4498..e28e5822d4 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -1147,7 +1147,7 @@ pub fn walk(path string, f fn(path string)) { return } -[unsafe_fn] +[unsafe] pub fn signal(signum int, handler voidptr) { unsafe { C.signal(signum, handler) diff --git a/vlib/picohttpparser/misc.v b/vlib/picohttpparser/misc.v index 038a2f25a3..38b53de86a 100644 --- a/vlib/picohttpparser/misc.v +++ b/vlib/picohttpparser/misc.v @@ -1,6 +1,6 @@ module picohttpparser -[inline] [unsafe_fn] +[inline] [unsafe] fn cpy(dst, src byteptr, len int) int { unsafe { C.memcpy(dst, src, len) } return len diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index cdb2c93662..590c961356 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -64,7 +64,7 @@ pub fn compile(command string, pref &pref.Preferences) { } // Temporary, will be done by -autofree -[unsafe_fn] +[unsafe] fn (mut b Builder) myfree() { // for file in b.parsed_files { // } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 781317ed6f..b399d7d6cb 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -141,7 +141,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.open_scope() // C. || JS. language := if p.tok.kind == .name && p.tok.lit == 'C' { - is_unsafe = !p.attrs.contains('trusted_fn') + is_unsafe = !p.attrs.contains('trusted') table.Language.c } else if p.tok.kind == .name && p.tok.lit == 'JS' { table.Language.js diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e4b5cc18d8..81a27ae6c0 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -742,10 +742,11 @@ fn (mut p Parser) parse_attr() table.Attr { name = p.tok.lit p.next() } else { - mut name = p.check_name() + name = p.check_name() if name == 'unsafe_fn' { - // p.error_with_pos('please use `[unsafe]` instead', p.tok.position()) - name = 'unsafe' + p.error_with_pos('please use `[unsafe]` instead', p.tok.position()) + } else if name == 'trusted_fn' { + p.error_with_pos('please use `[trusted]` instead', p.tok.position()) } if p.tok.kind == .colon { name += ':' diff --git a/vlib/v/tests/unsafe_test.v b/vlib/v/tests/unsafe_test.v index f61399fda8..6080a2718b 100644 --- a/vlib/v/tests/unsafe_test.v +++ b/vlib/v/tests/unsafe_test.v @@ -42,4 +42,5 @@ fn test_funcs() { unsafe { s.f() } + _ = C.strerror(0) // [trusted] function prototype in builtin/cfns.c.v }