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

checker: only allow &u8 with byteptr and itself (#18146)

This commit is contained in:
Swastik Baranwal 2023-05-11 13:58:49 +05:30 committed by GitHub
parent a87f2d9d11
commit d8cf65df1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 3 deletions

View File

@ -124,7 +124,7 @@ fn (mut state AppState) worker(id int, input chan MandelChunk, ready chan bool)
fn (mut state AppState) draw() {
mut istream_image := state.gg.get_cached_image_by_idx(state.iidx)
istream_image.update_pixel_data(state.pixels)
istream_image.update_pixel_data(unsafe { &u8(state.pixels) })
size := gg.window_size()
state.gg.draw_image(0, 0, size.width, size.height, istream_image)
}

View File

@ -30,7 +30,7 @@ fn (mut state AppState) update() {
fn (mut state AppState) draw() {
mut istream_image := state.gg.get_cached_image_by_idx(state.istream_idx)
istream_image.update_pixel_data(&state.pixels)
istream_image.update_pixel_data(unsafe { &u8(&state.pixels) })
size := gg.window_size()
state.gg.draw_image(0, 0, size.width, size.height, istream_image)
}

View File

@ -59,6 +59,6 @@ fn frame(mut app App) {
fn (mut app App) draw() {
mut istream_image := app.gg.get_cached_image_by_idx(app.iidx)
istream_image.update_pixel_data(&app.pixels[0])
istream_image.update_pixel_data(unsafe { &u8(&app.pixels[0]) })
app.gg.draw_image(0, 0, app.args.grid.width, app.args.grid.height, istream_image)
}

View File

@ -224,6 +224,15 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
&& got == ast.int_type_idx {
return
}
} else {
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] {
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
}
}
// check int signed/unsigned mismatch
if got == ast.int_literal_type_idx && expected in ast.unsigned_integer_type_idxs

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/fn_call_ref_incompatible_u8_test.vv:9:28: error: cannot use `&[]int` as `&u8` in argument 1 to `accept_only_u8_references`
7 | fn test_main() {
8 | a := [1, 2, 3]
9 | accept_only_u8_references(&a)
| ~~
10 | }

View File

@ -0,0 +1,10 @@
module main
fn accept_only_u8_references(x &u8) {
println(ptr_str(x))
}
fn test_main() {
a := [1, 2, 3]
accept_only_u8_references(&a)
}