From 65fb977e8934e9ecc8ce789120ec9573b5dd9332 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 4 Apr 2025 13:21:53 +0200 Subject: [PATCH] Clearer errors when using a &ref parameter with type. --- src/compiler/parse_global.c | 5 ----- src/compiler/sema_decls.c | 9 +++++++++ test/test_suite/functions/ref_param_fn.c3 | 9 +++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 test/test_suite/functions/ref_param_fn.c3 diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 84e876e95..833b021e3 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -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); diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index f8c102605..432bb6090 100755 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -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) { diff --git a/test/test_suite/functions/ref_param_fn.c3 b/test/test_suite/functions/ref_param_fn.c3 new file mode 100644 index 000000000..42abd554e --- /dev/null +++ b/test/test_suite/functions/ref_param_fn.c3 @@ -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"; +}