Skip to content

ObjC: a @protocol declaration is treated as a receiver type — one fabricated EXTRACTED edge, and a same-named class stops resolving #2589

Closed

Description

@xiongjianxu

Summary

The ObjC member-call resolver treats a @protocol declaration as a candidate type for a message receiver. A protocol is a contract, never a receiver, and ObjC keeps protocol and class names in separate namespaces — so this is wrong in both directions:

  • protocol alone in the corpus → a fabricated calls edge at confidence EXTRACTED / 1.0 pointing into the protocol's method declaration;
  • protocol + a same-named class → two candidates trip the single-definition god-node guard, so a call that resolves fine without the protocol produces no edge at all.

Same-named protocol/class pairs are idiomatic ObjC (@protocol Locking + class Locking, Reloading/Reloadable naming, and every <Foo>/Foo pair in a delegate-heavy codebase), so a real corpus gets both a false edge and a missing edge from the same root cause.

graphifyy 0.9.38 (v8 @ 10ad921), Python 3.12, macOS.

Reproduction

A. Protocol only → fabricated edge

Reload.h

@protocol Reload <NSObject>
- (void)reload;
@end

Widget.h

#import "Reload.h"
@interface Widget : NSObject
- (void)go;
@end

Widget.m

#import "Widget.h"
@implementation Widget
- (void)go { [Reload reload]; }
@end

[Reload reload] is not a real call — Reload is a protocol, it has no +reload. Expected: no edge (nothing to type the receiver to). Actual:

calls  '-go' -> '-reload'  EXTRACTED  L3
       src=..._widget_widget_go
       tgt=..._reload_reload_reload      <- Reload.h, the PROTOCOL's declaration

B. Protocol + same-named class → the real edge disappears

Locking.h

@protocol Locking <NSObject>
- (void)lock;
@end

LockingImpl.h

@interface Locking : NSObject
+ (void)lock;
@end

LockingImpl.m implements +lock; Runner.m calls it:

#import "LockingImpl.h"
@implementation Runner
- (void)run { [Locking lock]; }
@end

Expected -run -> +lock. Actual: zero calls edges. Dumping the resolver's type index shows why:

type_def_nids['locking']:
  '<Locking>'    tmp_iss_proto_locking_locking          <- the protocol
  'Locking'      tmp_iss_proto_lockingimpl_locking      <- the real class
len = 2   (god-node guard requires exactly 1)

Delete Locking.h and the edge comes back. The protocol's mere presence removes it.

Root cause

The ObjC extractor labels a protocol node <Name>:

# graphify/extractors/objc.py:264
add_node(proto_nid, f"<{name}>", line)

_resolve_objc_member_calls builds its receiver-type index with a key that strips every non-alphanumeric character:

# graphify/extract.py:3311
def _key(label: str) -> str:
    return re.sub(r"[^a-zA-Z0-9]+", "", str(label)).lower()
...
# graphify/extract.py:3316-3321
for n in all_nodes:
    if n.get("source_file") and n.get("id") in contained and _is_type_like_definition(n):
        type_def_nids.setdefault(_key(n.get("label", "")), []).append(n["id"])

So <Locking> and Locking both key to locking. The angle brackets — the only thing distinguishing a protocol from a class at this layer — are exactly what _key discards. grep -n 'f"<{' graphify/extractors/objc.py returns a single hit (line 264), so the <X> label shape is unique to ObjC protocols and can be tested for without ambiguity.

_is_type_like_definition cannot be the place to fix this: it has 7 call sites spanning the stub-binding pass plus Swift/Python/C++/ObjC/C#/Java, and other languages legitimately want protocol-like nodes as type candidates.

Fix

Patch is up as #2500 against v8 (opened 2026-08-06, no CI run — external PRs sit at action_required). It excludes <Name>-labelled nodes from this resolver's receiver-type index only; protocols stay valid implements targets and valid stub-rewire targets, and the other six _is_type_like_definition consumers are untouched.

tests/test_objc_member_calls.py, 3 tests, each positive assertion paired with a decoy that must stay unresolved: 2 fail on v8 without the patch, all 3 pass with it. Full suite green, uv.lock untouched.

Happy to have it reimplemented however you prefer — the reproduction above is the part that matters.

Note

Related but independent: an ObjC category also mints a duplicate class node and trips the same guard (filed separately, patch in #2501). A corpus with both a category and a same-named protocol needs both fixes to clear the guard.

Activity

  1. xiongjianxu commented on Aug 10, 2026

    @xiongjianxu
    ContributorAuthor

    Patch for this is in #2500 (base v8). Note the PR shows no CI result — external PRs land in action_required and need a maintainer to approve the workflow run; the suite is green locally (uv run --frozen pytest tests/ -q, plus the five tools.skillgen gates, uv.lock untouched).

  2. xiongjianxu commented on Aug 10, 2026

    @xiongjianxu
    ContributorAuthor

    For anyone triaging the ObjC member-call pass together: a third, independent gap in the same
    pass is now filed as #2591 — a @property or ivar receiver ([self.bar doIt],
    [_ivarBar doIt]) can never be typed, because objc_type_table holds only method-body local
    declarations and the raw-call gate admits only an identifier receiver. That one is about
    missing receiver types rather than colliding type candidates, so it needs a separate fix,
    but all three surface in the same resolver.

  3. safishamsi commented on Aug 10, 2026

    @safishamsi
    Member

    Fixed in v0.9.39. A @protocol declaration (labeled <Name>) is now excluded from the receiver-type index, so it no longer collides with a same-named class and mis-resolves or fabricates a member call. Thanks @xiongjianxu for the fix (PR #2500).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions