commit 3c068264d2a5701ad29d5efb3e549e46d1eb7beb
parent 97e68dda83471aea74f5eea64ff3b865b1fc99de
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 28 Feb 2021 10:47:08 -0500
Improve type-related assertion failure messaging
Diffstat:
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/src/type_store.c b/src/type_store.c
@@ -1,12 +1,25 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include "check.h"
#include "eval.h"
#include "scope.h"
+#include "typedef.h"
#include "type_store.h"
#include "util.h"
+static char *
+gen_typename(const struct type *type)
+{
+ size_t sz = 0;
+ char *ptr = NULL;
+ FILE *f = open_memstream(&ptr, &sz);
+ emit_type(type, f);
+ fclose(f);
+ return ptr;
+}
+
static size_t
ast_array_len(struct type_store *store, const struct ast_type *atype)
{
@@ -326,7 +339,14 @@ tagged_init(struct type *type, struct type_tagged_union **tu, size_t nmemb)
struct type_tagged_union **next = &type->tagged.next;
for (size_t i = 1; i < nmemb; ++i) {
- assert(tu[i]->type->size != SIZE_UNDEFINED); // TODO
+ if (tu[i]->type->size == SIZE_UNDEFINED) {
+ char *type = gen_typename(tu[i]->type);
+ fprintf(stderr,
+ "Cannot create tagged union from type of undefined size %s\n",
+ type);
+ free(type);
+ }
+
if (tu[i]->type->size > type->size) {
type->size = tu[i]->type->size;
}
@@ -448,7 +468,12 @@ type_init_from_atype(struct type_store *store,
return;
}
- assert(obj->otype == O_TYPE); // TODO: Bubble this up
+ if (obj->otype != O_TYPE) {
+ fprintf(stderr, "Object '%s' is not a type\n",
+ identifier_unparse(&obj->ident));
+ assert(0);
+ }
+
if (atype->unwrap) {
*type = *type_dealias(obj->type);
break;
diff --git a/src/types.c b/src/types.c
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdbool.h>
+#include <stdio.h>
#include <string.h>
#include "types.h"
#include "util.h"
@@ -24,7 +25,11 @@ const struct type *
type_dealias(const struct type *type)
{
while (type->storage == STORAGE_ALIAS) {
- assert(type->alias.type != NULL);
+ if (type->alias.type == NULL) {
+ fprintf(stderr, "Cannot dealias incomplete type %s\n",
+ identifier_unparse(&type->alias.ident));
+ assert(0);
+ }
type = type->alias.type;
}
return type;