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

checker: unsafe 0 for references (default value)

This commit is contained in:
Alexander Medvednikov 2022-06-26 06:40:40 +03:00
parent 546f9a544f
commit e76f74fd73
12 changed files with 28 additions and 22 deletions

View File

@ -9,13 +9,13 @@ mut:
// Value of the node
value T
// The parent of the node
parent &BSTreeNode<T> = 0
parent &BSTreeNode<T> = unsafe { 0 }
// The left side with value less than the
// value of this node
left &BSTreeNode<T> = 0
left &BSTreeNode<T> = unsafe { 0 }
// The right side with value grater than the
// value of thiss node
right &BSTreeNode<T> = 0
right &BSTreeNode<T> = unsafe { 0 }
}
// Create new root bst node
@ -61,7 +61,7 @@ fn (mut node BSTreeNode<T>) bind(mut to_bind BSTreeNode<T>, left bool) {
// Space complexity O(N)
pub struct BSTree<T> {
mut:
root &BSTreeNode<T> = 0
root &BSTreeNode<T> = unsafe { 0 }
}
// insert give the possibility to insert an element in the BST.

View File

@ -3,18 +3,18 @@ module datatypes
struct DoublyListNode<T> {
mut:
data T
next &DoublyListNode<T> = 0
prev &DoublyListNode<T> = 0
next &DoublyListNode<T> = unsafe { 0 }
prev &DoublyListNode<T> = unsafe { 0 }
}
pub struct DoublyLinkedList<T> {
mut:
head &DoublyListNode<T> = 0
tail &DoublyListNode<T> = 0
head &DoublyListNode<T> = unsafe { 0 }
tail &DoublyListNode<T> = 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<T> = 0
iter &DoublyListIter<T> = unsafe { 0 }
len int
}
@ -280,5 +280,5 @@ pub fn (mut list DoublyLinkedList<T>) next() ?T {
struct DoublyListIter<T> {
mut:
node &DoublyListNode<T> = 0
node &DoublyListNode<T> = unsafe { 0 }
}

View File

@ -3,12 +3,12 @@ module datatypes
pub struct ListNode<T> {
mut:
data T
next &ListNode<T> = 0
next &ListNode<T> = unsafe { 0 }
}
pub struct LinkedList<T> {
mut:
head &ListNode<T> = 0
head &ListNode<T> = unsafe { 0 }
len int
}

View File

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

View File

@ -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. <int, string>
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 {`

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
type Foo = Bar
struct Bar {
pfield &Foo = 0
pfield &Foo = unsafe { 0 }
field Foo = Bar{}
}

View File

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

View File

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