Skip to content

Commit 7b398de

Browse files
authored
Merge pull request #2769 from gaurav0107/fix/2033-go-type-alias-alias-of-is-never-register
fix(go-lsp): follow a type alias declared in another file of the package
2 parents c4e37c1 + ba246da commit 7b398de

10 files changed

Lines changed: 288 additions & 18 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ typedef struct {
313313
* change of all, store_impact.h follow_variant_keys). NULL on every other
314314
* definition. Tail field: zero-init stays valid. */
315315
const char *variant_key;
316+
/* Go only (#2033): the raw aliased type text of an identity alias
317+
* `type A = B` ("B"). Carried to the cross-file registries so a method
318+
* call on A from another file follows the alias to B's method set. NULL
319+
* for a defined type `type A B` (its method set is its own) and in every
320+
* other language. Tail field: zero-init stays valid. */
321+
const char *alias_target;
316322
} CBMDefinition;
317323

318324
/* Argument captured from a call expression */

‎internal/cbm/extract_defs.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8299,6 +8299,15 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
82998299
def.decl_namespace = cs_type_decl_namespace(a, node, ctx->source);
83008300
}
83018301
def.base_classes = extract_base_classes(a, node, ctx->source, ctx->language);
8302+
// Go identity alias `type A = B` (#2033): keep the aliased type text so the
8303+
// cross-file registries can set alias_of. A defined type `type A B` is a
8304+
// type_spec and never gets here with this kind.
8305+
if (ctx->language == CBM_LANG_GO && strcmp(kind, "type_alias") == 0) {
8306+
TSNode target = ts_node_child_by_field_name(node, TS_FIELD("type"));
8307+
if (!ts_node_is_null(target)) {
8308+
def.alias_target = cbm_node_text(a, target, ctx->source);
8309+
}
8310+
}
83028311
def.decorators = extract_decorators(a, node, ctx->source, ctx->language, spec);
83038312
def.docstring = extract_docstring(ctx, node, name);
83048313

‎internal/cbm/lsp/go_lsp.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,27 @@ static const char **split_pipe_strings(CBMArena *a, const char *text) {
27052705
return idx > 0 ? arr : NULL;
27062706
}
27072707

