#foo style arguments were not type checked when given a type. #1790

This commit is contained in:
Christoffer Lerno
2025-01-09 01:28:30 +01:00
parent 8fb3ec73ff
commit cdc1656f3a
6 changed files with 31 additions and 14 deletions

View File

@@ -550,19 +550,21 @@ fn void temp_pop(TempState old_state)
allocator::thread_temp_allocator = old_state.old;
}
macro void @pool(TempAllocator* #other_temp = null; @body) @builtin
<*
@require @is_empty_macro_slot(#other_temp) ||| $assignable(#other_temp, Allocator) "Must be an allocator"
*>
macro void @pool(#other_temp = EMPTY_MACRO_SLOT; @body) @builtin
{
TempAllocator* current = allocator::temp();
var $has_arg = !$is_const(#other_temp);
$if $has_arg:
$if @is_valid_macro_slot(#other_temp):
TempAllocator* original = current;
if (current == (void*)#other_temp) current = allocator::temp_allocator_next();
if (current == #other_temp.ptr) current = allocator::temp_allocator_next();
$endif
usz mark = current.used;
defer
{
current.reset(mark);
$if $has_arg:
$if @is_valid_macro_slot(#other_temp):
allocator::thread_temp_allocator = original;
$endif;
}

View File

@@ -64,6 +64,7 @@
- Macros with trailing bodys aren't allowed as the single statement after a while loop with no body #1772.
- Deref subscripts as needed for macro ref method arguments. #1789
- Change ordering to simplify adding methods to type in conditional modules.
- `#foo` style arguments were not type checked when given a type. #1790
### Stdlib changes
- Increase BitWriter.write_bits limit up to 32 bits.

View File

@@ -357,7 +357,6 @@ INLINE void llvm_emit_statement_chain(GenContext *c, AstId current)
{
while (current)
{
llvm_emit_stmt(c, ast_next(&current));
}
}

View File

@@ -4132,19 +4132,16 @@ bool sema_analyse_var_decl(SemaContext *context, Decl *decl, bool local)
decl->type = type_add_optional(init->type, IS_OPTIONAL(decl));
}
Expr *init_expr = decl->var.init_expr;
// 2. Check const-ness
if (global_level_var && !expr_is_runtime_const(init_expr))
if (global_level_var && !expr_is_runtime_const(init))
{
SEMA_ERROR(init_expr, "The expression must be a constant value.");
expr_is_runtime_const(init_expr);
SEMA_ERROR(init, "The expression must be a constant value.");
return decl_poison(decl);
}
if (global_level_var || !type_is_abi_aggregate(init_expr->type)) sema_cast_const(init_expr);
if (expr_is_const(init_expr))
if (global_level_var || !type_is_abi_aggregate(init->type)) sema_cast_const(init);
if (expr_is_const(init))
{
init_expr->const_expr.is_hex = false;
init->const_expr.is_hex = false;
}
}
EXIT_OK:;

View File

@@ -1390,6 +1390,14 @@ static bool sema_analyse_parameter(SemaContext *context, Expr *arg, Decl *param,
}
break;
case VARDECL_PARAM_EXPR:
if (param->type)
{
if (!sema_analyse_expr_rhs(context, param->type, arg, true, NULL, false))
{
SEMA_NOTE(definition, "The definition is here.");
return false;
}
}
// #foo
if (context->is_temp)
{
@@ -2137,6 +2145,8 @@ bool sema_expr_analyse_macro_call(SemaContext *context, Expr *call_expr, Expr *s
}
}
param->var.init_expr = args[i];
// Ref arguments doesn't affect optional arg.
if (param->var.kind == VARDECL_PARAM_EXPR) continue;
has_optional_arg = has_optional_arg || IS_OPTIONAL(args[i]);
}
@@ -5751,6 +5761,7 @@ static bool sema_binary_analyse_ct_common_assign(SemaContext *context, Expr *exp
expr->binary_expr.operator = binaryop_assign_base_op(expr->binary_expr.operator);
if (!sema_expr_analyse_binary(context, expr, NULL)) return false;
expr->resolve_status = RESOLVE_DONE;
if (!sema_cast_const(expr))
{

View File

@@ -0,0 +1,7 @@
macro void @foo(int #a)
{
}
fn void main()
{
@foo(1.0); // #error: cannot implicitly be converted
}