Skip to content

fix(resolution): bind Class.method() to the class itself, not a class whose name contains it - #1935

Merged
colbymchenry merged 13 commits into
colbymchenry:mainfrom
inth3shadows:fix/method-owner-class
Oct 20, 2026
Merged

colbymchenry merged 13 commits into
colbymchenry:mainfrom
inth3shadows:fix/method-owner-class

Conversation

@inth3shadows

@inth3shadows inth3shadows commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

matchMethodCall has two strategies that match on the class: a direct class name, and a capitalized instance receiver. Both found the method with

n.qualifiedName.includes(classNode.name)

That is a substring test, so any class whose name contains the receiver's class name matches. Given

class FileLogger:
    def log(self): ...

class Logger:
    def log(self): ...

def via_class():
    Logger.log()

via_class got a calls edge to FileLogger::log instead of Logger::log. TypeScript (static log() on both classes) and a capitalized instance receiver (logger.log()) behaved the same way. The first matching class declared in the file won.

Fix

A new findOwnMethod(nodesInFile, classNode, methodName) does the lookup:

  1. It tries the class's exact qualified name first: ${classNode.qualifiedName}::${method}.
  2. If that misses, it falls back to the owner rule the resolver already uses elsewhere: T::m, or a qualified name ending in ::T::m.

It never does a substring match. The class name is compared as a whole, so a dotted R class name like Bank.Account still owns its methods.

Tests

__tests__/method-call-owner-class.test.ts:

  • Python class receiver
  • Python capitalized instance receiver (asserts that the edge resolves to Logger::log)
  • TypeScript static call
  • R Bank.Account$deposit() next to a look-alike BankAccount class

The first three fail on main with expected [ 'FileLogger::log' ] to include 'Logger::log'.

