Skip to content

Commit 684dcf5

Browse files
authored
Merge pull request #2703 from DeusData/fix/go-lsp-multi-return
fix(go-lsp): type variables bound by cross-file multi-value and named results
2 parents 4123268 + bec0154 commit 684dcf5

3 files changed

Lines changed: 394 additions & 19 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: 172 additions & 4 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
@@ -520,10 +677,21 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
520677
src->decl_namespace ? cbm_arena_strdup(arena, src->decl_namespace) : NULL;
521678
}
522679
dst->is_interface = (strcmp(label, "Interface") == 0 || strcmp(label, "Protocol") == 0);
523-
/* Single return-type string. The per-language registrars split on '|'
524-
* for multi-return languages (Go); single-return languages just see one
525-
* piece, which is what's already stored. */
526-
dst->return_types = src->return_type ? cbm_arena_strdup(arena, src->return_type) : NULL;
680+
/* The return-type string. The Go registrar splits it on '|' (go_lsp.c
681+
* split_pipe_types), so a Go result LIST arrives split into its element
682+
* types: `(*T, error)` as `*T|error`, a named `(t *T)` as `*T`. The raw
683+
* list text did not parse, so every variable bound by a cross-file
684+
* multi-value or named-result call (`t, err := pkg.New()`) stayed untyped
685+
* (F-G8 D1). A single result, and every other language, keeps the raw
686+
* text the registrar already parses (`[]T` stays a slice). */
687+
const char *go_list = (lang == CBM_LANG_GO && src->return_type)
688+
? pxc_go_result_list(arena, src->return_type)
689+
: NULL;
690+
if (go_list) {
691+
dst->return_types = go_list;
692+
} else {
693+
dst->return_types = src->return_type ? cbm_arena_strdup(arena, src->return_type) : NULL;
694+
}
527695
/* Languages whose cross registrars read embedded_types as QNs get their
528696
* bases resolved against the project registry; everyone else keeps the
529697
* raw source spelling their own registrar already knows how to handle. */

0 commit comments

Comments
 (0)