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

checker: allow x := unsafe { nil }

This commit is contained in:
Alexander Medvednikov 2022-07-21 20:56:24 +03:00
parent dc79f1392b
commit a68d03ac59
5 changed files with 11 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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