I also indexed fixtures in about 25 languages (Kotlin, Go, Rust, C++, C#, Java, Swift, Objective-C, PHP, Ruby, Python, Dart, Scala, TS/JS, Pascal, Solidity, VB.NET, Lua, CFML, R) plus every directory under __tests__/fixtures, with and without this change, and compared call edges. The only difference is the corrected binding.

Full suite on Node 22: 4537 passed. The one remaining failure, ui-server-api.test.ts › the engine's own busiest symbol, fails the same way on unmodified main (expected 46 to be greater than or equal to 500).

Known, not changed here: a same-named nested class can still take a call that names the top-level class, in two ways: the nested class is itself a candidate (so declaring it first is enough), and the owner-by-name fallback also matches Outer::Logger::log. The code before this PR does the same. Both cases are pinned with it.fails tests, placed behind a check that the fixtures index, so a real fix flips them.

Choosing among same-named classes needs each language's scope rules:

  • a Java member class is in scope in its outer class and its subclasses;
  • a Python class body is not a scope;
  • a Scala nested class is a type, not a value.

I tried a lexical-scope ranking. It worked for Java, C#, Kotlin and Swift, but it hit exactly those per-language cases elsewhere, so it's left for a separate change. The test assertions are now file-qualified, because several fixtures share class names.

@danusha2345

Copy link
Copy Markdown
Contributor

Verified on 12 real repos: Kotlin ×3, C, Rust ×2, Go, Python ×2, PHP, C# and playframework (Java + Scala). Each was indexed with main (ba3c21e, this PR's base) and with this PR. The full suite passes (275/275 files, 4721 tests) and node counts are identical everywhere.

Changed edges. 10 of the 12 repos have none. All 50 changed edges in the other two were checked against source:

  • playframework: 16 removed, all false or less precise.
    • Nine went to F::Function3::apply and one to F::LazySupplier::get. The receiver was a java.util.function.Function / Supplier named f, and it matched only because F is a substring of F::Function3.
    • Three pointed at an arbitrary anonymous subclass (<MultipartFormData$anon@…>, <Task$anon@…>) instead of the declared type.
    • The rest were two unrelated getFiles/getHeaders targets and one supertype method where the declared type overrides it.
  • playframework: 6 added, 5 correct (Http::Request::asScala, Http::MultipartFormData::getFiles ×2, asFormUrlEncoded, ConcurrentReferenceHashMap::Task::execute).
    • The sixth is a self-loop, AhcWSRequest.getHeaders → AhcWSRequest.getHeaders, from return request.getHeaders(), where request is an out-of-repo StandaloneAhcWSRequest. Strategies 1–2 no longer match, so resolution falls through to strategy 3's word-overlap fallback, which then scores the caller's own method. That's a pre-existing weakness of strategy 3 that this PR exposes rather than introduces. A follow-up could keep strategy 3 from binding a call to its own caller.
  • A Kotlin app: 14 moved. endpoint.ready() used to reach the interface ControlEndpoint::ready only because ControlEndpoint contains Endpoint, the declared type. It now reaches ControlSessionSupervisor::Endpoint::ready, the only implementation. Neither version invents a call.

Owner-segment edge cases. A fixture exercised them across languages, and both builds produced identical edges:

  • Kotlin: companion objects, named and unnamed, object, and nested classes.
  • C++: ns::Outer::Inner.
  • Java: nested static classes and enums.
  • C#: partial classes in dotted namespaces.
  • Rust: inherent and trait impls.
  • Other languages: Swift extensions, Go pointer receivers, Ruby self. in modules, PHP namespaced statics, a Scala object + class pair, Python nested classes, and TS namespaced/generic classes.

I'd merge it as is.

danusha2345 pushed a commit to danusha2345/codegraph that referenced this pull request Sep 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@inth3shadows

Copy link
Copy Markdown
Contributor Author

Thanks for the verification across 12 repos. The edge-by-edge check on playframework is more than I could have asked for. I reproduced the self-loop you flagged on main as well, with a small fixture. It isn't only self-loops: sibling methods whose receiver is declared as an out-of-repo type get the same false edge. I filed it as #1961.

inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Sep 26, 2026
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Sep 29, 2026
Tree = upstream main + the open PRs that still merge cleanly: colbymchenry#1925, colbymchenry#1957,
colbymchenry#1962, colbymchenry#1955, colbymchenry#1965 and the current heads of colbymchenry#1842 and colbymchenry#1913. Upstream has
already carried colbymchenry#1969-colbymchenry#1971, colbymchenry#1082 and colbymchenry#1791.

Dropped until they are rebased onto upstream's resolver rewrite: colbymchenry#1704,
colbymchenry#1921, colbymchenry#1926 and colbymchenry#1935.
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Sep 30, 2026
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Sep 30, 2026
@inth3shadows
inth3shadows force-pushed the fix/method-owner-class branch from 8dd2153 to 9c911af Compare October 3, 2026 15:54
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Oct 3, 2026
@inth3shadows
inth3shadows force-pushed the fix/method-owner-class branch from 9c911af to 6d15ade Compare October 14, 2026 18:05
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Oct 14, 2026
… whose name contains it

matchMethodCall's class strategies (direct class name and capitalized
instance receiver) picked the method with `qualifiedName.includes(className)`,
so `Logger.log()` bound to `FileLogger::log` whenever FileLogger's method
came first in the file. Match the qualified name's owner segment exactly
(generic arguments and dotted namespaces stripped) instead.
…e owner rule

Review follow-up. The owner-segment helper cut a dotted class name down to its
last segment, so an R class named `Bank.Account` no longer owned its methods
and `Bank.Account$deposit()` fell through to a look-alike class. Replace it with
findOwnMethod: the class's exact qualified name first, then the
`T::m` / `…::T::m` owner rule the resolver already uses elsewhere.

Tests: assert the capitalized-receiver call resolves (not only that it avoids
FileLogger), and cover a dotted R class name.
…tation

findOwnMethod's comment claimed a nested `Outer::Logger` could not take a
top-level `Logger.log()`. It can, two ways: the owner-by-name fallback also
matches `Outer::Logger::log`, and every same-named class is a candidate in
the caller's loop. Choosing among same-named classes needs each language's
scope rules (a Java member class is in scope in its outer class and
subclasses; a Python class body is not a scope; a Scala nested class is a
type), which is its own change. Correct the comment, pin both shapes with
`it.fails` (behind a check that the fixtures index), and make the existing
assertions file-qualified, since several fixtures share class names.
@inth3shadows
inth3shadows force-pushed the fix/method-owner-class branch from 6d15ade to ee0ea0e Compare October 15, 2026 14:50
Keep both CHANGELOG bullets. In the capitalized-receiver strategy,
findOwnMethod replaces main's Go standard-method owner check, which it
covers: the method must be the class's own (`Status::String`) in every
language, not only for Go standard method names.
danusha2345 pushed a commit to danusha2345/codegraph that referenced this pull request Oct 20, 2026
…Rust Self:: and std impls, colbymchenry#2688 synthesis inputs, first foreign merges)

