mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
checker: disallow nil in non-nil arrays and vice versa (#21786)
This commit is contained in:
parent
81b095bcb2
commit
36e31d6908
5 changed files with 32 additions and 1 deletions
|
@ -129,6 +129,20 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (exp_idx == ast.nil_type_idx && got_idx == ast.string_type_idx)
|
||||||
|
|| (got_idx == ast.nil_type_idx && exp_idx == ast.string_type_idx) {
|
||||||
|
got_sym := c.table.sym(got)
|
||||||
|
exp_sym := c.table.sym(expected)
|
||||||
|
|
||||||
|
if expected.is_ptr() || got.is_ptr() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if got_sym.language != .c || exp_sym.language != .c {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// allow direct int-literal assignment for pointers for now
|
// allow direct int-literal assignment for pointers for now
|
||||||
// maybe in the future options should be used for that
|
// maybe in the future options should be used for that
|
||||||
if expected.is_any_kind_of_pointer() {
|
if expected.is_any_kind_of_pointer() {
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
vlib/v/checker/tests/non_nil_array_with_nil_element_err.vv:1:23: error: invalid array element: expected `string`, not `voidptr`
|
||||||
|
1 | const spnames = ['f', unsafe { nil }]!
|
||||||
|
| ~~~~~~
|
||||||
|
2 | const spnames1 = [unsafe { nil }, 'f']!
|
||||||
|
vlib/v/checker/tests/non_nil_array_with_nil_element_err.vv:2:35: error: invalid array element: expected `voidptr`, not `string`
|
||||||
|
1 | const spnames = ['f', unsafe { nil }]!
|
||||||
|
2 | const spnames1 = [unsafe { nil }, 'f']!
|
||||||
|
| ~~~
|
|
@ -0,0 +1,2 @@
|
||||||
|
const spnames = ['f', unsafe { nil }]!
|
||||||
|
const spnames1 = [unsafe { nil }, 'f']!
|
|
@ -1,3 +1,10 @@
|
||||||
|
vlib/v/checker/tests/struct_field_assign_internal_types_nil_err.vv:15:2: error: cannot assign to field `name`: expected `string`, not `voidptr`
|
||||||
|
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:15:8: error: cannot assign `nil` to struct field `name` with type `string`
|
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 |
|
13 |
|
||||||
14 | a := &Foo{
|
14 | a := &Foo{
|
||||||
|
|
|
@ -5,7 +5,7 @@ struct Foo {
|
||||||
fn test_main() {
|
fn test_main() {
|
||||||
foo := Foo{
|
foo := Foo{
|
||||||
bar: unsafe {
|
bar: unsafe {
|
||||||
&&char(['a', 'b', nil]!)
|
&&char([c'a', c'b', nil]!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println(foo)
|
println(foo)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue