From 8a7c01750a5f241bee94808aba1829bd7ff8733d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 13 Apr 2026 04:57:18 +0300 Subject: [PATCH] all: various fixes --- vlib/gg/text_rendering.c.v | 2 +- vlib/v/checker/checker.v | 5 +++-- ... voidptr_cast_to_ref_outside_unsafe_warn.out} | 4 ++-- ...> voidptr_cast_to_ref_outside_unsafe_warn.vv} | 0 vlib/v/gen/c/cgen.v | 14 +++++++++++++- vlib/v/gen/wasm/tests/arrays.vv | 2 +- vlib/v/gen/wasm/tests/misc.vv | 2 +- vlib/v2/builder/gen_arm64_parallel.v | 3 ++- vlib/v2/builder/gen_cleanc_parallel.v | 3 ++- vlib/v2/types/checker_ownership.v | 16 ++++++++-------- vlib/v2/types/checker_ownership_test.v | 3 ++- 11 files changed, 35 insertions(+), 19 deletions(-) rename vlib/v/checker/tests/{voidptr_cast_to_ref_outside_unsafe_err.out => voidptr_cast_to_ref_outside_unsafe_warn.out} (53%) rename vlib/v/checker/tests/{voidptr_cast_to_ref_outside_unsafe_err.vv => voidptr_cast_to_ref_outside_unsafe_warn.vv} (100%) diff --git a/vlib/gg/text_rendering.c.v b/vlib/gg/text_rendering.c.v index 6c104b248..f5af31ea0 100644 --- a/vlib/gg/text_rendering.c.v +++ b/vlib/gg/text_rendering.c.v @@ -8,7 +8,7 @@ import sokol.sgl import os import os.font -struct FT { +pub struct FT { pub: fons &fontstash.Context = unsafe { nil } font_normal int diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5530cc02a..5094c345b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4883,9 +4883,10 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { } else if to_type.has_flag(.option) && from_type == inner_to_type { return to_type } else if enforce_safe_voidptr_ref_cast && from_type == ast.voidptr_type_idx && to_type.is_ptr() - && !c.inside_unsafe && !c.pref.translated && !c.file.is_translated { + && !c.inside_unsafe && !c.pref.translated && !c.file.is_translated + && to_sym.kind !in [.sum_type, .struct, .interface] { tt := c.table.type_to_str(to_type) - c.error('cannot cast voidptr to `${tt}` outside `unsafe`', node.pos) + c.warn('casting voidptr to `${tt}` is only allowed in `unsafe` code', node.pos) } else if to_sym.kind == .sum_type { to_sym_info := to_sym.info as ast.SumType if c.pref.skip_unused && to_sym_info.concrete_types.len > 0 { diff --git a/vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.out b/vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.out similarity index 53% rename from vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.out rename to vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.out index b092d10fa..57b28e819 100644 --- a/vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.out +++ b/vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.out @@ -1,11 +1,11 @@ -vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.vv:3:6: error: cannot cast voidptr to `&u32` outside `unsafe` +vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.vv:3:6: warning: casting voidptr to `&u32` is only allowed in `unsafe` code 1 | fn main() { 2 | some_ptr := voidptr(1234) 3 | _ = &u32(some_ptr) | ~~~~~~~~~~~~~~ 4 | _ = &u32(voidptr(5678)) 5 | } -vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.vv:4:6: error: cannot cast voidptr to `&u32` outside `unsafe` +vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.vv:4:6: warning: casting voidptr to `&u32` is only allowed in `unsafe` code 2 | some_ptr := voidptr(1234) 3 | _ = &u32(some_ptr) 4 | _ = &u32(voidptr(5678)) diff --git a/vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.vv b/vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.vv similarity index 100% rename from vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_err.vv rename to vlib/v/checker/tests/voidptr_cast_to_ref_outside_unsafe_warn.vv diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9acad3ad8..286bef88e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2431,7 +2431,19 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) { g.styp(func.return_type) g.type_definitions.write_string('typedef ${ret_typ} (${msvc_call_conv}*${fn_name})(') for i, param in func.params { - g.type_definitions.write_string(g.fn_param_type_decl(param.typ, '')) + const_prefix := if param.typ.is_any_kind_of_pointer() && !param.is_mut + && param.name.starts_with('const_') { + 'const ' + } else { + '' + } + if const_prefix != '' { + styp := g.styp(param.typ) + g.type_definitions.write_string(const_prefix + + g.const_pointer_param_type_name(param.typ, styp)) + } else { + g.type_definitions.write_string(g.fn_param_type_decl(param.typ, '')) + } if i < func.params.len - 1 { g.type_definitions.write_string(',') } diff --git a/vlib/v/gen/wasm/tests/arrays.vv b/vlib/v/gen/wasm/tests/arrays.vv index d43543c6f..6a05c881f 100644 --- a/vlib/v/gen/wasm/tests/arrays.vv +++ b/vlib/v/gen/wasm/tests/arrays.vv @@ -46,7 +46,7 @@ mut: fn test_stuff() &int { a := AA{} - mut b := &int(0) + mut b := &int(unsafe { nil }) b = a.a[2] return b diff --git a/vlib/v/gen/wasm/tests/misc.vv b/vlib/v/gen/wasm/tests/misc.vv index 9af617255..63c4cee1e 100644 --- a/vlib/v/gen/wasm/tests/misc.vv +++ b/vlib/v/gen/wasm/tests/misc.vv @@ -94,7 +94,7 @@ fn ptr_in_struct() { mut s := HasPointers{1234, &msg, unsafe { nil }} println(*s.msg) s.same = &s - println(*&int(voidptr(s.same.a))) // FIXME: wont work without casts + println(*unsafe { &int(voidptr(s.same.a)) }) // FIXME: wont work without casts } fn main() { diff --git a/vlib/v2/builder/gen_arm64_parallel.v b/vlib/v2/builder/gen_arm64_parallel.v index 569b9d924..7470730a8 100644 --- a/vlib/v2/builder/gen_arm64_parallel.v +++ b/vlib/v2/builder/gen_arm64_parallel.v @@ -76,7 +76,8 @@ fn (mut b Builder) gen_arm64_parallel(mut gen arm64.Gen) { C.pthread_attr_setstacksize(attr, 64 * 1024 * 1024) for ci := 0; ci < chunk_idx; ci++ { - C.pthread_create(unsafe { &thread_ids[ci] }, attr, gen_arm64_chunk_thread, unsafe { voidptr(&args[ci]) }) + C.pthread_create(unsafe { &thread_ids[ci] }, attr, gen_arm64_chunk_thread, + unsafe { voidptr(&args[ci]) }) } C.pthread_attr_destroy(attr) diff --git a/vlib/v2/builder/gen_cleanc_parallel.v b/vlib/v2/builder/gen_cleanc_parallel.v index 5fcf17711..a46324a26 100644 --- a/vlib/v2/builder/gen_cleanc_parallel.v +++ b/vlib/v2/builder/gen_cleanc_parallel.v @@ -78,7 +78,8 @@ fn (mut b Builder) gen_cleanc_parallel(mut gen cleanc.Gen) { C.pthread_attr_setstacksize(attr, 64 * 1024 * 1024) for ci := 0; ci < chunk_idx; ci++ { - C.pthread_create(unsafe { &thread_ids[ci] }, attr, gen_cleanc_chunk_thread, unsafe { voidptr(&args[ci]) }) + C.pthread_create(unsafe { &thread_ids[ci] }, attr, gen_cleanc_chunk_thread, + unsafe { voidptr(&args[ci]) }) } C.pthread_attr_destroy(attr) diff --git a/vlib/v2/types/checker_ownership.v b/vlib/v2/types/checker_ownership.v index 7d1e6608e..8340c9709 100644 --- a/vlib/v2/types/checker_ownership.v +++ b/vlib/v2/types/checker_ownership.v @@ -32,8 +32,8 @@ fn (mut c Checker) ownership_check_ident(name string, pos token.Pos) { file := c.file_set.file(pos) move_file := c.file_set.file(info.move_pos) move_position := move_file.position(info.move_pos) - errors.error('use of moved value: `${name}`', errors.details(file, file.position(pos), - 2), .error, file.position(pos)) + errors.error('use of moved value: `${name}`', errors.details(file, file.position(pos), 2), + .error, file.position(pos)) eprintln(' --> move occurs because `${name}` has type `string`, which does not implement the `Copy` interface') if info.is_fn_call { eprintln(' --> value moved into function `${info.fn_name}` at ${move_position}') @@ -315,11 +315,11 @@ fn (mut c Checker) ownership_add_borrow(var_name string, borrower string, pos to borrow_file := c.file_set.file(borrow.pos) borrow_position := borrow_file.position(borrow.pos) if borrow.is_mut { - errors.error('cannot borrow `${var_name}` as mutable more than once', - errors.details(file, file.position(pos), 2), .error, file.position(pos)) + errors.error('cannot borrow `${var_name}` as mutable more than once', errors.details(file, + file.position(pos), 2), .error, file.position(pos)) } else { - errors.error('cannot borrow `${var_name}` as mutable because it is also borrowed as immutable', - errors.details(file, file.position(pos), 2), .error, file.position(pos)) + errors.error('cannot borrow `${var_name}` as mutable because it is also borrowed as immutable', errors.details(file, + file.position(pos), 2), .error, file.position(pos)) } eprintln(' --> previous borrow by `${borrow.borrower}` at ${borrow_position}') exit(1) @@ -331,8 +331,8 @@ fn (mut c Checker) ownership_add_borrow(var_name string, borrower string, pos to file := c.file_set.file(pos) borrow_file := c.file_set.file(borrow.pos) borrow_position := borrow_file.position(borrow.pos) - errors.error('cannot borrow `${var_name}` as immutable because it is borrowed as mutable', - errors.details(file, file.position(pos), 2), .error, file.position(pos)) + errors.error('cannot borrow `${var_name}` as immutable because it is borrowed as mutable', errors.details(file, + file.position(pos), 2), .error, file.position(pos)) eprintln(' --> mutable borrow by `${borrow.borrower}` at ${borrow_position}') exit(1) } diff --git a/vlib/v2/types/checker_ownership_test.v b/vlib/v2/types/checker_ownership_test.v index 87f84c980..a8157fef9 100644 --- a/vlib/v2/types/checker_ownership_test.v +++ b/vlib/v2/types/checker_ownership_test.v @@ -32,7 +32,8 @@ fn run_ownership_check(code string) (int, string) { tmp_file := os.join_path(tmp_dir, 'test.v') os.write_file(tmp_file, code) or { panic('failed to write temp file') } v2 := v2_ownership_exe() - res := os.execute('${os.quoted_path(v2)} -ownership -o ${os.join_path(tmp_dir, 'out')} ${os.quoted_path(tmp_file)} 2>&1') + res := + os.execute('${os.quoted_path(v2)} -ownership -o ${os.join_path(tmp_dir, 'out')} ${os.quoted_path(tmp_file)} 2>&1') return res.exit_code, res.output } -- 2.39.5