Conflicts: name-matcher takes main's `isGoOutsideImport` family in place of
colbymchenry#1954's `isGoExternalImport`; keeps colbymchenry#1942's Java same-language preference and
colbymchenry#1947's `cls` beside main's `withinCppTranslationUnit`; colbymchenry#1949's Java static
field block and main's Rust `Self::` block both stay; colbymchenry#1935's `findOwnMethod`
gets main's Go standard-method-name condition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry and others added 9 commits October 20, 2026 13:52
The exact-owner rule this PR brings stops a call from landing on a
class whose name merely contains the receiver's, but it also stopped
finding a method the named class inherits: the substring test had been
finding those by luck. On Django, form.is_valid() and form.has_changed()
(34 calls) moved from BaseForm, which Form inherits them from, to an
admin-internal form class some later strategy picked.

Both class strategies now take the class's own method, else the nearest
one a declared supertype has (inheritedClassMethod), as a call on an
instance of the class finds it. A test pins the inherited case with an
unrelated namesake class declared first, which a name test would pick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…m outside it

The class-name and capitalized-receiver strategies, and the method-name
fallback after them, now skip a class (or a method of one) that another
function declares: Django's `file.write(…)` in one test went to the `File`
a serializer test declares in its body. Inside the declaring function the
name is still that class.

Also pins Kotlin companion calls: `Foo.create()` is Foo's own, not that of
a `FooBar` declared first (main's substring rule picked FooBar's).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tory-built Python base names its class

The class-name and capitalized-receiver strategies now skip a test suite's
class when the call is in production code, as the method-name fallback
already did: Django's `manager.check()` in `Model._check_managers` went to
a test app's `Manager` model, through the `check` it inherits from `Model`.

A Python base written as a factory call on a class, Django's
`class Manager(BaseManager.from_queryset(QuerySet))`, now names that class
(`BaseManager`) as the supertype, besides the factory's arguments, so a
manager reaches BaseManager's methods again.

Ruling out a method of a class another function declares no longer counts
as narrowing the method-name fallback's candidates: such a method was never
in the running, and the narrowing dropped `cache.aadd()`'s link to
`BaseCache::aadd`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…y the lone-method fallback

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l a guess by the receiver name

The method-name fallback no longer takes the one method of a name it has
left after ruling out methods of classes other functions declare; that
method must win the receiver-name scoring instead. Django's
`cache.aadd()` keeps `BaseCache::aadd`, while `value.split()` no
longer goes to the one test model's `split`, nor `django.setup()` to
`View::setup`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@colbymchenry
colbymchenry merged commit e043efb into colbymchenry:main Oct 20, 2026
colbymchenry added a commit to inth3shadows/codegraph that referenced this pull request Oct 20, 2026
name-matcher: the method-name fallback's one-method shortcut and lone-
method scoring take both this branch's python attribute chains and
main's out-of-scope local classes (colbymchenry#1935).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
danusha2345 pushed a commit to danusha2345/codegraph that referenced this pull request Oct 21, 2026
…ymchenry#1921, colbymchenry#1935, colbymchenry#1955, colbymchenry#1957, colbymchenry#1965, colbymchenry#2575, colbymchenry#2595, colbymchenry#2597, colbymchenry#2603–colbymchenry#2608, colbymchenry#2611 landed upstream)

