Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions internal/cbm/cbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,20 @@ typedef struct {
bool is_default; // ES default import (`import X from "Y"`), JS/TS only (#1916)
bool type_checking_only; // Python: inside a module-level `if TYPE_CHECKING:` block --
// binds the name for annotations, never at runtime (#1277)
bool function_scope; // Python: inside a def body -- binds the name in that
// function's scope only, never in the module's (#2285)
CBMImportKind kind; // CBM_IMPORT_KIND_DEFAULT unless the syntax names a member kind
} CBMImport;

/* True when an import binds its local name at runtime. A Python import under
* `if TYPE_CHECKING:` exists for annotations only: a call through that name
* cannot happen, so no CALLS import map may bind it (an import_map edge
/* True when an import binds its local name in the module scope at runtime. A
* Python import under `if TYPE_CHECKING:` exists for annotations only: a call
* through that name cannot happen. A Python import inside a def binds a local
* of that function only: a call elsewhere in the file never goes through it.
* Either way no file-wide CALLS import map may bind it (an import_map edge
* through it would be fabricated by construction). THE one rule every CALLS
* import-map builder applies. */
* import-map builder applies; the Python LSP binds both where they stand. */
static inline bool cbm_import_binds_runtime_name(const CBMImport *imp) {
return imp && !imp->type_checking_only;
return imp && !imp->type_checking_only && !imp->function_scope;
}

/* Content-only ordered Python namespace facts; no resolved target identities.
Expand Down
4 changes: 3 additions & 1 deletion internal/cbm/extract_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -4116,7 +4116,9 @@ static bool python_receiver_is_exempt(CBMExtractCtx *ctx, TSNode receiver) {
* number (tens), never the corpus, so this stays O(file), not O(corpus). */
for (int i = 0; i < ctx->result->imports.count; i++) {
const char *local_name = ctx->result->imports.items[i].local_name;
if (local_name && strcmp(local_name, name) == 0) {
/* A def's own import (#2285) binds no root elsewhere in the file. */
if (local_name && !ctx->result->imports.items[i].function_scope &&
strcmp(local_name, name) == 0) {
return true;
}
}
Expand Down
46 changes: 46 additions & 0 deletions internal/cbm/extract_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,51 @@ static void process_py_type_checking_block(CBMExtractCtx *ctx, TSNode if_node) {
py_drop_rebound_imports(&ctx->result->imports, first, mark, star);
}

/* An import inside a def body binds a local of that function (#2285): `def f():
* from pkg.store import Store; Store().load()`. Record every such import, at
* any depth of the body (try/if/with, nested defs), flagged function_scope:
* no module-scope import map binds it, and the Python LSP binds it where the
* statement stands. A local name a module-level import binds as well stays
* unrecorded: every per-name consumer would conflate the two bindings, and
* the LSP then binds the function's own import as UNKNOWN (fail closed). An
* iterative walk: adversarially deep nesting must not exhaust the stack. */
static void py_record_function_imports(CBMExtractCtx *ctx) {
CBMImportArray *imports = &ctx->result->imports;
int module_count = imports->count;
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
for (bool walking = true; walking;) {
TSNode node = ts_tree_cursor_current_node(&cursor);
bool is_def = strcmp(ts_node_type(node), "function_definition") == 0;
if (is_def) {
TSNode body = ts_node_child_by_field_name(node, TS_FIELD("body"));
if (!ts_node_is_null(body)) {
(void)py_record_subtree_imports(ctx, body);
}
}
if (!is_def && ts_tree_cursor_goto_first_child(&cursor)) {
continue;
}
while (walking && !ts_tree_cursor_goto_next_sibling(&cursor)) {
walking = ts_tree_cursor_goto_parent(&cursor);
}
}
ts_tree_cursor_delete(&cursor);
int keep = module_count;
for (int i = module_count; i < imports->count; i++) {
CBMImport imp = imports->items[i];
bool module_bound = false;
for (int j = 0; j < module_count && !module_bound; j++) {
module_bound = imp.local_name && imports->items[j].local_name &&
strcmp(imp.local_name, imports->items[j].local_name) == 0;
}
if (!module_bound) {
imp.function_scope = true;
imports->items[keep++] = imp;
}
}
imports->count = keep;
}

static void parse_python_imports(CBMExtractCtx *ctx) {
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
if (!ts_tree_cursor_goto_first_child(&cursor)) {
Expand All @@ -481,6 +526,7 @@ static void parse_python_imports(CBMExtractCtx *ctx) {
}
} while (ts_tree_cursor_goto_next_sibling(&cursor));
ts_tree_cursor_delete(&cursor);
py_record_function_imports(ctx);
}

/* A proven same-module function (its OWN_DEF fact and definition), and
Expand Down
31 changes: 29 additions & 2 deletions internal/cbm/lsp/py_lsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node) {
static const CBMType *py_eval_expr_type(PyLSPContext *ctx, TSNode node);
static const CBMType *py_eval_expr_type_uncached(PyLSPContext *ctx, TSNode node);
static void py_process_statement(PyLSPContext *ctx, TSNode node);
static void py_replay_import_statement(PyLSPContext *ctx, TSNode stmt, unsigned char *consumed);
static void py_exec_class_definition(PyLSPContext *ctx, TSNode class_node);
static void py_invalidate_possible_bindings(PyLSPContext *ctx, TSNode node, int depth);
static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const char *type_qn,
Expand Down Expand Up @@ -409,23 +410,28 @@ void py_lsp_add_import(PyLSPContext *ctx, const char *local_name, const char *mo
sizeof(const char *));
const char **qns = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(capacity + 1) *
sizeof(const char *));
const char **sources =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(const char *));
unsigned char *kinds =
(unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(unsigned char));
if (!names || !qns || !kinds)
if (!names || !qns || !sources || !kinds)
return;
for (int i = 0; i < ctx->import_count; i++) {
CBM_LSP_WORK(1);
names[i] = ctx->import_local_names[i];
qns[i] = ctx->import_module_qns[i];
sources[i] = ctx->import_source_qns[i];
kinds[i] = ctx->import_kinds ? ctx->import_kinds[i] : 0;
}
ctx->import_local_names = names;
ctx->import_module_qns = qns;
ctx->import_source_qns = sources;
ctx->import_kinds = kinds;
ctx->import_capacity = capacity;
}
ctx->import_local_names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
ctx->import_module_qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
ctx->import_source_qns[ctx->import_count] = ctx->import_module_qns[ctx->import_count];
ctx->import_kinds[ctx->import_count] = 0;
ctx->import_local_names[new_count] = NULL;
ctx->import_module_qns[new_count] = NULL;
Expand Down Expand Up @@ -2995,6 +3001,24 @@ static void py_process_statement(PyLSPContext *ctx, TSNode node) {
return;
const char *k = ts_node_type(node);

/* An import inside a def binds in the current scope, where it stands
* (#2285); the module-level replay never reaches it. Extraction records it
* flagged function_scope, so it reaches this walk through the LSP import
* map only. Several defs may import one name: each statement matches the
* entries afresh instead of consuming one. Unmatched, it binds UNKNOWN. */
if (strcmp(k, "import_statement") == 0 || strcmp(k, "import_from_statement") == 0) {
if (ctx->import_count > ctx->def_import_consumed_cap) {
ctx->def_import_consumed =
(unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)ctx->import_count);
ctx->def_import_consumed_cap = ctx->def_import_consumed ? ctx->import_count : 0;
}
unsigned char *fresh = ctx->import_count > 0 ? ctx->def_import_consumed : NULL;
if (fresh)
memset(fresh, 0, (size_t)ctx->import_count);
py_replay_import_statement(ctx, node, fresh);
return;
}

if (strcmp(k, "assignment") == 0) {
TSNode left = ts_node_child_by_field_name(node, "left", 4);
TSNode right = ts_node_child_by_field_name(node, "right", 5);
Expand Down Expand Up @@ -5854,7 +5878,10 @@ static void py_replay_import_local(PyLSPContext *ctx, TSNode stmt, const char *l
!ctx->import_module_qns[i] || strcmp(ctx->import_local_names[i], local) != 0) {
continue;
}
const char *candidate_qn = ctx->import_module_qns[i];
/* Match the path the row was added with: an earlier replay may have
* canonicalized import_module_qns[i] to a re-export target, which the
* same statement in another def no longer spells (#2285). */
const char *candidate_qn = ctx->import_source_qns[i];
PyDirectImportKind kind = py_import_kind_from_statement(ctx, stmt, local, &candidate_qn);
if (!py_replayable_import_kind(kind))
continue;
Expand Down
8 changes: 8 additions & 0 deletions internal/cbm/lsp/py_lsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ typedef struct {
// Built from CBMFileResult.imports.
const char **import_local_names;
const char **import_module_qns;
// The module_qn each row was added with, never rewritten: an import
// statement is matched against it, while import_module_qns carries the
// canonical QN a replay bound (a re-export target, #2285).
const char **import_source_qns;
unsigned char *import_kinds; // internal PyDirectImportKind, classified once from the AST
// A def's import statement matches the rows afresh: their consumed flags,
// cleared per statement (py_process_statement), grown with import_count.
unsigned char *def_import_consumed;
int def_import_consumed_cap;
int import_count;
int import_capacity; // rows the three arrays hold (py_lsp_add_import)
CBMNameRows import_rows; // import_local_names by name, rows in table order
Expand Down
8 changes: 6 additions & 2 deletions src/pipeline/pass_semantic.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,

for (int i = 0; i < result->imports.count; i++) {
const CBMImport *imp = &result->imports.items[i];
if (!imp->local_name || !imp->local_name[0] || !imp->module_path) {
/* A def's own import (#2285) binds no file-wide name: the parallel
* driver's map (cbm_import_binds_runtime_name) skips it as well. */
if (!imp->local_name || !imp->local_name[0] || !imp->module_path ||
imp->function_scope) {
continue;
}
char *target_qn = cbm_pipeline_fqn_module(ctx->project_name, imp->module_path);
Expand All @@ -120,7 +123,8 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
return 0;
}

/* Slow path: scan graph buffer IMPORTS edges + parse JSON properties */
/* Slow path: scan graph buffer IMPORTS edges + parse JSON properties. Only a
* file with no extracted import reaches it, so no def's own import does. */
char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel_path, "__file__");
const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
free(file_qn);
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,9 @@ bool cbm_suppress_external_import_shadow(const char *callee_name, const char *st
if (!imp->local_name || !imp->module_path || strcmp(imp->local_name, callee_name) != 0) {
continue;
}
if (imp->function_scope) {
continue; /* a def's own import never enters the file-wide import map */
}
if (specifier_is_relative(imp->module_path) ||
specifier_names_indexed_package(indexed_packages, imp->module_path) ||
specifier_names_declared_package(declared_packages, imp->module_path)) {
Expand Down
45 changes: 45 additions & 0 deletions tests/test_extraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,50 @@ TEST(python_type_checking_rebound_in_else_unrecorded_issue1277) {
PASS();
}

/* #2285: an import inside a def, at any depth of its body, is recorded flagged
* function_scope; a module-level import is not. A def's import of a name the
* module binds as well stays unrecorded (the module-level one remains), and a
* module-level compound statement's import stays unrecorded as before. */
TEST(python_function_local_imports_flagged_issue2285) {
const char *src = "from pkg.other import Other\n"
"def use_local():\n"
" from pkg.store import Store\n"
" return Store().load_local(1)\n"
"class Holder:\n"
" def method(self):\n"
" try:\n"
" import pkg.sub.lib as lib\n"
" except ImportError:\n"
" return None\n"
" def inner():\n"
" from pkg.deep import Deep\n"
" return lib\n"
"def shadowed():\n"
" from pkg.store import Store as Other\n"
"if FLAG:\n"
" from pkg.flag import Conditional\n";
CBMFileResult *r = extract(src, CBM_LANG_PYTHON, "t", "use.py");
ASSERT_NOT_NULL(r);
const CBMImport *other = py_find_import(r, "Other");
const CBMImport *store = py_find_import(r, "Store");
const CBMImport *lib = py_find_import(r, "lib");
const CBMImport *deep = py_find_import(r, "Deep");
ASSERT_NOT_NULL(other);
ASSERT_FALSE(other->function_scope);
ASSERT_STR_EQ(other->module_path, "pkg.other.Other"); /* the def's alias is unrecorded */
ASSERT_NOT_NULL(store);
ASSERT_TRUE(store->function_scope);
ASSERT_FALSE(cbm_import_binds_runtime_name(store));
ASSERT_NOT_NULL(lib);
ASSERT_TRUE(lib->function_scope);
ASSERT_NOT_NULL(deep);
ASSERT_TRUE(deep->function_scope);
ASSERT_NULL(py_find_import(r, "Conditional"));
ASSERT_EQ(r->imports.count, 4);
cbm_free_result(r);
PASS();
}

TEST(python_function) {
CBMFileResult *r = extract(
"def greet(name):\n return f\"Hello {name}\"\n\ndef main():\n greet(\"World\")\n",
Expand Down Expand Up @@ -15818,6 +15862,7 @@ SUITE(extraction) {
RUN_TEST(python_annotated_instance_fields_exported_issue1277);
RUN_TEST(python_type_checking_imports_flagged_issue1277);
RUN_TEST(python_type_checking_rebound_in_else_unrecorded_issue1277);
RUN_TEST(python_function_local_imports_flagged_issue2285);
RUN_TEST(js_function);
RUN_TEST(js_class);
RUN_TEST(ts_function);
Expand Down
Loading
Loading