From 27e22daa10040999f6e5bfe47aa2b8b504b40071 Mon Sep 17 00:00:00 2001 From: lemon Date: Sun, 7 Aug 2022 06:41:48 +0200 Subject: union init --- bootstrap/parse.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'bootstrap/parse.c') diff --git a/bootstrap/parse.c b/bootstrap/parse.c index f61e65b..b18c169 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -731,6 +731,7 @@ parsestructini(struct parser *P, const struct type *ty) { vec_t(struct iniarg) args = {0}; int idx = 0; struct aggfield *fld; + const char *kind = ty->t == TYstruct ? "struct" : "union"; ex.span = lexpeek(P).span; @@ -742,7 +743,7 @@ parsestructini(struct parser *P, const struct type *ty) { lexexpect(P, ':'); idx = structfldnam2idx(ty, fnam); if (idx < 0) - fatal(P, tok.span, "struct %t has no field `%s'", ty, fnam); + fatal(P, tok.span, "%s %t has no field `%s'", kind, ty, fnam); } fld = structidx2fld(ty, idx++); @@ -753,8 +754,8 @@ parsestructini(struct parser *P, const struct type *ty) { "excess elements in struct initializer"); if (!typeof2(e.ty, fld->ty)) fatal(P, e.span, - "incompatible element `%s` type in struct initializer (%t, expected %t)", - fld->name, e.ty, fld->ty); + "incompatible element `%s` type in %s initializer (%t, expected %t)", + fld->name, kind, e.ty, fld->ty); vec_push(&args, ((struct iniarg) { .fld = fld->name, @@ -886,7 +887,7 @@ pexprimary(struct parser *P) { lexexpect(P, ':'); P->targty = ty; goto enumlit; - } else if (ty->t == TYstruct || ty->t == TYenum) { + } else if (ty->t == TYstruct || ty->t == TYunion) { lexexpect(P, '{'); P->targty = ty; goto aggini; @@ -954,7 +955,7 @@ pexprimary(struct parser *P) { fatal(P, tok.span, "cannot infer type for compound initializer"); if (lexmatch(P, &tok, '}')) ex.t = Ezeroini, ex.ty = P->targty; - else if (P->targty->t == TYstruct) + else if (P->targty->t == TYstruct || P->targty->t == TYunion) ex = parsestructini(P, P->targty); else if (P->targty->t == TYarr) ex = parsearrini(P, P->targty); -- cgit v1.2.3