From faf073885ffa854de325f5ec27ef7401270d0eb6 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 24 Jun 2025 22:28:14 +0200 Subject: [PATCH] Updated fix of #2218 --- src/compiler/sema_casts.c | 6 ++++++ .../distinct/distinct_bitstruct_conv.c3 | 15 +++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/test_suite/distinct/distinct_bitstruct_conv.c3 diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index d948cbd5e..da0a16ee3 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -1540,8 +1540,14 @@ static bool rule_bits_to_int(CastContext *cc, bool is_explicit, bool is_silent) if (is_silent && !is_explicit) return false; Type *base_type = cc->from->decl->strukt.container_type->type->canonical; Type *to = cc->to; +RETRY: if (base_type != to) { + if (base_type->type_kind == TYPE_DISTINCT && (base_type->decl->is_substruct || is_explicit)) + { + base_type = base_type->decl->distinct->type->canonical; + goto RETRY; + } if (!type_is_integer(base_type) || type_size(to) != type_size(base_type)) { return sema_cast_error(cc, false, is_silent); diff --git a/test/test_suite/distinct/distinct_bitstruct_conv.c3 b/test/test_suite/distinct/distinct_bitstruct_conv.c3 new file mode 100644 index 000000000..ff02eb90f --- /dev/null +++ b/test/test_suite/distinct/distinct_bitstruct_conv.c3 @@ -0,0 +1,15 @@ +typedef Foo = int; + +bitstruct Bar : Foo +{ + bool a; +} + +fn int main() +{ + Bar b; + (Foo)b; + (int)b; + ($typefrom(Bar.inner))b; + return 0; +} \ No newline at end of file