|
44 | 44 | static _Thread_local int py_test_memo_allocations_left = -1; |
45 | 45 | static _Thread_local bool py_test_depth_failure; |
46 | 46 | static _Thread_local int py_test_scope_allocations_left = -1; |
| 47 | +static _Thread_local int py_test_class_frames_left = -1; |
| 48 | +void cbm_py_lsp_test_class_frame_fail_after(int successful_allocations) { |
| 49 | + py_test_class_frames_left = successful_allocations; |
| 50 | +} |
47 | 51 | void cbm_py_lsp_test_scope_fail_after(int successful_allocations) { |
48 | 52 | py_test_scope_allocations_left = successful_allocations; |
49 | 53 | } |
@@ -227,6 +231,7 @@ static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node) { |
227 | 231 | static const CBMType *py_eval_expr_type(PyLSPContext *ctx, TSNode node); |
228 | 232 | static const CBMType *py_eval_expr_type_uncached(PyLSPContext *ctx, TSNode node); |
229 | 233 | static void py_process_statement(PyLSPContext *ctx, TSNode node); |
| 234 | +static void py_exec_class_definition(PyLSPContext *ctx, TSNode class_node); |
230 | 235 | static void py_invalidate_possible_bindings(PyLSPContext *ctx, TSNode node, int depth); |
231 | 236 | static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const char *type_qn, |
232 | 237 | const char *member_name); |
@@ -288,6 +293,25 @@ static CBMScope *py_scope_push_checked(PyLSPContext *ctx) { |
288 | 293 | return child; |
289 | 294 | } |
290 | 295 |
|
| 296 | +/* New class execution frames must never fall back to their parent: body |
| 297 | + * bindings would escape and subsequent calls could use those stale bindings. */ |
| 298 | +static CBMScope *py_class_frame_push(PyLSPContext *ctx, CBMScope *parent) { |
| 299 | +#ifdef CBM_ENABLE_TEST_SEAMS |
| 300 | + if (py_test_class_frames_left == 0) { |
| 301 | + ctx->eval_failure = CBM_LSP_SCOPE_FAILED; |
| 302 | + return NULL; |
| 303 | + } |
| 304 | + if (py_test_class_frames_left > 0) |
| 305 | + py_test_class_frames_left--; |
| 306 | +#endif |
| 307 | + CBMScope *frame = cbm_scope_push(ctx->arena, parent); |
| 308 | + if (!frame || frame == parent) { |
| 309 | + ctx->eval_failure = CBM_LSP_SCOPE_FAILED; |
| 310 | + return NULL; |
| 311 | + } |
| 312 | + return frame; |
| 313 | +} |
| 314 | + |
291 | 315 | static void py_scope_clear_callable(PyLSPContext *ctx, const char *name) { |
292 | 316 | if (cbm_scope_update_callable(ctx->current_scope, name, NULL)) { |
293 | 317 | ctx->type_cache_gen++; |
@@ -2565,13 +2589,16 @@ static void py_process_statement(PyLSPContext *ctx, TSNode node) { |
2565 | 2589 | } else { |
2566 | 2590 | py_scope_bind(ctx, name, rhs_type); |
2567 | 2591 | } |
| 2592 | + // Class attributes must not replace file-wide dispatch entries. |
2568 | 2593 | // Lambda registry: `fn = lambda x: ...`. |
2569 | | - if (!ts_node_is_null(right) && strcmp(ts_node_type(right), "lambda") == 0) { |
| 2594 | + if (!ctx->class_body_outer_scope && !ts_node_is_null(right) && |
| 2595 | + strcmp(ts_node_type(right), "lambda") == 0) { |
2570 | 2596 | py_register_lambda(ctx, name, right); |
2571 | 2597 | } |
2572 | 2598 | // Dict literal dispatch table: `funcs = {"a": foo, "b": bar}` |
2573 | 2599 | // with all values being known function QNs. |
2574 | | - if (!ts_node_is_null(right) && strcmp(ts_node_type(right), "dictionary") == 0) { |
| 2600 | + if (!ctx->class_body_outer_scope && !ts_node_is_null(right) && |
| 2601 | + strcmp(ts_node_type(right), "dictionary") == 0) { |
2575 | 2602 | py_register_dict_literal(ctx, name, right); |
2576 | 2603 | } |
2577 | 2604 | } |
@@ -2779,7 +2806,11 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) { |
2779 | 2806 | // Walk the lambda body for any nested call sites with the params |
2780 | 2807 | // bound to the call's arg types. Resolved calls get caller_qn |
2781 | 2808 | // set to a synthetic <lambda> child of the enclosing function. |
2782 | | - TSNode lambda_node = py_lookup_lambda(ctx, fname); |
| 2809 | + /* These legacy entries have no lexical frame identity. During class |
| 2810 | + * execution, leave lambda replay unresolved instead of bypassing a |
| 2811 | + * local shadow or running the lambda against class locals. */ |
| 2812 | + TSNode lambda_node = |
| 2813 | + ctx->class_body_outer_scope ? (TSNode){0} : py_lookup_lambda(ctx, fname); |
2783 | 2814 | if (!ts_node_is_null(lambda_node)) { |
2784 | 2815 | TSNode params = ts_node_child_by_field_name(lambda_node, "parameters", 10); |
2785 | 2816 | TSNode body = ts_node_child_by_field_name(lambda_node, "body", 4); |
@@ -2890,7 +2921,7 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) { |
2890 | 2921 |
|
2891 | 2922 | // Subscript-as-call: `funcs["a"]()` where funcs is a registered |
2892 | 2923 | // dict-literal dispatch table. |
2893 | | - if (strcmp(fk, "subscript") == 0) { |
| 2924 | + if (!ctx->class_body_outer_scope && strcmp(fk, "subscript") == 0) { |
2894 | 2925 | TSNode container = ts_node_child_by_field_name(fn, "value", 5); |
2895 | 2926 | TSNode key = ts_node_child_by_field_name(fn, "subscript", 9); |
2896 | 2927 | if (!ts_node_is_null(container) && !ts_node_is_null(key) && |
@@ -3465,6 +3496,65 @@ static void py_emit_dunder_call(PyLSPContext *ctx, const CBMType *recv, const ch |
3465 | 3496 | } |
3466 | 3497 | } |
3467 | 3498 |
|
| 3499 | +/* A class comprehension evaluates its leftmost iterable in the class frame, |
| 3500 | + * but the implicit body scope skips class locals. Nested comprehensions keep |
| 3501 | + * the surrounding comprehension frame. Keep class execution marked active so |
| 3502 | + * legacy bare-name dispatch cannot bypass either frame's lexical bindings. */ |
| 3503 | +static void py_resolve_class_comprehension(PyLSPContext *ctx, TSNode node) { |
| 3504 | + if (ctx->eval_failure) |
| 3505 | + return; |
| 3506 | + PyKids kids = py_kids(ctx->arena, node); |
| 3507 | + uint32_t first_for = kids.n; |
| 3508 | + TSNode first_right = {0}; |
| 3509 | + for (uint32_t i = 0; i < kids.n && !ctx->eval_failure; i++) { |
| 3510 | + TSNode child = py_kid(&kids, i); |
| 3511 | + if (strcmp(ts_node_type(child), "for_in_clause") == 0) { |
| 3512 | + first_for = i; |
| 3513 | + first_right = ts_node_child_by_field_name(child, "right", 5); |
| 3514 | + break; |
| 3515 | + } |
| 3516 | + } |
| 3517 | + if (first_for == kids.n) |
| 3518 | + return; |
| 3519 | + const CBMType *first_type = py_eval_expr_type(ctx, first_right); |
| 3520 | + py_resolve_calls_in(ctx, first_right); |
| 3521 | + if (ctx->eval_failure) |
| 3522 | + return; |
| 3523 | + const CBMType *first_elem = py_iterable_element_type(ctx, first_type); |
| 3524 | + CBMScope *saved_scope = ctx->current_scope; |
| 3525 | + bool saved_comprehension = ctx->class_body_in_comprehension; |
| 3526 | + CBMScope *parent = saved_comprehension ? saved_scope : ctx->class_body_outer_scope; |
| 3527 | + CBMScope *frame = py_class_frame_push(ctx, parent); |
| 3528 | + if (!frame) |
| 3529 | + return; |
| 3530 | + py_scope_restore(ctx, frame); |
| 3531 | + ctx->class_body_in_comprehension = true; |
| 3532 | + /* Clauses precede the body at evaluation time, although tree-sitter stores |
| 3533 | + * the body first. Walk each iterable/filter only once, before later binds. */ |
| 3534 | + for (uint32_t i = first_for; i < kids.n && !ctx->eval_failure; i++) { |
| 3535 | + TSNode child = py_kid(&kids, i); |
| 3536 | + if (strcmp(ts_node_type(child), "for_in_clause") == 0) { |
| 3537 | + TSNode left = ts_node_child_by_field_name(child, "left", 4); |
| 3538 | + const CBMType *elem = first_elem; |
| 3539 | + if (i != first_for) { |
| 3540 | + TSNode right = ts_node_child_by_field_name(child, "right", 5); |
| 3541 | + const CBMType *iter = py_eval_expr_type(ctx, right); |
| 3542 | + py_resolve_calls_in(ctx, right); |
| 3543 | + if (ctx->eval_failure) |
| 3544 | + break; |
| 3545 | + elem = py_iterable_element_type(ctx, iter); |
| 3546 | + } |
| 3547 | + py_bind_for_target(ctx, left, elem); |
| 3548 | + } else { |
| 3549 | + py_resolve_calls_in(ctx, child); |
| 3550 | + } |
| 3551 | + } |
| 3552 | + for (uint32_t i = 0; i < first_for && !ctx->eval_failure; i++) |
| 3553 | + py_resolve_calls_in(ctx, py_kid(&kids, i)); |
| 3554 | + ctx->class_body_in_comprehension = saved_comprehension; |
| 3555 | + py_scope_restore(ctx, saved_scope); |
| 3556 | +} |
| 3557 | + |
3468 | 3558 | static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) { |
3469 | 3559 | if (!ctx || ts_node_is_null(node)) |
3470 | 3560 | return; |
@@ -3667,6 +3757,10 @@ static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) { |
3667 | 3757 | // iterable's element type, then walk inner expressions. |
3668 | 3758 | if (strcmp(k, "list_comprehension") == 0 || strcmp(k, "dictionary_comprehension") == 0 || |
3669 | 3759 | strcmp(k, "set_comprehension") == 0 || strcmp(k, "generator_expression") == 0) { |
| 3760 | + if (ctx->class_body_outer_scope) { |
| 3761 | + py_resolve_class_comprehension(ctx, node); |
| 3762 | + return; |
| 3763 | + } |
3670 | 3764 | CBMScope *saved = ctx->current_scope; |
3671 | 3765 | ctx->current_scope = py_scope_push_checked(ctx); |
3672 | 3766 | uint32_t cnc = ts_node_named_child_count(node); |
@@ -3699,15 +3793,19 @@ static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) { |
3699 | 3793 | // Recurse: children. We don't push scope for control-flow blocks |
3700 | 3794 | // here (Python scoping is function-level apart from comprehension / |
3701 | 3795 | // lambda / class), but we do for nested function / class / lambda. |
3702 | | - if (strcmp(k, "function_definition") == 0 || strcmp(k, "class_definition") == 0 || |
3703 | | - strcmp(k, "lambda") == 0) { |
| 3796 | + if (strcmp(k, "function_definition") == 0 || strcmp(k, "lambda") == 0) { |
3704 | 3797 | // These are processed by the top-level pass; skip recursion to |
3705 | 3798 | // avoid double-walking their bodies. |
3706 | 3799 | return; |
3707 | 3800 | } |
3708 | 3801 |
|
| 3802 | + if (strcmp(k, "class_definition") == 0) { |
| 3803 | + py_exec_class_definition(ctx, node); |
| 3804 | + return; |
| 3805 | + } |
| 3806 | + |
3709 | 3807 | PyKids nk = py_kids(ctx->arena, node); |
3710 | | - for (uint32_t i = 0; i < nk.n; i++) { |
| 3808 | + for (uint32_t i = 0; i < nk.n && !ctx->eval_failure; i++) { |
3711 | 3809 | py_resolve_calls_in(ctx, py_kid(&nk, i)); |
3712 | 3810 | } |
3713 | 3811 | } |
@@ -4328,6 +4426,120 @@ static bool py_is_init_method(PyLSPContext *ctx, TSNode func_node) { |
4328 | 4426 | return nm && (strcmp(nm, "__init__") == 0 || strcmp(nm, "__post_init__") == 0); |
4329 | 4427 | } |
4330 | 4428 |
|
| 4429 | +/* Decorator expressions of a decorated definition, in source order. They |
| 4430 | + * evaluate in the scope enclosing the definition, before it is bound. A bare |
| 4431 | + * `@name` is no call and emits nothing; `@name(args)` is one. */ |
| 4432 | +static void py_resolve_decorators(PyLSPContext *ctx, TSNode decorated) { |
| 4433 | + if (!ctx || ctx->eval_failure) |
| 4434 | + return; |
| 4435 | + PyKids kids = py_kids(ctx->arena, decorated); |
| 4436 | + for (uint32_t i = 0; i < kids.n && !ctx->eval_failure; i++) { |
| 4437 | + TSNode child = py_kid(&kids, i); |
| 4438 | + if (strcmp(ts_node_type(child), "decorator") == 0) |
| 4439 | + py_resolve_calls_in(ctx, child); |
| 4440 | + } |
| 4441 | +} |
| 4442 | + |
| 4443 | +/* Bind the name a class-body `def`/`class` statement defines in the class |
| 4444 | + * frame, so later class-body statements see it (and never the same-named |
| 4445 | + * module symbol). Its value is exact only for an undecorated function or a |
| 4446 | + * registered class whose QN is known; anything else is an UNKNOWN shadow. */ |
| 4447 | +static void py_bind_class_body_def(PyLSPContext *ctx, TSNode stmt) { |
| 4448 | + if (ctx->eval_failure) |
| 4449 | + return; |
| 4450 | + TSNode def = stmt; |
| 4451 | + bool decorated = strcmp(ts_node_type(stmt), "decorated_definition") == 0; |
| 4452 | + if (decorated) { |
| 4453 | + def = ts_node_child_by_field_name(stmt, "definition", 10); |
| 4454 | + if (ts_node_is_null(def)) |
| 4455 | + return; |
| 4456 | + } |
| 4457 | + const char *kind = ts_node_type(def); |
| 4458 | + bool is_class = strcmp(kind, "class_definition") == 0; |
| 4459 | + if (!is_class && strcmp(kind, "function_definition") != 0) |
| 4460 | + return; |
| 4461 | + char *name = py_node_text(ctx, ts_node_child_by_field_name(def, "name", 4)); |
| 4462 | + if (!name || !name[0]) { |
| 4463 | + py_disable_callable_value_proof(ctx); |
| 4464 | + return; |
| 4465 | + } |
| 4466 | + const char *qn = ctx->class_body_qn |
| 4467 | + ? cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->class_body_qn, name) |
| 4468 | + : NULL; |
| 4469 | + if (is_class) { |
| 4470 | + const CBMRegisteredType *type = qn ? cbm_registry_lookup_type(ctx->registry, qn) : NULL; |
| 4471 | + py_scope_bind(ctx, name, |
| 4472 | + type ? cbm_type_named(ctx->arena, type->qualified_name) : cbm_type_unknown()); |
| 4473 | + return; |
| 4474 | + } |
| 4475 | + const CBMRegisteredFunc *func = qn ? cbm_registry_lookup_func(ctx->registry, qn) : NULL; |
| 4476 | + if (!decorated && py_func_is_exact_callable_value(func)) { |
| 4477 | + py_scope_bind_callable(ctx, name, cbm_type_unknown(), func->qualified_name); |
| 4478 | + } else { |
| 4479 | + py_scope_bind(ctx, name, cbm_type_unknown()); |
| 4480 | + } |
| 4481 | +} |
| 4482 | + |
| 4483 | +/* Execute a class definition where it stands: its bases in the enclosing |
| 4484 | + * scope, then its body statements in source order in a class frame. Calls |
| 4485 | + * keep the caller the extraction gives them: the enclosing function, or the |
| 4486 | + * module QN at module level (enclosing_func_qn is left unchanged). |
| 4487 | + * |
| 4488 | + * Scoping follows Python: a name bound earlier in the body is visible to |
| 4489 | + * later body statements (and to decorators of methods), but the class frame |
| 4490 | + * is not an enclosing scope for anything nested in it. Method bodies are |
| 4491 | + * resolved later by py_process_class against the module scope, and a nested |
| 4492 | + * class body chains to the nearest enclosing non-class scope. Each body |
| 4493 | + * statement is walked exactly once. */ |
| 4494 | +static void py_exec_class_definition(PyLSPContext *ctx, TSNode class_node) { |
| 4495 | + if (!ctx || ctx->eval_failure || ts_node_is_null(class_node)) |
| 4496 | + return; |
| 4497 | + TSNode supers = ts_node_child_by_field_name(class_node, "superclasses", 12); |
| 4498 | + if (!ts_node_is_null(supers)) |
| 4499 | + py_resolve_calls_in(ctx, supers); |
| 4500 | + TSNode body = ts_node_child_by_field_name(class_node, "body", 4); |
| 4501 | + if (ctx->eval_failure || ts_node_is_null(body)) |
| 4502 | + return; |
| 4503 | + |
| 4504 | + char *cname = py_node_text(ctx, ts_node_child_by_field_name(class_node, "name", 4)); |
| 4505 | + const char *class_qn = NULL; |
| 4506 | + if (cname && cname[0]) { |
| 4507 | + if (ctx->class_body_outer_scope) { |
| 4508 | + class_qn = ctx->class_body_qn |
| 4509 | + ? cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->class_body_qn, cname) |
| 4510 | + : NULL; |
| 4511 | + } else if (ctx->module_qn && ctx->enclosing_func_qn && ctx->file_module_qn && |
| 4512 | + strcmp(ctx->enclosing_func_qn, ctx->file_module_qn) == 0) { |
| 4513 | + class_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname); |
| 4514 | + } |
| 4515 | + } |
| 4516 | + |
| 4517 | + CBMScope *saved_scope = ctx->current_scope; |
| 4518 | + CBMScope *saved_outer = ctx->class_body_outer_scope; |
| 4519 | + const char *saved_qn = ctx->class_body_qn; |
| 4520 | + bool saved_comprehension = ctx->class_body_in_comprehension; |
| 4521 | + CBMScope *outer = saved_outer ? saved_outer : saved_scope; |
| 4522 | + CBMScope *frame = py_class_frame_push(ctx, outer); |
| 4523 | + if (!frame) |
| 4524 | + return; |
| 4525 | + py_scope_restore(ctx, frame); |
| 4526 | + ctx->class_body_outer_scope = outer; |
| 4527 | + ctx->class_body_qn = class_qn; |
| 4528 | + ctx->class_body_in_comprehension = false; |
| 4529 | + |
| 4530 | + PyKids bk = py_kids(ctx->arena, body); |
| 4531 | + for (uint32_t i = 0; i < bk.n && !ctx->eval_failure; i++) { |
| 4532 | + TSNode stmt = py_kid(&bk, i); |
| 4533 | + py_resolve_calls_in(ctx, stmt); |
| 4534 | + py_bind_class_body_def(ctx, stmt); |
| 4535 | + } |
| 4536 | + |
| 4537 | + ctx->class_body_outer_scope = saved_outer; |
| 4538 | + ctx->class_body_qn = saved_qn; |
| 4539 | + ctx->class_body_in_comprehension = saved_comprehension; |
| 4540 | + py_scope_restore(ctx, saved_scope); |
| 4541 | +} |
| 4542 | + |
4331 | 4543 | static void py_process_class(PyLSPContext *ctx, TSNode class_node) { |
4332 | 4544 | if (!ctx || ctx->eval_failure) |
4333 | 4545 | return; |
@@ -4990,15 +5202,21 @@ void py_lsp_process_file(PyLSPContext *ctx, TSNode root) { |
4990 | 5202 | } else if (strcmp(ck, "function_definition") == 0) { |
4991 | 5203 | py_bind_module_function(ctx, c); |
4992 | 5204 | } else if (strcmp(ck, "class_definition") == 0) { |
4993 | | - py_bind_module_class(ctx, c); |
| 5205 | + py_exec_class_definition(ctx, c); |
| 5206 | + if (!ctx->eval_failure) |
| 5207 | + py_bind_module_class(ctx, c); |
4994 | 5208 | } else if (strcmp(ck, "decorated_definition") == 0) { |
| 5209 | + py_resolve_decorators(ctx, c); |
4995 | 5210 | TSNode def = ts_node_child_by_field_name(c, "definition", 10); |
4996 | | - if (!ts_node_is_null(def)) { |
| 5211 | + if (!ctx->eval_failure && !ts_node_is_null(def)) { |
4997 | 5212 | const char *dk = ts_node_type(def); |
4998 | | - if (strcmp(dk, "function_definition") == 0) |
| 5213 | + if (strcmp(dk, "function_definition") == 0) { |
4999 | 5214 | py_bind_module_function(ctx, def); |
5000 | | - else if (strcmp(dk, "class_definition") == 0) |
5001 | | - py_bind_module_class(ctx, def); |
| 5215 | + } else if (strcmp(dk, "class_definition") == 0) { |
| 5216 | + py_exec_class_definition(ctx, def); |
| 5217 | + if (!ctx->eval_failure) |
| 5218 | + py_bind_module_class(ctx, def); |
| 5219 | + } |
5002 | 5220 | } |
5003 | 5221 | } else if (strcmp(ck, "expression_statement") == 0) { |
5004 | 5222 | /* An expression statement is resolved for calls, not scanned as a |
|
0 commit comments