Skip to content

Commit a6d0d6c

Browse files
authored
Merge pull request DeusData#1936 from ilyabrykau-orca/fix/go-field-type-hint-owner-segment
fix(pipeline): bind field_type_hint by owning QN segment, not substring
2 parents f6fb3e0 + 2578218 commit a6d0d6c

2 files changed

Lines changed: 242 additions & 1 deletion

File tree

‎src/pipeline/pass_parallel.c‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,29 @@ static const cbm_gbuf_node_t *find_source_node(const cbm_gbuf_t *gbuf, const cha
29852985
/* Field type hint resolution for obj.Method() with multiple candidates.
29862986
* Strips C# field prefixes (_ / m_), capitalizes to get type name, and
29872987
* checks if TypeName.Method or ITypeName.Method exists among candidates. */
2988+
/* #1927: the hint may only bind a candidate whose OWNING dot-segment — the
2989+
* segment immediately before the method name — EQUALS the hinted type. The
2990+
* previous raw strstr() accepted the hinted name anywhere in the QN, so a
2991+
* single-letter receiver (`f.Close()` → hint "F") matched nearly every
2992+
* candidate ("F" ⊂ "…FileStore.Close", the method name included) and rebound
2993+
* a call whose resolution had already failed to an arbitrary project symbol
2994+
* at PP_FIELD_HINT_CONF. Segment equality keeps the intended
2995+
* variable-named-after-its-type case (`repo.Find()` → `Repo.Find`,
2996+
* lrp_go_s8_field_type_hint) and picks the candidate whose owner actually
2997+
* carries the name instead of whichever QN contains the letters. */
2998+
static bool fth_owner_segment_is(const char *candidate_qn, const char *type_name) {
2999+
const char *last_dot = strrchr(candidate_qn, '.');
3000+
if (!last_dot || last_dot == candidate_qn) {
3001+
return false;
3002+
}
3003+
const char *seg_start = last_dot;
3004+
while (seg_start > candidate_qn && seg_start[-1] != '.') {
3005+
seg_start--;
3006+
}
3007+
size_t seg_len = (size_t)(last_dot - seg_start);
3008+
return seg_len > 0 && strncmp(seg_start, type_name, seg_len) == 0 && type_name[seg_len] == '\0';
3009+
}
3010+
29883011
static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const char *callee_name,
29893012
int64_t source_id) {
29903013
if (!res->qualified_name || res->candidate_count <= SKIP_ONE) {
@@ -3024,7 +3047,8 @@ static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const
30243047
int cand_count = 0;
30253048
cbm_registry_find_by_name(rc->registry, method, &cands, &cand_count);
30263049
for (int ci = 0; ci < cand_count; ci++) {
3027-
if (strstr(cands[ci], type_name) || strstr(cands[ci], iface_name)) {
3050+
if (fth_owner_segment_is(cands[ci], type_name) ||
3051+
fth_owner_segment_is(cands[ci], iface_name)) {
30283052
const cbm_gbuf_node_t *better = cbm_gbuf_find_by_qn(rc->main_gbuf, cands[ci]);
30293053
if (better && better->id != source_id) {
30303054
res->qualified_name = cands[ci];

‎tests/test_pipeline.c‎

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11666,6 +11666,221 @@ TEST(pipeline_go_bare_init_binds_nothing_parallel) {
1166611666
PASS();
1166711667
}
1166811668

11669+
TEST(pipeline_go_parallel_field_hint_requires_owner_segment) {
11670+
/* #1927: try_field_type_hint (parallel resolver only) matched the hinted
11671+
* type name as a raw SUBSTRING of the candidate QN. A single-letter
11672+
* receiver — `f.Close()` on an *os.File — hints "F", and "F" is a
11673+
* substring of "…FileStore.Close", so a stdlib call was rebound to an
11674+
* arbitrary project method at PP_FIELD_HINT_CONF and presented as
11675+
* field_type_hint (the one weak strategy the Go selector guard trusts).
11676+
* The hint must fire only when the candidate's OWNING segment — the
11677+
* dot-segment immediately before the method — EQUALS the hinted type;
11678+
* otherwise the resolution stays a weak short-name match and the Go
11679+
* guard (#1906) drops it. >= 50 files forces pass_parallel.c, the only
11680+
* path that runs try_field_type_hint(). */
11681+
char tmp[256];
11682+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_fth_XXXXXX");
11683+
if (!cbm_mkdtemp(tmp)) {
11684+
FAIL("tmpdir");
11685+
}
11686+
11687+
write_temp_file(tmp, "go.mod", "module example.com/fxhint\n\ngo 1.22\n");
11688+
/* Two project methods named Close → the registry resolves f.Close() as an
11689+
* ambiguous short-name match (candidate_count 2), the shape the hint
11690+
* upgrades. FileStore's QN carries the capital F the buggy substring
11691+
* latches onto; Conn is the second candidate. */
11692+
write_temp_file(tmp, "filestore/filestore.go",
11693+
"package filestore\n"
11694+
"\n"
11695+
"type FileStore struct{ open bool }\n"
11696+
"\n"
11697+
"func (fs *FileStore) Close() {\n"
11698+
"\tfs.open = false\n"
11699+
"}\n");
11700+
write_temp_file(tmp, "conn/conn.go",
11701+
"package conn\n"
11702+
"\n"
11703+
"type Conn struct{ live bool }\n"
11704+
"\n"
11705+
"func (cn *Conn) Close() {\n"
11706+
"\tcn.live = false\n"
11707+
"}\n");
11708+
/* Two project methods named Find → same ambiguity, but here the receiver
11709+
* variable IS named after its type (`finder`), the case the hint exists
11710+
* for: the owning segment of Finder.Find equals the hinted "Finder". */
11711+
write_temp_file(tmp, "finder/finder.go",
11712+
"package finder\n"
11713+
"\n"
11714+
"type Finder struct{ n int }\n"
11715+
"\n"
11716+
"func NewFinder() *Finder { return &Finder{n: 1} }\n"
11717+
"\n"
11718+
"func (fi *Finder) Find(id int) int {\n"
11719+
"\treturn fi.n + id\n"
11720+
"}\n");
11721+
write_temp_file(tmp, "locator/locator.go",
11722+
"package locator\n"
11723+
"\n"
11724+
"type Locator struct{ n int }\n"
11725+
"\n"
11726+
"func (lo *Locator) Find(id int) int {\n"
11727+
"\treturn lo.n - id\n"
11728+
"}\n");
11729+
write_temp_file(tmp, "app/app.go",
11730+
"package app\n"
11731+
"\n"
11732+
"import (\n"
11733+
"\t\"os\"\n"
11734+
"\n"
11735+
"\txf \"example.com/fxhint/finder\"\n"
11736+
")\n"
11737+
"\n"
11738+
"func Lookup(path string) int64 {\n"
11739+
"\tf, err := os.Open(path)\n"
11740+
"\tif err != nil {\n"
11741+
"\t\treturn 0\n"
11742+
"\t}\n"
11743+
"\tdefer f.Close()\n"
11744+
"\tst, err := f.Stat()\n"
11745+
"\tif err != nil {\n"
11746+
"\t\treturn 0\n"
11747+
"\t}\n"
11748+
"\treturn st.Size()\n"
11749+
"}\n"
11750+
"\n"
11751+
"func UseFinder(id int) int {\n"
11752+
"\tfinder := xf.NewFinder()\n"
11753+
"\treturn finder.Find(id)\n"
11754+
"}\n");
11755+
for (int i = 0; i < 52; i++) {
11756+
char name[64];
11757+
char body[128];
11758+
snprintf(name, sizeof(name), "pad/filler%d.go", i);
11759+
snprintf(body, sizeof(body), "package pad\n\nfunc filler%d() int { return %d }\n", i, i);
11760+
write_temp_file(tmp, name, body);
11761+
}
11762+
11763+
char db_path[512];
11764+
snprintf(db_path, sizeof(db_path), "%s/go_fth.db", tmp);
11765+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
11766+
ASSERT_NOT_NULL(p);
11767+
ASSERT_EQ(cbm_pipeline_run(p), 0);
11768+
const char *project = cbm_pipeline_project_name(p);
11769+
11770+
cbm_store_t *s = cbm_store_open_path(db_path);
11771+
ASSERT_NOT_NULL(s);
11772+
11773+
/* (1) Reproduce-first: RED before the fix. The substring hint rebinds the
11774+
* stdlib f.Close() to FileStore.Close ("F" ⊂ QN) at high confidence and
11775+
* the Go guard keeps it. With segment equality the upgrade is refused,
11776+
* the match stays suffix_match, and the Go guard drops it. */
11777+
ASSERT_FALSE(cross_file_call_exists(s, project, "Lookup", "Close"));
11778+
/* (2) The intended hint case survives: `finder` names its type, so the
11779+
* owning segment of Finder.Find equals the hint. */
11780+
ASSERT_TRUE(cross_file_call_exists(s, project, "UseFinder", "Find"));
11781+
11782+
cbm_store_close(s);
11783+
cbm_pipeline_free(p);
11784+
th_rmtree(tmp);
11785+
PASS();
11786+
}
11787+
11788+
static int count_calls_with(cbm_store_t *s, const char *project, const char *caller_suffix,
11789+
const char *callee_suffix, const char *fragment);
11790+
11791+
TEST(pipeline_csharp_parallel_field_hint_requires_owner_segment) {
11792+
/* #1927, non-Go twin. Go no longer runs try_field_type_hint (e554a2ab),
11793+
* but every other language on the parallel path still does, and C# is the
11794+
* hint's intended consumer (it strips the _ / m_ field prefixes). C#
11795+
* method names are PascalCase, so a one-letter receiver hints a letter
11796+
* that every candidate QN already contains in the METHOD name: `w.Write()`
11797+
* hints "W", and a raw substring test accepts "...FileStore.Write" and
11798+
* "...Conn.Write" alike, whichever the registry lists first. The hint must
11799+
* bind only a candidate whose OWNING segment equals the hinted type.
11800+
* >= 50 files forces pass_parallel.c, the only path with the hint. */
11801+
char tmp[256];
11802+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_cs_fth_XXXXXX");
11803+
if (!cbm_mkdtemp(tmp)) {
11804+
FAIL("tmpdir");
11805+
}
11806+
11807+
/* Two project methods named Write: an untyped receiver resolves as an
11808+
* ambiguous short-name match (candidate_count 2), the shape the hint
11809+
* upgrades. */
11810+
write_temp_file(tmp, "Storage/FileStore.cs",
11811+
"namespace Storage {\n"
11812+
" public class FileStore {\n"
11813+
" private int written;\n"
11814+
" public void Write(int v) { written += v; }\n"
11815+
" public void Put(int v) { written = v; }\n"
11816+
" }\n"
11817+
"}\n");
11818+
write_temp_file(tmp, "Net/Conn.cs",
11819+
"namespace Net {\n"
11820+
" public class Conn {\n"
11821+
" private int sent;\n"
11822+
" public void Write(int v) { sent += v; }\n"
11823+
" }\n"
11824+
"}\n");
11825+
/* Two project methods named Put, one owned by Store: the receiver `store`
11826+
* names its type, the case the hint exists for. FileStore's QN also
11827+
* contains "Store", so only segment equality picks Store.Put whatever
11828+
* the candidate order. */
11829+
write_temp_file(tmp, "Repo/Store.cs",
11830+
"namespace Repo {\n"
11831+
" public class Store {\n"
11832+
" private int held;\n"
11833+
" public void Put(int v) { held = v; }\n"
11834+
" }\n"
11835+
"}\n");
11836+
write_temp_file(tmp, "App/Worker.cs",
11837+
"namespace App {\n"
11838+
" public class Worker {\n"
11839+
" public void Log(System.IO.StreamWriter w) {\n"
11840+
" w.Write(1);\n"
11841+
" }\n"
11842+
" public void Keep() {\n"
11843+
" var store = Services.Resolve();\n"
11844+
" store.Put(2);\n"
11845+
" }\n"
11846+
" }\n"
11847+
"}\n");
11848+
for (int i = 0; i < 52; i++) {
11849+
char name[64];
11850+
char body[192];
11851+
snprintf(name, sizeof(name), "Pad/Filler%d.cs", i);
11852+
snprintf(body, sizeof(body),
11853+
"namespace Pad {\n public class Filler%d {\n"
11854+
" public int Get%d() { return %d; }\n }\n}\n",
11855+
i, i, i);
11856+
write_temp_file(tmp, name, body);
11857+
}
11858+
11859+
char db_path[512];
11860+
snprintf(db_path, sizeof(db_path), "%s/cs_fth.db", tmp);
11861+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
11862+
ASSERT_NOT_NULL(p);
11863+
ASSERT_EQ(cbm_pipeline_run(p), 0);
11864+
const char *project = cbm_pipeline_project_name(p);
11865+
11866+
cbm_store_t *s = cbm_store_open_path(db_path);
11867+
ASSERT_NOT_NULL(s);
11868+
11869+
/* (1) Reproduce-first: RED before the fix. "W" is a substring of every
11870+
* Write candidate, so the old hint upgraded the StreamWriter call to an
11871+
* arbitrary project Write at PP_FIELD_HINT_CONF. No owner segment is "W",
11872+
* so the fixed hint must not fire at all. */
11873+
ASSERT_FALSE(cross_file_call_has_strategy(s, project, "Log", "Write", "field_type_hint"));
11874+
/* (2) The intended hint case binds the owner it names, never FileStore. */
11875+
ASSERT_EQ(count_calls_with(s, project, ".Keep", ".Store.Put", "field_type_hint"), 1);
11876+
ASSERT_EQ(count_calls_with(s, project, ".Keep", ".FileStore.Put", NULL), 0);
11877+
11878+
cbm_store_close(s);
11879+
cbm_pipeline_free(p);
11880+
th_rmtree(tmp);
11881+
PASS();
11882+
}
11883+
1166911884
/* Fixture for the #1928 cross-language reference-guard probes (sequential and
1167011885
* parallel twins). pad_files > 0 adds filler files to push the index over the
1167111886
* parallel-pipeline threshold, since USAGE/WRITES/READS have one resolver per
@@ -30415,6 +30630,8 @@ SUITE(pipeline) {
3041530630
RUN_TEST(pipeline_go_receiver_parallel_suppresses_weak_method_edge);
3041630631
RUN_TEST(pipeline_go_multi_return_receiver_is_typed);
3041730632
RUN_TEST(pipeline_go_multi_return_receiver_is_typed_parallel);
30633+
RUN_TEST(pipeline_go_parallel_field_hint_requires_owner_segment);
30634+
RUN_TEST(pipeline_csharp_parallel_field_hint_requires_owner_segment);
3041830635
RUN_TEST(pipeline_go_multi_init_nodes_survive);
3041930636
RUN_TEST(pipeline_go_init_lsp_edges_per_file);
3042030637
RUN_TEST(pipeline_go_init_lsp_edges_per_file_parallel);

0 commit comments

Comments
 (0)