Skip to content

Commit 36bce46

Browse files
committed
fix(go-lsp): cross-file multi-value and named results type their variables
The cross-file pass handed every registrar the raw result text of a def (pass_lsp_cross.c: `dst->return_types = src->return_type`). The Go registrar splits that string on '|' (go_lsp.c split_pipe_types), so it got `(*Encoder, error)` as one piece. That piece did not parse, and a cross-file callee with more than one result, or with a named result `(e *Encoder)`, had no usable return type. As a result, `e, err := enc.NewEncoder()`, `h, _ := newHead()` and `_, d := diag.Run()` all left the variable untyped, and calls on it fell to name guesses. Same-file calls were fine, because they read the AST. Go defs now pass the parsed result list (extract_go_multi_return), '|'-joined: `*Encoder|error`, and `*Encoder` for the named result. Other languages keep the single raw string. The persisted LSP surface carries this string, so SURFACE_CODEC_VERSION goes from 2 to 3: existing caches re-extract Go surfaces once. The literal version-2 surfaces in the codec tests move to 3, so that they keep testing the envelope rule they name rather than the version mismatch. Tests: pipeline_go_multi_return_receiver_is_typed (+_parallel). A tuple result, a named result, a tuple-bound local reached through a field, and a named slice type each must bind the right method with an lsp_* strategy and none of its two same-named decoys. Signed-off-by: Martin Vogel <[email protected]>
1 parent 658639e commit 36bce46

3 files changed

Lines changed: 189 additions & 17 deletions

File tree

‎src/pipeline/lsp_surface.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include "pipeline/worker_pool.h"
3434
#include "yyjson/yyjson.h"
3535

36-
enum { SURFACE_CODEC_VERSION = 2 };
36+
/* 3: Go defs carry their parsed result list `|`-joined in "ret" (F-G8 D1);
37+
* a version-2 surface holds the raw result text and must be re-extracted. */
38+
enum { SURFACE_CODEC_VERSION = 3 };
3739

3840
/* Labels the incremental name registry serves that pxc_map_label does NOT
3941
* carry into the CBMLSPDef set. Their (name, qn, label) triple must still

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,17 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
513513
src->decl_namespace ? cbm_arena_strdup(arena, src->decl_namespace) : NULL;
514514
}
515515
dst->is_interface = (strcmp(label, "Interface") == 0 || strcmp(label, "Protocol") == 0);
516-
/* Single return-type string. The per-language registrars split on '|'
517-
* for multi-return languages (Go); single-return languages just see one
518-
* piece, which is what's already stored. */
519-
dst->return_types = src->return_type ? cbm_arena_strdup(arena, src->return_type) : NULL;
516+
/* The return-type string. The Go registrar splits it on '|' (go_lsp.c
517+
* split_pipe_types), so Go gets the PARSED result list, joined: `(*T,
518+
* error)` arrives as `*T|error` and a named result `(t *T)` as `*T`. The
519+
* raw result text it used to get did not parse, so every variable bound
520+
* by a cross-file multi-value or named-result call (`t, err := pkg.New()`)
521+
* stayed untyped (F-G8 D1). Other languages keep the single raw string. */
522+
if (lang == CBM_LANG_GO && src->return_types && src->return_types[0]) {
523+
dst->return_types = pxc_join_pipe(arena, src->return_types);
524+
} else {
525+
dst->return_types = src->return_type ? cbm_arena_strdup(arena, src->return_type) : NULL;
526+
}
520527
/* Languages whose cross registrars read embedded_types as QNs get their
521528
* bases resolved against the project registry; everyone else keeps the
522529
* raw source spelling their own registrar already knows how to handle. */

‎tests/test_pipeline.c‎

Lines changed: 175 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8819,6 +8819,167 @@ static void write_go_c_ref_guard_fixture(const char *tmp, int pad_files) {
88198819
}
88208820
}
88218821

8822+
/* F-G8 D1: a variable bound by a CROSS-file multi-value or named-result call
8823+
* is typed. The cross-file registrar received the raw result text
8824+
* `(*Encoder, error)`, which did not parse, so `e, err := enc.NewEncoder()`
8825+
* left e untyped and e.EncodeTuple() fell to a name guess. Every target
8826+
* method has two same-named decoys, so a guess cannot pass. pad_files > 0
8827+
* forces the parallel resolver. */
8828+
static void write_go_multi_return_fixture(const char *tmp, int pad_files) {
8829+
write_temp_file(tmp, "go.mod", "module example.com/fx\n\ngo 1.22\n");
8830+
write_temp_file(tmp, "enc/enc.go",
8831+
"package enc\n"
8832+
"\n"
8833+
"type Encoder struct{ n int }\n"
8834+
"\n"
8835+
"func NewEncoder() (*Encoder, error) { return &Encoder{}, nil }\n"
8836+
"\n"
8837+
"func NewNamed() (e *Encoder) { e = &Encoder{}; return }\n"
8838+
"\n"
8839+
"func (e *Encoder) EncodeTuple() int { return e.n }\n"
8840+
"\n"
8841+
"func (e *Encoder) EncodeNamed() int { return e.n }\n");
8842+
write_temp_file(tmp, "diag/diag.go",
8843+
"package diag\n"
8844+
"\n"
8845+
"type Diagnostic struct{ msg string }\n"
8846+
"\n"
8847+
"type Diagnostics []Diagnostic\n"
8848+
"\n"
8849+
"func (d Diagnostics) HasErrorsSlice() bool { return len(d) > 0 }\n"
8850+
"\n"
8851+
"func Run() (int, Diagnostics) { return 0, nil }\n");
8852+
write_temp_file(tmp, "app/types.go",
8853+
"package app\n"
8854+
"\n"
8855+
"type isolation struct{}\n"
8856+
"\n"
8857+
"func (i *isolation) LowWatermarkField() int { return 0 }\n"
8858+
"\n"
8859+
"type Head struct{ iso *isolation }\n"
8860+
"\n"
8861+
"func newHead() (*Head, error) { return &Head{iso: &isolation{}}, nil }\n");
8862+
write_temp_file(tmp, "app/app.go",
8863+
"package app\n"
8864+
"\n"
8865+
"import (\n"
8866+
"\t\"example.com/fx/diag\"\n"
8867+
"\t\"example.com/fx/enc\"\n"
8868+
")\n"
8869+
"\n"
8870+
"func UseTuple() int {\n"
8871+
"\te, err := enc.NewEncoder()\n"
8872+
"\t_ = err\n"
8873+
"\treturn e.EncodeTuple()\n"
8874+
"}\n"
8875+
"\n"
8876+
"func UseNamed() int {\n"
8877+
"\te := enc.NewNamed()\n"
8878+
"\treturn e.EncodeNamed()\n"
8879+
"}\n"
8880+
"\n"
8881+
"func UseField() int {\n"
8882+
"\th, _ := newHead()\n"
8883+
"\treturn h.iso.LowWatermarkField()\n"
8884+
"}\n"
8885+
"\n"
8886+
"func UseSlice() bool {\n"
8887+
"\t_, d := diag.Run()\n"
8888+
"\treturn d.HasErrorsSlice()\n"
8889+
"}\n");
8890+
write_temp_file(tmp, "decoy/decoy.go",
8891+
"package decoy\n"
8892+
"\n"
8893+
"type D1 struct{}\n"
8894+
"\n"
8895+
"type D2 struct{}\n"
8896+
"\n"
8897+
"func (D1) EncodeTuple() int { return 0 }\n"
8898+
"func (D2) EncodeTuple() int { return 0 }\n"
8899+
"func (D1) EncodeNamed() int { return 0 }\n"
8900+
"func (D2) EncodeNamed() int { return 0 }\n"
8901+
"func (D1) LowWatermarkField() int { return 0 }\n"
8902+
"func (D2) LowWatermarkField() int { return 0 }\n"
8903+
"func (D1) HasErrorsSlice() bool { return false }\n"
8904+
"func (D2) HasErrorsSlice() bool { return false }\n");
8905+
for (int i = 0; i < pad_files; i++) {
8906+
char name[64];
8907+
char body[128];
8908+
snprintf(name, sizeof(name), "filler/filler%d.go", i);
8909+
snprintf(body, sizeof(body), "package filler\n\nfunc Filler%d() int { return %d }\n", i, i);
8910+
write_temp_file(tmp, name, body);
8911+
}
8912+
}
8913+
8914+
static int go_multi_return_case(int pad_files) {
8915+
char tmp[256];
8916+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_multiret_XXXXXX");
8917+
if (!cbm_mkdtemp(tmp)) {
8918+
FAIL("tmpdir");
8919+
}
8920+
write_go_multi_return_fixture(tmp, pad_files);
8921+
char *saved = NULL;
8922+
if (pad_files > 0) {
8923+
char *old_workers = getenv("CBM_WORKERS");
8924+
saved = old_workers ? strdup(old_workers) : NULL;
8925+
cbm_setenv("CBM_WORKERS", "4", 1);
8926+
}
8927+
char db_path[512];
8928+
snprintf(db_path, sizeof(db_path), "%s/go_multiret.db", tmp);
8929+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8930+
int rc = p ? cbm_pipeline_run(p) : -1;
8931+
if (pad_files > 0) {
8932+
if (saved) {
8933+
cbm_setenv("CBM_WORKERS", saved, 1);
8934+
free(saved);
8935+
} else {
8936+
cbm_unsetenv("CBM_WORKERS");
8937+
}
8938+
}
8939+
ASSERT_NOT_NULL(p);
8940+
ASSERT_EQ(rc, 0);
8941+
const char *project = cbm_pipeline_project_name(p);
8942+
cbm_store_t *s = cbm_store_open_path(db_path);
8943+
ASSERT_NOT_NULL(s);
8944+
8945+
static const struct {
8946+
const char *caller;
8947+
const char *method;
8948+
const char *file;
8949+
} want[] = {
8950+
{"UseTuple", "EncodeTuple", "enc/enc.go"}, /* e, err := enc.NewEncoder() */
8951+
{"UseNamed", "EncodeNamed", "enc/enc.go"}, /* named result (e *Encoder) */
8952+
{"UseField", "LowWatermarkField", "app/types.go"}, /* h, _ := newHead(); h.iso.X() */
8953+
{"UseSlice", "HasErrorsSlice", "diag/diag.go"}, /* _, d := diag.Run() */
8954+
};
8955+
for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) {
8956+
int right = named_edge_to_file_count(s, project, "CALLS", want[i].caller, want[i].method,
8957+
want[i].file);
8958+
int decoy = named_edge_to_file_count(s, project, "CALLS", want[i].caller, want[i].method,
8959+
"decoy/decoy.go");
8960+
bool typed = cross_file_call_has_strategy(s, project, want[i].caller, want[i].method,
8961+
"\"strategy\":\"lsp_");
8962+
if (right < 1 || decoy != 0 || !typed) {
8963+
printf(" %s -> %s: right=%d decoy=%d lsp=%d\n", want[i].caller, want[i].method, right,
8964+
decoy, typed);
8965+
FAIL("multi-value / named-result receiver not typed");
8966+
}
8967+
}
8968+
8969+
cbm_store_close(s);
8970+
cbm_pipeline_free(p);
8971+
th_rmtree(tmp);
8972+
PASS();
8973+
}
8974+
8975+
TEST(pipeline_go_multi_return_receiver_is_typed) {
8976+
return go_multi_return_case(0);
8977+
}
8978+
8979+
TEST(pipeline_go_multi_return_receiver_is_typed_parallel) {
8980+
return go_multi_return_case(52);
8981+
}
8982+
88228983
TEST(pipeline_go_rw_usage_never_cross_into_c) {
88238984
/* #1928: USAGE and WRITES/READS resolve through the same short-name
88248985
* registry as CALLS but never consulted the #725 cross-language guard.
@@ -21988,7 +22149,7 @@ TEST(pipeline_python_namespace_change_admission) {
2198822149
}
2198922150
int body = -9, signature = -9, alias = -9, order = -9;
2199022151
int empty = -9, literal = -9, unknown = -9, deletion = -9, transition = -9;
21991-
const char *non_python = "{\"v\":2,\"lang\":0,\"lsp\":[],\"reg\":[],\"py\":null}";
22152+
const char *non_python = "{\"v\":3,\"lang\":0,\"lsp\":[],\"reg\":[],\"py\":null}";
2199222153
if (complete) {
2199322154
body = cbm_lsp_surface_python_change(json[0], json[1]);
2199422155
signature = cbm_lsp_surface_python_change(json[0], json[2]);
@@ -22115,22 +22276,22 @@ TEST(pipeline_python_namespace_codec_rejects_non_authoritative_rows) {
2211522276
* Even an empty LSP array must validate its namespace envelope. */
2211622277
const char *bad[] = {
2211722278
"{\"v\":1,\"lsp\":[]}",
22118-
"{\"v\":2,\"lang\":1,\"lsp\":[]}",
22119-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"py\":null}",
22120-
"{\"v\":2,\"lang\":1.0,\"lsp\":[],\"py\":null}",
22121-
"{\"v\":2,\"v\":2,\"lang\":0,\"lsp\":[],\"py\":null}",
22122-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
22279+
"{\"v\":3,\"lang\":1,\"lsp\":[]}",
22280+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"py\":null}",
22281+
"{\"v\":3,\"lang\":1.0,\"lsp\":[],\"py\":null}",
22282+
"{\"v\":3,\"v\":3,\"lang\":0,\"lsp\":[],\"py\":null}",
22283+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
2212322284
"\"module\":\"p.m\",\"path\":\"m.py\",\"status\":3,\"failure\":2,\"events\":[]}}",
22124-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"py\":{\"v\":99,\"lang\":1,"
22285+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"py\":{\"v\":99,\"lang\":1,"
2212522286
"\"module\":\"p.m\",\"path\":\"m.py\",\"status\":2,\"failure\":0,\"events\":[]}}",
22126-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
22287+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
2212722288
"\"module\":\"p.m\",\"path\":\"m.py\\u0000suffix\",\"status\":2,"
2212822289
"\"failure\":0,\"events\":[]}}",
22129-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
22290+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
2213022291
"\"module\":\"p.m\",\"path\":\"m.py\",\"status\":2,\"failure\":0,\"events\":["
2213122292
"{\"k\":999,\"d\":0,\"s\":0,\"r\":0,\"f\":0,\"l\":0,"
2213222293
"\"n\":null,\"m\":null,\"i\":null,\"a\":[]}]}}",
22133-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
22294+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"py\":{\"v\":1,\"lang\":1,"
2213422295
"\"module\":\"p.m\",\"path\":\"m.py\",\"status\":2,\"failure\":0,\"events\":["
2213522296
"{\"k\":8,\"d\":0,\"s\":1,\"r\":0,\"f\":0,\"l\":4294967296,"
2213622297
"\"n\":null,\"m\":null,\"i\":null,\"a\":[]}]}}",
@@ -22150,7 +22311,7 @@ TEST(pipeline_python_namespace_codec_rejects_non_authoritative_rows) {
2215022311
cbm_arena_destroy(&arena);
2215122312
}
2215222313
const char *unknown =
22153-
"{\"v\":2,\"lang\":1,\"lsp\":[],\"reg\":[],\"py\":{\"v\":1,\"lang\":1,"
22314+
"{\"v\":3,\"lang\":1,\"lsp\":[],\"reg\":[],\"py\":{\"v\":1,\"lang\":1,"
2215422315
"\"module\":\"p.m\",\"path\":\"m.py\",\"status\":2,\"failure\":0,\"events\":["
2215522316
"{\"k\":7,\"d\":0,\"s\":0,\"r\":7,\"f\":0,\"l\":0,"
2215622317
"\"n\":null,\"m\":null,\"i\":null,\"a\":[]}]}}";
@@ -22177,7 +22338,7 @@ TEST(pipeline_lsp_surface_large_object_array_decode) {
2217722338
const size_t capacity = (size_t)count * 192 + 64;
2217822339
char *json = malloc(capacity);
2217922340
ASSERT_NOT_NULL(json);
22180-
size_t used = (size_t)snprintf(json, capacity, "{\"v\":2,\"lang\":0,\"py\":null,\"lsp\":[");
22341+
size_t used = (size_t)snprintf(json, capacity, "{\"v\":3,\"lang\":0,\"py\":null,\"lsp\":[");
2218122342
for (int i = 0; i < count; i++) {
2218222343
int n = snprintf(json + used, capacity - used,
2218322344
"%s{\"qn\":\"pkg.f%d\",\"sn\":\"f%d\",\"lb\":\"Function\","
@@ -24830,6 +24991,8 @@ SUITE(pipeline) {
2483024991
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
2483124992
RUN_TEST(pipeline_python_receiver_keeps_specific_unique_name_member_call);
2483224993
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);
24994+
RUN_TEST(pipeline_go_multi_return_receiver_is_typed);
24995+
RUN_TEST(pipeline_go_multi_return_receiver_is_typed_parallel);
2483324996
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
2483424997
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
2483524998
RUN_TEST(pipeline_rust_call_never_binds_another_language);

0 commit comments

Comments
 (0)