Fix issue with @const where the statement $foo = 1; was not considered constant.

This commit is contained in:
Christoffer Lerno
2025-01-18 22:40:58 +01:00
parent c5dbbf9ff7
commit 5a36f0bc16
4 changed files with 41 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
- Fix bug in SHA1 for longer blocks #1854.
- Fix lack of location for reporting lambdas with missing return statement #1857.
- Compiler allows a generic module to be declared with different parameters #1856.
- Fix issue with `@const` where the statement `$foo = 1;` was not considered constant.
### Stdlib changes
- Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.

View File

@@ -319,7 +319,7 @@ bool sema_expr_analyse_ct_concat(SemaContext *context, Expr *concat_expr, Expr *
if (!sema_analyse_expr(context, left)) return false;
if (!sema_cast_const(left)) RETURN_SEMA_ERROR(left, "Expected this to evaluate to a constant value.");
if (!sema_analyse_expr(context, right)) return false;
if (!sema_cast_const(right)) RETURN_SEMA_ERROR(left, "Expected this to evaluate to a constant value.");
if (!sema_cast_const(right)) RETURN_SEMA_ERROR(right, "Expected this to evaluate to a constant value.");
Type *element_type = left->type->canonical;
Type *right_type = right->type->canonical;
switch (left->const_expr.const_kind)

View File

@@ -3829,6 +3829,23 @@ static inline bool sema_analyse_macro(SemaContext *context, Decl *decl, bool *er
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_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:
@@ -3840,7 +3857,6 @@ static inline bool sema_analyse_macro(SemaContext *context, Decl *decl, bool *er
case AST_CONTINUE_STMT:
case AST_DEFAULT_STMT:
case AST_DEFER_STMT:
case AST_EXPR_STMT:
case AST_FOR_STMT:
case AST_FOREACH_STMT:
case AST_IF_CATCH_SWITCH_STMT:

View File

@@ -0,0 +1,22 @@
module wi3enrich;
import std::io;
import std::os::env;
macro String to_string(uint $num) @const
{
char[] $res;
int $i = 0;
$for (;$num != 0;)
$res = $res +++ (char) ('0' + $num % 10);
$num = $num / 10;
$i++;
$endfor
$res = $res +++ '\0';
return (String) $res;
}
fn int main(String[] args)
{
io::printn(to_string(4));
return 0;
}