From a459364de37353a4a7a4bf77cc68bfdbd6b7fbc3 Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Tue, 18 Jul 2017 15:32:34 +0100 Subject: Ignore missing default values for struct literals at the end --- src/check_expr.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src/check_expr.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 1dad15c22..e4f21704f 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7220,6 +7220,21 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t { // Checker values isize field_count = t->Record.field_count; + isize min_field_count = t->Record.field_count; + for (isize i = min_field_count-1; i >= 0; i--) { + Entity *e = t->Record.fields_in_src_order[i]; + GB_ASSERT(e->kind == Entity_Variable); + if (e->Variable.default_is_nil) { + min_field_count--; + } else if (e->Variable.default_is_undef) { + min_field_count--; + } else if (e->Variable.default_value.kind != ExactValue_Invalid) { + min_field_count--; + } else { + break; + } + } + if (cl->elems[0]->kind == AstNode_FieldValue) { bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count); @@ -7293,7 +7308,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t continue; } if (index >= field_count) { - error(o->expr, "Too many values in structure literal, expected %td", field_count); + error(o->expr, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count); break; } @@ -7322,7 +7337,13 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t check_assignment(c, o, field->type, str_lit("structure literal")); } if (cl->elems.count < field_count) { - error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count); + if (min_field_count < field_count) { + if (cl->elems.count < min_field_count) { + error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count); + } + } else { + error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count); + } } } } -- cgit v1.2.3