defer is broken when placed before a $foreach #1912

This commit is contained in:
Christoffer Lerno
2025-01-31 14:39:51 +01:00
parent 7dd9256e2d
commit 9092defd46
15 changed files with 432 additions and 104 deletions

View File

@@ -342,6 +342,15 @@ bool ast_is_compile_time(Ast *ast)
return expr_is_runtime_const(ast->return_stmt.expr);
case AST_EXPR_STMT:
return expr_is_runtime_const(ast->expr_stmt);
case AST_CT_COMPOUND_STMT:
{
AstId current = ast->ct_compound_stmt;
while (current)
{
if (!ast_is_compile_time(ast_next(&current))) return false;
}
return true;
}
case AST_COMPOUND_STMT:
{
AstId current = ast->compound_stmt.first_stmt;

View File

@@ -821,6 +821,7 @@ static void c_emit_stmt(GenContext *c, Ast *stmt)
break;
case AST_CT_ECHO_STMT:
break;
case AST_CT_COMPOUND_STMT:
case AST_CT_ELSE_STMT:
break;
case AST_CT_FOREACH_STMT:

View File

@@ -1488,6 +1488,7 @@ typedef struct Ast_
AstAssertStmt assert_stmt; // 16
AstCaseStmt case_stmt; // 32
AstCompoundStmt compound_stmt; // 12
AstId ct_compound_stmt;
AstContinueBreakStmt contbreak_stmt;// 24
AstContractStmt contract_stmt; // 32
AstDocFault contract_fault; // 24

View File

@@ -697,6 +697,9 @@ RETRY:
MACRO_COPY_EXPRID(ast->ct_switch_stmt.cond);
MACRO_COPY_AST_LIST(ast->ct_switch_stmt.body);
break;
case AST_CT_COMPOUND_STMT:
MACRO_COPY_ASTID(ast->ct_compound_stmt);
break;
case AST_DECLARE_STMT:
MACRO_COPY_DECL(ast->declare_stmt);
break;

View File

@@ -189,15 +189,19 @@ typedef enum
typedef enum
{
AST_POISONED,
AST_ASM_STMT,
AST_ASM_BLOCK_STMT,
AST_ASM_LABEL,
AST_ASM_STMT,
AST_ASSERT_STMT,
AST_BLOCK_EXIT_STMT,
AST_BREAK_STMT,
AST_CASE_STMT,
AST_COMPOUND_STMT,
AST_CONTINUE_STMT,
AST_CONTRACT,
AST_CONTRACT_FAULT,
AST_CT_ASSERT,
AST_CT_COMPOUND_STMT,
AST_CT_ECHO_STMT,
AST_CT_ELSE_STMT,
AST_CT_FOREACH_STMT,
@@ -209,17 +213,14 @@ typedef enum
AST_DEFAULT_STMT,
AST_DEFER_STMT,
AST_EXPR_STMT,
AST_FOR_STMT,
AST_FOREACH_STMT,
AST_FOR_STMT,
AST_IF_CATCH_SWITCH_STMT,
AST_IF_STMT,
AST_NEXTCASE_STMT,
AST_NOP_STMT,
AST_RETURN_STMT,
AST_BLOCK_EXIT_STMT,
AST_SWITCH_STMT,
AST_NEXTCASE_STMT,
AST_CONTRACT,
AST_CONTRACT_FAULT,
} AstKind;
typedef enum

View File

@@ -29,6 +29,7 @@ void llvm_emit_compound_stmt(GenContext *c, Ast *ast)
if (old_block) c->debug.block_stack = old_block;
}
void llvm_emit_local_static(GenContext *c, Decl *decl, BEValue *value)
{
// In defers we might already have generated this variable.
@@ -1690,6 +1691,9 @@ void llvm_emit_stmt(GenContext *c, Ast *ast)
case AST_BLOCK_EXIT_STMT:
llvm_emit_block_exit_return(c, ast);
break;
case AST_CT_COMPOUND_STMT:
llvm_emit_statement_chain(c, ast->ct_compound_stmt);
break;
case AST_COMPOUND_STMT:
llvm_emit_compound_stmt(c, ast);
break;

View File

@@ -1100,9 +1100,9 @@ static inline Ast* parse_ct_foreach_stmt(ParseContext *c)
TRY_CONSUME_OR_RET(TOKEN_COLON, "Expected ':'.", poisoned_ast);
ASSIGN_EXPRID_OR_RET(ast->ct_foreach_stmt.expr, parse_expr(c), poisoned_ast);
CONSUME_OR_RET(TOKEN_RPAREN, poisoned_ast);
Ast *body = new_ast(AST_COMPOUND_STMT, ast->span);
Ast *body = new_ast(AST_CT_COMPOUND_STMT, ast->span);
ast->ct_foreach_stmt.body = astid(body);
AstId *current = &body->compound_stmt.first_stmt;
AstId *current = &body->ct_compound_stmt;
while (!try_consume(c, TOKEN_CT_ENDFOREACH))
{
ASSIGN_AST_OR_RET(Ast *stmt, parse_stmt(c), poisoned_ast);
@@ -1140,9 +1140,9 @@ static inline Ast* parse_ct_for_stmt(ParseContext *c)
CONSUME_OR_RET(TOKEN_RPAREN, poisoned_ast);
Ast *body = new_ast(AST_COMPOUND_STMT, ast->span);
Ast *body = new_ast(AST_CT_COMPOUND_STMT, ast->span);
ast->for_stmt.body = astid(body);
AstId *current = &body->compound_stmt.first_stmt;
AstId *current = &body->ct_compound_stmt;
while (!try_consume(c, TOKEN_CT_ENDFOR))
{
ASSIGN_AST_OR_RET(Ast *stmt, parse_stmt(c), poisoned_ast);

View File

@@ -3763,6 +3763,78 @@ INLINE bool sema_analyse_macro_body(SemaContext *context, Decl **body_parameters
}
return true;
}
static inline bool sema_check_body_const(SemaContext *context, Ast *body)
{
while (body)
{
switch (body->ast_kind)
{
case AST_CT_ASSERT:
case AST_CT_ECHO_STMT:
case AST_CT_FOREACH_STMT:
case AST_CT_FOR_STMT:
case AST_CT_IF_STMT:
case AST_CT_SWITCH_STMT:
case AST_CT_ELSE_STMT:
case AST_DECLARE_STMT:
case AST_DECLS_STMT:
case AST_NOP_STMT:
body = astptrzero(body->next);
continue;
case AST_CT_COMPOUND_STMT:
if (!sema_check_body_const(context, body)) return false;
body = astptrzero(body->next);
continue;
case AST_RETURN_STMT:
if (!body->return_stmt.expr) RETURN_SEMA_ERROR(body, "The 'return' in an `@const` must provide a value, e.g. 'return Foo.typeid;'");
if (body->next) RETURN_SEMA_ERROR(body, "There should not be any statements after 'return'.");
body = NULL;
continue;
case AST_EXPR_STMT:
// For some cases we KNOW it's not correct.
switch (body->expr_stmt->expr_kind)
{
case EXPR_IDENTIFIER:
case EXPR_UNRESOLVED_IDENTIFIER:
case EXPR_LAMBDA:
case EXPR_FORCE_UNWRAP:
case EXPR_ASM:
case EXPR_EXPR_BLOCK:
case EXPR_TERNARY:
case EXPR_RETHROW:
break;
default:
body = astptrzero(body->next);
continue;
}
FALLTHROUGH;
case AST_POISONED:
case AST_ASM_STMT:
case AST_ASM_LABEL:
case AST_ASM_BLOCK_STMT:
case AST_ASSERT_STMT:
case AST_BREAK_STMT:
case AST_CASE_STMT:
case AST_COMPOUND_STMT:
case AST_CONTINUE_STMT:
case AST_DEFAULT_STMT:
case AST_DEFER_STMT:
case AST_FOR_STMT:
case AST_FOREACH_STMT:
case AST_IF_CATCH_SWITCH_STMT:
case AST_IF_STMT:
case AST_BLOCK_EXIT_STMT:
case AST_SWITCH_STMT:
case AST_NEXTCASE_STMT:
case AST_CONTRACT:
case AST_CONTRACT_FAULT:
RETURN_SEMA_ERROR(body, "Only 'return' and compile time statements are allowed in an '@const' macro.");
}
UNREACHABLE
}
return true;
}
static inline bool sema_analyse_macro(SemaContext *context, Decl *decl, bool *erase_decl)
{
decl->func_decl.unit = context->unit;
@@ -3800,69 +3872,7 @@ static inline bool sema_analyse_macro(SemaContext *context, Decl *decl, bool *er
ASSERT(body->ast_kind == AST_COMPOUND_STMT);
body = astptrzero(body->compound_stmt.first_stmt);
if (!body) RETURN_SEMA_ERROR(decl, "'@const' macros cannot have an empty body.");
while (body)
{
switch (body->ast_kind)
{
case AST_CT_ASSERT:
case AST_CT_ECHO_STMT:
case AST_CT_FOREACH_STMT:
case AST_CT_FOR_STMT:
case AST_CT_IF_STMT:
case AST_CT_SWITCH_STMT:
case AST_CT_ELSE_STMT:
case AST_DECLARE_STMT:
case AST_DECLS_STMT:
case AST_NOP_STMT:
body = astptrzero(body->next);
continue;
case AST_RETURN_STMT:
if (!body->return_stmt.expr) RETURN_SEMA_ERROR(body, "The 'return' in an `@const` must provide a value, e.g. 'return Foo.typeid;'");
if (body->next) RETURN_SEMA_ERROR(body, "There should not be any statements after 'return'.");
body = NULL;
continue;
case AST_EXPR_STMT:
// For some cases we KNOW it's not correct.
switch (body->expr_stmt->expr_kind)
{
case EXPR_IDENTIFIER:
case EXPR_UNRESOLVED_IDENTIFIER:
case EXPR_LAMBDA:
case EXPR_FORCE_UNWRAP:
case EXPR_ASM:
case EXPR_EXPR_BLOCK:
case EXPR_TERNARY:
case EXPR_RETHROW:
break;
default:
body = astptrzero(body->next);
continue;
}
FALLTHROUGH;
case AST_POISONED:
case AST_ASM_STMT:
case AST_ASM_LABEL:
case AST_ASM_BLOCK_STMT:
case AST_ASSERT_STMT:
case AST_BREAK_STMT:
case AST_CASE_STMT:
case AST_COMPOUND_STMT:
case AST_CONTINUE_STMT:
case AST_DEFAULT_STMT:
case AST_DEFER_STMT:
case AST_FOR_STMT:
case AST_FOREACH_STMT:
case AST_IF_CATCH_SWITCH_STMT:
case AST_IF_STMT:
case AST_BLOCK_EXIT_STMT:
case AST_SWITCH_STMT:
case AST_NEXTCASE_STMT:
case AST_CONTRACT:
case AST_CONTRACT_FAULT:
RETURN_SEMA_ERROR(body, "Only 'return' and compile time statements are allowed in an '@const' macro.");
}
UNREACHABLE
}
sema_check_body_const(context, body);
}
decl->type = type_void;
return true;

View File

@@ -113,6 +113,9 @@ static void sema_trace_stmt_liveness(Ast *ast)
case AST_NOP_STMT:
case AST_ASM_LABEL:
return;
case AST_CT_COMPOUND_STMT:
sema_trace_stmt_chain_liveness(ast->ct_compound_stmt);
return;
case AST_COMPOUND_STMT:
sema_trace_stmt_chain_liveness(ast->compound_stmt.first_stmt);
return;

View File

@@ -232,6 +232,27 @@ static inline bool sema_analyse_compound_stmt(SemaContext *context, Ast *stateme
return success;
}
/**
* Ct compound statement
*/
static inline bool sema_analyse_ct_compound_stmt(SemaContext *context, Ast *statement)
{
if (!ast_ok(statement)) return false;
AstId current = statement->ct_compound_stmt;
Ast *ast = NULL;
bool all_ok = true;
while (current)
{
ast = ast_next(&current);
if (!sema_analyse_statement(context, ast))
{
ast_poison(ast);
all_ok = false;
}
}
return all_ok;
}
/**
* continue and continue FOO;
*/
@@ -2824,13 +2845,20 @@ static inline bool sema_analyse_ct_foreach_stmt(SemaContext *context, Ast *state
index->var.init_expr = expr_new_const_int(index->span, type_int, i);
index->type = type_int;
}
if (!sema_analyse_compound_statement_no_scope(context, compound_stmt)) goto FAILED;
if (!sema_analyse_statement(context, compound_stmt)) goto FAILED;
*current = astid(compound_stmt);
current = &compound_stmt->next;
}
sema_context_pop_ct_stack(context, ct_context);
statement->ast_kind = AST_COMPOUND_STMT;
statement->compound_stmt = (AstCompoundStmt) { .first_stmt = start };
if (!start)
{
statement->ast_kind = AST_NOP_STMT;
}
else
{
statement->ast_kind = AST_CT_COMPOUND_STMT;
statement->ct_compound_stmt = start;
}
return true;
FAILED_NO_LIST:
SEMA_ERROR(collection, "Expected a list to iterate over, but this was a non-list expression of type %s.",
@@ -3046,7 +3074,7 @@ static inline bool sema_analyse_ct_for_stmt(SemaContext *context, Ast *statement
Ast *compound_stmt = copy_ast_single(body);
// Analyse the body
if (!sema_analyse_compound_statement_no_scope(context, compound_stmt)) goto FAILED;
if (!sema_analyse_statement(context, compound_stmt)) goto FAILED;
// Append it.
*current = astid(compound_stmt);
@@ -3059,8 +3087,8 @@ static inline bool sema_analyse_ct_for_stmt(SemaContext *context, Ast *statement
}
}
// Analysis is done turn the generated statements into a compound statement for lowering.
statement->ast_kind = AST_COMPOUND_STMT;
statement->compound_stmt = (AstCompoundStmt) { .first_stmt = start };
statement->ast_kind = AST_CT_COMPOUND_STMT;
statement->ct_compound_stmt = start;
return true;
FAILED:
sema_context_pop_ct_stack(context, for_context);
@@ -3091,6 +3119,8 @@ static inline bool sema_analyse_statement_inner(SemaContext *context, Ast *state
RETURN_SEMA_ERROR(statement, "Unexpected 'case' outside of switch");
case AST_COMPOUND_STMT:
return sema_analyse_compound_stmt(context, statement);
case AST_CT_COMPOUND_STMT:
return sema_analyse_ct_compound_stmt(context, statement);
case AST_CONTINUE_STMT:
return sema_analyse_continue_stmt(context, statement);
case AST_CT_ASSERT: