Skip to content

Building Doom; global const array of pointers not created/initiated in C when pointers wrapped with "unsafe" #23674

Description

@BadaBingBee

Describe the bug

This v code fails to create the const "weapon_keys" when compiled to c using the c2v "build_whole_project.sh".

@[export: 'weapon_keys']
const weapon_keys = [unsafe { &key_weapon1 }, unsafe { &key_weapon2 }, unsafe { &key_weapon3 }, unsafe { &key_weapon4 }, unsafe { &key_weapon5 }, unsafe { &key_weapon6 }, unsafe { &key_weapon7 }, unsafe { &key_weapon8 }]!

When the "unsafe" is removed it creates the const as expected.

Reproduction Steps

  1. run doom build script:
    git clone git@github.com:vlang/doom.git
    cd doom/
    DOOM_FOLDER=pwd && ./build_whole_project.sh

  2. manually update the c2v translated "doom_v/_globals.v" const to use "unsafe":
    @[export: 'weapon_keys']
    const weapon_keys = [unsafe { &key_weapon1 }, unsafe { &key_weapon2 }, unsafe { &key_weapon3 }, unsafe { &key_weapon4 }, unsafe { &key_weapon5 }, unsafe { &key_weapon6 }, unsafe { &key_weapon7 }, unsafe { &key_weapon8 }]!
    // const weapon_keys = [&key_weapon1, &key_weapon2, &key_weapon3, &key_weapon4, &key_weapon5, &key_weapon6, &key_weapon7, &key_weapon8]!

  3. run the compiler step only (adjust target folder as required):
    v -cc clang -o x.c -w -translated chocolate-doom/src/doom/doom_v/

RESULT:
x.c will contain the const but will not be initiated:
Array_fixed_int_ptr1_8 main__weapon_keys; // inited later

Expected Behavior

fixed const array should be created/initiated

Current c2v (i.e. when there is no "unsafe") does this:

Array_fixed_int_ptr1_8 weapon_keys = {&key_weapon1, &key_weapon2, &key_weapon3, &key_weapon4, &key_weapon5, &key_weapon6, &key_weapon7, &key_weapon8};

Current Behavior

see reproduction steps

Possible Solution

No response

Additional Information/Context

No response

V version

Current V version: V 0.4.9 6e3f6ac

Environment details (OS name and version, etc.)

V full version V 0.4.9 d710d9e.6e3f6ac
OS linux, Linux version 6.12.11-200.fc41.x86_64 (mockbuild@8c05b49e2e66460390f7ce4d04d4f464) (gcc (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7), GNU ld version 2.43.1-5.fc41) #1 SMP PREEMPT_DYNAMIC Fri Jan 24 04:59:58 UTC 2025
Processor 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz
Memory 0.89GB/15.3GB
V executable /home/breandan/ws/v/v
V last modified time 2025-02-08 15:35:13
V home dir OK, value: /home/breandan/ws/v
VMODULES OK, value: /home/breandan/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/breandan/ws/doom
Git version git version 2.48.1
V git status weekly.2024.47-505-g6e3f6acf
.git/config present true
cc version cc (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7)
gcc version gcc (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7)
clang version clang version 19.1.7 (Fedora 19.1.7-2.fc41)
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (GNU libc) 2.40

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.ConstantsEvaluation of constants (compiletime and runtime)Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions