c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Segfault in the compiler when using a bitstruct constant defined using a cast with an operator

Open TechnicalFowl opened this issue 5 months ago • 4 comments

Pretty niche, but if I define a global bitstruct and define it's value by casting a number then use it in a math expression it causes the compiler to crash.

Minimal reproducer:

import std::io;

bitstruct BitstructFlags : uint
{
    bool first;
    bool second;
}
const BitstructFlags B_FIRST = { true, false };
const BitstructFlags B_FIRST2 = (BitstructFlags) 1;
const BitstructFlags B_SECOND = { false, true };

fn void main()
{
    BitstructFlags x = B_FIRST2 | B_SECOND;
    io::printfn("X: %d", (uint)x);
}

Changing this to either BitstructFlags x = B_FIRST | B_SECOND; or BitstructFlags x = B_FIRST2; avoids the crash, so it's specific to using it in an expression with an operator.

c3c.exe --version
C3 Compiler Version:       0.7.3 (Pre-release, Jun 27 2025 11:52:32)
Installed directory:       C:/Data/c3c/build/Debug/
Git Hash:                  962a1fc8733c386eeba68360213964da5eabd2a6
Backends:                  LLVM
LLVM version:              19.1.5
LLVM default target:       x86_64-pc-windows-msvc

TechnicalFowl avatar Jun 27 '25 20:06 TechnicalFowl

I love niche bugs

lerno avatar Jun 27 '25 20:06 lerno

Reports of those bugs at least

lerno avatar Jun 27 '25 20:06 lerno

This should work now, please have a try.

lerno avatar Jun 27 '25 21:06 lerno

Thanks, it no longer crashes but the output is incorrect:

import std::io;

bitstruct BitstructFlags : uint
{
    bool first;
    bool second;
}
const BitstructFlags B_FIRST = { true, false };
const BitstructFlags B_FIRST2 = (BitstructFlags) 1;
const BitstructFlags B_SECOND = { false, true };

fn void main()
{
    BitstructFlags x = B_FIRST | B_SECOND;
    BitstructFlags y = B_FIRST2 | B_SECOND;
    io::printfn("X: %d Y: %d", (uint)x, (uint)y);
}

Output:

X: 3 Y: 1

TechnicalFowl avatar Jun 27 '25 21:06 TechnicalFowl

Please try it, it should be correct now.

lerno avatar Jul 07 '25 15:07 lerno

Looks all good :+1:

TechnicalFowl avatar Jul 07 '25 20:07 TechnicalFowl