From 41e88423aa4f057591b066c5e7e4b85e9f38c9f2 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 1 Jul 2021 06:21:03 +0300 Subject: [PATCH] Revert "builtin: use == 0 instead of isnil()" This reverts commit 8650ec6916c44a119815950613c2159aecc9979f. --- vlib/builtin/array.v | 6 ++--- vlib/builtin/array_d_gcboehm_opt.v | 6 ++--- vlib/builtin/builtin_nix.c.v | 2 +- vlib/builtin/sorted_map.v | 43 +++++++++++++++--------------- vlib/v/ast/scope.v | 2 +- vlib/v/gen/c/cgen.v | 2 +- 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 740aeabd70..4bd6aa7fea 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -376,7 +376,7 @@ pub fn (a &array) clone_to_depth(depth int) array { } return arr } else { - if a.data != 0 { + if !isnil(a.data) { unsafe { C.memcpy(&byte(arr.data), a.data, a.cap * a.element_size) } } return arr @@ -409,7 +409,7 @@ fn (mut a array) push(val voidptr) { // `val` is array.data and user facing usage is `a << [1,2,3]` [unsafe] pub fn (mut a3 array) push_many(val voidptr, size int) { - if a3.data == val && val != 0 { + if a3.data == val && !isnil(a3.data) { // handle `arr << arr` copy := a3.clone() a3.ensure_cap(a3.len + size) @@ -419,7 +419,7 @@ pub fn (mut a3 array) push_many(val voidptr, size int) { } } else { a3.ensure_cap(a3.len + size) - if a3.data != 0 && val != 0 { + if !isnil(a3.data) && !isnil(val) { unsafe { C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) } } } diff --git a/vlib/builtin/array_d_gcboehm_opt.v b/vlib/builtin/array_d_gcboehm_opt.v index 08feca96fe..330977da5d 100644 --- a/vlib/builtin/array_d_gcboehm_opt.v +++ b/vlib/builtin/array_d_gcboehm_opt.v @@ -204,7 +204,7 @@ fn (a &array) clone_to_depth_noscan(depth int) array { } return arr } else { - if a.data != 0 { + if !isnil(a.data) { unsafe { C.memcpy(&byte(arr.data), a.data, a.cap * a.element_size) } } return arr @@ -221,7 +221,7 @@ fn (mut a array) push_noscan(val voidptr) { // `val` is array.data and user facing usage is `a << [1,2,3]` [unsafe] fn (mut a3 array) push_many_noscan(val voidptr, size int) { - if a3.data == val && val != 0 { + if a3.data == val && !isnil(a3.data) { // handle `arr << arr` copy := a3.clone() a3.ensure_cap_noscan(a3.len + size) @@ -231,7 +231,7 @@ fn (mut a3 array) push_many_noscan(val voidptr, size int) { } } else { a3.ensure_cap_noscan(a3.len + size) - if a3.data != 0 && val != 0 { + if !isnil(a3.data) && !isnil(val) { unsafe { C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) } } } diff --git a/vlib/builtin/builtin_nix.c.v b/vlib/builtin/builtin_nix.c.v index 0eba14efc4..1d1cf0d02b 100644 --- a/vlib/builtin/builtin_nix.c.v +++ b/vlib/builtin/builtin_nix.c.v @@ -110,7 +110,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool { cmd := 'addr2line -e $executable $addr' // taken from os, to avoid depending on the os module inside builtin.v f := C.popen(&char(cmd.str), c'r') - if f == 0 { + if isnil(f) { eprintln(sframe) continue } diff --git a/vlib/builtin/sorted_map.v b/vlib/builtin/sorted_map.v index 391d1c7c5b..2567a3cca4 100644 --- a/vlib/builtin/sorted_map.v +++ b/vlib/builtin/sorted_map.v @@ -73,7 +73,7 @@ fn (mut m SortedMap) set(key string, value voidptr) { mut parent := &mapnode(0) for { if node.len == max_len { - if parent != 0 { + if isnil(parent) { parent = new_node() m.root = parent } @@ -100,7 +100,7 @@ fn (mut m SortedMap) set(key string, value voidptr) { } return } - if node.children == 0 { + if isnil(node.children) { mut j := node.len - 1 for j >= 0 && key < node.keys[j] { node.keys[j + 1] = node.keys[j] @@ -130,7 +130,7 @@ fn (mut n mapnode) split_child(child_index int, mut y mapnode) { z.keys[j] = y.keys[j + degree] z.values[j] = y.values[j + degree] } - if y.children != 0 { + if !isnil(y.children) { z.children = unsafe { &voidptr(malloc(int(children_bytes))) } for jj := degree - 1; jj >= 0; jj-- { unsafe { @@ -139,7 +139,7 @@ fn (mut n mapnode) split_child(child_index int, mut y mapnode) { } } unsafe { - if n.children == 0 { + if isnil(n.children) { n.children = &voidptr(malloc(int(children_bytes))) } n.children[n.len + 1] = n.children[n.len] @@ -173,7 +173,7 @@ fn (m SortedMap) get(key string, out voidptr) bool { } return true } - if node.children == 0 { + if isnil(node.children) { break } node = unsafe { &mapnode(node.children[i + 1]) } @@ -182,7 +182,7 @@ fn (m SortedMap) get(key string, out voidptr) bool { } fn (m SortedMap) exists(key string) bool { - if m.root == 0 { // TODO: find out why root can be nil + if isnil(m.root) { // TODO: find out why root can be nil return false } mut node := m.root @@ -194,7 +194,7 @@ fn (m SortedMap) exists(key string) bool { if i != -1 && key == node.keys[i] { return true } - if node.children == 0 { + if isnil(node.children) { break } node = unsafe { &mapnode(node.children[i + 1]) } @@ -213,14 +213,14 @@ fn (n &mapnode) find_key(k string) int { fn (mut n mapnode) remove_key(k string) bool { idx := n.find_key(k) if idx < n.len && n.keys[idx] == k { - if n.children == 0 { + if isnil(n.children) { n.remove_from_leaf(idx) } else { n.remove_from_non_leaf(idx) } return true } else { - if n.children == 0 { + if isnil(n.children) { return false } flag := if idx == n.len { true } else { false } @@ -250,7 +250,7 @@ fn (mut n mapnode) remove_from_non_leaf(idx int) { k := n.keys[idx] if unsafe { &mapnode(n.children[idx]) }.len >= degree { mut current := unsafe { &mapnode(n.children[idx]) } - for current.children != 0 { + for !isnil(current.children) { current = unsafe { &mapnode(current.children[current.len]) } } predecessor := current.keys[current.len - 1] @@ -260,7 +260,7 @@ fn (mut n mapnode) remove_from_non_leaf(idx int) { node.remove_key(predecessor) } else if unsafe { &mapnode(n.children[idx + 1]) }.len >= degree { mut current := unsafe { &mapnode(n.children[idx + 1]) } - for current.children != 0 { + for !isnil(current.children) { current = unsafe { &mapnode(current.children[0]) } } successor := current.keys[0] @@ -294,7 +294,7 @@ fn (mut n mapnode) borrow_from_prev(idx int) { child.keys[i + 1] = child.keys[i] child.values[i + 1] = child.values[i] } - if child.children != 0 { + if !isnil(child.children) { for i := child.len; i >= 0; i-- { unsafe { child.children[i + 1] = child.children[i] @@ -303,7 +303,7 @@ fn (mut n mapnode) borrow_from_prev(idx int) { } child.keys[0] = n.keys[idx - 1] child.values[0] = n.values[idx - 1] - if child.children != 0 { + if !isnil(child.children) { unsafe { child.children[0] = sibling.children[sibling.len] } @@ -319,7 +319,7 @@ fn (mut n mapnode) borrow_from_next(idx int) { mut sibling := unsafe { &mapnode(n.children[idx + 1]) } child.keys[child.len] = n.keys[idx] child.values[child.len] = n.values[idx] - if child.children != 0 { + if !isnil(child.children) { unsafe { child.children[child.len + 1] = sibling.children[0] } @@ -330,7 +330,7 @@ fn (mut n mapnode) borrow_from_next(idx int) { sibling.keys[i - 1] = sibling.keys[i] sibling.values[i - 1] = sibling.values[i] } - if sibling.children != 0 { + if !isnil(sibling.children) { for i := 1; i <= sibling.len; i++ { unsafe { sibling.children[i - 1] = sibling.children[i] @@ -350,7 +350,7 @@ fn (mut n mapnode) merge(idx int) { child.keys[i + degree] = sibling.keys[i] child.values[i + degree] = sibling.values[i] } - if child.children != 0 { + if !isnil(child.children) { for i := 0; i <= sibling.len; i++ { unsafe { child.children[i + degree] = sibling.children[i] @@ -383,10 +383,11 @@ pub fn (mut m SortedMap) delete(key string) { if m.root.len == 0 { // tmp := t.root - if m.root.children == 0 { + if isnil(m.root.children) { return + } else { + m.root = unsafe { &mapnode(m.root.children[0]) } } - m.root = unsafe { &mapnode(m.root.children[0]) } // free(tmp) } } @@ -395,7 +396,7 @@ pub fn (mut m SortedMap) delete(key string) { // starting at `at`. Keys are inserted in order. fn (n &mapnode) subkeys(mut keys []string, at int) int { mut position := at - if n.children != 0 { + if !isnil(n.children) { // Traverse children and insert // keys inbetween children for i in 0 .. n.len { @@ -420,7 +421,7 @@ fn (n &mapnode) subkeys(mut keys []string, at int) int { pub fn (m &SortedMap) keys() []string { mut keys := []string{len: m.len} - if m.root == 0 || m.root.len == 0 { + if isnil(m.root) || m.root.len == 0 { return keys } m.root.subkeys(mut keys, 0) @@ -432,7 +433,7 @@ fn (mut n mapnode) free() { } pub fn (mut m SortedMap) free() { - if m.root == 0 { + if isnil(m.root) { return } m.root.free() diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index b32b936a94..31bc3d1d72 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -38,7 +38,7 @@ pub fn new_scope(parent &Scope, start_pos int) &Scope { */ fn (s &Scope) dont_lookup_parent() bool { - return s.parent == 0 || s.detached_from_parent + return isnil(s.parent) || s.detached_from_parent } pub fn (s &Scope) find_with_scope(name string) ?(ScopeObject, &Scope) { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9f4605d74a..83baf73f41 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2842,7 +2842,7 @@ fn (mut g Gen) trace_autofree(line string) { // fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, end_pos int) { fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int, line_nr int, free_parent_scopes bool, stop_pos int) { - if scope == 0 { + if isnil(scope) { return } for _, obj in scope.objects {