2708+
/* QN of the type an identity alias `type A = B` names, for the def-driven
2709+
* cross-file registries (#2033). The per-file AST scan sets alias_of only for
2710+
* the file it walks, so an alias declared in another file of the package
2711+
* reaches a call only through here. Only a bare identifier is qualified (B
2712+
* declared in A's package); a qualified, pointer, composite or generic target
2713+
* stays NULL rather than guessed. */
2714+
static const char *go_alias_target_qn(CBMArena *arena, const char *target,
2715+
const char *def_module_qn) {
2716+
if (!target || !target[0] || !def_module_qn || !def_module_qn[0]) {
2717+
return NULL;
2718+
}
2719+
for (const unsigned char *p = (const unsigned char *)target; *p; p++) {
2720+
bool ident = (*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
2721+
(*p >= '0' && *p <= '9') || *p == '_' || *p >= 0x80;
2722+
if (!ident) {
2723+
return NULL;
2724+
}
2725+
}
2726+
return cbm_arena_sprintf(arena, "%s.%s", def_module_qn, target);
2727+
}
2728+
27082729
// Helper: parse "|"-separated "name:type" field definitions and populate a registered type.
27092730
// Format: "Binder:Binder|Name:string|Count:int"
27102731
// type_text is resolved relative to def_module_qn.
@@ -3273,6 +3294,7 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
32733294
if (rt.is_interface && d->method_names_str && d->method_names_str[0]) {
32743295
rt.method_names = split_pipe_strings(arena, d->method_names_str);
32753296
}
3297+
rt.alias_of = go_alias_target_qn(arena, d->alias_target, def_mod);
32763298

32773299
cbm_registry_add_type(&reg, rt);
32783300

@@ -3569,6 +3591,7 @@ CBMTypeRegistry *cbm_go_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, i
35693591
if (rt.is_interface && d->method_names_str && d->method_names_str[0]) {
35703592
rt.method_names = split_pipe_strings(arena, d->method_names_str);
35713593
}
3594+
rt.alias_of = go_alias_target_qn(arena, d->alias_target, def_mod);
35723595
cbm_registry_add_type(reg, rt);
35733596
GO_NOTE_NEW_TYPES();
35743597
if (d->field_defs && d->field_defs[0]) {

‎internal/cbm/lsp/go_lsp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ typedef struct {
102102
/* Callable identity (#2061): signature suffix of this def's graph QN;
103103
* qualified_name stays the base QN. NULL = none (always, until enabled). */
104104
const char *sig;
105+
/* Go only (#2033): raw aliased type text of `type A = B` ("B"), qualified
106+
* by the registrar against def_module_qn like return_types. NULL = not an
107+
* identity alias. */
108+
const char *alias_target;
105109
} CBMLSPDef;
106110

107111
// Parse source, build registry from defs + stdlib, run LSP.

‎internal/cbm/result_compact.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static void cr_walk_def(cr_ctx_t *c, CBMDefinition *d) {
279279
cr_list(c, &d->extra_props);
280280
cr_str(c, &d->decl_namespace);
281281
cr_str(c, &d->variant_key);
282+
cr_str(c, &d->alias_target);
282283
}
283284

284285
static void cr_walk_call(cr_ctx_t *c, CBMCall *call) {

‎src/pipeline/lsp_surface.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
#include "yyjson/yyjson.h"
3535

3636
/* 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 };
37+
* a version-2 surface holds the raw result text and must be re-extracted.
38+
* 4: Go identity aliases carry their target in "al" (#2033); a version-3
39+
* surface has no alias targets and must be re-extracted. */
40+
enum { SURFACE_CODEC_VERSION = 4 };
3941

4042
/* Labels the incremental name registry serves that pxc_map_label does NOT
4143
* carry into the CBMLSPDef set. Their (name, qn, label) triple must still
@@ -394,6 +396,7 @@ static char *surface_file_to_json(const CBMFileResult *result, CBMLanguage langu
394396
yyjson_mut_obj_add_bool(doc, o, "ir", d->is_rust_impl_relation);
395397
yyjson_mut_obj_add_bool(doc, o, "ab", d->is_abstract);
396398
add_str_array_or_null(doc, o, "dec", d->decorators, -1);
399+
add_str_or_null(doc, o, "al", d->alias_target);
397400
yyjson_mut_arr_add_val(lsp, o);
398401
}
399402
yyjson_mut_obj_add_val(doc, root, "lsp", lsp);
@@ -670,6 +673,7 @@ int cbm_lsp_surface_defs_from_json(CBMArena *arena, const char *defs_json, CBMLS
670673
d->is_abstract = yyjson_get_bool(yyjson_obj_get(o, "ab"));
671674
int dec_count = 0;
672675
d->decorators = arena_str_array(arena, yyjson_obj_get(o, "dec"), true, &dec_count);
676+
d->alias_target = arena_str_or_null(arena, yyjson_obj_get(o, "al"));
673677
if (!d->qualified_name || !d->short_name || !d->label) {
674678
/* A def the writer could not have produced: qn/sn/label are
675679
* unconditionally present in build_lsp_def output. Corrupt row. */

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
715715
}
716716
}
717717
dst->lang = lang;
718+
dst->alias_target = (lang == CBM_LANG_GO && src->alias_target)
719+
? cbm_arena_strdup(arena, src->alias_target)
720+
: NULL;
718721
dst->decorators = NULL;
719722
if (src->decorators) {
720723
int n = 0;

‎src/pipeline/pipeline.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ static void log_result_census(const char *tag, CBMFileResult **cache, int file_c
25592559
str_def_misc += census_len(def->route_path) + census_len(def->route_method) +
25602560
census_len(def->impl_trait) + census_len(def->http_base_url) +
25612561
census_list(def->extra_props) + census_len(def->decl_namespace) +
2562-
census_len(def->variant_key);
2562+
census_len(def->variant_key) + census_len(def->alias_target);
25632563
}
25642564
for (int c = 0; c < r->calls.count; c++) {
25652565
const CBMCall *call = &r->calls.items[c];

‎tests/test_go_lsp.c‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,127 @@ TEST(golsp_crossfile_aliased_field_requal) {
13781378
PASS();
13791379
}
13801380

1381+
/* #2033: an identity alias declared in another file of the same package.
1382+
* `type FeatureSet = bitmap` lives in one file, `bitmap.Set` in a second, the
1383+
* call `reqs.Set` in a third, so none of it is in the calling file's AST and
1384+
* only the defs can carry the alias. A defined type (`type Defined bitmap`)
1385+
* must not inherit bitmap's methods. The decoy `other.Set` keeps a name-only
1386+
* match from passing. Both cross-file registries are checked: the per-file
1387+
* one and the prebuilt one shared by the parallel resolve. */
1388+
static CBMLSPDef go_crossfile_alias_defs[] = {
1389+
{.qualified_name = "test.main.bitmap",
1390+
.short_name = "bitmap",
1391+
.label = "Class",
1392+
.def_module_qn = "test.main"},
1393+
{.qualified_name = "test.main.bitmap.Set",
1394+
.short_name = "Set",
1395+
.label = "Method",
1396+
.def_module_qn = "test.main",
1397+
.receiver_type = "test.main.bitmap"},
1398+
{.qualified_name = "test.main.FeatureSet",
1399+
.short_name = "FeatureSet",
1400+
.label = "Type",
1401+
.def_module_qn = "test.main",
1402+
.alias_target = "bitmap"},
1403+
{.qualified_name = "test.main.Defined",
1404+
.short_name = "Defined",
1405+
.label = "Class",
1406+
.def_module_qn = "test.main"},
1407+
{.qualified_name = "test.main.Framework",
1408+
.short_name = "Framework",
1409+
.label = "Struct",
1410+
.def_module_qn = "test.main"},
1411+
{.qualified_name = "test.main.Framework.NewFeatureSet",
1412+
.short_name = "NewFeatureSet",
1413+
.label = "Method",
1414+
.def_module_qn = "test.main",
1415+
.receiver_type = "test.main.Framework",
1416+
.return_types = "FeatureSet"},
1417+
{.qualified_name = "test.main.Framework.NewDefined",
1418+
.short_name = "NewDefined",
1419+
.label = "Method",
1420+
.def_module_qn = "test.main",
1421+
.receiver_type = "test.main.Framework",
1422+
.return_types = "Defined"},
1423+
{.qualified_name = "test.main.other",
1424+
.short_name = "other",
1425+
.label = "Struct",
1426+
.def_module_qn = "test.main"},
1427+
{.qualified_name = "test.main.other.Set",
1428+
.short_name = "Set",
1429+
.label = "Method",
1430+
.def_module_qn = "test.main",
1431+
.receiver_type = "test.main.other"},
1432+
};
1433+
1434+
static const char *go_crossfile_alias_source = "package main\n\n"
1435+
"func useAlias(f *Framework) {\n"
1436+
"\treqs := f.NewFeatureSet()\n"
1437+
"\treqs.Set(3)\n}\n\n"
1438+
"func useDefined(f *Framework) {\n"
1439+
"\td := f.NewDefined()\n"
1440+
"\td.Set(3)\n}\n";
1441+
1442+
static int go_crossfile_alias_check(const CBMResolvedCallArray *out) {
1443+
int idx = find_resolved_arr_confident(out, "useAlias", ".Set");
1444+
if (idx < 0 || strcmp(out->items[idx].callee_qn, "test.main.bitmap.Set") != 0) {
1445+
printf(" resolved calls (%d):\n", out->count);
1446+
for (int i = 0; i < out->count; i++) {
1447+
const CBMResolvedCall *rc = &out->items[i];
1448+
printf(" %s -> %s [%s %.2f]\n", rc->caller_qn ? rc->caller_qn : "(null)",
1449+
rc->callee_qn ? rc->callee_qn : "(null)", rc->strategy ? rc->strategy : "(null)",
1450+
rc->confidence);
1451+
}
1452+
}
1453+
ASSERT_GTE(idx, 0);
1454+
ASSERT_STR_EQ(out->items[idx].callee_qn, "test.main.bitmap.Set");
1455+
/* Reached through alias_of, so the emitter reports it the way it reports
1456+
* a same-file alias today (lsp_embed_dispatch): typed, not a name guess. */
1457+
ASSERT_TRUE(strncmp(out->items[idx].strategy, "lsp_", 4) == 0);
1458+
ASSERT_TRUE(strcmp(out->items[idx].strategy, "lsp_unresolved") != 0);
1459+
ASSERT_TRUE(out->items[idx].confidence >= 0.9f);
1460+
/* A defined type has its own (empty) method set. */
1461+
ASSERT_EQ(find_resolved_arr_confident(out, "useDefined", "bitmap.Set"), -1);
1462+
return 0;
1463+
}
1464+
1465+
TEST(golsp_crossfile_type_alias_method) {
1466+
int n = (int)(sizeof(go_crossfile_alias_defs) / sizeof(go_crossfile_alias_defs[0]));
1467+
CBMArena arena;
1468+
cbm_arena_init(&arena);
1469+
CBMResolvedCallArray out = {0};
1470+
1471+
cbm_run_go_lsp_cross(&arena, go_crossfile_alias_source, (int)strlen(go_crossfile_alias_source),
1472+
"test.main", go_crossfile_alias_defs, n, NULL, NULL, 0, NULL, &out);
1473+
if (go_crossfile_alias_check(&out) != 0) {
1474+
cbm_arena_destroy(&arena);
1475+
return 1;
1476+
}
1477+
1478+
cbm_arena_destroy(&arena);
1479+
PASS();
1480+
}
1481+
1482+
TEST(golsp_crossfile_type_alias_method_prebuilt) {
1483+
int n = (int)(sizeof(go_crossfile_alias_defs) / sizeof(go_crossfile_alias_defs[0]));
1484+
CBMArena arena;
1485+
cbm_arena_init(&arena);
1486+
CBMResolvedCallArray out = {0};
1487+
1488+
CBMTypeRegistry *reg = cbm_go_build_cross_registry(&arena, go_crossfile_alias_defs, n);
1489+
ASSERT_NOT_NULL(reg);
1490+
cbm_run_go_lsp_cross_with_registry(&arena, go_crossfile_alias_source,
1491+
(int)strlen(go_crossfile_alias_source), "test.main", reg,
1492+
NULL, NULL, 0, NULL, &out);
1493+
if (go_crossfile_alias_check(&out) != 0) {
1494+
cbm_arena_destroy(&arena);
1495+
return 1;
1496+
}
1497+
1498+
cbm_arena_destroy(&arena);
1499+
PASS();
1500+
}
1501+
13811502
TEST(golsp_crossfile_local_interface_single_impl) {
13821503
const char *source =
13831504
"package main\n\n"
@@ -1542,6 +1663,8 @@ SUITE(go_lsp) {
15421663
RUN_TEST(golsp_crossfile_interface_dispatch);
15431664
RUN_TEST(golsp_crossfile_interface_field_chain);
15441665
RUN_TEST(golsp_crossfile_aliased_field_requal);
1666+
RUN_TEST(golsp_crossfile_type_alias_method);
1667+
RUN_TEST(golsp_crossfile_type_alias_method_prebuilt);
15451668
RUN_TEST(golsp_crossfile_map_index);
15461669
RUN_TEST(golsp_crossfile_stdlib_interface);
15471670
RUN_TEST(golsp_crossfile_local_interface_single_impl);

0 commit comments

Comments
 (0)