Skip to content

Commit bec0154

Browse files
committed
fix(go-lsp): split the raw Go result list, keep a single result raw
The first D1 change fed the Go cross-file registrar the extractor's return_types. Those are cleaned names for type-reference edges: clean_type_name strips `[]` and `*`. A cross-file single result `[]T` therefore reached the registrar as `T`. `xs := pkg.Generate(); xs[0].M()` lost its typing and fell back to a name guess. On prometheus the base binds `sample[0].T()` to chunks.Sample.T, but the first D1 change bound it to tsdb.T. pass_lsp_cross.c now leaves a single result's raw text alone, as before D1; the registrar already parses `*T` and `[]T`. Only a result LIST `(...)` is split, at its top-level commas, into raw element types: - `(*T, error)` -> `*T|error`; - the named `(t *T, err error)` -> `*T|error`; - the grouped `(a, b int)` -> `int|int`. `chan`, `func`, `map`, `struct` and `interface` never count as result names. A list that does not split cleanly keeps its raw text. Tests: the D1 fixture gains three call sites, each with two same-named decoys: - a single slice result indexed (`xs[0].TS()`); - a tuple with a slice element; - grouped named results. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9129c48 commit bec0154

2 files changed

Lines changed: 214 additions & 12 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,163 @@ static const char *pxc_join_pipe(CBMArena *arena, const char *const *items) {
166166
return buf;
167167
}
168168

169+
/* ── Go result lists for the cross-file registrar ─────────────────
170+
*
171+
* The Go registrar reads a def's return_types as '|'-separated type texts and
172+
* parses each piece on its own (go_lsp.c split_pipe_types ->
173+
* cbm_parse_return_type_text: `*T`, `[]T`, builtins, named types). A single
174+
* result already is one such text. A result LIST arrives as its raw source,
175+
* `(*T, error)` or the named `(t *T, err error)`, which parses as nothing, so
176+
* pxc_go_result_list splits it into its element types. The extractor's
177+
* return_types are no substitute: clean_type_name strips `[]` and `*` for
178+
* type-reference edges, and `[]T` read as `T` mistypes `xs[0].M()`. */
179+
180+
enum { PXC_GO_MAX_RESULTS = 16 };
181+
182+
typedef struct {
183+
const char *text;
184+
size_t len;
185+
} pxc_span_t;
186+
187+
static bool pxc_is_space(char c) {
188+
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
189+
}
190+
191+
static bool pxc_is_ident_char(char c) {
192+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
193+
}
194+
195+
static int pxc_go_depth(char c, int depth) {
196+
if (c == '(' || c == '[' || c == '{') {
197+
return depth + 1;
198+
}
199+
if ((c == ')' || c == ']' || c == '}') && depth > 0) {
200+
return depth - 1;
201+
}
202+
return depth;
203+
}
204+
205+
static pxc_span_t pxc_trim(const char *s, size_t n) {
206+
while (n > 0 && pxc_is_space(*s)) {
207+
s++;
208+
n--;
209+
}
210+
while (n > 0 && pxc_is_space(s[n - 1])) {
211+
n--;
212+
}
213+
return (pxc_span_t){s, n};
214+
}
215+
216+
/* Split `(a, b)` at its top-level commas into trimmed elements. A trailing
217+
* comma (a multi-line list) leaves no element. Returns the element count, or
218+
* -1 for text that is no result list or holds an empty element. */
219+
static int pxc_go_split_results(const char *raw, pxc_span_t *out) {
220+
size_t n = strlen(raw);
221+
if (n < 2 || raw[0] != '(' || raw[n - 1] != ')') {
222+
return -1;
223+
}
224+
const char *s = raw + 1;
225+
size_t len = n - 2;
226+
int count = 0;
227+
int depth = 0;
228+
size_t start = 0;
229+
for (size_t i = 0; i <= len; i++) {
230+
bool end = i == len;
231+
if (!end && (s[i] != ',' || depth != 0)) {
232+
depth = pxc_go_depth(s[i], depth);
233+
continue;
234+
}
235+
pxc_span_t e = pxc_trim(s + start, i - start);
236+
if (e.len == 0 && !(end && count > 0)) {
237+
return -1;
238+
}
239+
if (e.len > 0) {
240+
if (count == PXC_GO_MAX_RESULTS) {
241+
return -1;
242+
}
243+
out[count++] = e;
244+
}
245+
start = i + 1;
246+
}
247+
return count;
248+
}
249+
250+
/* Length of the result NAME that starts an element (`err` in `err error`),
251+
* 0 when the element is a bare type. A type keyword is never a name: `chan T`
252+
* and `func() T` are types. *lone is set when the whole element is one
253+
* identifier: a type in `(T, error)`, a grouped name in `(a, b int)`. */
254+
static size_t pxc_go_result_name(pxc_span_t e, bool *lone) {
255+
size_t k = 0;
256+
while (k < e.len && pxc_is_ident_char(e.text[k])) {
257+
k++;
258+
}
259+
*lone = k > 0 && k == e.len;
260+
if (k == 0 || k == e.len || !pxc_is_space(e.text[k])) {
261+
return 0;
262+
}
263+
static const char *const type_keywords[] = {"chan", "func", "map", "struct", "interface", NULL};
264+
for (int i = 0; type_keywords[i]; i++) {
265+
if (strlen(type_keywords[i]) == k && memcmp(e.text, type_keywords[i], k) == 0) {
266+
return 0;
267+
}
268+
}
269+
return k;
270+
}
271+
272+
/* Named results: each lone name takes the type of the next `name type`
273+
* element, as Go groups them. Returns count, or -1 when the list is not a
274+
* well-formed named list. */
275+
static int pxc_go_named_types(CBMArena *arena, const pxc_span_t *el, int count,
276+
const char **types) {
277+
int pending = 0;
278+
for (int i = 0; i < count; i++) {
279+
bool lone = false;
280+
size_t k = pxc_go_result_name(el[i], &lone);
281+
if (k == 0) {
282+
if (!lone) {
283+
return -1;
284+
}
285+
pending++;
286+
continue;
287+
}
288+
pxc_span_t t = pxc_trim(el[i].text + k, el[i].len - k);
289+
const char *type = cbm_arena_sprintf(arena, "%.*s", (int)t.len, t.text);
290+
for (int j = i - pending; j <= i; j++) {
291+
types[j] = type;
292+
}
293+
pending = 0;
294+
}
295+
return pending == 0 ? count : -1;
296+
}
297+
298+
/* `(*T, error)` -> `*T|error`; `(t *T, err error)` -> `*T|error`;
299+
* `(a, b int)` -> `int|int`. NULL when raw is no result list (a single
300+
* result) or does not split cleanly: the caller keeps the raw text then. */
301+
static const char *pxc_go_result_list(CBMArena *arena, const char *raw) {
302+
pxc_span_t el[PXC_GO_MAX_RESULTS];
303+
int count = pxc_go_split_results(raw, el);
304+
if (count <= 0) {
305+
return NULL;
306+
}
307+
bool named = false;
308+
for (int i = 0; i < count && !named; i++) {
309+
bool lone = false;
310+
named = pxc_go_result_name(el[i], &lone) > 0;
311+
}
312+
const char *types[PXC_GO_MAX_RESULTS + 1];
313+
if (named) {
314+
if (pxc_go_named_types(arena, el, count, types) < 0) {
315+
return NULL;
316+
}
317+
} else {
318+
for (int i = 0; i < count; i++) {
319+
types[i] = cbm_arena_sprintf(arena, "%.*s", (int)el[i].len, el[i].text);
320+
}
321+
}
322+
types[count] = NULL;
323+
return pxc_join_pipe(arena, types);
324+
}
325+
169326
/* ── Cross-file base-class QN resolution ──────────────────────────
170327
*
171328
* CBMDefinition.base_classes carries the SOURCE SPELLING of each base
@@ -514,13 +671,17 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
514671
}
515672
dst->is_interface = (strcmp(label, "Interface") == 0 || strcmp(label, "Protocol") == 0);
516673
/* 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);
674+
* split_pipe_types), so a Go result LIST arrives split into its element
675+
* types: `(*T, error)` as `*T|error`, a named `(t *T)` as `*T`. The raw
676+
* list text did not parse, so every variable bound by a cross-file
677+
* multi-value or named-result call (`t, err := pkg.New()`) stayed untyped
678+
* (F-G8 D1). A single result, and every other language, keeps the raw
679+
* text the registrar already parses (`[]T` stays a slice). */
680+
const char *go_list = (lang == CBM_LANG_GO && src->return_type)
681+
? pxc_go_result_list(arena, src->return_type)
682+
: NULL;
683+
if (go_list) {
684+
dst->return_types = go_list;
524685
} else {
525686
dst->return_types = src->return_type ? cbm_arena_strdup(arena, src->return_type) : NULL;
526687
}

‎tests/test_pipeline.c‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8822,9 +8822,11 @@ static void write_go_c_ref_guard_fixture(const char *tmp, int pad_files) {
88228822
/* F-G8 D1: a variable bound by a CROSS-file multi-value or named-result call
88238823
* is typed. The cross-file registrar received the raw result text
88248824
* `(*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. */
8825+
* left e untyped and e.EncodeTuple() fell to a name guess. A single result
8826+
* keeps its raw text: `[]Sample` must stay a slice so that `xs[0].TS()` is
8827+
* typed (the extractor's cleaned return_types read it as `Sample`). Every
8828+
* target method has two same-named decoys, so a guess cannot pass.
8829+
* pad_files > 0 forces the parallel resolver. */
88288830
static void write_go_multi_return_fixture(const char *tmp, int pad_files) {
88298831
write_temp_file(tmp, "go.mod", "module example.com/fx\n\ngo 1.22\n");
88308832
write_temp_file(tmp, "enc/enc.go",
@@ -8836,9 +8838,25 @@ static void write_go_multi_return_fixture(const char *tmp, int pad_files) {
88368838
"\n"
88378839
"func NewNamed() (e *Encoder) { e = &Encoder{}; return }\n"
88388840
"\n"
8841+
"func NewPair() (a, b *Encoder) { return &Encoder{}, &Encoder{} }\n"
8842+
"\n"
88398843
"func (e *Encoder) EncodeTuple() int { return e.n }\n"
88408844
"\n"
8841-
"func (e *Encoder) EncodeNamed() int { return e.n }\n");
8845+
"func (e *Encoder) EncodeNamed() int { return e.n }\n"
8846+
"\n"
8847+
"func (e *Encoder) EncodeGrouped() int { return e.n }\n");
8848+
write_temp_file(tmp, "samples/samples.go",
8849+
"package samples\n"
8850+
"\n"
8851+
"type Sample interface{ TS() int64 }\n"
8852+
"\n"
8853+
"type sample struct{}\n"
8854+
"\n"
8855+
"func (sample) TS() int64 { return 0 }\n"
8856+
"\n"
8857+
"func Generate() []Sample { return []Sample{sample{}} }\n"
8858+
"\n"
8859+
"func List() ([]Sample, error) { return []Sample{sample{}}, nil }\n");
88428860
write_temp_file(tmp, "diag/diag.go",
88438861
"package diag\n"
88448862
"\n"
@@ -8865,8 +8883,24 @@ static void write_go_multi_return_fixture(const char *tmp, int pad_files) {
88658883
"import (\n"
88668884
"\t\"example.com/fx/diag\"\n"
88678885
"\t\"example.com/fx/enc\"\n"
8886+
"\t\"example.com/fx/samples\"\n"
88688887
")\n"
88698888
"\n"
8889+
"func UseIndex() int64 {\n"
8890+
"\txs := samples.Generate()\n"
8891+
"\treturn xs[0].TS()\n"
8892+
"}\n"
8893+
"\n"
8894+
"func UseTupleSlice() int64 {\n"
8895+
"\tys, _ := samples.List()\n"
8896+
"\treturn ys[0].TS()\n"
8897+
"}\n"
8898+
"\n"
8899+
"func UseGrouped() int {\n"
8900+
"\tx, _ := enc.NewPair()\n"
8901+
"\treturn x.EncodeGrouped()\n"
8902+
"}\n"
8903+
"\n"
88708904
"func UseTuple() int {\n"
88718905
"\te, err := enc.NewEncoder()\n"
88728906
"\t_ = err\n"
@@ -8901,7 +8935,11 @@ static void write_go_multi_return_fixture(const char *tmp, int pad_files) {
89018935
"func (D1) LowWatermarkField() int { return 0 }\n"
89028936
"func (D2) LowWatermarkField() int { return 0 }\n"
89038937
"func (D1) HasErrorsSlice() bool { return false }\n"
8904-
"func (D2) HasErrorsSlice() bool { return false }\n");
8938+
"func (D2) HasErrorsSlice() bool { return false }\n"
8939+
"func (D1) EncodeGrouped() int { return 0 }\n"
8940+
"func (D2) EncodeGrouped() int { return 0 }\n"
8941+
"func (D1) TS() int64 { return 0 }\n"
8942+
"func (D2) TS() int64 { return 0 }\n");
89058943
for (int i = 0; i < pad_files; i++) {
89068944
char name[64];
89078945
char body[128];
@@ -8951,6 +8989,9 @@ static int go_multi_return_case(int pad_files) {
89518989
{"UseNamed", "EncodeNamed", "enc/enc.go"}, /* named result (e *Encoder) */
89528990
{"UseField", "LowWatermarkField", "app/types.go"}, /* h, _ := newHead(); h.iso.X() */
89538991
{"UseSlice", "HasErrorsSlice", "diag/diag.go"}, /* _, d := diag.Run() */
8992+
{"UseGrouped", "EncodeGrouped", "enc/enc.go"}, /* (a, b *Encoder) */
8993+
{"UseIndex", "TS", "samples/samples.go"}, /* single []Sample, xs[0] */
8994+
{"UseTupleSlice", "TS", "samples/samples.go"}, /* ([]Sample, error) */
89548995
};
89558996
for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) {
89568997
int right = named_edge_to_file_count(s, project, "CALLS", want[i].caller, want[i].method,

0 commit comments

Comments
 (0)