diff --git a/doc/docs.md b/doc/docs.md index c113bc7ca7b238..7a6695f37d56d6 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -6707,16 +6707,16 @@ struct Node { } // Reference fields must be initialized unless an initial value is declared. -// Zero (0) is OK but use with caution, it's a nil pointer. +// Nil is OK but use with caution, it's a nil pointer. foo := Node{ - a: 0 + a: unsafe { nil } } bar := Node{ a: &foo } baz := Node{ - a: 0 - b: 0 + a: unsafe { nil } + b: unsafe { nil } } qux := Node{ a: &foo diff --git a/vlib/v/README.md b/vlib/v/README.md index cb15bddea45059..3e849b07645a56 100644 --- a/vlib/v/README.md +++ b/vlib/v/README.md @@ -51,7 +51,7 @@ and a new scope is created: import v.ast scope := ast.Scope{ - parent: 0 + parent: unsafe { nil } } ``` after that, you can parse your files. diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 83ee4663e33676..a2e22efb3ef000 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -760,7 +760,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', && !got_type.is_any_kind_of_pointer() && !exp_type.has_flag(.option) && !(init_field.expr is ast.UnsafeExpr && init_field.expr.expr.str() == '0') { if init_field.expr.str() == '0' { - c.note('assigning `0` to a reference field is only allowed in `unsafe` blocks', + c.error('assigning `0` to a reference field is only allowed in `unsafe` blocks', init_field.pos) } else { c.error('reference field must be initialized with reference', diff --git a/vlib/v/checker/tests/struct_ref_fields_init_0_err.out b/vlib/v/checker/tests/struct_ref_fields_init_0_err.out index d70342c1e0b22c..e4804e746a0318 100644 --- a/vlib/v/checker/tests/struct_ref_fields_init_0_err.out +++ b/vlib/v/checker/tests/struct_ref_fields_init_0_err.out @@ -1,11 +1,11 @@ -vlib/v/checker/tests/struct_ref_fields_init_0_err.vv:6:10: notice: assigning `0` to a reference field is only allowed in `unsafe` blocks +vlib/v/checker/tests/struct_ref_fields_init_0_err.vv:6:10: error: assigning `0` to a reference field is only allowed in `unsafe` blocks 4 | 5 | fn main() { 6 | _ = Foo{0} | ^ 7 | _ = Foo{ 8 | foo: 0 -vlib/v/checker/tests/struct_ref_fields_init_0_err.vv:8:3: notice: assigning `0` to a reference field is only allowed in `unsafe` blocks +vlib/v/checker/tests/struct_ref_fields_init_0_err.vv:8:3: error: assigning `0` to a reference field is only allowed in `unsafe` blocks 6 | _ = Foo{0} 7 | _ = Foo{ 8 | foo: 0 diff --git a/vlib/v/parser/tests/invalid_struct_decl_script_err.out b/vlib/v/parser/tests/invalid_struct_decl_script_err.out index ed5ebea0554483..018e91306aa9d2 100644 --- a/vlib/v/parser/tests/invalid_struct_decl_script_err.out +++ b/vlib/v/parser/tests/invalid_struct_decl_script_err.out @@ -3,10 +3,10 @@ vlib/v/parser/tests/invalid_struct_decl_script_err.vv:1:1: notice: script mode s | ~~~~~ 2 | 3 | struct Abc { -vlib/v/parser/tests/invalid_struct_decl_script_err.vv:3:1: error: all definitions must occur before code in script mode +vlib/v/parser/tests/invalid_struct_decl_script_err.vv:3:8: error: all definitions must occur before code in script mode 1 | mynum := 10 2 | 3 | struct Abc { - | ~~~~~~ + | ~~~ 4 | x int 5 | } diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index 0749e1032848aa..3ed1b9bfce171d 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -41,7 +41,7 @@ fn test_eval() { vpref := &pref.Preferences{} mut scope := &ast.Scope{ start_pos: 0 - parent: 0 + parent: unsafe { nil } } mut stmts := []ast.Stmt{} for input in inputs { diff --git a/vlib/v/tests/builtin_strings_and_interpolation/str_gen_test.v b/vlib/v/tests/builtin_strings_and_interpolation/str_gen_test.v index 26f2ba0aaa4095..dc4a7e56f6bfbb 100644 --- a/vlib/v/tests/builtin_strings_and_interpolation/str_gen_test.v +++ b/vlib/v/tests/builtin_strings_and_interpolation/str_gen_test.v @@ -209,7 +209,7 @@ fn test_struct_with_struct_pointer() { } fn test_struct_with_nil() { - w := Wrapper4{0} + w := Wrapper4{unsafe { nil }} assert '${w}' == 'Wrapper4{\n foo: &nil\n}' assert w.str() == 'Wrapper4{\n foo: &nil\n}' } diff --git a/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_struct_test.v b/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_struct_test.v index d6b21cf2bb6bb4..26aeab997eaf5e 100644 --- a/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_struct_test.v +++ b/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_struct_test.v @@ -60,14 +60,14 @@ mut: } fn test_stack_circular_elem_auto_str() { - mut elem := Circular{0} + mut elem := Circular{unsafe { nil }} elem.next = &elem s := '${elem}'.replace('\n', '|') assert s == 'Circular{| next: &|}' } fn test_heap_circular_elem_auto_str() { - mut elem := &Circular{0} + mut elem := &Circular{unsafe { nil }} elem.next = elem s := '${elem}'.replace('\n', '|') assert s == '&Circular{| next: &|}' diff --git a/vlib/v/tests/builtin_strings_and_interpolation/string_ref_struct_test.v b/vlib/v/tests/builtin_strings_and_interpolation/string_ref_struct_test.v index db76413d01b822..2f2030dc37f615 100644 --- a/vlib/v/tests/builtin_strings_and_interpolation/string_ref_struct_test.v +++ b/vlib/v/tests/builtin_strings_and_interpolation/string_ref_struct_test.v @@ -1,13 +1,7 @@ struct Node { val int - left &Node - right &Node -} - -fn test_string_ref_struct() { - n := Node{123, 0, 0} - println(n.left) - assert '${n.left}' == '&nil' + left &Node = unsafe { nil } + right &Node = unsafe { nil } } fn test_string_ref_struct_with_nil_instead_of_0() { diff --git a/vlib/v/tests/generics/generic_fn_assign_generics_struct_test.v b/vlib/v/tests/generics/generic_fn_assign_generics_struct_test.v index 96d76e886b3103..4f917887d02b91 100644 --- a/vlib/v/tests/generics/generic_fn_assign_generics_struct_test.v +++ b/vlib/v/tests/generics/generic_fn_assign_generics_struct_test.v @@ -38,7 +38,7 @@ fn new[T]() &Node[T] { } fn (mut n Node[T]) add(val T) { - node := &Node[T]{val, 0} + node := &Node[T]{val, unsafe { nil }} n.next = node } diff --git a/vlib/v/tests/generics/generics_assign_reference_generic_struct_test.v b/vlib/v/tests/generics/generics_assign_reference_generic_struct_test.v index e4a3703a67fe1d..85a5c5b1c2e914 100644 --- a/vlib/v/tests/generics/generics_assign_reference_generic_struct_test.v +++ b/vlib/v/tests/generics/generics_assign_reference_generic_struct_test.v @@ -14,8 +14,8 @@ pub fn list_new[T]() List[T] { } pub fn (mut l List[T]) add(value T) { - mut node := &ListNode[T]{value, 0} - if unsafe { l.head == 0 } { + mut node := &ListNode[T]{value, unsafe { nil }} + if unsafe { l.head == nil } { l.head = node } else { node.next = l.head diff --git a/vlib/v/tests/generics/generics_return_generics_struct_test.v b/vlib/v/tests/generics/generics_return_generics_struct_test.v index 81383fb5182897..fdb79063a0e7b2 100644 --- a/vlib/v/tests/generics/generics_return_generics_struct_test.v +++ b/vlib/v/tests/generics/generics_return_generics_struct_test.v @@ -136,8 +136,8 @@ fn (mut node ListNode[T]) test() &ListNode[T] { } fn test_generics_return_generic_struct_field() { - mut node1 := &ListNode[int]{100, 0} - mut node2 := &ListNode[int]{200, 0} + mut node1 := &ListNode[int]{100, unsafe { nil }} + mut node2 := &ListNode[int]{200, unsafe { nil }} node1.next = node2 ret := node1.test() println(ret) diff --git a/vlib/v/tests/generics/generics_return_recursive_generics_struct_test.v b/vlib/v/tests/generics/generics_return_recursive_generics_struct_test.v index a657dc4aa467f8..1eaf31cd7ce173 100644 --- a/vlib/v/tests/generics/generics_return_recursive_generics_struct_test.v +++ b/vlib/v/tests/generics/generics_return_recursive_generics_struct_test.v @@ -1,13 +1,12 @@ struct Node[T] { mut: val T - next &Node[T] + next &Node[T] = unsafe { nil } } fn make_node[T](val []T) Node[T] { return Node[T]{ - val: val[0] - next: 0 + val: val[0] } } diff --git a/vlib/v/tests/generics/generics_with_generics_struct_init_test.v b/vlib/v/tests/generics/generics_with_generics_struct_init_test.v index 46f19d92287b25..66f65810292d20 100644 --- a/vlib/v/tests/generics/generics_with_generics_struct_init_test.v +++ b/vlib/v/tests/generics/generics_with_generics_struct_init_test.v @@ -15,7 +15,7 @@ fn create[T](arr []T) &List[T] { assert arr.len > 0 mut n := &ListNode[T]{ val: arr[0] - next: 0 + next: unsafe { nil } } mut l := &List[T]{ first: n diff --git a/vlib/v/tests/sumtypes/sumtype_test.v b/vlib/v/tests/sumtypes/sumtype_test.v index 6360279b6f4c9f..a7dc69fc006256 100644 --- a/vlib/v/tests/sumtypes/sumtype_test.v +++ b/vlib/v/tests/sumtypes/sumtype_test.v @@ -50,7 +50,7 @@ fn test_sum_type_match() { baz: &IntAndStr{ foo: 3 bar: 'hello' - baz: 0 + baz: unsafe { nil } } }) == 'This is the string representation of "5_hi_hello"' assert as_string(true) == 'This is the string representation of "true"' @@ -532,7 +532,7 @@ fn sumtype_match_with_string_interpolation(code int) string { baz: &IntAndStr{ foo: 3 bar: 'hello' - baz: 0 + baz: unsafe { nil } } }) match bar {