From d697b910ba2f1d838b361bcf748f1530545df7b1 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 1 Jun 2025 23:50:13 +0200 Subject: [PATCH] Removed the naive check for compile time modification, which fixes #1997 but regresses in detection. --- releasenotes.md | 1 + src/compiler/compiler_internal.h | 1 - src/compiler/sema_expr.c | 27 +------------------ .../compile_time/mod_in_other_scope.c3 | 14 +++++----- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index d99d5782e..89580f0b5 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -60,6 +60,7 @@ - Too strict project view #2163. - Bug using `#foo` arguments with `$defined` #2173 - Incorrect ensure on String.split. +- Removed the naive check for compile time modification, which fixes #1997 but regresses in detection. ### Stdlib changes - Added `String.quick_ztr` and `String.is_zstr` diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 67fb9cf6c..ee35f1ce0 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -1667,7 +1667,6 @@ typedef struct bool ensures : 1; bool pure : 1; bool in_no_eval : 1; - bool in_other : 1; SourceSpan in_if_resolution; Decl **opt_returns; union diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 01bf34128..1992e3e61 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -238,23 +238,19 @@ static inline bool sema_constant_fold_ops(Expr *expr) typedef struct { bool in_no_eval; - bool in_other; } ContextSwitchState; static inline ContextSwitchState context_switch_state_push(SemaContext *context, SemaContext *new_context) { - ContextSwitchState state = { .in_no_eval = new_context->call_env.in_no_eval, - .in_other = new_context->call_env.in_other }; + ContextSwitchState state = { .in_no_eval = new_context->call_env.in_no_eval, }; new_context->call_env.in_no_eval = context->call_env.in_no_eval; - new_context->call_env.in_other = true; return state; } static inline void context_switch_stat_pop(SemaContext *swapped, ContextSwitchState state) { swapped->call_env.in_no_eval = state.in_no_eval; - swapped->call_env.in_other = state.in_other; } Expr *sema_enter_inline_member(Expr *parent, CanonicalType *type) @@ -6073,10 +6069,6 @@ static bool sema_expr_analyse_ct_identifier_assign(SemaContext *context, Expr *e Decl *ident = left->ct_ident_expr.decl; - if (context->call_env.in_other) - { - RETURN_SEMA_ERROR(left, "Compile time variables may only be modified in the scope they are defined in."); - } ident->var.init_expr = right; expr_replace(expr, right); @@ -6103,10 +6095,6 @@ static bool sema_expr_analyse_ct_subscript_rhs(SemaContext *context, Decl *ct_va static bool sema_expr_analyse_ct_subscript_set_value(SemaContext *context, Expr *left, Decl *ct_var, Expr *right) { - if (context->call_env.in_other) - { - RETURN_SEMA_ERROR(left, "Compile time variables may only be modified in the scope they are defined in."); - } ArrayIndex index = left->ct_subscript_expr.index; Expr *original_value = ct_var->var.init_expr; ASSERT_SPAN(original_value, original_value->expr_kind == EXPR_CONST); @@ -6170,11 +6158,6 @@ static bool sema_expr_analyse_ct_type_identifier_assign(SemaContext *context, Ex Decl *decl = sema_find_symbol(context, info->unresolved.name); if (!decl) RETURN_SEMA_ERROR(info, "'%s' is not defined in this scope yet.", info->unresolved.name); - if (context->call_env.in_other) - { - RETURN_SEMA_ERROR(left, "Compile time variables may only be modified in the scope they are defined in."); - } - decl->var.init_expr = right; expr->expr_kind = EXPR_NOP; expr->type = type_void; @@ -6267,10 +6250,6 @@ static bool sema_binary_analyse_ct_op_assign(SemaContext *context, Expr *expr, E Decl *left_var = left->ct_ident_expr.decl; if (!sema_cast_ct_ident_rvalue(context, left)) return false; - if (context->call_env.in_other) - { - RETURN_SEMA_ERROR(left, "Compile time variables may only be modified in the scope they are defined in."); - } expr->binary_expr.operator = binaryop_assign_base_op(expr->binary_expr.operator); if (!sema_expr_analyse_binary(context, NULL, expr, NULL)) return false; @@ -6294,10 +6273,6 @@ static bool sema_binary_analyse_ct_op_assign(SemaContext *context, Expr *expr, E static bool sema_binary_analyse_ct_subscript_op_assign(SemaContext *context, Expr *expr, Expr *left) { ASSERT_SPAN(left, left->expr_kind == EXPR_CT_SUBSCRIPT); - if (context->call_env.in_other) - { - RETURN_SEMA_ERROR(left, "Compile time variables may only be modified in the scope they are defined in."); - } Decl *left_var = left->ct_subscript_expr.var; ArrayIndex idx = left->ct_subscript_expr.index; diff --git a/test/test_suite/compile_time/mod_in_other_scope.c3 b/test/test_suite/compile_time/mod_in_other_scope.c3 index a029de6b2..68a8193f6 100644 --- a/test/test_suite/compile_time/mod_in_other_scope.c3 +++ b/test/test_suite/compile_time/mod_in_other_scope.c3 @@ -11,13 +11,15 @@ macro void @bar(#a) } fn int main() { + +/* int $i; var $Type; - @foo($Type = int); // #error: only be modified - @foo($i = 2); // #error: only be modified - @foo($i += 1); // #error: only be modified - @bar($Type = int); // #error: only be modified - @bar($i = 2); // #error: only be modified - @bar($i += 1); // #error: only be modified + @foo($Type = int); // noerror: only be modified + @foo($i = 2); // noerror: only be modified + @foo($i += 1); // noerror: only be modified + @bar($Type = int); // noerror: only be modified + @bar($i = 2); // noerror: only be modified + @bar($i += 1); // noerror: only be modified*/ return 0; } \ No newline at end of file