From a68d03ac599460401c635b173477a16098de7d43 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 21 Jul 2022 20:56:24 +0300 Subject: [PATCH] checker: allow `x := unsafe { nil }` --- vlib/strings/builder.c.v | 4 ++-- vlib/term/ui/input.v | 10 +++++----- vlib/v/ast/types.v | 2 +- vlib/v/checker/assign.v | 3 ++- vlib/v/checker/tests/nil.out | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/vlib/strings/builder.c.v b/vlib/strings/builder.c.v index dcdc5e2957..34c3afdfc4 100644 --- a/vlib/strings/builder.c.v +++ b/vlib/strings/builder.c.v @@ -191,7 +191,7 @@ pub fn (mut b Builder) ensure_cap(n int) { } new_data := vcalloc(n * b.element_size) - if b.data != voidptr(0) { + if b.data != unsafe { nil } { unsafe { vmemcpy(new_data, b.data, b.len * b.element_size) } // TODO: the old data may be leaked when no GC is used (ref-counting?) if b.flags.has(.noslices) { @@ -212,7 +212,7 @@ pub fn (mut b Builder) free() { if b.data != 0 { unsafe { free(b.data) } unsafe { - b.data = voidptr(0) + b.data = nil } } } diff --git a/vlib/term/ui/input.v b/vlib/term/ui/input.v index 81b887f837..6041f10070 100644 --- a/vlib/term/ui/input.v +++ b/vlib/term/ui/input.v @@ -206,35 +206,35 @@ pub struct Config { [inline] fn (ctx &Context) init() { - if ctx.cfg.init_fn != voidptr(0) { + if ctx.cfg.init_fn != unsafe { nil } { ctx.cfg.init_fn(ctx.cfg.user_data) } } [inline] fn (ctx &Context) frame() { - if ctx.cfg.frame_fn != voidptr(0) { + if ctx.cfg.frame_fn != unsafe { nil } { ctx.cfg.frame_fn(ctx.cfg.user_data) } } [inline] fn (ctx &Context) cleanup() { - if ctx.cfg.cleanup_fn != voidptr(0) { + if ctx.cfg.cleanup_fn != unsafe { nil } { ctx.cfg.cleanup_fn(ctx.cfg.user_data) } } [inline] fn (ctx &Context) fail(error string) { - if ctx.cfg.fail_fn != voidptr(0) { + if ctx.cfg.fail_fn != unsafe { nil } { ctx.cfg.fail_fn(error) } } [inline] fn (ctx &Context) event(event &Event) { - if ctx.cfg.event_fn != voidptr(0) { + if ctx.cfg.event_fn != unsafe { nil } { ctx.cfg.event_fn(event, ctx.cfg.user_data) } } diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 944738318d..dedf336e27 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -812,7 +812,7 @@ pub fn (mut t Table) register_builtin_type_symbols() { } ) t.register_sym(kind: .interface_, name: 'IError', cname: 'IError', mod: 'builtin') - t.register_sym(kind: .voidptr, name: 'nil', cname: 'nil', mod: 'builtin') + t.register_sym(kind: .voidptr, name: 'nil', cname: 'voidptr', mod: 'builtin') } [inline] diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 0df46b0cff..75d3d3d157 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -222,7 +222,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } if right is ast.Nil { // `x := unsafe { nil }` is allowed - c.error('use of untyped nil in assignment', right.pos()) + c.error('use of untyped nil in assignment (use `unsafe`)', + right.pos()) } } mut ident_var_info := left.info as ast.IdentVar diff --git a/vlib/v/checker/tests/nil.out b/vlib/v/checker/tests/nil.out index 6d4d981d28..74f4da2ba4 100644 --- a/vlib/v/checker/tests/nil.out +++ b/vlib/v/checker/tests/nil.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/nil.vv:3:18: error: use of untyped nil in assignment +vlib/v/checker/tests/nil.vv:3:18: error: use of untyped nil in assignment (use `unsafe`) 1 | fn main() { 2 | unsafe { 3 | value := nil