Skip to content

Commit

Permalink
Macros with trailing bodys aren't allowed as the single statement aft…
Browse files Browse the repository at this point in the history
…er a while loop with no body #1772.
  • Loading branch information
lerno committed Jan 5, 2025
1 parent c6c7baa commit ab2d223
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- Fix vector float -> bool conversion.
- Fix `+a = 1` erronously being accepted.
- Fix not freeing a zero length String
- Macros with trailing bodys aren't allowed as the single statement after a while loop with no body #1772.

### Stdlib changes
- Increase BitWriter.write_bits limit up to 32 bits.
Expand Down
7 changes: 5 additions & 2 deletions src/compiler/parse_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ static inline Ast* parse_while_stmt(ParseContext *c)
CONSUME_OR_RET(TOKEN_RPAREN, poisoned_ast);
unsigned row = c->prev_span.row;
ASSIGN_AST_OR_RET(Ast *body, parse_stmt(c), poisoned_ast);
if (body->ast_kind != AST_COMPOUND_STMT && row != c->prev_span.row)
if (body->ast_kind != AST_COMPOUND_STMT && row != body->span.row)
{
PRINT_ERROR_AT(body, "A single statement after 'while' must be placed on the same line, or be enclosed in {}.");
return poisoned_ast;
Expand Down Expand Up @@ -1465,8 +1465,11 @@ Ast *parse_stmt(ParseContext *c)
advance(c);
return poisoned_ast;
case TOKEN_EOS:
{
Ast *nop = ast_new_curr(c, AST_NOP_STMT);
advance(c);
return ast_new_curr(c, AST_NOP_STMT);
return nop;
}
case TOKEN_EOF:
PRINT_ERROR_HERE("Reached the end of the file when expecting a statement.");
return poisoned_ast;
Expand Down
34 changes: 34 additions & 0 deletions test/test_suite/statements/while_statement_placement2.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import std;

fn void loop_start() => io::printn("Loop Start");
fn void loop_end() => io::printn("Loop End");
fn void do_something() => io::printn("Do something");

macro @thing(; @body())
{
loop_start();
@body();
loop_end();
}

fn void strcpy(char *s1, char *s2) {
while (*s1++ = *s2++);
}

fn void main()
{
while (true) @thing()
{
do_something();
break;
};
for (;;) @thing()
{
do_something();
break;
};
if (true) @thing()
{
do_something();
};
}

0 comments on commit ab2d223

Please sign in to comment.