mirror of
https://github.com/vlang/v.git
synced 2025-09-15 10:27:19 -04:00
checker: fix option checker arg validation for ptr passing on non expected ptr (#21087)
This commit is contained in:
parent
3e37643e81
commit
b01e091473
@ -225,8 +225,18 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if expected.has_flag(.option) {
|
||||
got_is_ptr := got.is_ptr()
|
||||
|| (arg.expr is ast.Ident && (arg.expr as ast.Ident).is_mut())
|
||||
if (expected.is_ptr() && !got_is_ptr) || (!expected.is_ptr() && got.is_ptr()) {
|
||||
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
|
||||
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
|
||||
}
|
||||
}
|
||||
|
||||
exp_sym_idx := c.table.sym(expected).idx
|
||||
got_sym_idx := c.table.sym(got).idx
|
||||
|
||||
if expected.is_ptr() && got.is_ptr() && exp_sym_idx != got_sym_idx
|
||||
&& exp_sym_idx in [ast.u8_type_idx, ast.byteptr_type_idx]
|
||||
&& got_sym_idx !in [ast.u8_type_idx, ast.byteptr_type_idx] {
|
||||
|
7
vlib/v/checker/tests/option_receive_ptr_err.out
Normal file
7
vlib/v/checker/tests/option_receive_ptr_err.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/option_receive_ptr_err.vv:14:23: error: cannot use `&MyStruct` as `?MyStruct` in argument 1 to `unwrap_option`
|
||||
12 |
|
||||
13 | fn wrap_unwrap_ptr(event_data &MyStruct) int {
|
||||
14 | return unwrap_option(event_data)
|
||||
| ~~~~~~~~~~
|
||||
15 | }
|
||||
16 |
|
21
vlib/v/checker/tests/option_receive_ptr_err.vv
Normal file
21
vlib/v/checker/tests/option_receive_ptr_err.vv
Normal file
@ -0,0 +1,21 @@
|
||||
struct MyStruct {
|
||||
x int
|
||||
}
|
||||
|
||||
fn unwrap_option(event_data ?MyStruct) int {
|
||||
if val := event_data {
|
||||
return val.x
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
fn wrap_unwrap_ptr(event_data &MyStruct) int {
|
||||
return unwrap_option(event_data)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
data := MyStruct { x: 0 }
|
||||
result := wrap_unwrap_ptr(&data)
|
||||
assert data.x == result
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user