Clearer errors when using a &ref parameter with type.

This commit is contained in:
Christoffer Lerno
2025-04-04 13:21:53 +02:00
parent e3f3b6f5f1
commit 65fb977e89
3 changed files with 18 additions and 5 deletions

View File

@@ -1443,11 +1443,6 @@ bool parse_parameters(ParseContext *c, Decl ***params_ref, Variadic *variadic, i
PRINT_ERROR_LAST("'&foo' parameters may not be followed by '...");
return false;
}
if (type)
{
PRINT_ERROR_LAST("'&foo' should be untyped.");
return false;
}
// Span includes the "&"
span = extend_span_with_token(span, c->span);

View File

@@ -1145,6 +1145,15 @@ static inline bool sema_analyse_signature(SemaContext *context, Signature *sig,
if (!sema_resolve_type_info(context, method_parent,
is_macro ? RESOLVE_TYPE_MACRO_METHOD : RESOLVE_TYPE_FUNC_METHOD)) return false;
}
if (params && params[0] && params[0]->var.self_addr && params[0]->var.type_info)
{
if (method_parent)
{
RETURN_SEMA_ERROR(type_infoptr(params[0]->var.type_info), "A ref parameter should always be untyped, please remove the type here.");
}
RETURN_SEMA_ERROR(params[0], "Ref parameters are only allowed on methods.");
}
// Fill in the type if the first parameter is lacking a type.
if (method_parent && params && params[0] && !params[0]->var.type_info)
{

View File

@@ -0,0 +1,9 @@
fn String str(int& element) // #error: A ref parameter should always be untyped
{
return "abc";
}
fn String int.str(int& element) // #error: Ref parameters are only allowed on methods
{
return "abc";
}