Skip to content

[autofree] string intp generate wrong result #25481

@kbkpbot

Description

@kbkpbot

Describe the bug

In code with string intp, it will free the tmp var too early, cause a runtime wrong result.

Reproduction Steps

c.v

module main

fn return_str(x int) string {
        if x == 300 {
                t := '${x}'
                return '${t}_400'
        }

        return ''
}

fn main() {
        dump(return_str(300))
}
v -autofree run c.v

Expected Behavior

[c.v:13] return_str(300): 300_400

Current Behavior

[c.v:13] return_str(300): _400

Possible Solution

No response

Additional Information/Context

VV_LOC string main__return_str(int x) {
        if (x == 300) {
                string t = builtin__str_intp(2, _MOV((StrIntpData[]){{_SLIT0, 0xfe07, {.d_i32 = x}}, {_SLIT0, 0, { .d_c = 0 }}}));
                        builtin__string_free(&t); // autofreed var main false
return builtin__str_intp(2, _MOV((StrIntpData[]){{_SLIT0, 0xfe10, {.d_s = t}}, {_S("_400"), 0, { .d_c = 0 }}}));
                builtin__string_free(&t); // autofreed var main false
}

note that the first builtin__string_free(&t); // autofreed var main false free the t too early.

V version

V 0.4.12 60b0ee9

Environment details (OS name and version, etc.)

V full version V 0.4.12 60b0ee9
OS linux, Ubuntu 24.04.3 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 8.75GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-10-11 13:42:47
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars
Git version git version 2.43.0
V git status weekly.2025.03-23-g329719e5-dirty (1328 commit(s) behind V master)
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.6) 2.39

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

    AutofreeBugs/feature requests, that are related to -autofree.BugThis tag is applied to issues which reports bugs.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