From 963b8f28ef3e26d2026f1f1beff0e57f72421cbc Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 6 Dec 2022 20:09:40 +0100 Subject: [PATCH] Fix of multiple returns in macros retaining ct values. Correctly require ; after endfor etc. --- src/compiler/parse_stmt.c | 2 ++ src/compiler/sema_expr.c | 17 ++++++++--------- src/version.h | 2 +- test/test_suite/compile_time/ct_memberof.c3t | 4 ++-- test/test_suite2/compile_time/ct_memberof.c3t | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index fef0cb431..c4f0ae5b9 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -939,6 +939,7 @@ static inline Ast* parse_ct_foreach_stmt(ParseContext *c) *current = astid(stmt); current = &stmt->next; } + CONSUME_EOS_OR_RET(poisoned_ast); return ast; } @@ -980,6 +981,7 @@ static inline Ast* parse_ct_for_stmt(ParseContext *c) *current = astid(stmt); current = &stmt->next; } + CONSUME_EOS_OR_RET(poisoned_ast); return ast; } diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 5c55e34a5..f9e68721f 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -1528,7 +1528,7 @@ static inline Type *context_unify_returns(SemaContext *context) if (!max) { SEMA_ERROR(return_stmt, "Cannot find a common parent type of %s and %s", - rtype, common_type); + type_quoted_error_string(rtype), type_quoted_error_string(common_type)); Ast *prev = context->returns[i - 1]; assert(prev); SEMA_NOTE(prev, "The previous return was here."); @@ -1840,20 +1840,19 @@ bool sema_expr_analyse_macro_call(SemaContext *context, Expr *call_expr, Expr *s { is_no_return = true; } - if (returns_found == 1) + if (returns_found) { Ast *ret = macro_context.returns[0]; Expr *result = ret ? ret->return_stmt.expr : NULL; - if (result && expr_is_constant_eval(result, CONSTANT_EVAL_CONSTANT_VALUE)) + if (!result) goto NOT_CT; + if (!expr_is_constant_eval(result, CONSTANT_EVAL_CONSTANT_VALUE)) goto NOT_CT; + if (ast_is_compile_time(body)) { - if (ast_is_compile_time(body)) - { - expr_replace(call_expr, result); - goto EXIT; - } + expr_replace(call_expr, result); + goto EXIT; } } - +NOT_CT: call_expr->expr_kind = EXPR_MACRO_BLOCK; call_expr->macro_block.first_stmt = body->compound_stmt.first_stmt; call_expr->macro_block.params = params; diff --git a/src/version.h b/src/version.h index 551b6af5c..5635dcee0 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.3.106" \ No newline at end of file +#define COMPILER_VERSION "0.3.107" \ No newline at end of file diff --git a/test/test_suite/compile_time/ct_memberof.c3t b/test/test_suite/compile_time/ct_memberof.c3t index 443e812bb..18e4d116c 100644 --- a/test/test_suite/compile_time/ct_memberof.c3t +++ b/test/test_suite/compile_time/ct_memberof.c3t @@ -9,7 +9,7 @@ macro print_args($Type) var $params = $Type.params; $foreach ($param : $params): io::println($param.nameof); - $endforeach + $endforeach; } bitstruct Bark : ulong @@ -37,7 +37,7 @@ macro print_fields($Type) var $params = $Type.membersof; $foreach ($param : $params): io::printfln("%s: %s", $param.nameof, $param.typeid.nameof); - $endforeach + $endforeach; } fn void hello(int a, double b, int[4] d, args...) {} diff --git a/test/test_suite2/compile_time/ct_memberof.c3t b/test/test_suite2/compile_time/ct_memberof.c3t index 71de872f2..72b420e93 100644 --- a/test/test_suite2/compile_time/ct_memberof.c3t +++ b/test/test_suite2/compile_time/ct_memberof.c3t @@ -9,7 +9,7 @@ macro print_args($Type) var $params = $Type.params; $foreach ($param : $params): io::println($param.nameof); - $endforeach + $endforeach; } bitstruct Bark : ulong @@ -37,7 +37,7 @@ macro print_fields($Type) var $params = $Type.membersof; $foreach ($param : $params): io::printfln("%s: %s", $param.nameof, $param.typeid.nameof); - $endforeach + $endforeach; } fn void hello(int a, double b, int[4] d, args...) {}