1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: wrap up unsafe { nil } (p. 3)

This commit is contained in:
Alexander Medvednikov 2022-07-21 21:01:30 +03:00
parent a68d03ac59
commit 9099594a49
51 changed files with 86 additions and 86 deletions

View File

@ -72,7 +72,7 @@ fn (mut a array) ensure_cap_noscan(required int) {
} }
new_size := u64(cap) * u64(a.element_size) new_size := u64(cap) * u64(a.element_size)
new_data := vcalloc_noscan(new_size) new_data := vcalloc_noscan(new_size)
if a.data != voidptr(0) { if a.data != unsafe { nil } {
unsafe { vmemcpy(new_data, a.data, u64(a.len) * u64(a.element_size)) } unsafe { vmemcpy(new_data, a.data, u64(a.len) * u64(a.element_size)) }
// TODO: the old data may be leaked when no GC is used (ref-counting?) // TODO: the old data may be leaked when no GC is used (ref-counting?)
} }

View File

@ -653,4 +653,4 @@ pub fn print_backtrace() {
__global g_main_argc = int(0) __global g_main_argc = int(0)
[markused] [markused]
__global g_main_argv = voidptr(0) __global g_main_argv = unsafe { nil }

View File

@ -267,7 +267,7 @@ fn break_if_debugger_attached() {
$if tinyc { $if tinyc {
unsafe { unsafe {
mut ptr := &voidptr(0) mut ptr := &voidptr(0)
*ptr = voidptr(0) *ptr = nil
_ = ptr _ = ptr
} }
} $else { } $else {

View File

@ -4,7 +4,7 @@ fn test_isnil_byteptr() {
} }
fn test_isnil_voidptr() { fn test_isnil_voidptr() {
pv := voidptr(0) pv := unsafe { nil }
assert isnil(pv) assert isnil(pv)
} }

View File

@ -20,7 +20,7 @@ fn (mut m map) internal_set(key JS.Any, val JS.Any) {
} }
fn (mut m map) internal_get(key JS.Any) JS.Any { fn (mut m map) internal_get(key JS.Any) JS.Any {
mut val := JS.Any(voidptr(0)) mut val := JS.Any(unsafe { nil })
//$if es5 { //$if es5 {
#if (typeof key != "string" && '$toJS' in key) key = key.$toJS(); #if (typeof key != "string" && '$toJS' in key) key = key.$toJS();
#val = m.val.map[key] #val = m.val.map[key]

View File

@ -43,11 +43,11 @@ fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) {
if e == .enoerror { if e == .enoerror {
return a, size, 0 return a, size, 0
} }
return voidptr(0), 0, 0 return unsafe { nil }, 0, 0
} }
fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr { fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr {
return voidptr(0) return unsafe { nil }
} }
fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool { fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool {
@ -87,6 +87,6 @@ fn get_linux_allocator() dlmalloc.Allocator {
can_release_part: system_can_release_part can_release_part: system_can_release_part
allocates_zeros: system_allocates_zeros allocates_zeros: system_allocates_zeros
page_size: system_page_size page_size: system_page_size
data: voidptr(0) data: unsafe { nil }
} }
} }

View File

@ -488,7 +488,7 @@ fn (mut m map) get_and_set(key voidptr, zero voidptr) voidptr {
// Key not found, insert key with zero-value // Key not found, insert key with zero-value
m.set(key, zero) m.set(key, zero)
} }
return voidptr(0) return unsafe { nil }
} }
// If `key` matches the key of an element in the container, // If `key` matches the key of an element in the container,

View File

