Skip to content

json.encode_pretty error when passing an inline initialized struct inside a function #24879

Description

@khalyomede

Describe the bug

When passing an inline initialized struct to json.encode_pretty(), which is ran inside a function, it raises a C error.

Reproduction Steps

Run this code:

module main

import json

struct Definition {
    version u8
}

struct Logic {
    run fn () i8 @[required]
}

fn main() {
    logic := Logic{
        run: fn () i8 {
            json.encode_pretty(Definition{})

            return 0
        }
    }

    logic.run()
}

Or run it on the V playground

Expected Behavior

No error when formatting (pretty) a struct to JSON using an inline initialized struct.

Current Behavior

It raises this C error:

================== C compilation error (from gcc): ==============
cc: /tmp/v_0/main3.01JZXAN0TFJBFZ2DV2WMF20R8G.tmp.c: In function 'anon_fn_afedf1f6577f2f11___i8_165':
cc: /tmp/v_0/main3.01JZXAN0TFJBFZ2DV2WMF20R8G.tmp.c:2019:2: error: cannot convert to a pointer type
cc:  2019 |  cJSON* _t1 = json__encode_main__Definition((voidptr)((main__Definition){.version = 0,}));
cc:       |  ^~~~~
cc: /tmp/v_0/main3.01JZXAN0TFJBFZ2DV2WMF20R8G.tmp.c:2019:45: error: incompatible type for argument 1 of 'json__encode_main__Definition'
cc:  2019 |  cJSON* _t1 = json__encode_main__Definition((voidptr)((main__Definition){.version = 0,}));
cc:       |                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc:       |                                             |
cc:       |                                             void *
cc: /tmp/v_0/main3.01JZXAN0TFJBFZ2DV2WMF20R8G.tmp.c:2008:55: note: expected 'main__Definition' but argument is of type 'void *'
cc:  2008 | cJSON* json__encode_main__Definition(main__Definition val) {
cc:       |                                      ~~~~~~~~~~~~~~~~~^~~
=================================================================

Possible Solution

For the moment I countered this error by initializing the struct in a variable and passing the initialized variable to the function.

Additional Information/Context

Also note the same code will not raise a C error when the JSON pretty formatting is performed outside a function.

module main

import json

struct Definition {
	version u8
}

fn main() {
	json.encode_pretty(Definition{})
	
	println("Hello world!")
}

(or view it on the online V playground)

V version

V 0.4.11 d1d43ab

Environment details (OS name and version, etc.)

V full version V 0.4.11 d1d43ab
OS linux, N/A (VM)
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
Memory 2.98GB/3.68GB
V executable /opt/v/v
V last modified time 2025-06-28 03:25:29
V home dir OK, value: /opt/v
VMODULES OK, value: /home/alpine/app/vine_packages
VTMP OK, value: /tmp/v_0
Current working dir OK, value: /home/alpine/app
env VFLAGS "-cc gcc"
Git version git version 2.30.6
V git status d1d43ab
.git/config present true
cc version cc (Alpine 10.2.1_pre1) 10.2.1 20201203
gcc version gcc (Alpine 10.2.1_pre1) 10.2.1 20201203
clang version N/A
tcc version tcc version 0.9.27 mob:d3e940c (x86_64 Linux)
tcc git status thirdparty-linuxmusl-amd64 a3e24da2
emcc version N/A
glibc version Error: musl libc (x86_64)
Version 1.2.2
Dynamic Program Loader
Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname

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 fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions