From f65ca07b624a9200229c68471370f8f8577f3074 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 12 Jan 2025 13:31:35 +0100 Subject: [PATCH] Fix bug when multiple `$else` clauses followed an `$if` #1824. --- releasenotes.md | 2 ++ src/compiler/parse_stmt.c | 2 +- test/test_suite/compile_time/ct_else_else.c3 | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/test_suite/compile_time/ct_else_else.c3 diff --git a/releasenotes.md b/releasenotes.md index a823e08e4..18a28a03c 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -31,6 +31,7 @@ - Handle bytes and string literals the same way in terms of zero termination. - Function comments are stored and displayed with -P. - Prevent `#hash` arguments from taking code that modifies ct variables. #1794 + ### Fixes - Fix case trying to initialize a `char[*]*` from a String. - Fix Map & HashMap `put_all_for_create` not copying all elements, causing `init_from_map` to create incomplete copy. @@ -79,6 +80,7 @@ - Assert when partially initializing a constant struct containing a slice #1812. - Assert concatenating constant slices #1805. - Do not link "ld" on Linux with no libc. +- Fix bug when multiple `$else` clauses followed an `$if` #1824. ### Stdlib changes - Increase BitWriter.write_bits limit up to 32 bits. diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index b2bfc03a4..ea5c27f32 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -1040,7 +1040,7 @@ static inline Ast *parse_ct_if_stmt(ParseContext *c) if (!parse_ct_compound_stmt(c, &else_ast->ct_else_stmt)) return poisoned_ast; ast->ct_if_stmt.elif = astid(else_ast); } - advance_and_verify(c, TOKEN_CT_ENDIF); + CONSUME_OR_RET(TOKEN_CT_ENDIF, poisoned_ast); RANGE_EXTEND_PREV(ast); return ast; } diff --git a/test/test_suite/compile_time/ct_else_else.c3 b/test/test_suite/compile_time/ct_else_else.c3 new file mode 100644 index 000000000..d6dcd7e23 --- /dev/null +++ b/test/test_suite/compile_time/ct_else_else.c3 @@ -0,0 +1,8 @@ +// Issue #1824 +fn void main() +{ + $if true: + $else + $else // #error: Expected '$endif' + $endif +} \ No newline at end of file