v icon indicating copy to clipboard operation
v copied to clipboard

Compiler error when building global arrays of empty Structs

Open raw-bin opened this issue 7 months ago • 1 comments

Describe the bug

Consider the following code:

❯ cat 1.v
module main

const num_elements = 10

struct DummyStruct {}

__global (
        d [num_elements]DummyStruct
)

fn main() {}

When built with the following invocation, the compilation fails:

❯ v -enable-globals 1.v
==================
/tmp/v_501/1.01J1JR6W82J8GJBYBQHK0EWBH8.tmp.c:2068:41: error: initializer for aggregate with no elements requires explicit braces
 Array_fixed_main__DummyStruct_10  d = {0}; // global4
                                        ^
1 error generated.
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

On the other hand, if DummyStruct is non-empty then the compilation succeeds.

Consider the following:

❯ cat 2.v
module main

const num_elements = 10

struct DummyStruct {
        dummy_item i32
}

__global (
        d [num_elements]DummyStruct
)

fn main() {}

The only change is that DummyStruct now has a dummy_item.

The compilation succeeds as follows:

❯ ~/Work/repos/vlang/v/v -enable-globals 2.v

NOTE:

❯ v --version
V 0.4.6 23e3894

❯ neofetch --off
[email protected]
-------------------
OS: macOS 14.5 23F79 arm64
Host: Mac14,9
Kernel: 23.5.0
Uptime: 11 days, 11 hours, 48 mins
Packages: 250 (port)
Shell: fish 3.7.1
Resolution: 5120x1440 , 3024x1964
DE: Aqua
WM: Quartz Compositor
WM Theme: Blue (Dark)
Terminal: tmux
CPU: Apple M2 Pro
GPU: Apple M2 Pro
Memory: 4652MiB / 32768MiB

Reproduction Steps

See above

Expected Behavior

Structs containing no members is a perfectly valid use case and is often referenced in the official documentation. There is nothing special about them that warrants a compiler build failure in the case of global arrays.

Current Behavior

See bug description above.

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.6 23e3894

Environment details (OS name and version, etc.)

macOS 14.5 arm64

[!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.

raw-bin avatar Jun 29 '24 19:06 raw-bin