- Reference macro parameters syntax does not error in certain cases. #2612

This commit is contained in:
Christoffer Lerno
2025-11-30 02:23:00 +01:00
parent 9d79c3f33d
commit a2aa9fae6b
5 changed files with 22 additions and 7 deletions

View File

@@ -48,6 +48,7 @@
- Bug on rethrow in return with defer #2603. - Bug on rethrow in return with defer #2603.
- Fix bug when converting from vector to distinct type of wider vector. #2604 - Fix bug when converting from vector to distinct type of wider vector. #2604
- `$defined(hashmap.init(mem))` causes compiler segfault #2611. - `$defined(hashmap.init(mem))` causes compiler segfault #2611.
- Reference macro parameters syntax does not error in certain cases. #2612
### Stdlib changes ### Stdlib changes
- Add `CGFloat` `CGPoint` `CGSize` `CGRect` types to core_foundation (macOS). - Add `CGFloat` `CGPoint` `CGSize` `CGRect` types to core_foundation (macOS).

View File

@@ -1639,7 +1639,7 @@ CHECK_ELLIPSIS:
} }
if (vec_size(params) > 0) 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; return false;
} }
// This will catch Type... &foo and &foo..., neither is allowed. // This will catch Type... &foo and &foo..., neither is allowed.

View File

@@ -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; 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. // Fill in the type if the first parameter is lacking a type.
if (method_parent && params && params[0] && !params[0]->var.type_info) if (method_parent && params && params[0] && !params[0]->var.type_info)

View File

@@ -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"; 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"; return "abc";
} }

View File

@@ -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);
}