Skip to content

Commit 993e21e

Browse files
authored
checker: check fntype mismatch of struct field init (fix #16372) (#16381)
1 parent 196b01a commit 993e21e

File tree

10 files changed

+35
-9
lines changed

10 files changed

+35
-9
lines changed

examples/news_fetcher.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct Story {
1010
url string
1111
}
1212

13-
fn worker_fetch(p &pool.PoolProcessor, cursor int, worker_id int) voidptr {
13+
fn worker_fetch(mut p pool.PoolProcessor, cursor int, worker_id int) voidptr {
1414
id := p.get_item<int>(cursor)
1515
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
1616
println('failed to fetch data from /v0/item/${id}.json')

vlib/crypto/ed25519/internal/ed25519_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn works_check_on_sign_input_string(item string) bool {
129129
return true
130130
}
131131

132-
fn worker_for_string_content(p &pool.PoolProcessor, idx int, worker_id int) &SignResult {
132+
fn worker_for_string_content(mut p pool.PoolProcessor, idx int, worker_id int) &SignResult {
133133
item := p.get_item<string>(idx)
134134
// println('worker_s worker_id: $worker_id | idx: $idx ')
135135
res := works_check_on_sign_input_string(item)

vlib/sync/pool/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct SResult {
1616
s string
1717
}
1818
19-
fn sprocess(pp &pool.PoolProcessor, idx int, wid int) &SResult {
19+
fn sprocess(mut pp pool.PoolProcessor, idx int, wid int) &SResult {
2020
item := pp.get_item<string>(idx)
2121
println('idx: $idx, wid: $wid, item: ' + item)
2222
return &SResult{item.reverse()}

vlib/sync/pool/pool.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mut:
2222
thread_contexts []voidptr
2323
}
2424

25-
pub type ThreadCB = fn (p &PoolProcessor, idx int, task_id int) voidptr
25+
pub type ThreadCB = fn (mut p PoolProcessor, idx int, task_id int) voidptr
2626

2727
pub struct PoolProcessorConfig {
2828
maxjobs int
@@ -110,7 +110,7 @@ fn process_in_thread(mut pool PoolProcessor, task_id int) {
110110
if idx >= ilen {
111111
break
112112
}
113-
pool.results[idx] = cb(pool, idx, task_id)
113+
pool.results[idx] = cb(mut pool, idx, task_id)
114114
}
115115
pool.waitgroup.done()
116116
}

vlib/sync/pool/pool_test.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ pub struct IResult {
99
i int
1010
}
1111

12-
fn worker_s(p &pool.PoolProcessor, idx int, worker_id int) &SResult {
12+
fn worker_s(mut p pool.PoolProcessor, idx int, worker_id int) &SResult {
1313
item := p.get_item<string>(idx)
1414
println('worker_s worker_id: $worker_id | idx: $idx | item: $item')
1515
time.sleep(3 * time.millisecond)
1616
return &SResult{'$item $item'}
1717
}
1818

19-
fn worker_i(p &pool.PoolProcessor, idx int, worker_id int) &IResult {
19+
fn worker_i(mut p pool.PoolProcessor, idx int, worker_id int) &IResult {
2020
item := p.get_item<int>(idx)
2121
println('worker_i worker_id: $worker_id | idx: $idx | item: $item')
2222
time.sleep(5 * time.millisecond)

vlib/v/builder/cbuilder/parallel_cc.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn parallel_cc(mut b builder.Builder, header string, res string, out_str string,
6868
eprint_time('link_cmd', link_cmd, link_res, sw_link)
6969
}
7070

71-
fn build_parallel_o_cb(p &pool.PoolProcessor, idx int, wid int) voidptr {
71+
fn build_parallel_o_cb(mut p pool.PoolProcessor, idx int, wid int) voidptr {
7272
postfix := p.get_item<string>(idx)
7373
sw := time.new_stopwatch()
7474
cmd := '${os.quoted_path(cbuilder.cc_compiler)} $cbuilder.cc_cflags -c -w -o out_${postfix}.o out_${postfix}.c'

vlib/v/checker/check_types.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSym
409409
got_arg_typ := c.unwrap_generic(got_arg.typ)
410410
exp_arg_is_ptr := exp_arg_typ.is_ptr() || exp_arg_typ.is_pointer()
411411
got_arg_is_ptr := got_arg_typ.is_ptr() || got_arg_typ.is_pointer()
412+
if exp_arg.is_mut && !got_arg.is_mut {
413+
return false
414+
}
412415
if exp_arg_is_ptr != got_arg_is_ptr {
413416
exp_arg_pointedness := if exp_arg_is_ptr { 'a pointer' } else { 'NOT a pointer' }
414417
got_arg_pointedness := if got_arg_is_ptr { 'a pointer' } else { 'NOT a pointer' }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/struct_field_init_fntype_mismatch.vv:13:3: error: cannot assign to field `foo_fn`: expected `fn (mut Foo)`, not `fn (&Foo)`
2+
11 | fn main(){
3+
12 | srv := Server{
4+
13 | foo_fn: foo
5+
| ~~~~~~~~~~~
6+
14 | }
7+
15 | dump(isnil(srv.foo_fn))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Foo{}
2+
3+
type FooFn = fn(mut f Foo)
4+
5+
struct Server{
6+
foo_fn FooFn = unsafe{ nil }
7+
}
8+
9+
fn foo(f &Foo) {}
10+
11+
fn main(){
12+
srv := Server{
13+
foo_fn: foo
14+
}
15+
dump(isnil(srv.foo_fn))
16+
}

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) (string,
598598
return header, res, out_str, out_fn_start_pos
599599
}
600600

601-
fn cgen_process_one_file_cb(p &pool.PoolProcessor, idx int, wid int) &Gen {
601+
fn cgen_process_one_file_cb(mut p pool.PoolProcessor, idx int, wid int) &Gen {
602602
file := p.get_item<&ast.File>(idx)
603603
mut global_g := &Gen(p.get_shared_context())
604604
mut g := &Gen{

0 commit comments

Comments
 (0)