@ -210,8 +210,8 @@ fn test_various_map_value() {
// m13['test'] = rune(0) // m13['test'] = rune(0)
// assert m13['test'] == rune(0) // assert m13['test'] == rune(0)
mut m14 := map[string]voidptr{} mut m14 := map[string]voidptr{}
m14['test'] = voidptr(0) m14['test'] = unsafe { nil }
assert m14['test'] == voidptr(0) assert m14['test'] == unsafe { nil }
mut m15 := map[string]&u8{} mut m15 := map[string]&u8{}
m15['test'] = &u8(0) m15['test'] = &u8(0)
assert m15['test'] == &u8(0) assert m15['test'] == &u8(0)

View File

@ -65,7 +65,7 @@ fn vmemory_block_malloc(n isize) &byte {
[unsafe] [unsafe]
fn prealloc_vinit() { fn prealloc_vinit() {
unsafe { unsafe {
g_memory_block = vmemory_block_new(voidptr(0), prealloc_block_size) g_memory_block = vmemory_block_new(nil, prealloc_block_size)
$if !freestanding { $if !freestanding {
C.atexit(prealloc_vcleanup) C.atexit(prealloc_vcleanup)
} }

View File

@ -148,7 +148,7 @@ fn bare_backtrace() string {
fn __exit(code int) { fn __exit(code int) {
unsafe { unsafe {
// the only way to abort process execution in WASM // the only way to abort process execution in WASM
mut x := &int(voidptr(0)) mut x := &int(nil)
*x = code *x = code
} }
for {} for {}

View File

@ -14,7 +14,7 @@ mut:
struct Transition { struct Transition {
mut: mut:
to string to string
condition_handler ConditionFn = voidptr(0) condition_handler ConditionFn = unsafe { nil }
} }
pub struct StateMachine { pub struct StateMachine {

View File

@ -87,14 +87,14 @@ pub fn (mut list LinkedList<T>) pop() ?T {
if unsafe { node.next == 0 } { if unsafe { node.next == 0 } {
// first node case // first node case
// set to null // set to null
list.head = voidptr(0) list.head = unsafe { nil }
} else { } else {
for unsafe { node.next.next != 0 } { for unsafe { node.next.next != 0 } {
node = node.next node = node.next
} }
to_return = node.next.data to_return = node.next.data
// set to null // set to null
node.next = voidptr(0) node.next = unsafe { nil }
} }
list.len -= 1 list.len -= 1
return to_return return to_return

View File

@ -55,22 +55,22 @@ fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) {
unsafe { unsafe {
mem_prot := MemProt(int(MemProt.prot_read) | int(MemProt.prot_write)) mem_prot := MemProt(int(MemProt.prot_read) | int(MemProt.prot_write))
map_flags := MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous)) map_flags := MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous))
addr := C.mmap(voidptr(0), size, int(mem_prot), int(map_flags), -1, 0) addr := C.mmap(nil, size, int(mem_prot), int(map_flags), -1, 0)
if addr == voidptr(-1) { if addr == voidptr(-1) {
return voidptr(0), 0, 0 return nil, 0, 0
} else { } else {
return addr, size, 0 return addr, size, 0
} }
} }
} $else { } $else {
return voidptr(0), 0, 0 return unsafe { nil }, 0, 0
} }
return voidptr(0), 0, 0 return unsafe { nil }, 0, 0
} }
fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr { fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr {
return voidptr(0) return unsafe { nil }
} }
fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool { fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool {
@ -121,6 +121,6 @@ pub fn get_system_allocator() Allocator {
can_release_part: system_can_release_part can_release_part: system_can_release_part
allocates_zeros: system_allocates_zeros allocates_zeros: system_allocates_zeros
page_size: system_page_size page_size: system_page_size
data: voidptr(0) data: unsafe { nil }
} }
} }

View File

@ -1,11 +1,11 @@
module dlmalloc module dlmalloc
fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) { fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) {
return voidptr(0), 0, 0 return unsafe { nil }, 0, 0
} }
fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr { fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr {
return voidptr(0) return unsafe { nil }
} }
fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool { fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool {
@ -37,6 +37,6 @@ pub fn get_system_allocator() Allocator {
can_release_part: system_can_release_part can_release_part: system_can_release_part
allocates_zeros: system_allocates_zeros allocates_zeros: system_allocates_zeros
page_size: system_page_size page_size: system_page_size
data: voidptr(0) data: unsafe { nil }
} }
} }

View File

@ -20,7 +20,7 @@ mut:
struct EventHandler { struct EventHandler {
name string name string
handler EventHandlerFn handler EventHandlerFn
receiver voidptr = voidptr(0) receiver voidptr = unsafe { nil }
once bool once bool
} }

View File

