Skip to content

Commit d8cf65d

Browse files
authored
checker: only allow &u8 with byteptr and itself (#18146)
1 parent a87f2d9 commit d8cf65d

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

examples/gg/mandelbrot.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn (mut state AppState) worker(id int, input chan MandelChunk, ready chan bool)
124124

125125
fn (mut state AppState) draw() {
126126
mut istream_image := state.gg.get_cached_image_by_idx(state.iidx)
127-
istream_image.update_pixel_data(state.pixels)
127+
istream_image.update_pixel_data(unsafe { &u8(state.pixels) })
128128
size := gg.window_size()
129129
state.gg.draw_image(0, 0, size.width, size.height, istream_image)
130130
}

examples/gg/random.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn (mut state AppState) update() {
3030

3131
fn (mut state AppState) draw() {
3232
mut istream_image := state.gg.get_cached_image_by_idx(state.istream_idx)
33-
istream_image.update_pixel_data(&state.pixels)
33+
istream_image.update_pixel_data(unsafe { &u8(&state.pixels) })
3434
size := gg.window_size()
3535
state.gg.draw_image(0, 0, size.width, size.height, istream_image)
3636
}

examples/pendulum-simulation/modules/sim/anim/app.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ fn frame(mut app App) {
5959

6060
fn (mut app App) draw() {
6161
mut istream_image := app.gg.get_cached_image_by_idx(app.iidx)
62-
istream_image.update_pixel_data(&app.pixels[0])
62+
istream_image.update_pixel_data(unsafe { &u8(&app.pixels[0]) })
6363
app.gg.draw_image(0, 0, app.args.grid.width, app.args.grid.height, istream_image)
6464
}

vlib/v/checker/check_types.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
224224
&& got == ast.int_type_idx {
225225
return
226226
}
227+
} else {
228+
exp_sym_idx := c.table.sym(expected).idx
229+
got_sym_idx := c.table.sym(got).idx
230+
if expected.is_ptr() && got.is_ptr() && exp_sym_idx != got_sym_idx
231+
&& exp_sym_idx in [ast.u8_type_idx, ast.byteptr_type_idx]
232+
&& got_sym_idx !in [ast.u8_type_idx, ast.byteptr_type_idx] {
233+
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
234+
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
235+
}
227236
}
228237
// check int signed/unsigned mismatch
229238
if got == ast.int_literal_type_idx && expected in ast.unsigned_integer_type_idxs
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
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`
2+
7 | fn test_main() {
3+
8 | a := [1, 2, 3]
4+
9 | accept_only_u8_references(&a)
5+
| ~~
6+
10 | }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module main
2+
3+
fn accept_only_u8_references(x &u8) {
4+
println(ptr_str(x))
5+
}
6+
7+
fn test_main() {
8+
a := [1, 2, 3]
9+
accept_only_u8_references(&a)
10+
}

0 commit comments

Comments
 (0)