Update advice when an enum is found with "= 2" and no associated values.

This commit is contained in:
Christoffer Lerno
2026-02-05 13:57:35 +01:00
parent d71aa10f62
commit 054fcd2deb

View File

@@ -1633,6 +1633,18 @@ static inline bool sema_analyse_enum_param(SemaContext *context, Decl *param)
return true;
}
static inline void sema_print_enum_to_cenum_error(SemaContext *context, Decl *decl, Expr *arg)
{
TypeInfo *type_info = decl->enums.type_info;
if (type_info->type == type_int)
{
SEMA_ERROR(arg, "Assigning a value requires the declaration of associated values for the enum. Did you perhaps want C-style enums? In that case use const enums, defined like 'enum %s : const { ... }'", decl->name);
}
else
{
SEMA_ERROR(arg, "Assigning a value requires the declaration of associated values for the enum. Did you perhaps want C-style enums? In that case use const enums, defined like 'enum %s : const %s { ... }'", decl->name, type_to_error_string(type_info->type));
}
}
static inline bool sema_analyse_enum(SemaContext *context, Decl *decl, bool *erase_decl)
{
if (!sema_analyse_attributes(context, decl, decl->attributes, ATTR_ENUM, erase_decl)) return decl_poison(decl);
@@ -1733,7 +1745,8 @@ static inline bool sema_analyse_enum(SemaContext *context, Decl *decl, bool *era
{
if (!associated_value_count)
{
RETURN_SEMA_ERROR(args[0], "There are no associated values defined for this enum. Did you perhaps want C style gap enums? In that case, try enums with inline associated values.");
sema_print_enum_to_cenum_error(context, decl, args[0]);
return false;
}
RETURN_SEMA_ERROR(args[associated_value_count], "You're adding too many values, only %d associated value%s are defined for '%s'.", associated_value_count, associated_value_count != 1 ? "s" : "", decl->name);
}
@@ -1758,7 +1771,8 @@ static inline bool sema_analyse_enum(SemaContext *context, Decl *decl, bool *era
{
if (!associated_value_count)
{
RETURN_SEMA_ERROR(args[0], "There are no associated values defined for this enum. Did you perhaps want C style gap enums? In that case, try enums with inline associated values.");
sema_print_enum_to_cenum_error(context, decl, args[0]);
return false;
}
RETURN_SEMA_ERROR(args[associated_value_count], "You're adding too many values, only %d associated value%s are defined for '%s'.", associated_value_count, associated_value_count != 1 ? "s" : "", decl->name);
}