Skip to content

Passed SumType reference is passed by copy while it should be passed as is #24019

@lenaing

Description

@lenaing

Describe the bug

When I create a reference to a SumType type, pass it to a function and modify it inside, the original struct does not have it's field updated.

Reproduction Steps

module main

type MySumType = MyStructA | MyStructB

struct MyStructA {
mut:
    test bool
}

struct MyStructB {
}

fn main() {
    mut my_struct := &MyStructA{
        test: false
    }
    println(my_struct)
    my_struct.test = true
    println(my_struct)
    but_why(mut my_struct)
    println(my_struct)
}

fn but_why(mut passed MySumType) {
    if mut passed is MyStructA {
        passed.test = false
    }
}

Expected Behavior

Expected :

&MyStructA{
    test: false
}
&MyStructA{
    test: true
}
&MyStructA{
    test: false
}

Current Behavior

Got :

&MyStructA{
    test: false
}
&MyStructA{
    test: true
}
&MyStructA{
    test: true
}

Possible Solution

Current workaround is to pass a copy :

module main

type Event = CloseEvent | OpenEvent

struct CloseEvent {
mut:
    can_close bool
}

struct OpenEvent {
}

fn main() {
    mut close_event := &CloseEvent{
        can_close: true
    }

    // Pass a copy
    mut close_event_copy := Event(close_event)
    close_callback(mut close_event_copy)
    if mut close_event_copy is CloseEvent {
        if !close_event_copy.can_close {
            println("Won't close, sorry.")
        }

        // Update reference, is this the best way to do this?
        close_event = &close_event_copy
    }

}

fn close_callback(mut passed Event) {
    if mut passed is CloseEvent {
        passed.can_close = false
    }
}

But it would be easier to just :

fn main() {
    mut close_event := &CloseEvent{
        can_close: true
    }

    close_callback(mut close_event)
    if !close_event.can_close {
        println("Won't close, sorry.")
    }
}

Additional Information/Context

#help request on Discord

V version

V 0.4.10 d629a01

Environment details (OS name and version, etc.)

V full version V 0.4.10 d970a8f.d629a01
OS linux, Debian GNU/Linux trixie/sid
Processor 12 cpus, 64bit, little endian, Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Memory 18.08GB/31.19GB
V executable /home/lenain/.bin/v/v
V last modified time 2025-03-23 10:50:30
V home dir OK, value: /home/lenain/.bin/v
VMODULES OK, value: /home/lenain/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/lenain/Documents/Projects/V/test
Git version git version 2.47.2
V git status weekly.2023.33-3706-gd629a017
.git/config present true
cc version cc (Debian 14.2.0-17) 14.2.0
gcc version gcc (Debian 14.2.0-17) 14.2.0
clang version Debian clang version 19.1.7 (1+b1)
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (Debian GLIBC 2.41-6) 2.41

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

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

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions