v icon indicating copy to clipboard operation
v copied to clipboard

Problem with mutable pointer parameters under certain circumstances

Open Xyndra opened this issue 11 months ago • 2 comments

Describe the bug

The following code throws runtime errors of "invalid memory access"

module main

import veb

pub struct App {}
struct Context { veb.Context }

struct Quizzes {
    quizzes []string
}
    
fn (app &App) index(mut ctx &Context) veb.Result {
    mut dirs := []string{}
    dirs << 'a'
    dump(dirs)
    return ctx.json(Quizzes{dirs})
}

fn main() {
    mut app := &App{}
    veb.run_at[App, Context](mut app)!
}

The fix is the following:

- fn (app &App) index(mut ctx &Context) veb.Result {
+ fn (app &App) index(mut ctx Context) veb.Result {

Note that this seems to be because of the generics in vlib/veb/veb.v:781: app.$method(mut user_context). When not using generics, the compiler notices:

module main

struct Abc {
mut:
	def []string
}

fn write_to_abc(mut abc &Abc) {
	// list out all directories in the quizzes directory
	abc.def << 'Test'
}

fn main() {
	mut abc := &Abc{}
	write_to_abc(mut abc)
}

Reproduction Steps

see above

Expected Behavior

compiler error

Current Behavior

runtime error

Possible Solution

apply what happens when not using generics to generics

Additional Information/Context

No response

V version

2bd2d00ca

Environment details (OS name and version, etc.)

V full version V 0.4.10 d0ce8a2.27e4b02
OS windows, Microsoft Windows 11 Pro 22635 64-bit
Processor 8 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
Memory 40.2GB/63.67GB
V executable D:\software\v\v.exe
V last modified time 2025-05-01 16:41:10
V home dir OK, value: D:\software\v
VMODULES OK, value: C:\Users\Sammy.vmodules
VTMP OK, value: C:\Users\Sammy\AppData\Local\Temp\v_0
Current working dir OK, value: D:\Code\v\vocab\bug
Git version git version 2.49.0.windows.1
V git status weekly.2025.16-83-g27e4b027-dirty
.git/config present true
cc version N/A
gcc version gcc (MinGW.org GCC-6.3.0-1) 6.3.0
clang version clang version 20.1.4
msvc version N/A
tcc version tcc version 0.9.27 (x86_64 Windows)
tcc git status thirdparty-windows-amd64 b425ac82
emcc version emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.3 (a9651ff57165f5710bb09a5fe52590fd6ddb72df)
glibc version N/A

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

Xyndra avatar May 04 '25 10:05 Xyndra

Connected to Huly®: V_0.6-22780

huly-for-github[bot] avatar May 04 '25 10:05 huly-for-github[bot]

Using clang-18 with -cstrict:

================== C compilation error (from clang-18): ==============
cc: /tmp/v_1000/bug.tmp.c:22754:30: error: incompatible pointer types passing 'main__Context *' (aka 'struct main__Context *') to parameter of type 'main__Context **' (aka 'struct main__Context **'); take the address with & [-Werror,-Wincompatible-pointer-types]
cc:  22754 |                                                                 main__App_index(app, user_context);
cc:        |                                                                                      ^~~~~~~~~~~~
cc:        |                                                                                      &
cc: /tmp/v_1000/bug.tmp.c:5919:77: note: passing argument to parameter 'ctx' here
cc:  5919 | VV_LOCAL_SYMBOL veb__Result main__App_index(main__App* app, main__Context** ctx);
cc:       |                                                                             ^
cc: /tmp/v_1000/bug.tmp.c:22787:30: error: incompatible pointer types passing 'main__Context *' (aka 'struct main__Context *') to parameter of type 'main__Context **' (aka 'struct main__Context **'); take the address with & [-Werror,-Wincompatible-pointer-types]
cc:  22787 |                                                                 main__App_index(app, user_context);
cc:        |                                                                                      ^~~~~~~~~~~~
cc:        |                                                                                      &
cc: /tmp/v_1000/bug.tmp.c:5919:77: note: passing argument to parameter 'ctx' here
cc:  5919 | VV_LOCAL_SYMBOL veb__Result main__App_index(main__App* app, main__Context** ctx);
cc:       |                                                                             ^
cc: 2 errors generated.

felipensp avatar May 17 '25 22:05 felipensp