fixes a bug with forward declarations of packed structs
Signed-off-by: Ember Sawady <ecs@d2evs.net>
---
include/types.h | 1 -
src/type_store.c | 15 +++++----------
src/types.c | 12 ------------
tests/26-regression.ha | 6 ++++++
4 files changed, 11 insertions(+), 23 deletions(-)
diff --git a/include/types.h b/include/types.h
index e4a824c..470701b 100644
--- a/include/types.h
+++ b/include/types.h
@@ -192,7 +192,6 @@ bool type_is_assignable(struct context *ctx,
const struct type *to, const struct type *from);
const struct type *type_is_castable(struct context *ctx,
const struct type *to, const struct type *from);
-bool type_is_complete(const struct type *type);
const struct type *type_create_const(enum type_storage storage,
int64_t min, int64_t max);
diff --git a/src/type_store.c b/src/type_store.c
index 4d572fc..c5a93d3 100644
--- a/src/type_store.c
+++ b/src/type_store.c
@@ -910,20 +910,15 @@ type_init_from_atype(struct type_store *store,
break;
}
- bool packed = false;
- if (type_is_complete(type)) {
- const struct type *final = type_dealias(store->check_context, type);
- if (final->storage == STORAGE_STRUCT) {
- packed = final->struct_union.packed;
- }
- }
-
struct dimensions dim = {
.size = type->size,
.align = type->align,
};
- if (!packed) {
- add_padding(&dim.size, dim.align);
+ if (type->storage != STORAGE_STRUCT || !type->struct_union.packed) {
+ // padding an alias can only break packed structs
+ if (type->storage != STORAGE_ALIAS) {
+ add_padding(&dim.size, dim.align);
+ };
}
return dim;
}
diff --git a/src/types.c b/src/types.c
index 0456551..ecb52ae 100644
--- a/src/types.c
+++ b/src/types.c
@@ -67,18 +67,6 @@ type_dealias(struct context *ctx, const struct type *type)
return type;
}
-bool
-type_is_complete(const struct type *type)
-{
- while (type->storage == STORAGE_ALIAS) {
- if (type->alias.type == NULL) {
- return false;
- }
- type = type->alias.type;
- }
- return true;
-}
-
const struct struct_field *
type_get_field(struct context *ctx, const struct type *type, const char *name)
{
diff --git a/tests/26-regression.ha b/tests/26-regression.ha
index 3aed22d..e22ccb1 100644
--- a/tests/26-regression.ha
+++ b/tests/26-regression.ha
@@ -26,6 +26,10 @@ type b = struct { c: c };
type a = struct { b };
type c = *a;
+// order of these two matters
+let packedsz = size(packed);
+type packed = struct @packed { a: u64, b: u8 };
+
type d = [3]int;
type e = bool;
let x = [1, 2, 3]: d: []int;
@@ -204,4 +208,6 @@ export fn main() void = {
assert(rt::compile(*(&buf: *str)) as rt::exited == rt::EXIT_SUCCESS);
static insert(buf[n], rt::toutf8("::a")...);
assert(rt::compile(*(&buf: *str)) as rt::exited != rt::EXIT_SUCCESS);
+ assert(size(packed) == packedsz);
+ assert(size(packed) == 9);
};
--
2.42.0