@ -10,7 +10,7 @@ pub struct DrawImageConfig {
pub: pub:
flip_x bool flip_x bool
flip_y bool flip_y bool
img &Image = voidptr(0) img &Image = unsafe { nil }
img_id int img_id int
img_rect Rect // defines the size and position on image when rendering to the screen img_rect Rect // defines the size and position on image when rendering to the screen
part_rect Rect // defines the size and position of part of the image to use when rendering part_rect Rect // defines the size and position of part of the image to use when rendering

View File

@ -343,7 +343,7 @@ pub const (
) )
pub fn window() JS.Window { pub fn window() JS.Window {
mut x := JS.Any(voidptr(0)) mut x := JS.Any(unsafe { nil })
#x = window; #x = window;
return x return x
@ -359,7 +359,7 @@ pub type EventCallback = fn (JS.Event)
// that is EventTarget. When you need access only to Event itself you can just use `fn (JS.Event)` as listener. // that is EventTarget. When you need access only to Event itself you can just use `fn (JS.Event)` as listener.
pub fn event_listener(callback fn (JS.EventTarget, JS.Event)) EventCallback { pub fn event_listener(callback fn (JS.EventTarget, JS.Event)) EventCallback {
return fn [callback] (event JS.Event) { return fn [callback] (event JS.Event) {
mut target := JS.EventTarget(voidptr(0)) mut target := JS.EventTarget(unsafe { nil })
#target = this; #target = this;
callback(target, event) callback(target, event)
} }
@ -490,7 +490,7 @@ pub type OnDeviceOrientation = fn (ev JS.DeviceOrientationEvent) JS.Any
pub fn on_device_motion(cb fn (win JS.Window, ev JS.DeviceMotionEvent) JS.Any) OnDeviceMotion { pub fn on_device_motion(cb fn (win JS.Window, ev JS.DeviceMotionEvent) JS.Any) OnDeviceMotion {
clos := fn [cb] (ev JS.DeviceMotionEvent) JS.Any { clos := fn [cb] (ev JS.DeviceMotionEvent) JS.Any {
mut win := JS.Any(voidptr(0)) mut win := JS.Any(unsafe { nil })
#win = this; #win = this;
return cb(win, ev) return cb(win, ev)
@ -500,7 +500,7 @@ pub fn on_device_motion(cb fn (win JS.Window, ev JS.DeviceMotionEvent) JS.Any) O
pub fn on_device_orientation(cb fn (win JS.Window, ev JS.DeviceOrientationEvent) JS.Any) OnDeviceOrientation { pub fn on_device_orientation(cb fn (win JS.Window, ev JS.DeviceOrientationEvent) JS.Any) OnDeviceOrientation {
clos := fn [cb] (ev JS.DeviceOrientationEvent) JS.Any { clos := fn [cb] (ev JS.DeviceOrientationEvent) JS.Any {
mut win := JS.Any(voidptr(0)) mut win := JS.Any(unsafe { nil })
#win = this; #win = this;
return cb(win, ev) return cb(win, ev)
@ -837,7 +837,7 @@ pub fn event_type(ev JS.Event) string {
} }
pub fn create_event(typ string, bubbles bool, cancelable bool, composed bool) JS.Event { pub fn create_event(typ string, bubbles bool, cancelable bool, composed bool) JS.Event {
mut ev := JS.Event(voidptr(0)) mut ev := JS.Event(unsafe { nil })
#ev = new Event(typ.str,bubbles.val,cancelable.val,composed.val); #ev = new Event(typ.str,bubbles.val,cancelable.val,composed.val);
return ev return ev

View File

@ -23,7 +23,7 @@ pub interface JS.Response {
} }
pub fn fetch(input string, init map[string]JS.Any) promise.Promise<JS.Response, JS.String> { pub fn fetch(input string, init map[string]JS.Any) promise.Promise<JS.Response, JS.String> {
p_init := JS.Any(voidptr(0)) p_init := JS.Any(unsafe { nil })
p := promise.Promise<JS.Response, String>{p_init} p := promise.Promise<JS.Response, String>{p_init}
#let obj = {}; for (let [key,val] of init.map) { obj[key] = val; } #let obj = {}; for (let [key,val] of init.map) { obj[key] = val; }

View File

@ -158,7 +158,7 @@ pub fn (mut conn Connection) set_option(option_type int, val voidptr) {
// get_option - return the value of an option, settable by `set_option`. // get_option - return the value of an option, settable by `set_option`.
// https://dev.mysql.com/doc/c-api/5.7/en/mysql-get-option.html // https://dev.mysql.com/doc/c-api/5.7/en/mysql-get-option.html
pub fn (conn &Connection) get_option(option_type int) ?voidptr { pub fn (conn &Connection) get_option(option_type int) ?voidptr {
ret := voidptr(0) ret := unsafe { nil }
if C.mysql_get_option(conn.conn, option_type, &ret) != 0 { if C.mysql_get_option(conn.conn, option_type, &ret) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn)) return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
} }

View File

@ -33,7 +33,7 @@ pub fn getenv_opt(key string) ?string {
return string_from_wide(s) return string_from_wide(s)
} $else { } $else {
s := C.getenv(&char(key.str)) s := C.getenv(&char(key.str))
if s == voidptr(0) { if s == nil {
return none return none
} }
// Note: C.getenv *requires* that the result be copied. // Note: C.getenv *requires* that the result be copied.

View File

@ -264,7 +264,7 @@ pub fn vfopen(path string, mode string) ?&C.FILE {
if path.len == 0 { if path.len == 0 {
return error('vfopen called with ""') return error('vfopen called with ""')
} }
mut fp := voidptr(0) mut fp := unsafe { nil }
$if windows { $if windows {
fp = C._wfopen(path.to_wide(), mode.to_wide()) fp = C._wfopen(path.to_wide(), mode.to_wide())
} $else { } $else {

View File

@ -271,7 +271,7 @@ const (
// ptr_win_get_error_msg return string (voidptr) // ptr_win_get_error_msg return string (voidptr)
// representation of error, only for windows. // representation of error, only for windows.
fn ptr_win_get_error_msg(code u32) voidptr { fn ptr_win_get_error_msg(code u32) voidptr {
mut buf := voidptr(0) mut buf := unsafe { nil }
// Check for code overflow // Check for code overflow
if code > u32(os.max_error_code) { if code > u32(os.max_error_code) {
return buf return buf

View File

@ -190,7 +190,8 @@ fn (mut p Process) win_read_string(idx int, maxbytes int) (string, int) {
return '', 0 return '', 0
} }
mut bytes_avail := int(0) mut bytes_avail := int(0)
if !C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) { if !C.PeekNamedPipe(rhandle, unsafe { nil }, int(0), unsafe { nil }, &bytes_avail,
unsafe { nil }) {
return '', 0 return '', 0
} }
if bytes_avail == 0 { if bytes_avail == 0 {

View File

@ -57,7 +57,7 @@ pub:
port int = 8080 port int = 8080
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response)
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
user_data voidptr = voidptr(0) user_data voidptr = unsafe { nil }
timeout_secs int = 8 timeout_secs int = 8
max_headers int = 100 max_headers int = 100
} }

View File

@ -124,10 +124,10 @@ pub fn (mut ch Channel) close() {
if !C.atomic_compare_exchange_strong_u16(&ch.closed, &open_val, 1) { if !C.atomic_compare_exchange_strong_u16(&ch.closed, &open_val, 1) {
return return
} }
mut nulladr := unsafe { voidptr(0) } mut nulladr := unsafe { nil }
for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_written) }, &nulladr, for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_written) }, &nulladr,
voidptr(-1)) { voidptr(-1)) {
nulladr = unsafe { voidptr(0) } nulladr = unsafe { nil }
} }
ch.readsem_im.post() ch.readsem_im.post()
ch.readsem.post() ch.readsem.post()
@ -191,10 +191,10 @@ fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
{ {
// there is a reader waiting for us // there is a reader waiting for us
unsafe { C.memcpy(wradr, src, ch.objsize) } unsafe { C.memcpy(wradr, src, ch.objsize) }
mut nulladr := unsafe { voidptr(0) } mut nulladr := unsafe { nil }
for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_written) }, for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_written) },
&nulladr, wradr) { &nulladr, wradr) {
nulladr = unsafe { voidptr(0) } nulladr = unsafe { nil }
} }
ch.readsem_im.post() ch.readsem_im.post()
return .success return .success
@ -382,7 +382,7 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
{ {
// there is a writer waiting for us // there is a writer waiting for us
unsafe { C.memcpy(dest, rdadr, ch.objsize) } unsafe { C.memcpy(dest, rdadr, ch.objsize) }
mut nulladr := unsafe { voidptr(0) } mut nulladr := unsafe { nil }
for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_read) }, for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_read) },
&nulladr, rdadr) { &nulladr, rdadr) {
nulladr = unsafe { nil } nulladr = unsafe { nil }

View File

@ -7,7 +7,7 @@ import runtime
fn C.atomic_fetch_add_u32(voidptr, u32) u32 fn C.atomic_fetch_add_u32(voidptr, u32) u32
pub const ( pub const (
no_result = voidptr(0) no_result = unsafe { nil }
) )
pub struct PoolProcessor { pub struct PoolProcessor {
@ -48,7 +48,7 @@ pub fn new_pool_processor(context PoolProcessorConfig) &PoolProcessor {
mut pool := PoolProcessor{ mut pool := PoolProcessor{
items: [] items: []
results: [] results: []
shared_context: voidptr(0) shared_context: unsafe { nil }
thread_contexts: [] thread_contexts: []
njobs: context.maxjobs njobs: context.maxjobs
ntask: 0 ntask: 0

View File

@ -114,7 +114,7 @@ pub fn (mut ctx Context) run() ? {
} }
if !ctx.paused { if !ctx.paused {
sw.restart() sw.restart()
if ctx.cfg.event_fn != voidptr(0) { if ctx.cfg.event_fn != unsafe { nil } {
ctx.parse_events() ctx.parse_events()
} }
ctx.frame() ctx.frame()

View File

@ -233,7 +233,7 @@ fn (mut ctx Context) termios_loop() {
} }
if !ctx.paused { if !ctx.paused {
sw.restart() sw.restart()
if ctx.cfg.event_fn != voidptr(0) { if ctx.cfg.event_fn != unsafe { nil } {
unsafe { unsafe {
len := C.read(C.STDIN_FILENO, &u8(ctx.read_buf.data) + ctx.read_buf.len, len := C.read(C.STDIN_FILENO, &u8(ctx.read_buf.data) + ctx.read_buf.len,
ctx.read_buf.cap - ctx.read_buf.len) ctx.read_buf.cap - ctx.read_buf.len)

View File

@ -110,7 +110,7 @@ pub fn (t Time) local() Time {
millisecond: u16(t.microsecond / 1000) millisecond: u16(t.microsecond / 1000)
} }
st_local := SystemTime{} st_local := SystemTime{}
C.SystemTimeToTzSpecificLocalTime(voidptr(0), &st_utc, &st_local) C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, &st_utc, &st_local)
t_local := Time{ t_local := Time{
year: st_local.year year: st_local.year
month: st_local.month month: st_local.month
@ -133,7 +133,7 @@ fn win_now() Time {
st_utc := SystemTime{} st_utc := SystemTime{}
C.FileTimeToSystemTime(&ft_utc, &st_utc) C.FileTimeToSystemTime(&ft_utc, &st_utc)
st_local := SystemTime{} st_local := SystemTime{}
C.SystemTimeToTzSpecificLocalTime(voidptr(0), &st_utc, &st_local) C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, &st_utc, &st_local)
t := Time{ t := Time{
year: st_local.year year: st_local.year
month: st_local.month month: st_local.month

View File

@ -33,7 +33,7 @@ pub mut:
used_vweb_types []Type // vweb context types, filled in by checker, when pref.skip_unused = true; used_vweb_types []Type // vweb context types, filled in by checker, when pref.skip_unused = true;
used_maps int // how many times maps were used, filled in by checker, when pref.skip_unused = true; used_maps int // how many times maps were used, filled in by checker, when pref.skip_unused = true;
panic_handler FnPanicHandler = default_table_panic_handler panic_handler FnPanicHandler = default_table_panic_handler
panic_userdata voidptr = voidptr(0) // can be used to pass arbitrary data to panic_handler; panic_userdata voidptr = unsafe { nil } // can be used to pass arbitrary data to panic_handler;
panic_npanics int panic_npanics int
cur_fn &FnDecl = unsafe { 0 } // previously stored in Checker.cur_fn and Gen.cur_fn cur_fn &FnDecl = unsafe { 0 } // previously stored in Checker.cur_fn and Gen.cur_fn
cur_concrete_types []Type // current concrete types, e.g. <int, string> cur_concrete_types []Type // current concrete types, e.g. <int, string>

View File

@ -46,7 +46,7 @@ fn test_inspect() {
module main module main
' '
file := parse_text(source) file := parse_text(source)
walker.inspect(file, voidptr(0), fn (node &ast.Node, data voidptr) bool { walker.inspect(file, unsafe { nil }, fn (node &ast.Node, data voidptr) bool {
// Second visit must be ast.Stmt // Second visit must be ast.Stmt
if node is ast.Stmt { if node is ast.Stmt {
if node !is ast.Module { if node !is ast.Module {

View File

@ -144,7 +144,7 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
if mut r_expr is ast.Ident { if mut r_expr is ast.Ident {
if mut r_expr.obj is ast.Var { if mut r_expr.obj is ast.Var {
mut obj := unsafe { &r_expr.obj } mut obj := unsafe { &r_expr.obj }
if c.fn_scope != voidptr(0) { if c.fn_scope != unsafe { nil } {
obj = c.fn_scope.find_var(r_expr.obj.name) or { obj } obj = c.fn_scope.find_var(r_expr.obj.name) or { obj }
} }
if obj.is_stack_obj && !c.inside_unsafe { if obj.is_stack_obj && !c.inside_unsafe {

View File

@ -1,5 +1,5 @@
arr := [2, 3]! arr := [2, 3]!
mut p := voidptr(0) mut p := unsafe { nil }
p = arr p = arr
mut ip := &int(0) mut ip := &int(0)
ip = arr ip = arr

View File

@ -1,7 +1,7 @@
// void* arithmetic is not allowed in MSVC, so ban it // void* arithmetic is not allowed in MSVC, so ban it
fn test_voidptr() { fn test_voidptr() {
unsafe { unsafe {
mut p := voidptr(0) mut p := nil
_ = p + 1 _ = p + 1
p++ p++
p += 3 p += 3

View File

@ -1,5 +1,5 @@
fn main() { fn main() {
mut a := voidptr(0) mut a := unsafe { nil }
mut b := 123 mut b := 123
a = &b a = &b
println(*a) println(*a)

View File

@ -38,7 +38,7 @@ pub struct NewNodeConfig {
node_name string node_name string
should_highlight bool should_highlight bool
tooltip string tooltip string
ctx voidptr = voidptr(0) ctx voidptr = unsafe { nil }
name2node_fn FnLabel2NodeName = node_name name2node_fn FnLabel2NodeName = node_name
} }
@ -58,7 +58,7 @@ pub fn (mut d DotGraph) new_node(nlabel string, cfg NewNodeConfig) {
pub struct NewEdgeConfig { pub struct NewEdgeConfig {
should_highlight bool should_highlight bool
ctx voidptr = voidptr(0) ctx voidptr = unsafe { nil }
name2node_fn FnLabel2NodeName = node_name name2node_fn FnLabel2NodeName = node_name
} }

View File

@ -2,14 +2,13 @@ import v.ast { InfixExpr }
import v.table { Type } import v.table { Type }
fn test_as() { fn test_as() {
_ := sum_expr() as ast.InfixExpr _ := sum_expr() as InfixExpr
_ := sum_expr() as ast.PrefixExpr _ := sum_expr() as ast.PrefixExpr
} }
fn test_cast() { fn test_cast() {
_ := f32(0) _ := f32(0)
_ := table.Type(0) _ := Type(0)
_ := ast.Expr(sum_expr()) _ := ast.Expr(sum_expr())
_ := voidptr(0) _ := unsafe { nil }
} }

View File

@ -17,10 +17,10 @@ pub type MouseMoveFn = fn (e MouseMoveEvent, func voidptr)
[heap] [heap]
pub struct Window { pub struct Window {
pub mut: pub mut:
ui &UI = voidptr(0) ui &UI = unsafe { nil }
children []Widget children []Widget
child_window &Window = voidptr(0) child_window &Window = unsafe { nil }
parent_window &Window = voidptr(0) parent_window &Window = unsafe { nil }
has_textbox bool // for initial focus has_textbox bool // for initial focus
tab_index int tab_index int
just_tabbed bool just_tabbed bool

View File

@ -158,7 +158,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
ret_styp := g.typ(val.decl.return_type) ret_styp := g.typ(val.decl.return_type)
g.write('$ret_styp (*$ident.name) (') g.write('$ret_styp (*$ident.name) (')
def_pos := g.definitions.len def_pos := g.definitions.len
g.fn_decl_params(val.decl.params, voidptr(0), false) g.fn_decl_params(val.decl.params, unsafe { nil }, false)
g.definitions.go_back(g.definitions.len - def_pos) g.definitions.go_back(g.definitions.len - def_pos)
g.write(') = ') g.write(') = ')
} else { } else {
@ -344,7 +344,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
g.write('$ret_styp ($msvc_call_conv*${g.get_ternary_name(ident.name)}) (') g.write('$ret_styp ($msvc_call_conv*${g.get_ternary_name(ident.name)}) (')
def_pos := g.definitions.len def_pos := g.definitions.len
g.fn_decl_params(func.func.params, voidptr(0), false) g.fn_decl_params(func.func.params, unsafe { nil }, false)
g.definitions.go_back(g.definitions.len - def_pos) g.definitions.go_back(g.definitions.len - def_pos)
g.write(')$call_conv_attribute_suffix') g.write(')$call_conv_attribute_suffix')
} else { } else {

View File

@ -44,7 +44,7 @@ pub mut:
in_lambda_depth int in_lambda_depth int
inside_const bool inside_const bool
is_mbranch_expr bool // match a { x...y { } } is_mbranch_expr bool // match a { x...y { } }
fn_scope &ast.Scope = voidptr(0) fn_scope &ast.Scope = unsafe { nil }
wsinfix_depth int wsinfix_depth int
nlines int nlines int
} }

View File

@ -1124,7 +1124,7 @@ fn (mut g JsGen) gen_ctemp_var(tvar ast.CTempVar) {
fn (mut g JsGen) gen_assert_metainfo(node ast.AssertStmt) string { fn (mut g JsGen) gen_assert_metainfo(node ast.AssertStmt) string {
mod_path := g.file.path mod_path := g.file.path
fn_name := if g.fn_decl == voidptr(0) || g.fn_decl.is_anon { 'anon' } else { g.fn_decl.name } fn_name := if g.fn_decl == unsafe { nil } || g.fn_decl.is_anon { 'anon' } else { g.fn_decl.name }
line_nr := node.pos.line_nr line_nr := node.pos.line_nr
src := node.expr.str() src := node.expr.str()
metaname := 'v_assert_meta_info_$g.new_tmp_var()' metaname := 'v_assert_meta_info_$g.new_tmp_var()'
@ -1244,7 +1244,7 @@ fn (mut g JsGen) gen_assert_stmt(mut node ast.AssertStmt) {
} }
g.writeln('} else {') g.writeln('} else {')
g.inc_indent() g.inc_indent()
fname := if g.fn_decl == voidptr(0) || g.fn_decl.is_anon { 'anon' } else { g.fn_decl.name } fname := if g.fn_decl == unsafe { nil } || g.fn_decl.is_anon { 'anon' } else { g.fn_decl.name }
g.writeln('builtin__eprintln(new string("$mod_path:${node.pos.line_nr + 1}: FAIL: fn ${fname}(): assert $s_assertion"));') g.writeln('builtin__eprintln(new string("$mod_path:${node.pos.line_nr + 1}: FAIL: fn ${fname}(): assert $s_assertion"));')
g.writeln('builtin__exit(1);') g.writeln('builtin__exit(1);')
g.dec_indent() g.dec_indent()

View File

@ -102,7 +102,7 @@ pub fn change_test_runner(x &TestRunner) {
if pobj != 0 { if pobj != 0 {
test_runner.free() test_runner.free()
unsafe { unsafe {
(&C.main__TestRunner(&test_runner))._object = voidptr(0) (&C.main__TestRunner(&test_runner))._object = nil
} }
} }
test_runner = *x test_runner = *x

View File

@ -2,7 +2,7 @@ struct Abc {
prev &Abc prev &Abc
} }
const a = [Abc{voidptr(0)}, Abc{unsafe { &a[0] }}, Abc{unsafe { &a[1] }}]! const a = [Abc{unsafe { nil }}, Abc{unsafe { &a[0] }}, Abc{unsafe { &a[1] }}]!
fn test_fixed_array() { fn test_fixed_array() {
eprintln(a) eprintln(a)

View File

@ -15,7 +15,7 @@ struct Abc {
fn (a Abc) xyz() {} fn (a Abc) xyz() {}
fn resource__null() &IAbc { fn resource__null() &IAbc {
return voidptr(0) return unsafe { nil }
} }
fn test_fn_returning_voidptr_casted_as_interface_works() { fn test_fn_returning_voidptr_casted_as_interface_works() {

View File

@ -15,5 +15,5 @@ fn test_passing_voidptr_as_an_interface_reference() {
assert f(&i) == '&IAbc(Abc{})' assert f(&i) == '&IAbc(Abc{})'
// a voidptr() cast is an escape hatch, that should be allowed // a voidptr() cast is an escape hatch, that should be allowed
// but perhaps it should be forced by the compiler to be in unsafe{} // but perhaps it should be forced by the compiler to be in unsafe{}
assert f(unsafe { voidptr(0) }) == '&IAbc(0x0)' assert f(unsafe { nil }) == '&IAbc(0x0)'
} }

View File

@ -15,7 +15,7 @@ fn f(i &IAbc) string {
} }
fn test_voidptr_casted_as_an_interface_reference() { fn test_voidptr_casted_as_an_interface_reference() {
mut pi := &IAbc(voidptr(0)) mut pi := &IAbc(unsafe { nil })
dump(pi) dump(pi)
assert f(pi) == '&IAbc(0x0)' assert f(pi) == '&IAbc(0x0)'
// //

View File

@ -45,7 +45,7 @@ interface PopView {
[heap] [heap]
pub struct Button { pub struct Button {
mut: mut:
window &Window = voidptr(0) window &Window = unsafe { nil }
} }
pub fn (mut b Button) init(window &Window) { pub fn (mut b Button) init(window &Window) {

View File

@ -1,5 +1,5 @@
fn multi_voidptr_ret() (voidptr, bool) { fn multi_voidptr_ret() (voidptr, bool) {
return voidptr(0), true return unsafe { nil }, true
} }
fn multi_byteptr_ret() (&u8, bool) { fn multi_byteptr_ret() (&u8, bool) {

View File

@ -3,24 +3,24 @@ struct Zest {
} }
fn (t Zest) get_a_finger_to_the_moon() voidptr { fn (t Zest) get_a_finger_to_the_moon() voidptr {
return voidptr(0) return unsafe { nil }
} }
fn get_the_dao_way() voidptr { fn get_the_dao_way() voidptr {
return voidptr(0) return unsafe { nil }
} }
fn test_returning_a_void_pointer_from_a_method() { fn test_returning_a_void_pointer_from_a_method() {
t := &Zest{ t := &Zest{
val: 123 val: 123
} }
z := voidptr(0) z := unsafe { nil }
assert z == t.get_a_finger_to_the_moon() assert z == t.get_a_finger_to_the_moon()
assert t.get_a_finger_to_the_moon() == 0 assert t.get_a_finger_to_the_moon() == 0
} }
fn test_returning_a_void_pointer_from_a_function() { fn test_returning_a_void_pointer_from_a_function() {
z := voidptr(0) z := unsafe { nil }
assert z == get_the_dao_way() assert z == get_the_dao_way()
assert get_the_dao_way() == 0 assert get_the_dao_way() == 0
} }

View File

@ -4,7 +4,7 @@ type Fnc = fn ()
struct Foo { struct Foo {
Bar Bar
fnc_fn Fnc = voidptr(0) fnc_fn Fnc = unsafe { nil }
} }
struct App { struct App {

View File

@ -7,6 +7,6 @@
fn main() { fn main() {
mut y := unsafe { malloc(1000) } mut y := unsafe { malloc(1000) }
// unsafe { free(y) } // leak if commented out // unsafe { free(y) } // leak if commented out
y = voidptr(0) y = unsafe { nil }
gc_check_leaks() gc_check_leaks()
} }