harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 27275e119da0c4fe109f137374d9fa508ae6ff59
parent 536450338a0166c0e49ca54389a1ec61a897b54e
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Sat, 18 Feb 2023 04:39:34 +0100

check: detect repeated struct field initialization

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Msrc/check.c | 6+++++-
Mtests/06-structs.ha | 9+++++++++
2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/check.c b/src/check.c @@ -2657,8 +2657,12 @@ check_struct_exhaustive(struct context *ctx, continue; } if (strcmp(f->name, sf->name) == 0) { + if (found) { + error(ctx, aexpr->loc, expr, + "Field '%s' is initialized multiple times", + sf->name); + } found = true; - break; } } diff --git a/tests/06-structs.ha b/tests/06-structs.ha @@ -167,6 +167,15 @@ fn invariants() void = { }; ", + // multiple initializations for single field + "type s = struct { x: int }; fn test() s = s { x = 5, x = 7 };", + + "type e = struct { x: int }, s = struct { y: int, e };" + "fn test() s = s { x = 5, x = 7 };", + + "type e = struct { x: int }, s = struct { e, y: int};" + "fn test() s = s { x = 5, x = 7 };", + // Duplicate members "fn test() void = { let x: struct { a: int, a: int } = struct { a: int = 2 };" // Dereference non-nullable pointer: