Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bitstruct value cannot be used to index a const array in compile time. #2512

@xnhp0320

Description

@xnhp0320
 import std::io;

 bitstruct Some : char {
     char v : 0 .. 4;
 }

 const int[8] SOME_MAP = some_macro();

 macro some_macro () {
     Some $s = { 8 };
     int[8] $map;
     $for var $i = 0; $i < 4; $i++:
         $s = { .v = $s.v >> 1 };
     $endfor;
     $map[$s.v] = 1;
     return $map;
 }

 fn void main() {
     io::printfn("value is %s", SOME_MAP);
 }

has following error:

➜  c3lang git:(main) ✗ c3c compile-run test.c3
12:     $for var $i = 0; $i < 4; $i++:
13:         $s = { .v = $s.v >> 1 };
14:     $endfor;
15:     $map[$s.v] = 1;
             ^^^^
(CodePlay/c3lang/test.c3:15:10) Error: Assigning to a compile time constant requires a constant index.

changes into $map[(uint)$s.v] = 1 works. however, I think the compile-time behavior should be the same with the runtime as the runtime it can work.

➜  c3lang git:(main) ✗ c3c -V
C3 Compiler Version:       0.7.7 (Pre-release, Oct  5 2025 17:51:29)
Git Hash:                  1eb8c0ced147dc617de2a5f12235e95417f7e380
Backends:                  LLVM
LLVM version:              21.1.2
LLVM default target:       arm64-apple-darwin24.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingFixed needs testingNeeds verification / testing that it now works

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions