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

Maps with constant keys are broken #23255

Closed
gechandesu opened this issue Dec 24, 2024 · 0 comments · Fixed by #23256
Closed

Maps with constant keys are broken #23255

gechandesu opened this issue Dec 24, 2024 · 0 comments · Fixed by #23256
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@gechandesu
Copy link
Contributor

gechandesu commented Dec 24, 2024

Describe the bug

If the map keys are constants the map breaks and has only two elements of the declared ones — the first and the last. The keys themselves are incorrect.

Reproduction Steps

const bit = f64(1)
const nibble = bit * 4
const bytes = bit * 8
const kb = bytes * 1000
const mb = kb * 1000

const units_map = {
	bit: 'bit'
	nibble: 'nibble'
	bytes: 'byte'
	kb: 'kB'
	mb: 'MB'
}

fn main() {
	println(units_map.len)
	println(units_map)
}

Expected Behavior

Output:

5
{1.0: 'bit', 4.0: 'nibble', 8.0: 'byte', 8000.0: 'kB', 8.e+06: 'MB'}

Current Behavior

Output:

2
{1.0: 'bit', 0.0: 'MB'}

Possible Solution

No response

Additional Information/Context

Issue in example above reproduces with any numberic primitive type except int.

Also the following code works:

const k1 = f64(1)
const k2 = f64(2)
const k3 = f64(3)
const k4 = f64(4)
const k5 = f64(5)

const my_map = {
	k1: 'first'
	k2: 'second'
	k3: 'third'
	k4: 'fourth'
	k5: 'fifth'
}

fn main() {
	assert my_map.len == 5
	assert my_map == {1.0: 'first', 2.0: 'second', 3.0: 'third', 4.0: 'fourth', 5.0: 'fifth'}
}

But if a arithmetic operations appears in a constant, it breaks the map:

const k1 = f64(1)
const k2 = f64(2 * 2)
const k3 = f64(3)
const k4 = f64(4)
const k5 = f64(5)

const my_map = {
	k1: 'first'
	k2: 'second'
	k3: 'third'
	k4: 'fourth'
	k5: 'fifth'
}

fn main() {
	assert my_map.len == 5
	assert my_map == {1.0: 'first', 2.0: 'second', 3.0: 'third', 4.0: 'fourth', 5.0: 'fifth'}
}
code.v:16:NaN: fn main.main: assert my_map.len == 5
  left value: my_map.len = 4
  right value: 5
V panic: Assertion failed...
v hash: e6c1637
/tmp/v_60000/../../../../../../home/admin/v/vlib/builtin/builtin.c.v:142: at _v_panic: Backtrace
/tmp/v_60000/../../../../../../box/code.v:18: by main__main
/tmp/v_60000/../../../../../../tmp/v_60000/code.01JFV5CEGNQ8HYH9Y2EK762PX4.tmp.c:13634: by main
Exited with error status 1

V version

V 0.4.9 e6c1637

Environment details (OS name and version, etc.)

V full version: V 0.4.9 5ec9bb5.e6c1637
OS: linux, Linux version 6.6.8-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0) #1 SMP PREEMPT_DYNAMIC Thu, 21 Dec 2023 19:01:01 +0000
Processor: 16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P

getwd: /home/ge/Code/v/dataunits
vexe: /home/ge/.vlang/v
vexe mtime: 2024-12-24 01:46:31

vroot: OK, value: /home/ge/.vlang
VMODULES: OK, value: /home/ge/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.47.1
Git vroot status: weekly.2024.51-52-ge6c1637d
.git/config present: true

CC version: cc (GCC) 14.2.1 20240910
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

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.

Huly®: V_0.6-21689

@gechandesu gechandesu added the Bug This tag is applied to issues which reports bugs. label Dec 24, 2024
@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Dec 24, 2024
@yuyi98 yuyi98 self-assigned this Dec 24, 2024
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants