Macros with trailing bodys aren't allowed as the single statement after a while loop with no body #1772.

This commit is contained in:
Christoffer Lerno
2025-01-05 16:00:39 +01:00
parent c6c7baa3b4
commit ab2d223e71
3 changed files with 40 additions and 2 deletions

View File

@@ -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.

View File

@@ -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;
@@ -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;

View File

@@ -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();
};
}