Skip to content

Commit 0f8b719

Browse files
committed
test(ruby-lsp): exact-name assertions for nested-class rows
The nested-modules row matched caller and callee by substring, which would have let a wrongly-spelled class path pass; it now pins the full extractor QNs on both ends with strcmp, as the compact-class rows already do. Refresh the file headers to describe the scope gates, fail-closed ancestry and self-rebinding rules the resolver now follows. Assisted by OpenCode using anthropic/claude-fable-5-1. Signed-off-by: Ben Fairless <ben@oaf.org.au>
1 parent 8bc99a4 commit 0f8b719

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

‎internal/cbm/lsp/ruby_lsp.c‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,41 @@
1212
* PASS 1 — collect class/module declarations (with lexical nesting),
1313
* superclasses, include/prepend/extend mixins, and method tables
1414
* (instance vs singleton classified from the AST — `def m` vs
15-
* `def self.m` / `class << self`).
15+
* `def self.m` / `class << self`). Defs and mixin calls inside a
16+
* BLOCK (`class_methods do … end`, `included do … end`) belong to
17+
* some other module and are not recorded.
1618
* PASS 1.5 — infer instance-variable types from `@x = Const.new`
17-
* assignments inside method bodies (conflicting types latch to
18-
* "conflicted" and resolve to nothing).
19+
* (and `@x ||= Const.new`) inside method bodies. Any other write
20+
* to the ivar latches it as untypable.
1921
* PASS 2 — walk method bodies, track local variable types through
20-
* assignments, and resolve call expressions into CBMResolvedCall
21-
* edges via Ruby method lookup (prepends → own → includes →
22-
* superclass chain; extends feed the singleton side).
22+
* assignments with Ruby's scope gates (fresh scope per def/class,
23+
* chained scope per block, parameters unknown, untyped writes
24+
* rebind to unknown), and resolve call expressions into
25+
* CBMResolvedCall edges via Ruby method lookup (prepends → own →
26+
* includes → superclass chain; extends feed the singleton side).
27+
* The walk fails closed the moment an ancestor is unresolved.
2328
*
2429
* Verified tree-sitter-ruby node/field names (vendored compiled grammar at
2530
* internal/cbm/vendored/grammars/ruby/parser.c — ts_symbol_names and
2631
* ts_field_names tables):
2732
* - call : fields `receiver`, `method`, `arguments`, `block`. Both
28-
* `foo(x)` and command calls (`foo x`) surface as "call" nodes.
33+
* `foo(x)` and command calls (`foo x`) surface as "call" nodes. A lone
34+
* `foo` is an `identifier`, never a call — and the extractor emits no
35+
* call for it either, so the resolver does not treat it as one.
2936
* - class : fields `name` (constant | scope_resolution), `superclass`
3037
* (a `superclass` wrapper node), `body` (body_statement).
3138
* - module : fields `name`, `body`.
3239
* - method : fields `name`, `parameters`, `body`.
3340
* - singleton_method : fields `object` (self | constant), `name`, `body`.
3441
* - singleton_class : `class << self` — fields `value`, body children.
35-
* - assignment : fields `left`, `right`.
42+
* - assignment : fields `left`, `right` (`left` may be a
43+
* left_assignment_list for `a, b = …`).
44+
* - operator_assignment : fields `left`, `operator`, `right` (`||=` etc.).
45+
* - block / do_block / lambda : field `parameters` (block_parameters /
46+
* lambda_parameters), body.
47+
* - method_parameters children: identifier | optional_parameter |
48+
* keyword_parameter | splat_parameter | hash_splat_parameter |
49+
* block_parameter (each with a `name` field) | destructured_parameter.
3650
* - scope_resolution : fields `scope` (may be absent for ::Foo), `name`.
3751
* - leaves: constant, identifier, self, instance_variable, super,
3852
* string, array, hash, integer, float, simple_symbol, regex.
@@ -50,9 +64,17 @@
5064
* the edge lands on the Class node, exactly like Python's lsp_constructor.
5165
*
5266
* Zero-edge guarantee: if a receiver's type is unknown/unindexed, NO edge
53-
* is emitted (false edges are worse than missing edges). Dynamic dispatch
54-
* (`send`, `public_send`, `method_missing`, `define_method`) is
55-
* intentionally ignored.
67+
* is emitted (false edges are worse than missing edges). Concretely:
68+
* - dynamic dispatch (`send`, `public_send`, `__send__`, `method_missing`,
69+
* `define_method`, the eval/exec family) is never guessed through;
70+
* - a block that rebinds `self` (instance_eval/exec, class_eval/exec,
71+
* module_eval/exec, define_method, Class.new/Module.new) suppresses bare,
72+
* `self.` and `super` dispatch inside it;
73+
* - an unresolved prepend blocks even an own-method hit, an unresolved
74+
* include/extend/superclass stops the walk, and a blocked walk never
75+
* falls through to a same-named top-level def or the Object fallback;
76+
* - a local or ivar written with anything the resolver cannot type loses
77+
* its type rather than keeping a stale one.
5678
*/
5779

