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

map[Enum]u32 where Enum is an @[flag] enum Enum as u64 is not recognized as map index beyond the 32nd bit #23630

Closed
revosw opened this issue Feb 2, 2025 · 1 comment · Fixed by #23632
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Enums Bugs involving enumerations. Maps Bugs involving the builtin `map` type and derivatives from it like `map[string]int` etc. 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. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@revosw
Copy link

revosw commented Feb 2, 2025

Describe the bug

Using u64 enum flags as keys in a map only works for the first 32 bits.

Reproduction Steps

Code: https://play.vlang.io/p/c6dad391f8

@[flag]
enum TableFlags as u64 {
	module                   // bit 1
	type_ref                 // bit 2
	type_def                 // bit 3
	field_ptr                // bit 4
	field                    // bit 5
	method_ptr               // bit 6
	method_def               // bit 7
	param_ptr                // bit 8
	param                    // bit 9
	interface_impl           // bit 10
	member_ref               // bit 11
	constant                 // bit 12
	custom_attribute         // bit 13
	field_marshal            // bit 14
	decl_security            // bit 15
	class_layout             // bit 16
	field_layout             // bit 17
	stand_alone_sig          // bit 18
	event_map                // bit 19
	event_ptr                // bit 20
	event                    // bit 21
	property_map             // bit 22
	property_ptr             // bit 23
	property                 // bit 24
	method_semantics         // bit 25
	method_impl              // bit 26
	module_ref               // bit 27
	type_spec                // bit 28
	impl_map                 // bit 29
	field_rva                // bit 30
	enc_lg                   // bit 31
	enc_map                  // bit 32
	assembly                 // bit 33
	assembly_processor       // bit 34
	assembly_os              // bit 35
	assembly_ref             // bit 36
	assembly_ref_processor   // bit 37
	assembly_ref_os          // bit 38
	file                     // bit 39
	exported_type            // bit 40
	manifest_resource        // bit 41
	nested_class             // bit 42
	generic_param            // bit 43
	method_spec              // bit 44
	generic_param_constraint // bit 45
}

fn main() {
	unsafe {
		mut num_rows := map[TableFlags]u32{}

		for i in 0 .. 45 {
			num_rows[TableFlags(u64(1) << i)] = i
		}

		for key, val in num_rows {
			println('${key} ${val}')
		}
	}
}

Expected Behavior

All expected bits from 0..44 are set

TableFlags{.module} 0
TableFlags{.type_ref} 1
TableFlags{.type_def} 2
TableFlags{.field_ptr} 3
TableFlags{.field} 4
TableFlags{.method_ptr} 5
TableFlags{.method_def} 6
TableFlags{.param_ptr} 7
TableFlags{.param} 8
TableFlags{.interface_impl} 9
TableFlags{.member_ref} 10
TableFlags{.constant} 11
TableFlags{.custom_attribute} 12
TableFlags{.field_marshal} 13
TableFlags{.decl_security} 14
TableFlags{.class_layout} 15
TableFlags{.field_layout} 16
TableFlags{.stand_alone_sig} 17
TableFlags{.event_map} 18
TableFlags{.event_ptr} 19
TableFlags{.event} 20
TableFlags{.property_map} 21
TableFlags{.property_ptr} 22
TableFlags{.property} 23
TableFlags{.method_semantics} 24
TableFlags{.method_impl} 25
TableFlags{.module_ref} 26
TableFlags{.type_spec} 27
TableFlags{.impl_map} 28
TableFlags{.field_rva} 29
TableFlags{.enc_lg} 30
TableFlags{.enc_map} 31
TableFlags{.assembly} 32
TableFlags{.assembly_processor} 33
TableFlags{.assembly_os} 34
TableFlags{.assembly_ref} 35
TableFlags{.assembly_ref_processor} 36
TableFlags{.assembly_ref_os} 37
TableFlags{.file} 38
TableFlags{.exported_type} 39
TableFlags{.manifest_resource} 40
TableFlags{.nested_class} 41
TableFlags{.generic_param} 42
TableFlags{.method_spec} 43
TableFlags{.generic_param_constraint} 44

Current Behavior

Output:

The last 32..44 entries are empty

TableFlags{.module} 0
TableFlags{.type_ref} 1
TableFlags{.type_def} 2
TableFlags{.field_ptr} 3
TableFlags{.field} 4
TableFlags{.method_ptr} 5
TableFlags{.method_def} 6
TableFlags{.param_ptr} 7
TableFlags{.param} 8
TableFlags{.interface_impl} 9
TableFlags{.member_ref} 10
TableFlags{.constant} 11
TableFlags{.custom_attribute} 12
TableFlags{.field_marshal} 13
TableFlags{.decl_security} 14
TableFlags{.class_layout} 15
TableFlags{.field_layout} 16
TableFlags{.stand_alone_sig} 17
TableFlags{.event_map} 18
TableFlags{.event_ptr} 19
TableFlags{.event} 20
TableFlags{.property_map} 21
TableFlags{.property_ptr} 22
TableFlags{.property} 23
TableFlags{.method_semantics} 24
TableFlags{.method_impl} 25
TableFlags{.module_ref} 26
TableFlags{.type_spec} 27
TableFlags{.impl_map} 28
TableFlags{.field_rva} 29
TableFlags{.enc_lg} 30
TableFlags{.enc_map} 31
TableFlags{} 44

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 b91bbad

Environment details (OS name and version, etc.)

|V full version      |V 0.4.9 b91bbad
|:-------------------|:-------------------
|OS                  |linux, Debian GNU/Linux 12 (bookworm) (VM)
|Processor           |2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
|Memory              |1.31GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-01-31 21:25:26
|                    |
|V home dir          |OK, value: /home/admin/v
|VMODULES            |OK, value: .vmodules
|VTMP                |OK, value: /tmp/v_0
|Current working dir |OK, value: /home/admin/playground
|                    |
|Git version         |git version 2.39.5
|V git status        |Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v
|.git/config present |true
|                    |
|cc version          |cc (Debian 12.2.0-14) 12.2.0
|gcc version         |gcc (Debian 12.2.0-14) 12.2.0
|clang version       |Debian clang version 14.0.6
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git status      |Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

|emcc version        |N/A
|glibc version       |ldd (Debian GLIBC 2.36-9+deb12u8) 2.36

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.

@revosw revosw added the Bug This tag is applied to issues which reports bugs. label Feb 2, 2025
Copy link

Connected to Huly®: V_0.6-22054

spytheman added a commit to spytheman/v that referenced this issue Feb 2, 2025
@spytheman spytheman self-assigned this Feb 2, 2025
@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Maps Bugs involving the builtin `map` type and derivatives from it like `map[string]int` etc. Enums Bugs involving enumerations. labels Feb 2, 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. Enums Bugs involving enumerations. Maps Bugs involving the builtin `map` type and derivatives from it like `map[string]int` etc. 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. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants