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 // we manually inline this for single operations for performance without -prod
[inline] [unsafe_fn] [inline] [unsafe]
fn (a array) get_unsafe(i int) voidptr { fn (a array) get_unsafe(i int) voidptr {
unsafe { unsafe {
return byteptr(a.data) + i * a.element_size 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 // 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) { fn (mut a array) set_unsafe(i int, val voidptr) {
unsafe { unsafe {
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size) 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() { // pub fn (a []int) free() {
[unsafe_fn] [unsafe]
pub fn (a &array) free() { pub fn (a &array) free() {
$if prealloc { $if prealloc {
return return

View File

@ -40,7 +40,7 @@ pub fn mem_copy(dest0 voidptr, src0 voidptr, n int) voidptr {
return dest0 return dest0
} }
[unsafe_fn] [unsafe]
pub fn malloc(n int) byteptr { pub fn malloc(n int) byteptr {
if n < 0 { if n < 0 {
panic('malloc(<0)') panic('malloc(<0)')
@ -52,7 +52,7 @@ pub fn malloc(n int) byteptr {
return ptr return ptr
} }
[unsafe_fn] [unsafe]
pub fn free(ptr voidptr) { pub fn free(ptr voidptr) {
assert mm_free(ptr) == .enoerror assert mm_free(ptr) == .enoerror
} }

View File

@ -136,7 +136,7 @@ __global nr_mallocs int=0
fn looo(){} // TODO remove, [ pratt fn looo(){} // TODO remove, [ pratt
[unsafe_fn] [unsafe]
pub fn malloc(n int) byteptr { pub fn malloc(n int) byteptr {
if n <= 0 { if n <= 0 {
panic('malloc(<=0)') panic('malloc(<=0)')
@ -174,7 +174,7 @@ TODO
//#include <malloc/malloc.h> //#include <malloc/malloc.h>
//fn malloc_size(b byteptr) int //fn malloc_size(b byteptr) int
[unsafe_fn] [unsafe]
pub fn v_realloc(b byteptr, n u32) byteptr { pub fn v_realloc(b byteptr, n u32) byteptr {
$if prealloc { $if prealloc {
unsafe { unsafe {
@ -192,12 +192,12 @@ pub fn v_realloc(b byteptr, n u32) byteptr {
} }
} }
[unsafe_fn] [unsafe]
pub fn v_calloc(n int) byteptr { pub fn v_calloc(n int) byteptr {
return C.calloc(1, n) return C.calloc(1, n)
} }
[unsafe_fn] [unsafe]
pub fn vcalloc(n int) byteptr { pub fn vcalloc(n int) byteptr {
if n < 0 { if n < 0 {
panic('calloc(<=0)') panic('calloc(<=0)')
@ -207,7 +207,7 @@ pub fn vcalloc(n int) byteptr {
return C.calloc(1, n) return C.calloc(1, n)
} }
[unsafe_fn] [unsafe]
pub fn free(ptr voidptr) { pub fn free(ptr voidptr) {
$if prealloc { $if prealloc {
return return

View File

@ -125,7 +125,7 @@ fn C.mktime() int
fn C.gettimeofday() int fn C.gettimeofday() int
[trusted_fn] [trusted]
fn C.sleep(int) int fn C.sleep(int) int
@ -153,11 +153,11 @@ fn C.tolower() int
fn C.toupper() int fn C.toupper() int
[trusted_fn] [trusted]
fn C.getchar() int fn C.getchar() int
[trusted_fn] [trusted]
fn C.strerror(int) charptr fn C.strerror(int) charptr

View File

@ -105,7 +105,7 @@ mut:
} }
[inline] [inline]
[unsafe_fn] [unsafe]
fn new_dense_array(value_bytes int) DenseArray { fn new_dense_array(value_bytes int) DenseArray {
return DenseArray{ return DenseArray{
value_bytes: value_bytes value_bytes: value_bytes
@ -501,7 +501,7 @@ pub fn (m &map) keys() []string {
return keys return keys
} }
[unsafe_fn] [unsafe]
pub fn (d DenseArray) clone() DenseArray { pub fn (d DenseArray) clone() DenseArray {
res := DenseArray { res := DenseArray {
value_bytes: d.value_bytes value_bytes: d.value_bytes
@ -518,7 +518,7 @@ pub fn (d DenseArray) clone() DenseArray {
return res return res
} }
[unsafe_fn] [unsafe]
pub fn (m map) clone() map { pub fn (m map) clone() map {
metas_size := sizeof(u32) * (m.cap + 2 + m.extra_metas) metas_size := sizeof(u32) * (m.cap + 2 + m.extra_metas)
res := map{ res := map{
@ -537,7 +537,7 @@ pub fn (m map) clone() map {
return res return res
} }
[unsafe_fn] [unsafe]
pub fn (m &map) free() { pub fn (m &map) free() {
unsafe { unsafe {
free(m.metas) free(m.metas)

View File

@ -66,14 +66,14 @@ pub mut:
len int len int
} }
[unsafe_fn] [unsafe]
pub fn vstrlen(s byteptr) int { pub fn vstrlen(s byteptr) int {
return unsafe {C.strlen(charptr(s))} return unsafe {C.strlen(charptr(s))}
} }
// Converts a C string to a V string. // Converts a C string to a V string.
// String data is reused, not copied. // String data is reused, not copied.
[unsafe_fn] [unsafe]
pub fn tos(s byteptr, len int) string { pub fn tos(s byteptr, len int) string {
// This should never happen. // This should never happen.
if s == 0 { 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 // 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 { pub fn cstring_to_vstring(cstr byteptr) string {
return tos_clone(cstr) return tos_clone(cstr)
} }
@ -1217,7 +1217,7 @@ pub fn (u ustring) at(idx int) string {
return u.substr(idx, idx + 1) return u.substr(idx, idx + 1)
} }
[unsafe_fn] [unsafe]
fn (u &ustring) free() { fn (u &ustring) free() {
$if prealloc { $if prealloc {
return return

View File

@ -1147,7 +1147,7 @@ pub fn walk(path string, f fn(path string)) {
return return
} }
[unsafe_fn] [unsafe]
pub fn signal(signum int, handler voidptr) { pub fn signal(signum int, handler voidptr) {
unsafe { unsafe {
C.signal(signum, handler) C.signal(signum, handler)

View File

@ -1,6 +1,6 @@
module picohttpparser module picohttpparser
[inline] [unsafe_fn] [inline] [unsafe]
fn cpy(dst, src byteptr, len int) int { fn cpy(dst, src byteptr, len int) int {
unsafe { C.memcpy(dst, src, len) } unsafe { C.memcpy(dst, src, len) }
return len return len

View File

@ -64,7 +64,7 @@ pub fn compile(command string, pref &pref.Preferences) {
} }
// Temporary, will be done by -autofree // Temporary, will be done by -autofree
[unsafe_fn] [unsafe]
fn (mut b Builder) myfree() { fn (mut b Builder) myfree() {
// for file in b.parsed_files { // for file in b.parsed_files {
// } // }

View File

@ -141,7 +141,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
p.open_scope() p.open_scope()
// C. || JS. // C. || JS.
language := if p.tok.kind == .name && p.tok.lit == 'C' { 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 table.Language.c
} else if p.tok.kind == .name && p.tok.lit == 'JS' { } else if p.tok.kind == .name && p.tok.lit == 'JS' {
table.Language.js table.Language.js

View File

@ -742,10 +742,11 @@ fn (mut p Parser) parse_attr() table.Attr {
name = p.tok.lit name = p.tok.lit
p.next() p.next()
} else { } else {
mut name = p.check_name() name = p.check_name()
if name == 'unsafe_fn' { if name == 'unsafe_fn' {
// p.error_with_pos('please use `[unsafe]` instead', p.tok.position()) p.error_with_pos('please use `[unsafe]` instead', p.tok.position())
name = 'unsafe' } else if name == 'trusted_fn' {
p.error_with_pos('please use `[trusted]` instead', p.tok.position())
} }
if p.tok.kind == .colon { if p.tok.kind == .colon {
name += ':' name += ':'

View File

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