Skip to content

ObjC: a category/class-extension interface mints a duplicate class node — moving a method into a category destroys its call edges #2590

Closed

Description

@xiongjianxu

Summary

An ObjC category (@interface Foo (Cat)) or class extension (@interface Foo ()) declares members of an existing class, but its class node is keyed off the file stem — so Foo.h and Foo+Cat.h each mint a node labelled Foo. Every [Foo ...] receiver then has two type candidates, trips the member-call resolver's single-definition god-node guard, and produces no edge.

This is not a cosmetic duplicate-node problem: moving a method from a class body into a category silently destroys call edges the same corpus resolved fine before. Categories are pervasive in real ObjC (NSString+Trim.h, a private @interface Foo () at the top of nearly every .m), so this hits ordinary code rather than an edge case. grep -ic 'categor' graphify/extractors/objc.py → 0.

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

Reproduction

Controlled pair — identical call site, only the location of the method declaration differs.

Caller.m, byte-identical in both runs:

@implementation Caller
- (void)go { [Base useIt]; }
@end

Control — +useIt declared in Base.h's @interface Base:

@interface Base : NSObject
+ (void)useIt;
@end

Variant — Base.h is now bare, +useIt moved into Base+Extra.h:

#import "Base.h"
@interface Base (Extra)
+ (void)useIt;
@end
where +useIt is declared calls edges
Base.h's @interface Base 1 — -go -> +useIt EXTRACTED
Base+Extra.h's @interface Base (Extra) 0

The variant's node dump shows the two Base nodes, and the resolver's type index shows the collision:

tmp_iss_cat_after_base_extra_base   'Base'    Base+Extra.h
tmp_iss_cat_after_base_base         'Base'    Base.h

type_def_nids['base']:
  'Base'   tmp_iss_cat_after_base_extra_base
  'Base'   tmp_iss_cat_after_base_base
len = 2   (god-node guard requires exactly 1)

Also affected: two categories on a class outside the corpus

NSString+Trim.h + NSString+Trim.m + a caller resolves fine (1 INFERRED edge into -trimmed). Add a second, unrelated category header — NSString+JSON.h — and it drops to 0. Two categories on the same framework class is completely ordinary.

Not affected (scoping, for reference)

A class extension inside the class's own .m (@interface Base () in Base.m) already works, because _decldef_class_stem pairs Base.m with Base.h. It breaks once the extension lives in a separately named header (BasePrivate.h), which mints a third Base node. Worth knowing so the two cases aren't conflated.

Root cause

The extractor keys the class node off the file stem, with no notion of a category:

# graphify/extractors/objc.py:190  (class_interface branch)
cls_nid = _make_id(stem, name)     # stem == "Foo+Cat" for Foo+Cat.h
# graphify/extractors/objc.py:246  (class_implementation branch)
impl_nid = _make_id(stem, name)

_merge_decl_def_classes is the pass that would normally fold same-named siblings, and it already strips the category suffix when computing the sibling key:

# graphify/extractors/resolution.py:2054
stem = p.stem.split("+", 1)[0]   # ObjC category: Foo+Cat -> Foo

but it refuses to pick a keeper once more than one header is present:

# graphify/extractors/resolution.py:2140-2143
# All from one (dir, base_stem) sibling family, with a UNIQUE header.
if len(sibling_keys) != 1 or len(headers) != 1:
    continue
keeper = headers[0]

A category corpus reaches it as Foo.h and Foo+Cat.h — two headers — so the merge bails, id-disambiguation then splits the nodes by path, and the guard sees two candidates.

Fix

Patch is up as #2501 against v8 (opened 2026-08-06, no CI run — external PRs sit at action_required). Two coordinated changes:

  1. objc.py — a category/extension interface (and implementation) keys its class node off the base stem. The test is on the grammar, not on a + in the filename: the parentheses appear as an anonymous ( child only for a category or extension, whereas a generic class (@interface Box<T>) uses parameterized_arguments. Verified against the tree-sitter-objc grammar:

    category       ['@interface', 'identifier', '(', 'identifier', ')', ...]
    extension      ['@interface', 'identifier', '(', ')', ...]
    generic class  ['@interface', 'identifier', 'parameterized_arguments', ':', 'identifier', ...]
    

    Being keyed on syntax is what makes it safe: Extra+Helpers.h declaring a plain @interface Helper keeps its own stem. The stem split also only fires on a well-formed Name+Suffix identifier pair, so C++Bridge.h and Foo+.h are left intact.

  2. resolution.py — _merge_decl_def_classes folds a category header into the base header: it keeps the base header (the stem with no +), or the lowest-sorting category header when the base class lives outside the corpus (NSString+Trim.h / NSString+JSON.h). Two non-category headers still bail to id-disambiguation, so an unrelated Foo.h / Foo.hpp pair behaves exactly as before.

tests/test_objc_category_interfaces.py, 4 tests — two behavioral, two scoping guards (a plain @interface in a +-named file must keep its stem; two same-named Thing classes in different directories must stay distinct and stay unresolved). 2 fail on v8 without the patch, all 4 pass with it. Full suite green, uv.lock untouched.

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

Note

Related but independent: a same-named @protocol also collides in that type index and trips the same guard (filed separately, patch in #2500). 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 #2501 (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 category or class-extension interface (@interface Foo (Bar) / @interface Foo ()) now folds into the base Foo class node instead of minting a duplicate, so its methods merge and cross-half calls resolve. (A category whose file stem does not match the class name, or a base class in a different directory, is a known conservative follow-up — a missing edge, never a wrong one.) Thanks @xiongjianxu for the fix (PR #2501).

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