diff --git a/releasenotes.md b/releasenotes.md index 95f7e741b..b1948f275 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -45,6 +45,7 @@ - `net::poll()` with negative timeout behaved incorrectly. - Return type inference bugs with macros #1757 - `$defined` in a global scope should accept testing normal macros. +- Assert on add to uninitialized ct variable #1765. ### Stdlib changes - Increase BitWriter.write_bits limit up to 32 bits. diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index a4d4cfa31..b166a9286 100755 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -4608,17 +4608,6 @@ bool sema_analyse_method_register(SemaContext *context, Decl *method) RETURN_SEMA_ERROR(parent_type_info, "Methods can not be associated with '%s'", type_to_error_string(parent_type)); } - - // We need at least one argument (the parent type) - if (!vec_size(method->func_decl.signature.params)) - { - RETURN_SEMA_ERROR(method, "Expected at least one parameter - of type '%s'.", type_to_error_string(parent_type)); - } - - // Check the first argument. - Decl *first_param = method->func_decl.signature.params[0]; - if (!first_param) RETURN_SEMA_ERROR(method, "The first parameter to this method must be of type '%s'.", type_to_error_string(parent_type)); - return unit_add_method(context, parent_type->canonical, method); } diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 1dc26a269..42288c73f 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -5609,6 +5609,7 @@ static bool sema_expr_analyse_ct_identifier_assign(SemaContext *context, Expr *e left->ct_ident_expr.decl->var.init_expr = right; expr_replace(expr, right); + left->ct_ident_expr.decl->type = right->type; return true; } @@ -5696,11 +5697,7 @@ static bool sema_binary_analyse_ct_common_assign(SemaContext *context, Expr *exp if (!sema_analyse_expr_lvalue(context, left)) return false; Decl *left_var = left->ct_ident_expr.decl; - Expr *left_value = left_var->var.init_expr; - ASSERT_SPAN(expr, left_value); - ASSERT_SPAN(expr, !IS_OPTIONAL(left_value)); - - expr->binary_expr.left = exprid(left_value); + if (!sema_cast_ct_ident_rvalue(context, left)) return false; expr->binary_expr.operator = binaryop_assign_base_op(expr->binary_expr.operator); @@ -5711,8 +5708,8 @@ static bool sema_binary_analyse_ct_common_assign(SemaContext *context, Expr *exp RETURN_SEMA_ERROR(exprptr(expr->binary_expr.right), "Expected a constant expression."); } - left->ct_ident_expr.decl->var.init_expr = expr; - + left_var->var.init_expr = expr; + left->type = expr->type; return true; } diff --git a/test/test_suite/compile_time/add_to_ct_undefined.c3 b/test/test_suite/compile_time/add_to_ct_undefined.c3 new file mode 100644 index 000000000..e9fd8f5cc --- /dev/null +++ b/test/test_suite/compile_time/add_to_ct_undefined.c3 @@ -0,0 +1,6 @@ +fn int main() +{ + var $a; + $a += 1; // #error: not yet initialized + return 0; +} \ No newline at end of file diff --git a/test/test_suite/overloading/construct_op_zero_args.c3 b/test/test_suite/overloading/construct_op_zero_args.c3 new file mode 100644 index 000000000..670f5c845 --- /dev/null +++ b/test/test_suite/overloading/construct_op_zero_args.c3 @@ -0,0 +1,5 @@ +struct Foo { int a; } +fn Foo Foo.foo() @operator(construct) +{ + return { 1 }; +}