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

wrong error for union field type using const when > 1 type present #23946

Closed
larpon opened this issue Mar 15, 2025 · 1 comment · Fixed by #23949
Closed

wrong error for union field type using const when > 1 type present #23946

larpon opened this issue Mar 15, 2025 · 1 comment · Fixed by #23949
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. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@larpon
Copy link
Contributor

larpon commented Mar 15, 2025

Describe the bug

Odd error about a type used in another union field.

Reproduction Steps

module main

const c_u16_size = sizeof(u16)
const c_u32_size = sizeof(u32)

pub enum DataKind as u8 {
	u8_array
}

union U16Bytes {
	value u16
	bytes [c_u16_size]u8
}

union U32Bytes {
	value u32
	bytes [c_u32_size]u8
}

fn tcp_recv() {
	mut buf_u16u8 := [c_u16_size]u8{}

	_ := U16Bytes{
		bytes: buf_u16u8
	}

	kind := DataKind.u8_array
	match kind {
		.u8_array {
			buf_4u8 := [c_u32_size]u8{}
			_ := U32Bytes{
				bytes: buf_4u8 // error: cannot assign to field `bytes`: expected `[c_u16_size]u8`, not `[c_u32_size]u8` ??
			}
		}
	}
}

Expected Behavior

No checker error

Current Behavior

t.v:32:5: error: cannot assign to field `bytes`: expected `[c_u16_size]u8`, not `[c_u32_size]u8`
   30 |             buf_4u8 := [c_u32_size]u8{}
   31 |             _ := U32Bytes{
   32 |                 bytes: buf_4u8
      |                 ~~~~~~~~~~~~~~
   33 |             }
   34 |         }

Possible Solution

Commenting out everything related to other union compiles cleanly:

module main

// const c_u16_size = sizeof(u16)
const c_u32_size = sizeof(u32)

pub enum DataKind as u8 {
	u8_array
}

// union U16Bytes {
// 	value u16
// 	bytes [c_u16_size]u8
// }

union U32Bytes {
	value u32
	bytes [c_u32_size]u8
}

fn tcp_recv() {
	// mut buf_u16u8 := [c_u16_size]u8{}
	//
	// _ := U16Bytes{
	// 	bytes: buf_u16u8
	// }

	kind := DataKind.u8_array
	match kind {
		.u8_array {
			buf_4u8 := [c_u32_size]u8{}
			_ := U32Bytes{
				bytes: buf_4u8
			}
		}
	}
}

Additional Information/Context

No response

V version

0.4.9

Environment details (OS name and version, etc.)

Linux EndeavourOS (rolling/Arch based distro)

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.

@larpon larpon added the Bug This tag is applied to issues which reports bugs. label Mar 15, 2025
Copy link

Connected to Huly®: V_0.6-22342

@felipensp felipensp self-assigned this Mar 15, 2025
@felipensp felipensp added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Mar 15, 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: 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