Skip to content

Commit 6b3724d

Browse files
committed
checker: do not allow auto reference of voidptr params
1 parent 3523c44 commit 6b3724d

9 files changed

Lines changed: 47 additions & 14 deletions

File tree

‎vlib/os/file.c.v‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ pub fn (f &File) read_bytes_into(pos u64, mut buf []u8) !int {
503503
return nbytes
504504
} $else {
505505
C.fseeko(f.cfile, pos, C.SEEK_SET)
506+
// TODO(alex): require casts for voidptrs? &C.FILE(f.cfile)
506507
nbytes := fread(buf.data, 1, buf.len, f.cfile)!
507508
$if debug {
508509
C.fseeko(f.cfile, 0, C.SEEK_SET)

‎vlib/os/os.c.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn vfopen(path string, mode string) !&C.FILE {
278278
} $else {
279279
fp = C.fopen(&char(path.str), &char(mode.str))
280280
}
281-
if isnil(fp) {
281+
if isnil(voidptr(fp)) {
282282
return error_posix(msg: 'failed to open file "${path}"')
283283
} else {
284284
return fp

‎vlib/time/time_windows.c.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn vpc_now() u64 {
9696
fn local_as_unix_time() i64 {
9797
t := C.time(0)
9898
tm := C.localtime(&t)
99-
return make_unix_time(tm)
99+
return make_unix_time(*tm)
100100
}
101101

102102
// local - return the time `t`, converted to the currently active local timezone

‎vlib/v/checker/autocomplete.v‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import strings
66
import v.ast
77
import os
88

9+
struct ACFieldMethod {
10+
name string
11+
typ string
12+
}
13+
914
fn (mut c Checker) ident_autocomplete(node ast.Ident) {
1015
// Mini LS hack (v -line-info "a.v:16")
1116
println(
@@ -81,7 +86,7 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
8186
}
8287
}
8388

84-
fn build_method_summary(method &ast.Fn) string {
89+
fn build_method_summary(method ast.Fn) string {
8590
mut s := method.name + '('
8691
for i, param in method.params {
8792
s += param.name

‎vlib/v/checker/checker.v‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,11 +3904,6 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
39043904
return ast.string_type
39053905
}
39063906

3907-
struct ACFieldMethod {
3908-
name string
3909-
typ string
3910-
}
3911-
39123907
fn (mut c Checker) resolve_var_fn(func &ast.Fn, mut node ast.Ident, name string) ast.Type {
39133908
mut fn_type := ast.new_type(c.table.find_or_register_fn_type(func, false, true))
39143909
if func.generic_names.len > 0 {

‎vlib/v/checker/fn.v‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,11 +1754,25 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
17541754
call_arg.pos)
17551755
}
17561756
// Warn about automatic (de)referencing, which will be removed soon.
1757-
if func.language != .c && !c.inside_unsafe && arg_typ.nr_muls() != param.typ.nr_muls()
1758-
&& !(call_arg.is_mut && param.is_mut) && !(!call_arg.is_mut && !param.is_mut)
1759-
&& param.typ !in [ast.byteptr_type, ast.charptr_type, ast.voidptr_type, ast.nil_type] {
1760-
c.warn('automatic referencing/dereferencing is deprecated and will be removed soon (got: ${arg_typ.nr_muls()} references, expected: ${param.typ.nr_muls()} references)',
1761-
call_arg.pos)
1757+
if func.language != .c && !c.inside_unsafe && !(call_arg.is_mut && param.is_mut) {
1758+
if arg_typ.nr_muls() != param.typ.nr_muls()
1759+
&& param.typ !in [ast.byteptr_type, ast.charptr_type, ast.voidptr_type, ast.nil_type]
1760+
&& arg_typ != ast.voidptr_type && !(!call_arg.is_mut && !param.is_mut) //&& !(!call_arg.is_mut && !param.is_mut)
1761+
{
1762+
c.warn('automatic referencing/dereferencing is deprecated and will be removed soon (got: ${arg_typ.nr_muls()} references, expected: ${param.typ.nr_muls()} references)',
1763+
call_arg.pos)
1764+
}
1765+
// A special case of the check to not allow voidptr params like in the recently reported raylib
1766+
// bug with fn...
1767+
// fn f(p &Foo) => f(foo) -- do not allow this, force f(&foo)
1768+
// if !c.is_builtin_mod
1769+
if param.typ == ast.voidptr_type && func.language == .v
1770+
&& arg_typ !in [ast.voidptr_type, ast.nil_type] && arg_typ.nr_muls() == 0
1771+
&& func.name !in ['isnil', 'ptr_str'] && !func.name.starts_with('json.')
1772+
&& arg_typ_sym.kind !in [.float_literal, .int_literal] && !c.pref.backend.is_js() {
1773+
c.warn('automatic ${arg_typ_sym.name} referencing/dereferencing into voidptr is deprecated and will be removed soon; use `foo(&x)` instead of `foo(x)`',
1774+
call_arg.pos)
1775+
}
17621776
}
17631777
}
17641778
if func.generic_names.len != node.concrete_types.len {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
auto_ref_voidptr.vv:9:9: warning: automatic Aa referencing/dereferencing into voidptr is deprecated and will be removed soon; use `foo(&x)` instead of `foo(x)`
2+
7 | fn main() {
3+
8 | a := Aa{}
4+
9 | foo(3, a)
5+
| ^
6+
10 | // foo2(a)
7+
11 | }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn foo(x int, p voidptr) {}
2+
3+
fn foo2(p &Aa) {}
4+
5+
struct Aa {}
6+
7+
fn main() {
8+
a := Aa{}
9+
foo(3, a)
10+
// foo2(a)
11+
}

‎vlib/v/parser/parser.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,7 @@ fn (p &Parser) is_generic_call() bool {
25972597
const valid_tokens_inside_types = [token.Kind.lsbr, .rsbr, .name, .dot, .comma, .key_fn, .lt]
25982598

25992599
fn (mut p Parser) is_generic_cast() bool {
2600-
if !ast.type_can_start_with_token(p.tok) {
2600+
if !ast.type_can_start_with_token(&p.tok) {
26012601
return false
26022602
}
26032603
mut i := 0

0 commit comments

Comments
 (0)