Skip to content

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

Closed
@larpon

Description

@larpon

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.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions