Skip to content

Compile time struct with union member access causes compiler error #3382

Description

@Ben0981

Example

module c3test;

struct Test {
	char i;
	union {	
		char a;
	}
}

fn int main()
{
	Test $v = { .i = 5 };
	char $c = $v.a;
	return 0;
}

Problem

If Test.i is set but no member of the union is set trying to access $v.a causes a compiler error. If Test.i is not set or if the union is not anonymous the code runs without issue.

Maybe setting Test.i causes the rest of the struct to not be initialized whereas if no member is initialized the whole struct is properly initialized.

Compiler output

⚠️ The compiler encountered an unexpected error: "Violated assert: init->kind == CONST_INIT_STRUCT".

- Function: sema_expr_fold_to_struct_member(...)
- Source file: /home/ben/git/c3c/src/compiler/sema_expr.c:6340

and the version

C3 Compiler Version:       0.8.3 (Pre-release, Jul 17 2026 01:46:03)
Installed directory:       /usr/local/bin
Git Hash:                  303dd2230308420098f0f8d6601ba7d02338e2c8
Backends:                  LLVM
LLVM version:              22.1.8
LLVM default target:       x86_64-unknown-linux-gnu

Metadata

Metadata

Assignees

Labels

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

Type

Fields

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions