Lambdas now properly follow its attributes #2346.

This commit is contained in:
Christoffer Lerno
2025-07-28 19:49:40 +02:00
parent 86034353ec
commit 6641155892
5 changed files with 37 additions and 12 deletions

View File

@@ -82,6 +82,7 @@
- `@format` did not work correctly with macros #2341.
- Crash when parsing recursive type declaration #2345.
- Remove unnecessary "ret" in naked functions #2344.
- Lambdas now properly follow its attributes #2346.
### Stdlib changes
- Improve contract for readline. #2280

View File

@@ -4,7 +4,6 @@
#include "sema_internal.h"
static inline bool sema_analyse_func_macro(SemaContext *context, Decl *decl, AttributeDomain domain, bool *erase_decl);
static inline bool sema_analyse_func(SemaContext *context, Decl *decl, bool *erase_decl);
static inline bool sema_analyse_macro(SemaContext *context, Decl *decl, bool *erase_decl);
static inline bool sema_analyse_signature(SemaContext *context, Signature *sig, TypeInfo *method_parent, Decl *decl);
@@ -43,8 +42,6 @@ static const char *attribute_domain_to_string(AttributeDomain domain);
static bool sema_analyse_attribute(SemaContext *context, ResolvedAttrData *attr_data, Decl *decl, Attr *attr, AttributeDomain domain, bool *erase_decl);
static bool sema_analyse_attributes_inner(SemaContext *context, ResolvedAttrData *attr_data, Decl *decl, Attr **attrs, AttributeDomain domain,
Decl *top, bool *erase_decl);
static bool sema_analyse_attributes(SemaContext *context, Decl *decl, Attr **attrs, AttributeDomain domain,
bool *erase_decl);
static bool sema_analyse_attributes_for_var(SemaContext *context, Decl *decl, bool *erase_decl);
static bool sema_check_section(SemaContext *context, Attr *attr);
static inline bool sema_analyse_attribute_decl(SemaContext *context, SemaContext *c, Decl *decl, bool *erase_decl);
@@ -3723,8 +3720,7 @@ static bool sema_analyse_attributes_inner(SemaContext *context, ResolvedAttrData
return true;
}
static bool sema_analyse_attributes(SemaContext *context, Decl *decl, Attr **attrs, AttributeDomain domain,
bool *erase_decl)
bool sema_analyse_attributes(SemaContext *context, Decl *decl, Attr **attrs, AttributeDomain domain, bool *erase_decl)
{
if (decl->resolved_attributes) return true;
ResolvedAttrData data = { .tags = NULL, .overload = INVALID_SPAN };
@@ -4154,13 +4150,6 @@ REGISTER_MAIN:
return true;
}
static inline bool sema_analyse_func_macro(SemaContext *context, Decl *decl, AttributeDomain domain, bool *erase_decl)
{
assert((domain & CALLABLE_TYPE) == domain);
if (!sema_analyse_attributes(context, decl, decl->attributes, domain,
erase_decl)) return decl_poison(decl);
return true;
}
static inline bool sema_analyse_func(SemaContext *context, Decl *decl, bool *erase_decl)
{

View File

@@ -10159,6 +10159,12 @@ static inline bool sema_expr_analyse_lambda(SemaContext *context, Type *target_t
decl->name = scratch_buffer_copy();
decl->extname = decl->name;
decl->type = type_new_func(decl, sig);
bool erase_decl = false;
if (!sema_analyse_func_macro(context, decl, ATTR_FUNC, &erase_decl)) return false;
if (erase_decl)
{
RETURN_SEMA_ERROR(decl, "`@if` can't be placed on a lambda.");
}
if (!sema_analyse_function_signature(context, decl, NULL, sig->abi, sig)) return false;
if (flat && flat->pointer->function.prototype->raw_type != decl->type->function.prototype->raw_type)
{

View File

@@ -131,6 +131,15 @@ INLINE bool sema_set_alloca_alignment(SemaContext *context, Type *type, AlignSiz
INLINE void sema_display_deprecated_warning_on_use(SemaContext *context, Decl *decl, SourceSpan span);
bool sema_expr_analyse_ct_concat(SemaContext *context, Expr *concat_expr, Expr *left, Expr *right, bool *failed_ref);
bool sema_analyse_const_enum_constant_val(SemaContext *context, Decl *decl);
bool sema_analyse_attributes(SemaContext *context, Decl *decl, Attr **attrs, AttributeDomain domain, bool *erase_decl);
INLINE bool sema_analyse_func_macro(SemaContext *context, Decl *decl, AttributeDomain domain, bool *erase_decl)
{
assert((domain & CALLABLE_TYPE) == domain);
if (!sema_analyse_attributes(context, decl, decl->attributes, domain,
erase_decl)) return decl_poison(decl);
return true;
}
INLINE bool sema_check_left_right_const(SemaContext *context, Expr *left, Expr *right)
{

View File

@@ -0,0 +1,20 @@
// #target: macos-x64
module test;
alias IntFn = fn int();
IntFn lambda @export = fn () => 1;
IntFn lambda_naked @export = fn () @naked => 2;
/* #expect: test.ll
; Function Attrs: naked nounwind uwtable
define internal i32 @"test.$global$lambda2"() #0 {
entry:
ret i32 2
}
; Function Attrs: nounwind uwtable
define internal i32 @"test.$global$lambda1"() #1 {
entry:
ret i32 1
}