From e76f74fd73ddc18447be192bfcfe1580ee9b558c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 26 Jun 2022 06:40:40 +0300 Subject: [PATCH] checker: unsafe 0 for references (default value) --- vlib/datatypes/bstree.v | 8 ++++---- vlib/datatypes/doubly_linked_list.v | 12 ++++++------ vlib/datatypes/linked_list.v | 4 ++-- vlib/v/ast/ast.v | 2 +- vlib/v/ast/table.v | 2 +- vlib/v/callgraph/callgraph.v | 6 +++--- vlib/v/checker/checker.v | 2 +- vlib/v/checker/struct.v | 6 ++++++ ...ot_be_from_the_same_type_as_containing_struct.out | 2 +- ...not_be_from_the_same_type_as_containing_struct.vv | 2 +- vlib/v/gen/c/cgen.v | 2 +- vlib/v/transformer/transformer.v | 2 +- 12 files changed, 28 insertions(+), 22 deletions(-) diff --git a/vlib/datatypes/bstree.v b/vlib/datatypes/bstree.v index 306ac66f0..db8530254 100644 --- a/vlib/datatypes/bstree.v +++ b/vlib/datatypes/bstree.v @@ -9,13 +9,13 @@ mut: // Value of the node value T // The parent of the node - parent &BSTreeNode = 0 + parent &BSTreeNode = unsafe { 0 } // The left side with value less than the // value of this node - left &BSTreeNode = 0 + left &BSTreeNode = unsafe { 0 } // The right side with value grater than the // value of thiss node - right &BSTreeNode = 0 + right &BSTreeNode = unsafe { 0 } } // Create new root bst node @@ -61,7 +61,7 @@ fn (mut node BSTreeNode) bind(mut to_bind BSTreeNode, left bool) { // Space complexity O(N) pub struct BSTree { mut: - root &BSTreeNode = 0 + root &BSTreeNode = unsafe { 0 } } // insert give the possibility to insert an element in the BST. diff --git a/vlib/datatypes/doubly_linked_list.v b/vlib/datatypes/doubly_linked_list.v index 6ed3122f4..eca1dd936 100644 --- a/vlib/datatypes/doubly_linked_list.v +++ b/vlib/datatypes/doubly_linked_list.v @@ -3,18 +3,18 @@ module datatypes struct DoublyListNode { mut: data T - next &DoublyListNode = 0 - prev &DoublyListNode = 0 + next &DoublyListNode = unsafe { 0 } + prev &DoublyListNode = unsafe { 0 } } pub struct DoublyLinkedList { mut: - head &DoublyListNode = 0 - tail &DoublyListNode = 0 + head &DoublyListNode = unsafe { 0 } + tail &DoublyListNode = unsafe { 0 } // Internal iter pointer for allowing safe modification // of the list while iterating. TODO: use an option // instead of a pointer to determine it is initialized. - iter &DoublyListIter = 0 + iter &DoublyListIter = unsafe { 0 } len int } @@ -280,5 +280,5 @@ pub fn (mut list DoublyLinkedList) next() ?T { struct DoublyListIter { mut: - node &DoublyListNode = 0 + node &DoublyListNode = unsafe { 0 } } diff --git a/vlib/datatypes/linked_list.v b/vlib/datatypes/linked_list.v index 719a6c161..d3c48b5b0 100644 --- a/vlib/datatypes/linked_list.v +++ b/vlib/datatypes/linked_list.v @@ -3,12 +3,12 @@ module datatypes pub struct ListNode { mut: data T - next &ListNode = 0 + next &ListNode = unsafe { 0 } } pub struct LinkedList { mut: - head &ListNode = 0 + head &ListNode = unsafe { 0 } len int } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 33a4340a5..9f1e19138 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -549,7 +549,7 @@ pub mut: end_comments []Comment // comments *after* header declarations. E.g.: `fn C.C_func(x int) int // Comment` next_comments []Comment // comments that are one line after the decl; used for InterfaceDecl // - source_file &File = 0 + source_file &File = unsafe { 0 } scope &Scope label_names []string pos token.Pos // function declaration position diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 45480f662..4fe2a0a45 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -36,7 +36,7 @@ pub mut: panic_handler FnPanicHandler = default_table_panic_handler panic_userdata voidptr = voidptr(0) // can be used to pass arbitrary data to panic_handler; panic_npanics int - cur_fn &FnDecl = 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. gostmts int // how many `go` statements there were in the parsed files. // When table.gostmts > 0, __VTHREADS__ is defined, which can be checked with `$if threads {` diff --git a/vlib/v/callgraph/callgraph.v b/vlib/v/callgraph/callgraph.v index 536c3ff27..114d9d4bf 100644 --- a/vlib/v/callgraph/callgraph.v +++ b/vlib/v/callgraph/callgraph.v @@ -27,9 +27,9 @@ struct Mapper { mut: pref &pref.Preferences table &ast.Table - file &ast.File = 0 - node &ast.Node = 0 - fn_decl &ast.FnDecl = 0 + file &ast.File = unsafe { 0 } + node &ast.Node = unsafe { 0 } + fn_decl &ast.FnDecl = unsafe { 0 } caller_name string dot_caller_name string is_caller_used bool diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 72e9d5379..8fce5a8aa 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -57,7 +57,7 @@ pub struct Checker { pref &pref.Preferences // Preferences shared from V struct pub mut: table &ast.Table - file &ast.File = 0 + file &ast.File = unsafe { 0 } nr_errors int nr_warnings int nr_notices int diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 1cd2b5d4a..b285bbe1b 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -101,6 +101,12 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) { } // Check for unnecessary inits like ` = 0` and ` = ''` if field.typ.is_ptr() { + if field.default_expr is ast.IntegerLiteral { + if !c.inside_unsafe && !c.is_builtin_mod && field.default_expr.val == '0' { + c.warn('default value of `0` for references can only be used inside `unsafe`', + field.default_expr.pos) + } + } continue } if field.default_expr is ast.IntegerLiteral { diff --git a/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.out b/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.out index 26ceecc4f..516930a31 100644 --- a/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.out +++ b/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.out @@ -1,6 +1,6 @@ vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.vv:5:9: error: field `field` is part of `Bar`, they can not both have the same type 3 | struct Bar { - 4 | pfield &Foo = 0 + 4 | pfield &Foo = unsafe { 0 } 5 | field Foo = Bar{} | ~~~ 6 | } diff --git a/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.vv b/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.vv index 298bbc52f..8f600377b 100644 --- a/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.vv +++ b/vlib/v/checker/tests/field_can_not_be_from_the_same_type_as_containing_struct.vv @@ -1,7 +1,7 @@ type Foo = Bar struct Bar { - pfield &Foo = 0 + pfield &Foo = unsafe { 0 } field Foo = Bar{} } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index ce63ad40e..8f1306c54 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -204,7 +204,7 @@ mut: // main_fn_decl_node ast.FnDecl cur_mod ast.Module cur_concrete_types []ast.Type // do not use table.cur_concrete_types because table is global, so should not be accessed by different threads - cur_fn &ast.FnDecl = 0 // same here + cur_fn &ast.FnDecl = unsafe { 0 } // same here cur_lock ast.LockExpr autofree_methods map[int]bool generated_free_methods map[int]bool diff --git a/vlib/v/transformer/transformer.v b/vlib/v/transformer/transformer.v index 1c50138a2..04030c328 100644 --- a/vlib/v/transformer/transformer.v +++ b/vlib/v/transformer/transformer.v @@ -8,7 +8,7 @@ pub struct Transformer { pref &pref.Preferences pub mut: index &IndexState - table &ast.Table = 0 + table &ast.Table = unsafe { 0 } mut: is_assert bool } -- 2.30.2