diff --git a/releasenotes.md b/releasenotes.md index 08070681d..d121a6eac 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -48,6 +48,7 @@ - Bug on rethrow in return with defer #2603. - Fix bug when converting from vector to distinct type of wider vector. #2604 - `$defined(hashmap.init(mem))` causes compiler segfault #2611. +- Reference macro parameters syntax does not error in certain cases. #2612 ### Stdlib changes - Add `CGFloat` `CGPoint` `CGSize` `CGRect` types to core_foundation (macOS). diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 26afabb59..0ffa82a90 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -1639,7 +1639,7 @@ CHECK_ELLIPSIS: } if (vec_size(params) > 0) { - PRINT_ERROR_HERE("Only the first parameter may use '&'."); + PRINT_ERROR_HERE("Only the first parameter may be a self parameter using '&'."); return false; } // This will catch Type... &foo and &foo..., neither is allowed. diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index a4787ff5a..53b6f8c8b 100644 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -1204,13 +1204,16 @@ static inline bool sema_analyse_signature(SemaContext *context, Signature *sig, 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 (params && params[0] && params[0]->var.self_addr) { - if (method_parent) + 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], "Self parameters are only allowed on methods."); + } + if (params[0]->var.type_info) + { + RETURN_SEMA_ERROR(type_infoptr(params[0]->var.type_info), "A self 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 index 58a225e7a..ad59ef8d5 100644 --- a/test/test_suite/functions/ref_param_fn.c3 +++ b/test/test_suite/functions/ref_param_fn.c3 @@ -1,9 +1,9 @@ -fn String str(int& element) // #error: Ref parameters are only allowed on methods +fn String str(int& element) // #error: Self parameters are only allowed on methods { return "abc"; } -fn String int.str(int& element) // #error: A ref parameter should always be untyped +fn String int.str(int& element) // #error: A self parameter should always be untyped, please remove the type here { return "abc"; } diff --git a/test/test_suite/methods/self_not_method.c3 b/test/test_suite/methods/self_not_method.c3 new file mode 100644 index 000000000..7266ea00a --- /dev/null +++ b/test/test_suite/methods/self_not_method.c3 @@ -0,0 +1,11 @@ + +macro void m(&x) // #error: Self parameters are only allowed on methods +{ + $echo $typeof(x); +} + +fn void main() +{ + int x; + m(x); +} \ No newline at end of file