mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-11-24 18:32:19 +08:00
feat(lsp): bind Go closure parameters wherever the literal stands
The Go walker recursed into a func literal's body as plain children of the
enclosing scope; its declared parameters were bound only when the literal
was evaluated as an expression (an argument, an assignment). A literal in
a composite literal's field value, a test table's
`run: func(reg *httpmock.Registry) { reg.Register(...) }`, left `reg`
untyped and every call through it unresolved. The walker now walks every
func literal it reaches with its parameters bound, through the same helper
the evaluator uses.
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
+47
-36
@@ -346,6 +346,45 @@ static void go_unify_type(const CBMType *param_type, const CBMType *arg_type,
|
||||
|
||||
// --- go_eval_expr_type: recursive expression type evaluator ---
|
||||
|
||||
/* Walk a closure's body in a child scope (inheriting every outer binding)
|
||||
* with its declared parameters bound: `func(reg *httpmock.Registry) {
|
||||
* reg.Register(...) }` types `reg` wherever the literal stands (an
|
||||
* argument, an assignment, a composite literal's field value). */
|
||||
static void go_walk_func_literal(GoLSPContext *ctx, TSNode node) {
|
||||
TSNode body = ts_node_child_by_field_name(node, "body", 4);
|
||||
if (ts_node_is_null(body)) {
|
||||
return;
|
||||
}
|
||||
CBMScope *saved = ctx->current_scope;
|
||||
ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);
|
||||
TSNode params = ts_node_child_by_field_name(node, "parameters", 10);
|
||||
if (!ts_node_is_null(params)) {
|
||||
uint32_t nc = ts_node_child_count(params);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode param = ts_node_child(params, i);
|
||||
if (ts_node_is_null(param) || !ts_node_is_named(param) ||
|
||||
strcmp(ts_node_type(param), "parameter_declaration") != 0) {
|
||||
continue;
|
||||
}
|
||||
TSNode type_node = ts_node_child_by_field_name(param, "type", 4);
|
||||
const CBMType *pt = go_parse_type_node(ctx, type_node);
|
||||
uint32_t pnc = ts_node_child_count(param);
|
||||
for (uint32_t j = 0; j < pnc; j++) {
|
||||
TSNode ch = ts_node_child(param, j);
|
||||
if (!ts_node_is_null(ch) && ts_node_is_named(ch) &&
|
||||
strcmp(ts_node_type(ch), "identifier") == 0) {
|
||||
char *pname = lsp_node_text(ctx, ch);
|
||||
if (pname && strcmp(pname, "_") != 0) {
|
||||
cbm_scope_bind(ctx->current_scope, pname, pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
resolve_calls_in_node(ctx, body);
|
||||
ctx->current_scope = saved;
|
||||
}
|
||||
|
||||
const CBMType *go_eval_expr_type(GoLSPContext *ctx, TSNode node) {
|
||||
if (ts_node_is_null(node))
|
||||
return cbm_type_unknown();
|
||||
@@ -805,42 +844,7 @@ const CBMType *go_eval_expr_type(GoLSPContext *ctx, TSNode node) {
|
||||
// --- Func literal (closure) ---
|
||||
if (strcmp(kind, "func_literal") == 0) {
|
||||
// Process the closure body to resolve calls with captured scope
|
||||
TSNode body = ts_node_child_by_field_name(node, "body", 4);
|
||||
if (!ts_node_is_null(body)) {
|
||||
// Push child scope (inherits all outer bindings via parent chain)
|
||||
CBMScope *saved = ctx->current_scope;
|
||||
ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);
|
||||
|
||||
// Bind closure parameters
|
||||
TSNode params = ts_node_child_by_field_name(node, "parameters", 10);
|
||||
if (!ts_node_is_null(params)) {
|
||||
uint32_t nc = ts_node_child_count(params);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode param = ts_node_child(params, i);
|
||||
if (ts_node_is_null(param) || !ts_node_is_named(param))
|
||||
continue;
|
||||
if (strcmp(ts_node_type(param), "parameter_declaration") != 0)
|
||||
continue;
|
||||
TSNode type_node = ts_node_child_by_field_name(param, "type", 4);
|
||||
const CBMType *pt = go_parse_type_node(ctx, type_node);
|
||||
uint32_t pnc = ts_node_child_count(param);
|
||||
for (uint32_t j = 0; j < pnc; j++) {
|
||||
TSNode ch = ts_node_child(param, j);
|
||||
if (!ts_node_is_null(ch) && ts_node_is_named(ch) &&
|
||||
strcmp(ts_node_type(ch), "identifier") == 0) {
|
||||
char *pname = lsp_node_text(ctx, ch);
|
||||
if (pname && strcmp(pname, "_") != 0)
|
||||
cbm_scope_bind(ctx->current_scope, pname, pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Walk closure body to resolve calls
|
||||
resolve_calls_in_node(ctx, body);
|
||||
|
||||
ctx->current_scope = saved;
|
||||
}
|
||||
go_walk_func_literal(ctx, node);
|
||||
|
||||
// Build full FUNC type with param/return types from AST
|
||||
const CBMType *pt_arr[16];
|
||||
@@ -1529,6 +1533,13 @@ static void resolve_calls_in_node_inner(GoLSPContext *ctx, TSNode node) {
|
||||
return;
|
||||
const char *kind = ts_node_type(node);
|
||||
|
||||
/* A closure the walk reaches is walked with its parameters bound, not
|
||||
* as plain children in the enclosing scope. */
|
||||
if (strcmp(kind, "func_literal") == 0) {
|
||||
go_walk_func_literal(ctx, node);
|
||||
return;
|
||||
}
|
||||
|
||||
// Process statements to build scope
|
||||
go_process_statement(ctx, node);
|
||||
|
||||
|
||||
@@ -987,6 +987,35 @@ TEST(type_scope_go_shared_type_qn_keeps_no_fields_shared) {
|
||||
return go_shared_qn_case(true);
|
||||
}
|
||||
|
||||
/* ── Go: closure parameters ───────────────────────────────────────── */
|
||||
|
||||
/* A closure parameter is typed wherever the literal stands, here as a
|
||||
* composite literal's field value (a test table), not only where the literal
|
||||
* is evaluated. decoy/ declares the same Store with the same method. */
|
||||
static const ts_file_t go_closure_files[] = {
|
||||
{"go.mod", "module example.com/demo\n\ngo 1.21\n"},
|
||||
{"store/store.go", "package store\n\ntype Store struct{ n int }\n\n"
|
||||
"func (s *Store) Get(k string) string { return k }\n"},
|
||||
{"decoy/store.go", "package decoy\n\ntype Store struct{ n int }\n\n"
|
||||
"func (s *Store) Get(k string) string { return k }\n"},
|
||||
{"app/app.go", "package app\n\nimport \"example.com/demo/store\"\n\n"
|
||||
"type stub struct {\n\trun func(s *store.Store) string\n}\n\n"
|
||||
"func Closure() string {\n"
|
||||
"\ttt := []stub{{run: func(s *store.Store) string { return s.Get(\"x\") }}}\n"
|
||||
"\treturn tt[0].run(nil)\n}\n"},
|
||||
{NULL, NULL}};
|
||||
|
||||
static int go_closure_check(cbm_store_t *s, const char *project, const char *label) {
|
||||
int closure = ts_calls_to(s, project, "Closure", "store.Store.Get", TS_LSP);
|
||||
int decoy = ts_calls_to(s, project, "Closure", "decoy.Store.Get", NULL);
|
||||
printf(" go closure (%s): closure=%d decoy=%d\n", label, closure, decoy);
|
||||
return !(closure == 1 && decoy == 0);
|
||||
}
|
||||
|
||||
TEST(type_scope_go_closure_parameters_in_composite_literals) {
|
||||
return ts_twins("goclos", go_closure_files, CBM_LANG_GO, go_closure_check);
|
||||
}
|
||||
|
||||
/* ── Full index versus every incremental route ───────────────────── */
|
||||
|
||||
static void ts_listing_append(sqlite3 *db, const char *sql, const char *project, char **buf,
|
||||
@@ -1353,6 +1382,7 @@ SUITE(type_scope) {
|
||||
RUN_TEST(type_scope_go_fields_survive_an_earlier_receiver_shared);
|
||||
RUN_TEST(type_scope_go_shared_type_qn_keeps_no_fields_per_file);
|
||||
RUN_TEST(type_scope_go_shared_type_qn_keeps_no_fields_shared);
|
||||
RUN_TEST(type_scope_go_closure_parameters_in_composite_literals);
|
||||
RUN_TEST(type_scope_incremental_caller_edit_per_file_go);
|
||||
RUN_TEST(type_scope_incremental_body_edit_stays_one_file);
|
||||
RUN_TEST(type_scope_incremental_named_type_field_change_matches_full);
|
||||
|
||||
Reference in New Issue
Block a user