5880
#include "ruby_lsp.h"

‎tests/test_ruby_lsp.c‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616
* 10. Instance-variable typing (@thing = Foo.new; @thing.bar)
1717
* 11. Chained call typing (Foo.new.bar)
1818
* 12. Nested modules (lexical nesting + `A::B` references)
19+
* 12b. Compact `class A::B` names (exact extractor spelling, scope)
1920
* 13. ActiveRecord model typing (User.find(1).full_name)
2021
* 14. Top-level function calls
2122
* 15. Unresolvable receiver emits NO spurious edge (negative)
23+
* 15b. Scope gates (def, params, blocks, reassignment)
2224
* 16. send() / the whole dynamic-dispatch family emits NO edge (negative)
25+
* 16c. Blocks that rebind self emit NO self edge (negative)
26+
* Fail-closed ancestry (7c) and block-nested defs (7d) sit with mixins.
2327
*
2428
* Scope: every row here is SINGLE-FILE, driven through cbm_extract_file so it
2529
* exercises the per-file resolver exactly as the extraction pipeline calls it.
2630
* Cross-file resolution (cbm_run_ruby_lsp_cross, wired through
2731
* pass_lsp_cross.c) is a separate tier and is covered by its own rows.
2832
*
2933
* The resolver populates result->resolved_calls with CBMResolvedCall rows.
30-
* Ruby defs weave the class path into the QN (module_qn.Animal.speak), so
31-
* for these single-file fixtures ("test"/"main.rb" -> module QN test.main)
32-
* callee fragments like "main.Animal.speak" are unique join keys.
34+
* The pipeline joins a row to its caller def by EXACT QN equality, so every
35+
* row that pins a class-path spelling (nested or compact) asserts the full
36+
* caller and callee QN with strcmp ("test"/"main.rb" -> module QN test.main).
37+
* Substring helpers remain for rows where only presence/absence matters.
3338
*/
3439
#include "test_framework.h"
3540
#include "cbm.h"
@@ -832,11 +837,18 @@ TEST(rubylsp_nested_modules) {
832837
"end\n";
833838
CBMFileResult *r = extract_ruby(src);
834839
ASSERT(r);
840+
/* Exact spellings: lexically nested declarations join with '.', matching
841+
* the extractor's `<enclosing_qn>.<name>` — a substring match would let
842+
* a wrongly-spelled row pass. */
835843
/* Lexical reference from sibling class inside the module. */
836-
ASSERT(require_resolved(r, "Panel.show", "User.lookup") >= 0);
837-
ASSERT(require_resolved(r, "Panel.show", "User.name") >= 0);
844+
ASSERT(require_resolved_exact(r, "test.main.Admin.Panel.show", "test.main.Admin.User.lookup") >=
845+
0);
846+
ASSERT(require_resolved_exact(r, "test.main.Admin.Panel.show", "test.main.Admin.User") >= 0);
847+
ASSERT(require_resolved_exact(r, "test.main.Admin.Panel.show", "test.main.Admin.User.name") >=
848+
0);
838849
/* Fully-qualified reference from outside. */
839-
ASSERT(require_resolved(r, "Outside.probe", "User.lookup") >= 0);
850+
ASSERT(require_resolved_exact(r, "test.main.Outside.probe", "test.main.Admin.User.lookup") >=
851+
0);
840852
cbm_free_result(r);
841853
PASS();
842854
}

0 commit comments

Comments
 (0)