Skip to content

Problem with mutable pointer parameters under certain circumstances #24408

Open
@Xyndra

Description

@Xyndra

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

2bd2d00

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions