fix: P1审查修复 — error.c arena化 + Self类型解析 + trait查找加固 + 缓冲区安全

P1-①: error_init/add 从 malloc/realloc/strdup 改为 arena_alloc/arena_strdup
P1-②: impl 方法中 Self 类型在 sema 解析为实际结构体名
P1-③: trait 方法 fallback 增加前缀校验(strncmp),method_name 统一更新
P1-④: codegen args[16] 增加溢出检查,移除 parser 未使用的 mods/uses 数组
新增: 36_self_type.l 集成测试(Self 类型 + trait 方法)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-06 17:09:28 +08:00
parent 17c19fd9b9
commit 466be76fd8
9 changed files with 99 additions and 56 deletions
+11 -11
View File
@@ -1,22 +1,25 @@
#include "error.h"
#include "arena.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
void error_init(ErrorList* list) {
list->capacity = 8;
list->errors = malloc(list->capacity * sizeof(ErrorInfo));
void error_init(ErrorList* list, Arena* a) {
list->capacity = 64;
list->errors = arena_alloc(a, list->capacity * sizeof(ErrorInfo));
list->count = 0;
list->arena = a;
if (!list->errors) list->capacity = 0;
}
void error_add(ErrorList* list, const char* filename, int line, int col, const char* fmt, ...) {
if (!list->errors) return;
if (list->count >= list->capacity) {
size_t new_cap = list->capacity * 2;
ErrorInfo* new_errs = realloc(list->errors, new_cap * sizeof(ErrorInfo));
size_t new_cap = list->capacity + 64;
ErrorInfo* new_errs = arena_alloc(list->arena, new_cap * sizeof(ErrorInfo));
if (!new_errs) return;
memcpy(new_errs, list->errors, list->capacity * sizeof(ErrorInfo));
list->errors = new_errs;
list->capacity = new_cap;
}
@@ -26,12 +29,9 @@ void error_add(ErrorList* list, const char* filename, int line, int col, const c
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (n < 0) return;
char* msg = strdup(buf);
char* fname = strdup(filename);
if (!msg || !fname) {
free(msg); free(fname);
return;
}
char* msg = arena_strdup(list->arena, buf);
char* fname = arena_strdup(list->arena, filename);
if (!msg || !fname) return;
list->errors[list->count++] = (ErrorInfo){
.message = msg,
.filename = fname,