Skip to content

Commit 4eb087a

Browse files
authored
checker: add missing check for ref passing to non-ref (#22194)
1 parent 469a532 commit 4eb087a

File tree

13 files changed

+54
-14
lines changed

13 files changed

+54
-14
lines changed

examples/gg/path_finding_algorithm_visualizer/aStar.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn astar_path_finding(mut app App, mut grid [][]Cell, start Point, end Point) {
329329

330330
g_score[start.x][start.y] = 0
331331
f_score[start.x][start.y] = g_score[start.x][start.y] + hf(start, end)
332-
priority_queue.insert(&Node{
332+
priority_queue.insert(Node{
333333
f_score: f_score[start.x][start.y]
334334
cell: &Point{
335335
x: start.x
@@ -360,7 +360,7 @@ fn astar_path_finding(mut app App, mut grid [][]Cell, start Point, end Point) {
360360
if temp_g_score < g_score[neighbor.x][neighbor.y] {
361361
g_score[neighbor.x][neighbor.y] = temp_g_score
362362
if !(neighbor.x == start.x && neighbor.y == start.y) {
363-
priority_queue.insert(&Node{
363+
priority_queue.insert(Node{
364364
f_score: g_score[neighbor.x][neighbor.y] + hf(neighbor, end)
365365
cell: neighbor
366366
count: curr_node.count + 1

vlib/crypto/ed25519/internal/edwards25519/element.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub fn (mut v Element) pow_22523(x Element) Element {
538538
for i := 1; i < 100; i++ { // 2^200 - 2^100
539539
t2.square(t2)
540540
}
541-
t1.multiply(t2, &t1) // 2^200 - 1
541+
t1.multiply(t2, t1) // 2^200 - 1
542542
t1.square(t1) // 2^201 - 2
543543
for i := 1; i < 50; i++ { // 2^250 - 2^50
544544
t1.square(t1)

vlib/crypto/ed25519/internal/edwards25519/extra.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn (mut v Point) bytes_montgomery_generic(mut buf [32]u8) []u8 {
108108
mut u := Element{}
109109

110110
y.multiply(v.y, y.invert(v.z)) // y = Y / Z
111-
recip.invert(recip.subtract(fe_one, &y)) // r = 1/(1 - y)
111+
recip.invert(recip.subtract(fe_one, y)) // r = 1/(1 - y)
112112
u.multiply(u.add(fe_one, y), recip) // u = (1 + y)*r
113113

114114
return copy_field_element(mut buf, mut u)

vlib/crypto/ed25519/internal/edwards25519/point.v

+4-4
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ fn (mut v ProjectiveP1) sub(p Point, q ProjectiveCached) ProjectiveP1 {
353353
ypx.add(p.y, p.x)
354354
ymx.subtract(p.y, p.x)
355355

356-
pp.multiply(&ypx, q.ymx) // flipped sign
357-
mm.multiply(&ymx, q.ypx) // flipped sign
356+
pp.multiply(ypx, q.ymx) // flipped sign
357+
mm.multiply(ymx, q.ypx) // flipped sign
358358
tt2d.multiply(p.t, q.t2d)
359359
zz2.multiply(p.z, q.z)
360360

@@ -378,8 +378,8 @@ fn (mut v ProjectiveP1) add_affine(p Point, q AffineCached) ProjectiveP1 {
378378
ypx.add(p.y, p.x)
379379
ymx.subtract(p.y, p.x)
380380

381-
pp.multiply(&ypx, q.ypx)
382-
mm.multiply(&ymx, q.ymx)
381+
pp.multiply(ypx, q.ypx)
382+
mm.multiply(ymx, q.ymx)
383383
tt2d.multiply(p.t, q.t2d)
384384

385385
z2.add(p.z, p.z)

vlib/crypto/ed25519/internal/edwards25519/table.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn (mut v ProjLookupTable) select_into(mut dest ProjectiveCached, x i8) {
9494
for j := 1; j <= 8; j++ {
9595
// Set dest = j*Q if |x| = j
9696
cond := subtle.constant_time_byte_eq(xabs, u8(j))
97-
dest.selected(&v.points[j - 1], dest, cond)
97+
dest.selected(v.points[j - 1], dest, cond)
9898
}
9999
// Now dest = |x|*Q, conditionally negate to get x*Q
100100
dest.cond_neg(int(xmask & 1))

vlib/net/tcp.c.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ! {
583583
// return err_option_wrong_type
584584
// }
585585
x := int(value)
586-
s.set_option(C.SOL_SOCKET, int(opt), &x)!
586+
s.set_option(C.SOL_SOCKET, int(opt), x)!
587587
}
588588

589589
pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ! {
@@ -592,7 +592,7 @@ pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ! {
592592

593593
pub fn (mut s TcpSocket) set_dualstack(on bool) ! {
594594
x := int(!on)
595-
s.set_option(C.IPPROTO_IPV6, int(SocketOption.ipv6_only), &x)!
595+
s.set_option(C.IPPROTO_IPV6, int(SocketOption.ipv6_only), x)!
596596
}
597597

598598
fn (mut s TcpSocket) set_default_options() ! {

vlib/net/unix/stream.c.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub fn (mut s StreamSocket) set_option_bool(opt net.SocketOption, value bool) !
425425
return net.err_option_wrong_type
426426
}
427427
x := int(value)
428-
s.set_option(C.SOL_SOCKET, int(opt), &x)!
428+
s.set_option(C.SOL_SOCKET, int(opt), x)!
429429
}
430430

431431
// set_option_bool sets an int option on the socket

vlib/v/ast/ast.v

+17
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,23 @@ fn gen_all_registers(mut t Table, without_numbers []string, with_numbers map[str
25792579
return res
25802580
}
25812581

2582+
pub fn (expr Expr) is_reference() bool {
2583+
return match expr {
2584+
PrefixExpr {
2585+
expr.op == .amp
2586+
}
2587+
UnsafeExpr {
2588+
expr.expr.is_reference()
2589+
}
2590+
ParExpr {
2591+
expr.expr.is_reference()
2592+
}
2593+
else {
2594+
false
2595+
}
2596+
}
2597+
}
2598+
25822599
// is `expr` a literal, i.e. it does not depend on any other declarations (C compile time constant)
25832600
pub fn (expr Expr) is_literal() bool {
25842601
return match expr {

vlib/v/checker/check_types.v

+6
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
239239
return
240240
}
241241
} else {
242+
// passing &expr where no-pointer is expected
243+
if expected != ast.voidptr_type && !expected.is_ptr() && got.is_ptr()
244+
&& arg.expr.is_reference() {
245+
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
246+
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
247+
}
242248
if expected.has_flag(.option) {
243249
got_is_ptr := got.is_ptr()
244250
|| (arg.expr is ast.Ident && (arg.expr as ast.Ident).is_mut())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/checker/tests/ref_to_non_ref_err.vv:10:17: error: cannot use `&Foo` as `Foo` in argument 1 to `.index()`
2+
8 | }
3+
9 |
4+
10 | dump(list.index(unsafe { &list[1] }))
5+
| ~~~~~~~~~~~~~~~~~~~
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct Foo {
2+
idx int
3+
}
4+
5+
mut list := []Foo{}
6+
for i in 0..3 {
7+
list << Foo { idx: i }
8+
}
9+
10+
dump(list.index(unsafe { &list[1] }))

vlib/v/gen/c/fn.v

+2
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
27322732
} else if arg.expr is ast.None {
27332733
g.expr_with_opt(arg.expr, arg_typ, expected_type)
27342734
return
2735+
} else if arg.expr.is_literal() {
2736+
g.write('(voidptr)')
27352737
} else {
27362738
needs_closing = true
27372739
if arg_typ_sym.kind in [.sum_type, .interface_] {

vlib/v/gen/js/array.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn (mut g JsGen) gen_array_index_method(left_type ast.Type) string {
5050
fn_builder.writeln('\treturn new int(-1);')
5151
fn_builder.writeln('}')
5252
g.definitions.writeln(fn_builder.str())
53-
left_sym.register_method(&ast.Fn{
53+
left_sym.register_method(ast.Fn{
5454
name: 'index'
5555
params: [ast.Param{
5656
typ: unwrap_left_type
@@ -199,7 +199,7 @@ fn (mut g JsGen) gen_array_contains_method(left_type ast.Type) string {
199199
fn_builder.writeln('\treturn new bool(false);')
200200
fn_builder.writeln('}')
201201
g.definitions.writeln(fn_builder.str())
202-
left_sym.register_method(&ast.Fn{
202+
left_sym.register_method(ast.Fn{
203203
name: 'contains'
204204
params: [ast.Param{
205205
typ: unwrap_left_type

0 commit comments

Comments
 (0)