From 863118c95caac0d69a35f6ae4d2e83844734a8a1 Mon Sep 17 00:00:00 2001 From: SAJJA EASWAR Date: Sat, 24 Jan 2026 13:05:12 +0530 Subject: Fix regressions: derive(Eq) for pointers and mixed type comparisons --- src/parser/parser_core.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/parser/parser_core.c') diff --git a/src/parser/parser_core.c b/src/parser/parser_core.c index 7468bfb..ff5dd1c 100644 --- a/src/parser/parser_core.c +++ b/src/parser/parser_core.c @@ -591,22 +591,34 @@ static ASTNode *generate_derive_impls(ParserContext *ctx, ASTNode *strct, char * } char cmp[256]; - ASTNode *fdef = find_struct_def(ctx, ft); - if (fdef && fdef->type == NODE_ENUM) + // Detect pointer using type_info OR string check (fallback) + int is_ptr = 0; + if (f->type_info && f->type_info->kind == TYPE_POINTER) { - // Enum field: compare tags (pointer access via auto-deref) + is_ptr = 1; + } + // Fallback: check if type string ends with '*' + if (!is_ptr && ft && strchr(ft, '*')) + { + is_ptr = 1; + } + + // Only look up struct def for non-pointer types + ASTNode *fdef = is_ptr ? NULL : find_struct_def(ctx, ft); + + if (!is_ptr && fdef && fdef->type == NODE_ENUM) + { + // Enum field: compare tags sprintf(cmp, "self.%s.tag == other.%s.tag", fn, fn); } - else if (fdef && fdef->type == NODE_STRUCT) + else if (!is_ptr && fdef && fdef->type == NODE_STRUCT) { - // Struct field: use _eq function, pass addresses - // self.field is L-value, other.field is L-value (auto-deref from - // pointer) We need addresses of them: &self.field, &other.field + // Struct field: use __eq function sprintf(cmp, "%s__eq(&self.%s, &other.%s)", ft, fn, fn); } else { - // Primitive or unknown: use == (auto-deref) + // Primitive, POINTER, or unknown: use == sprintf(cmp, "self.%s == other.%s", fn, fn); } strcat(body, cmp); -- cgit v1.2.3