Skip to content

Commit 5cc7b5c

Browse files
committed
fix(php): prove an unaliased global class use absent from the project
A PHP file's import evidence is written as complete only when every `use` either resolved or is proven absent. A global-namespace class use such as `use Exception;` had no absence proof, so one such line left the whole file's bindings unknown, and its project imports reached the PHP LSP as File QNs again (the DeusData#2520 path). An unaliased global class use is now proven absent when no PHP file declares a class-like symbol of that name (Class, Interface, Trait, Enum, File or Module; namespaced ones included, so the proof stays conservative). Aliased global uses keep the old, unknown verdict. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent bddacfe commit 5cc7b5c

2 files changed

Lines changed: 214 additions & 7 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,15 +2797,53 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
27972797
static const char php_import_complete[] = "{\"php_imports_complete\":1,";
27982798

27992799
/* A resolver NULL can also mean unsupported input or a failed lookup. Only
2800-
* the bounded namespace-only path admits an allocation-free absence proof.
2801-
* A covered-but-missing PSR-4 file, global use, or unavailable namespace map
2802-
* leaves the whole file unknown; already resolved IMPORTS are still written. */
2803-
static bool php_import_known_absent(const char *rel, const CBMImport *imp,
2800+
* the bounded namespace-only path and an unaliased global class `use` with no
2801+
* same-named PHP type anywhere admit an absence proof. A covered-but-missing
2802+
* PSR-4 file, any other global use, or an unavailable namespace map leaves the
2803+
* whole file unknown; already resolved IMPORTS are still written. */
2804+
/* A global-namespace class `use Exception;` names a project type only when a
2805+
* PHP file declares a class-like symbol of that name. With no such node at all
2806+
* (namespaced ones included, so the proof stays conservative), the import is
2807+
* absent from the project: the PHP builtin or a vendor class. Without this a
2808+
* single `use Exception;` left the whole file's evidence unknown, and its
2809+
* project imports reached the PHP LSP as File QNs again. */
2810+
static bool php_global_use_known_absent(const cbm_gbuf_t *gbuf, const CBMImport *imp) {
2811+
if (!gbuf || imp->kind != CBM_IMPORT_KIND_DEFAULT) {
2812+
return false;
2813+
}
2814+
const char *name = imp->module_path[0] == '\\' ? imp->module_path + 1 : imp->module_path;
2815+
if (!name[0] || strcmp(name, imp->local_name) != 0) {
2816+
return false; /* `use X as Y;` keeps the old, unknown verdict */
2817+
}
2818+
const cbm_gbuf_node_t **nodes = NULL;
2819+
int count = 0;
2820+
if (cbm_gbuf_find_by_name(gbuf, name, &nodes, &count) != 0) {
2821+
return false;
2822+
}
2823+
for (int i = 0; i < count; i++) {
2824+
const cbm_gbuf_node_t *n = nodes[i];
2825+
if (!n || !n->label || !node_in_php_file(n)) {
2826+
continue;
2827+
}
2828+
if (strcmp(n->label, "Class") == 0 || strcmp(n->label, "Interface") == 0 ||
2829+
strcmp(n->label, "Trait") == 0 || strcmp(n->label, "Enum") == 0 ||
2830+
strcmp(n->label, "File") == 0 || strcmp(n->label, "Module") == 0) {
2831+
return false;
2832+
}
2833+
}
2834+
return true;
2835+
}
2836+
2837+
static bool php_import_known_absent(const cbm_gbuf_t *gbuf, const char *rel, const CBMImport *imp,
28042838
CBMHashTable *namespace_map) {
28052839
if (!namespace_map || !imp->module_path || !imp->local_name || !imp->local_name[0] ||
28062840
(imp->kind != CBM_IMPORT_KIND_DEFAULT && imp->kind != CBM_IMPORT_KIND_FUNCTION &&
2807-
imp->kind != CBM_IMPORT_KIND_CONST) ||
2808-
php_use_shape(rel, imp) != PHP_USE_NAMESPACED)
2841+
imp->kind != CBM_IMPORT_KIND_CONST))
2842+
return false;
2843+
php_use_shape_t shape = php_use_shape(rel, imp);
2844+
if (shape == PHP_USE_GLOBAL)
2845+
return php_global_use_known_absent(gbuf, imp);
2846+
if (shape != PHP_USE_NAMESPACED)
28092847
return false;
28102848
const char *name = imp->module_path[0] == '\\' ? imp->module_path + 1 : imp->module_path;
28112849
char key[CBM_SZ_1K];
@@ -3036,7 +3074,7 @@ int cbm_pipeline_php_create_import_edges(cbm_pipeline_ctx_t *ctx, const CBMFileR
30363074
: NULL;
30373075
if (targets[i] && targets[i]->id == file->id)
30383076
targets[i] = NULL;
3039-
if (!targets[i] && !php_import_known_absent(rel, imp, namespace_map))
3077+
if (!targets[i] && !php_import_known_absent(ctx->gbuf, rel, imp, namespace_map))
30403078
complete = false;
30413079
yyjson_mut_val *entry =
30423080
php_binding_json(doc, imp, targets[i] ? targets[i]->qualified_name : NULL);

‎tests/test_parallel.c‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,6 +5148,173 @@ TEST(parallel_php_vendor_types_never_bind_by_name_issue1186) {
51485148
PASS();
51495149
}
51505150

5151+
/* ── PHP LSP types receivers of imported project classes (#1186) ─── */
5152+
5153+
/* Foo.php calls methods on receivers typed with classes imported from OTHER
5154+
* project files: a promoted property (`$this->svc->go()`) and an aliased
5155+
* import (`R $r` -> `$r->find()`). The import map handed to the PHP LSP held
5156+
* the IMPORTS edge target -- the imported file's File node QN
5157+
* (`...Svc.php.__file__`) -- as the class, so no receiver ever typed and the
5158+
* calls fell to name-only strategies (the #1186 "useful calls at 0.38"). The
5159+
* decoy App\Other\Svc shares the short name and the method, so a name-only
5160+
* match cannot tell the two apart. Controls: the vendor-typed receiver
5161+
* (`Client $c`) must stay unresolved, and the same-file calls keep their LSP
5162+
* strategies. */
5163+
static const struct {
5164+
const char *rel;
5165+
const char *src;
5166+
} k_php_xfile_files[] = {
5167+
{"lib/Services/Svc.php", "<?php\nnamespace App\\Services;\n\nclass Svc {\n"
5168+
" public function go() { return 1; }\n}\n"},
5169+
{"lib/Other/Svc.php", "<?php\nnamespace App\\Other;\n\nclass Svc {\n"
5170+
" public function go() { return 2; }\n}\n"},
5171+
{"lib/Repo/Repo.php", "<?php\nnamespace App\\Repo;\n\nclass Repo {\n"
5172+
" public function find() { return 3; }\n}\n"},
5173+
{"lib/Http/Foo.php",
5174+
"<?php\nnamespace App\\Http;\n\n"
5175+
"use App\\Services\\Svc;\nuse App\\Repo\\Repo as R;\nuse Vendor\\Pkg\\Client;\n\n"
5176+
"class Foo {\n"
5177+
" public function __construct(private Svc $svc) {}\n"
5178+
" public function run(R $r, Client $c) {\n"
5179+
" $this->svc->go();\n"
5180+
" $r->find();\n"
5181+
" $c->go();\n"
5182+
" $this->local();\n"
5183+
" (new Local())->ping();\n"
5184+
" }\n"
5185+
" private function local() { return 4; }\n}\n\n"
5186+
"class Local {\n"
5187+
" public function ping() { return 5; }\n}\n"},
5188+
};
5189+
enum { PHP_XFILE_FILE_COUNT = (int)(sizeof(k_php_xfile_files) / sizeof(k_php_xfile_files[0])) };
5190+
5191+
typedef struct {
5192+
const cbm_gbuf_t *gbuf;
5193+
const char *src_rel;
5194+
const char *tgt_rel;
5195+
const char *tgt_suffix;
5196+
const char *strategy_prefix;
5197+
int total;
5198+
int with_strategy;
5199+
} php_xfile_edge_count_t;
5200+
5201+
static void php_xfile_count_edge(const cbm_gbuf_edge_t *edge, void *ud) {
5202+
php_xfile_edge_count_t *c = ud;
5203+
if (!edge->type || strcmp(edge->type, "CALLS") != 0) {
5204+
return;
5205+
}
5206+
const cbm_gbuf_node_t *s = cbm_gbuf_find_by_id(c->gbuf, edge->source_id);
5207+
const cbm_gbuf_node_t *t = cbm_gbuf_find_by_id(c->gbuf, edge->target_id);
5208+
if (!s || !t || !s->file_path || !t->file_path || !t->qualified_name ||
5209+
strcmp(s->file_path, c->src_rel) != 0 || strcmp(t->file_path, c->tgt_rel) != 0) {
5210+
return;
5211+
}
5212+
size_t qlen = strlen(t->qualified_name);
5213+
size_t slen = strlen(c->tgt_suffix);
5214+
if (qlen < slen || strcmp(t->qualified_name + qlen - slen, c->tgt_suffix) != 0) {
5215+
return;
5216+
}
5217+
c->total++;
5218+
char needle[128];
5219+
snprintf(needle, sizeof(needle), "\"strategy\":\"%s", c->strategy_prefix);
5220+
if (edge->properties_json && strstr(edge->properties_json, needle)) {
5221+
c->with_strategy++;
5222+
}
5223+
}
5224+
5225+
/* 1 when every cross-file receiver resolved through the PHP LSP, the decoy and
5226+
* the vendor receiver drew nothing, and the same-file controls held. */
5227+
static int php_xfile_graph_ok(const cbm_gbuf_t *gbuf, const char *driver) {
5228+
static const char *const foo = "lib/Http/Foo.php";
5229+
struct {
5230+
const char *tgt;
5231+
const char *suffix;
5232+
int want_total;
5233+
int want_lsp;
5234+
} checks[] = {
5235+
{"lib/Services/Svc.php", ".Svc.go", 1, 1}, /* $this->svc->go(), not $c->go() */
5236+
{"lib/Other/Svc.php", ".Svc.go", 0, 0}, /* decoy: same short name + method */
5237+
{"lib/Repo/Repo.php", ".Repo.find", 1, 1}, /* aliased import */
5238+
{foo, ".Foo.local", 1, 1}, /* same-file control */
5239+
{foo, ".Local.ping", 1, 1}, /* same-file control */
5240+
};
5241+
int ok = 1;
5242+
for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) {
5243+
php_xfile_edge_count_t c = {gbuf, foo, checks[i].tgt, checks[i].suffix, "php_", 0, 0};
5244+
cbm_gbuf_foreach_edge(gbuf, php_xfile_count_edge, &c);
5245+
if (c.total != checks[i].want_total || c.with_strategy != checks[i].want_lsp) {
5246+
printf(" [%s] CALLS Foo.php -> %s%s: %d edge(s), %d via php_*; want %d, %d\n", driver,
5247+
checks[i].tgt, checks[i].suffix, c.total, c.with_strategy, checks[i].want_total,
5248+
checks[i].want_lsp);
5249+
ok = 0;
5250+
}
5251+
}
5252+
return ok;
5253+
}
5254+
5255+
/* Run the fixture on both venues. extra_use (nullable) is one more `use` line
5256+
* added to Foo.php. Returns 1 when both graphs pass, 0 when not, -1 on setup
5257+
* failure. */
5258+
static int php_xfile_run(const char *extra_use) {
5259+
char tmpdir[256];
5260+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_phpxfile_XXXXXX");
5261+
if (!cbm_mkdtemp(tmpdir)) {
5262+
return -1;
5263+
}
5264+
char paths[PHP_XFILE_FILE_COUNT][512];
5265+
cbm_file_info_t files[PHP_XFILE_FILE_COUNT];
5266+
memset(files, 0, sizeof(files));
5267+
static const char foo_use[] = "use Vendor\\Pkg\\Client;\n";
5268+
for (int i = 0; i < PHP_XFILE_FILE_COUNT; i++) {
5269+
snprintf(paths[i], sizeof(paths[i]), "%s/%s", tmpdir, k_php_xfile_files[i].rel);
5270+
char dir[512];
5271+
snprintf(dir, sizeof(dir), "%s", paths[i]);
5272+
*strrchr(dir, '/') = '\0';
5273+
cbm_mkdir_p(dir, 0755);
5274+
char src[2048];
5275+
const char *s = k_php_xfile_files[i].src;
5276+
const char *at = extra_use ? strstr(s, foo_use) : NULL;
5277+
if (at) {
5278+
size_t head = (size_t)(at - s) + sizeof(foo_use) - 1;
5279+
snprintf(src, sizeof(src), "%.*s%s%s", (int)head, s, extra_use, s + head);
5280+
s = src;
5281+
}
5282+
if (th_write_file(paths[i], s) != 0) {
5283+
th_rmtree(tmpdir);
5284+
return -1;
5285+
}
5286+
files[i].path = paths[i];
5287+
files[i].rel_path = (char *)k_php_xfile_files[i].rel;
5288+
files[i].language = CBM_LANG_PHP;
5289+
}
5290+
5291+
cbm_gbuf_t *seq = run_sequential_with_lsp_cross_and_mutator(
5292+
"cbm_par_phpxfile", tmpdir, files, PHP_XFILE_FILE_COUNT, NULL, NULL, true);
5293+
cbm_gbuf_t *par = run_parallel_with_extract_opts_and_mutator(
5294+
"cbm_par_phpxfile", tmpdir, files, PHP_XFILE_FILE_COUNT, 2, NULL, NULL, NULL, true);
5295+
int seq_ok = seq ? php_xfile_graph_ok(seq, "sequential") : 0;
5296+
int par_ok = par ? php_xfile_graph_ok(par, "parallel") : 0;
5297+
cbm_gbuf_free(seq);
5298+
cbm_gbuf_free(par);
5299+
th_rmtree(tmpdir);
5300+
return seq_ok && par_ok;
5301+
}
5302+
5303+
TEST(parallel_php_lsp_types_imported_project_receivers_issue1186) {
5304+
ASSERT_EQ(php_xfile_run(NULL), 1);
5305+
PASS();
5306+
}
5307+
5308+
/* The same file with one more import that no file in the project declares,
5309+
* the global class `use Exception;` written in most namespaced PHP files.
5310+
* The typed import evidence leaves such a file "unknown" as a whole (#2520),
5311+
* so the class-valued import mapping must not depend on that evidence. */
5312+
TEST(parallel_php_lsp_types_imported_project_receivers_unknown_bindings_issue1186) {
5313+
ASSERT_EQ(php_xfile_run("use Exception;\n"), 1);
5314+
PASS();
5315+
}
5316+
5317+
51515318
/* A fresh PHP external row replaces stale evidence for its exact occurrence,
51525319
* including an older external row, without clearing a real project invocation
51535320
* of the same method at the next occurrence. Test both old-row orders. */
@@ -5261,6 +5428,8 @@ TEST(parallel_php_external_row_replaces_only_its_exact_occurrence) {
52615428
SUITE(parallel) {
52625429
RUN_TEST(parallel_php_external_row_replaces_only_its_exact_occurrence);
52635430
RUN_TEST(parallel_php_vendor_types_never_bind_by_name_issue1186);
5431+
RUN_TEST(parallel_php_lsp_types_imported_project_receivers_issue1186);
5432+
RUN_TEST(parallel_php_lsp_types_imported_project_receivers_unknown_bindings_issue1186);
52645433
RUN_TEST(usage_semantic_reference_candidate_trusts_marked_producer);
52655434
RUN_TEST(lsp_bare_segment_skips_preprocessor_spacing);
52665435
RUN_TEST(lsp_resolve_qualified_static_call_normalizes_colons);

0 commit comments

Comments
 (0)