Conflicts: files owned by the foreign PRs now in main (colbymchenry#1921's Python import
resolver and tests, colbymchenry#1935's class-strategy method) take main's version; our
colbymchenry#1954/colbymchenry#1942/colbymchenry#1945/colbymchenry#1939/colbymchenry#1715/colbymchenry#1951/colbymchenry#2308 hunks sit beside main's new Rust and
Python blocks as in the rebased PR branches; java.rs keeps the integration's
`constant_declaration` fields under main's initializer walk; three CHANGELOG
lines the merged PRs had already contributed are deduplicated, and the method
blocks the merge duplicated in queries.ts and extraction/index.ts (the
resolution-edge queries and the sync resurrection helpers, present once on
main) are kept once.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 1, 2026
…rents and traits (#3168)

Since #1935 a receiver named after a class took what that class inherits,
walked by bare supertype names. Drupal's `$entity->access()` went to the
views plugin `Entity`, in a namespace the calling file never mentions, and
up three classes to `HandlerBase::access`; Laravel's `$comments->pluck()`
took `Support\Collection::pluck` past the `Eloquent\Collection` the class
imports, and `$pivot->getForeignKey()` took `Model`'s past its trait
`AsPivot`. A typed receiver whose method sat on a supertype found nothing in
the first pass, since no `extends` edge exists yet, and went on to the same
guess.

A class is now found the way PHP finds one: through the calling file's
namespace and `use` imports (group imports included), its parents through
the imports of the file declaring it, past any attributes above it, a
trait before the parent.
- A receiver named after a class the file names reaches all it inherits,
  and that class comes before its namesakes. One the file can't name gives
  only its own method, or what it inherits when it is the one class of that
  name: a word picks no namesake.
- A declared type answers through its source ancestry in the first pass.
  Its name no longer leads to a class named after the variable, and a
  namesake's method is asked only past a parent outside the project or a
  `__call`.
- A Laravel facade hands a static call to the class `getFacadeAccessor()`
  names. A local an Eloquent model's static factory gives is that model.
- `parent::x()` above a parent from outside the project is no trait of the
  project.

Validation, base 4499d00 against this change, every class read and a sample
of each against source (numbers in the PR):
- framework 974cd809: 169,744 to 171,080 edges, 3,209 moved, 1,398 added, 62 lost.
  Moved: typed receivers onto the class their file names (271 `$v->fails()`
  from the Validator contract to the `new Validator`, 142 event dispatches off
  `Bus\Dispatcher`), traits (`make`, `pluck`). 48 of the 62 lost are
  `parent::__construct` above Exception reaching an unrelated trait; 7 were true
  only through a namesake (`$user->getKey()` on a `User` the file can't name).
- drupal-core (REG's sparse copy, no .git): 38,099 to 39,053, 381 moved, 1,066
  added, 112 lost. 52 `HandlerBase` edges go. 10 are left: 9 `$url->access()`
  on the one `Url` left when core/lib/Drupal/Core/Url.php is outside the sparse
  checkout (with that file back they reach `Url::access`), and a `$view->access()`
  inside `class View` itself. 834 `parent::` and
  `$this->` calls link through classes with attributes above them; 353 calls off
  unrelated traits (`BlockPluginTrait::__construct`) move or go.
- bookstack 045c6a98: 6 moved, 7 added. koel da0e70f4: 53 moved, 30 added.
- ospos, ci3, invoiceplane, horizon, controls gin and flask: byte-identical.
- Read against source: 125 sampled entries, 0 false added. Index CPU on framework,
  median of 5 interleaved: 47.9 to 48.1 s (+0.5%).
- REG: all 16 PUB entries for framework and drupal-core now hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 1, 2026
Since #1935 a receiver named after a class took what its supertypes declare,
read by bare names: `class Human < ActiveRecord::Base` asked every class
called `Base`, so rails' `human.name` reached Journey's `Node#name`, and
`Topic.create` Digestor's `Node.create`. A value of a known class walked
`extends` edges the same way, so `company.has_attribute?` took the
`has_attribute?` of a `ClassMethods` module. And `class Mapper; include
HttpHelpers` lost `mapper.get`, which 1.6.2 reached by a substring of the
qualified name.

The ancestors are now read as Ruby reads them: superclass and mixins found
by constant lookup, a class's own body only, the module mixed in last asked
first, and for an instance no module it `extend`s.
- A receiver named after a class takes the class constant lookup finds, and
  past that class's own methods a word picks no namesake.
- A call on a class reaches a superclass's `def self.`, what it `extend`s and
  the `ClassMethods` its concerns bring, never a mixin's instance method or
  a mixin's own `def self.`.
- A bare name that is a method of the class around the call, taking no
  arguments and handing back what one class makes (`@jar ||= Jar.build(…)`),
  types the call on it, so `cookies.signed` reaches `ChainedCookieJars`.
- A `?` or `!` method of Ruby's core classes needs a receiver named after the
  owner itself: sinatra's `bad_cookies.empty?` is no `Cookies::Jar#empty?`.

Validation, base 4499d00 against this change (numbers in the PR):
- rails-src 793d744c: 171,738 to 179,247 edges, 2,159 moved, 8,179 added, 670
  lost. Added: Concern class methods (`Topic.find`), inherited instance methods
  (`topic.id`), helpers on bare calls (`concat`). Moved: to the first method in
  Ruby's order, off `Digestor::Node.create`. 86 sampled entries read: 0 false added.
  Lost: 442 `Node::name`-style false edges of the 670; about 30 were true by luck.
- sinatra cb22afd7: 2 lost, both `bad_cookies.empty?`. mastodon (Ruby only)
  b0555670: 1 moved, 34 added, 1 lost. jekyll 97c3bdc8: same edges, 9 guesses
  now typed. Controls gin and flask byte-identical.
- REG: rails-src 3 lost back; `company.has_attribute?` 14 of 14 on the instance
  method; rails-src/moved/24 (`table.foreign_key`, right by a substring) stays:
  it needs call-site flow, and the substring rule took 429 `error.message`.
- Index CPU on rails-src, median of 5 interleaved: 74.9 to 73.2 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 1, 2026
…3169)

Since #1935 a receiver named after a class took what its supertypes declare,
read by bare names: `class Human < ActiveRecord::Base` asked every class
called `Base`, so rails' `human.name` reached Journey's `Node#name`, and
`Topic.create` Digestor's `Node.create`. A value of a known class walked
`extends` edges the same way, so `company.has_attribute?` took the
`has_attribute?` of a `ClassMethods` module. And `class Mapper; include
HttpHelpers` lost `mapper.get`, which 1.6.2 reached by a substring of the
qualified name.

The ancestors are now read as Ruby reads them: superclass and mixins found
by constant lookup, a class's own body only, the module mixed in last asked
first, and for an instance no module it `extend`s.
- A receiver named after a class takes the class constant lookup finds, and
  past that class's own methods a word picks no namesake.
- A call on a class reaches a superclass's `def self.`, what it `extend`s and
  the `ClassMethods` its concerns bring, never a mixin's instance method or
  a mixin's own `def self.`.
- A bare name that is a method of the class around the call, taking no
  arguments and handing back what one class makes (`@jar ||= Jar.build(…)`),
  types the call on it, so `cookies.signed` reaches `ChainedCookieJars`.
- A `?` or `!` method of Ruby's core classes needs a receiver named after the
  owner itself: sinatra's `bad_cookies.empty?` is no `Cookies::Jar#empty?`.

Validation, base 4499d00 against this change (numbers in the PR):
- rails-src 793d744c: 171,738 to 179,247 edges, 2,159 moved, 8,179 added, 670
  lost. Added: Concern class methods (`Topic.find`), inherited instance methods
  (`topic.id`), helpers on bare calls (`concat`). Moved: to the first method in
  Ruby's order, off `Digestor::Node.create`. 86 sampled entries read: 0 false added.
  Lost: 442 `Node::name`-style false edges of the 670; about 30 were true by luck.
- sinatra cb22afd7: 2 lost, both `bad_cookies.empty?`. mastodon (Ruby only)
  b0555670: 1 moved, 34 added, 1 lost. jekyll 97c3bdc8: same edges, 9 guesses
  now typed. Controls gin and flask byte-identical.
- REG: rails-src 3 lost back; `company.has_attribute?` 14 of 14 on the instance
  method; rails-src/moved/24 (`table.foreign_key`, right by a substring) stays:
  it needs call-site flow, and the substring rule took 429 `error.message`.
- Index CPU on rails-src, median of 5 interleaved: 74.9 to 73.2 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants