Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checker: disallow assigning
nil
to struct fields (#18725)
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:15:8: error: cannot assign `nil` to struct field `name` with type `string` | ||
13 | | ||
14 | a := &Foo{ | ||
15 | name: unsafe { nil } | ||
| ~~~~~~~~~~~~ | ||
16 | bar: unsafe { nil } | ||
17 | name2: unsafe { nil } | ||
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:16:7: error: cannot assign `nil` to struct field `bar` with type `Bar` | ||
14 | a := &Foo{ | ||
15 | name: unsafe { nil } | ||
16 | bar: unsafe { nil } | ||
| ~~~~~~~~~~~~ | ||
17 | name2: unsafe { nil } | ||
18 | arr: unsafe { nil } | ||
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:17:9: error: cannot assign `nil` to struct field `name2` with type `string` | ||
15 | name: unsafe { nil } | ||
16 | bar: unsafe { nil } | ||
17 | name2: unsafe { nil } | ||
| ~~~~~~~~~~~~ | ||
18 | arr: unsafe { nil } | ||
19 | mp: unsafe { nil } | ||
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:18:7: error: cannot assign `nil` to struct field `arr` with type `[]int` | ||
16 | bar: unsafe { nil } | ||
17 | name2: unsafe { nil } | ||
18 | arr: unsafe { nil } | ||
| ~~~~~~~~~~~~ | ||
19 | mp: unsafe { nil } | ||
20 | arr_fix: unsafe { nil } | ||
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:19:6: error: cannot assign `nil` to struct field `mp` with type `map[string]string` | ||
17 | name2: unsafe { nil } | ||
18 | arr: unsafe { nil } | ||
19 | mp: unsafe { nil } | ||
| ~~~~~~~~~~~~ | ||
20 | arr_fix: unsafe { nil } | ||
21 | } | ||
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:20:11: error: cannot assign `nil` to struct field `arr_fix` with type `[3]int` | ||
18 | arr: unsafe { nil } | ||
19 | mp: unsafe { nil } | ||
20 | arr_fix: unsafe { nil } | ||
| ~~~~~~~~~~~~ | ||
21 | } | ||
22 | println(a) |
22 changes: 22 additions & 0 deletions
22
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
type String = string | ||
|
||
struct Bar {} | ||
|
||
struct Foo { | ||
name string | ||
bar Bar | ||
name2 String | ||
arr []int | ||
mp map[string]string | ||
arr_fix [3]int | ||
} | ||
|
||
a := &Foo{ | ||
name: unsafe { nil } | ||
bar: unsafe { nil } | ||
name2: unsafe { nil } | ||
arr: unsafe { nil } | ||
mp: unsafe { nil } | ||
arr_fix: unsafe { nil } | ||
} | ||
println(a) |