Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C error when used both json2.decode[json2.Any] and json2.decode[map[string]json2.Any] #24128

Open
jorgeluismireles opened this issue Apr 3, 2025 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Apr 3, 2025

Describe the bug

We got C error cannot convert 'struct x__json2__Any' to 'int' when decoding a map with generics json2.Any and map[string]json2.Any. Commenting one of the two generics code gives different results.

Reproduction Steps

import x.json2

fn decode_any(j string) {
	t := json2.decode[json2.Any](j) or {
		println('ERR any: ${j} -> ${err}')	
		return
	}
	println(' OK any: ${j} -> ${t}')
}

fn decode_map(j string) {
	s := json2.decode[map[string]json2.Any](j) or {
		println('ERR map: ${j} -> ${err}')
		return
	}
	println(' OK map: ${s}')
}

fn main() {
	for j in [ '{}' '{"a":"b"}' '{"a":1}' '{"a":3.14}' '{"a":{"b":"c"}}' '{"a":true}' ] {
		decode_any(j)
		decode_map(j)
	}
}

Expected Behavior

Trying to decode a map of any instead of any.

Current Behavior

Can't run code. The server returned an error:
/home/admin/v/vlib/x/json2/decoder.v:67: error: cannot convert 'struct x__json2__Any' to 'int'
builder error: 
==================
C error found. It should never happen, when compiling pure V code.

Possible Solution

Workaround 1

Comment decode_map() inner code:

import x.json2

fn decode_any(j string) {
	t := json2.decode[json2.Any](j) or {
		println('ERR any: ${j} -> ${err}')	
		return
	}
	println(' OK any: ${j} -> ${t}')
}

fn decode_map(j string) {
	/*s := json2.decode[map[string]json2.Any](j) or {
		println('ERR map: ${j} -> ${err}')
		return
	}
	println(' OK map: ${s}')*/
}

fn main() {
	for j in [ '{}' '{"a":"b"}' '{"a":1}' '{"a":3.14}' '{"a":{"b":"c"}}' '{"a":true}' ] {
		decode_any(j)
		decode_map(j)
	}
}

and we got rid of C error and get expected results:

OK any: {} -> {}
 OK any: {"a":"b"} -> {"a":"b"}
 OK any: {"a":1} -> {"a":1}
 OK any: {"a":3.14} -> {"a":3.14}
 OK any: {"a":{"b":"c"}} -> {"a":{"b":"c"}}
 OK any: {"a":true} -> {"a":true}

Workaround 2

Comment decode_any() inner code:

import x.json2

fn decode_any(j string) {
	/*t := json2.decode[json2.Any](j) or {
		println('ERR any: ${j} -> ${err}')	
		return
	}
	println(' OK any: ${j} -> ${t}')*/
}

fn decode_map(j string) {
	s := json2.decode[map[string]json2.Any](j) or {
		println('ERR map: ${j} -> ${err}')
		return
	}
	println(' OK map: ${s}')
}

fn main() {
	for j in [ '{}' '{"a":"b"}' '{"a":1}' '{"a":3.14}' '{"a":{"b":"c"}}' '{"a":true}' ] {
		decode_any(j)
		decode_map(j)
	}
}

And we get rid of C error but results are not correct (maybe is another issue):

 OK map: {}
 OK map: {"a":"b"}
 OK map: {}
 OK map: {}
 OK map: {}
 OK map: {}

Additional Information/Context

No response

V version

V 0.4.10 87f92b1

Environment details (OS name and version, etc.)

https://play.vlang.io/p/d3c19509a9

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.

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Apr 3, 2025
Copy link

Connected to Huly®: V_0.6-22519

@felipensp felipensp added Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

2 participants