Passing a map to a generic argument changes a key value from int to an empty string #22035
Labels
Bug
This tag is applied to issues which reports bugs.
Comptime
Features processed during compile time, like $if, $for, $env etc
Generics[T]
Bugs/feature requests, that are related to the V generics.
Unit: cgen
Bugs/feature requests, that are related to the default C generating backend.
Unit: Checker
Bugs/feature requests, that are related to the type checker.
Describe the bug
{ 'a': 1 }
marshal
marshal_map
{ 'a': '' }
This is simplified code. Runnable file is further below.
Apparently, the value
map[string]int
was passed asmap[string]string
frommarshal
tomarshal_map
and the value of the key'a'
became the default value forstring
-''
.Reproduction Steps
marshal_test.v
v marshal_test.v
.When
marshal_map[T](val map[string]T)
is called directly, the input map enters it correctly asmap[string]int
.When
marshal_map[T](val map[string]T)
is called frommarshal[T](val T)
, the input map enters it wrong asmap[string]string
.The code of
marshal_test.v
, which is very simplified from a JSON parser and formatter:Expected Behavior
When
marshal_map[T](val map[string]T)
is called frommarshal[T](val T)
with an input parameter of the typemap[string]int
, this type will be passed properly viamarshal
tomarshal_map
and the value of the key'a'
will be1
.Current Behavior
I guess that
map[string]int
was converted tomap[string]string
and all key values were replaced by strings with default string values -''
.Possible Solution
I wish I knew :-)
Additional Information/Context
When enabling the commented out
assert
inmarshal_map
:The compiler fails with a complaint that the right argument was supposed to be a string, as expected from my assumption above:
However, when changing the argument from
1
to'1'
:The compiler fails again, now desiring an integer:
It doesn't know what it wants :-)
V version
V 0.4.7 ddb6685
Environment details (OS name and version, etc.)
V full version: V 0.4.7 ddb6685
OS: macos, macOS, 14.5, 23F79
Processor: 12 cpus, 64bit, little endian, Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
getwd: /Users/ferdipr/Sources/github/v-bug
vexe: /Users/ferdipr/.vup/weekly.2024.32/v
vexe mtime: 2024-08-06 07:59:48
vroot: OK, value: /Users/ferdipr/.vup/weekly.2024.32
VMODULES: OK, value: /Users/ferdipr/.vmodules
VTMP: OK, value: /tmp/v_501
Git version: git version 2.46.0
Git vroot status: Error: fatal: not a git repository (or any of the parent directories): .git
.git/config present: false
CC version: Apple clang version 15.0.0 (clang-1500.3.9.4)
thirdparty/tcc: 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.
The text was updated successfully, but these errors were encountered: