parser: error on `[unsafe_fn]` (#6090)

pull/6100/head
Nick Treleaven 2020-08-09 10:22:11 +01:00 committed by GitHub
parent 200f8dacb7
commit 2dd90de993
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 30 additions and 28 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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 <malloc/malloc.h>
//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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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 {
// }

View File

@ -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

View File

@ -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 += ':'

View File

@ -42,4 +42,5 @@ fn test_funcs() {
unsafe {
s.f()
}
_ = C.strerror(0) // [trusted] function prototype in builtin/cfns.c.v
}