Skip to content

fix(swift): a qualified inheritance entry lands on the type it names - #3201

Open
bob-codedaptive wants to merge 1 commit into
colbymchenry:mainfrom
codedaptive:pr/swift-qualified-inheritance
Open

bob-codedaptive wants to merge 1 commit into
colbymchenry:mainfrom
codedaptive:pr/swift-qualified-inheritance

Conversation

@bob-codedaptive

@bob-codedaptive bob-codedaptive commented Nov 1, 2026 •

Copy link
Copy Markdown

What was wrong

class QC: Kit.Runner {} was extracted as an extends reference named Kit, the first segment of the entry, so the supertype edge landed on the namespace enum Kit instead of on Kit.Runner. Anything that walks supertypes (callers, impact, the Swift conformance pass) then saw QC inheriting from the wrong type. The qualifier can also name an external module, as in struct Y: Swift.Error; such an entry has no project target, and must not land on a project enum Error that merely shares the last segment.

What this changes

  • Both Swift extractors (TreeSitterExtractor for the wasm path, extract_inheritance in the native kernel) record an inheritance entry as the dotted path of its type_identifier children, Kit.Runner or Kit.Inner.Deep, with generic arguments dropped (Kit.Runner<Int> is Kit.Runner), at the position of the entry's user_type. An unqualified entry (: B) is unchanged.
  • resolveSwiftQualifiedSupertype places such a reference on the declaration the path names, with no fallthrough to a name match on the last segment (class QC: Kit.Runner never lands on an unrelated top-level Runner). A type declared inside an extension is matched by the extension's written path, so Right.Box.Base lands on the Base in extension Right.Box and not on a twin in extension Left.Box. A declaration whose qualified name is the written path wins over one that fits only by sharing a file with the qualifier's type, so with class Base {} and enum Kit { class Base {} } in one file, Kit.Base lands on the nested class. A type declared in a test-suite file, or another file's private type, is no target for a supertype written in a file that cannot see it, as for any other cross-file name match.
  • gateSwiftTypeTarget gives a supertype whose qualifier names no project type (Swift.Error, Foundation.Operation) no target, and reads a dotted inheritance reference as the extractor wrote it instead of reading source text back.
  • The Swift kernel port checklist describes the new extraction, and __tests__/fixtures/kernel-parity/torture.swift gains a qualified entry and a generic qualified entry so the kernel parity suite compares the two extractors on them.

Known limit

A supertype written with the project's own module name (MyModule.Base) names no project type in its qualifier, so it gets no edge.

Tests

__tests__/swift-qualified-inheritance.test.ts, each vector once per arm (wasm always; kernel when a kernel is staged): Kit.Runner, Kit.Runner<Int> and Kit.Inner.Deep land on the nested protocol; not on an unrelated Runner; A: B unchanged; Foundation.NSObject, Swift.Error and Foundation.Operation get no edge; Right.Box.Base and Left.Box.Base land on the Base of their own extension; and the extracted reference name and position. Two more blocks, on the wasm path only: Kit.Base beside a top-level Base declared first in the same file lands on the nested class (with a guard that a type the grammar indexes at the top level still lands through its file), and a production class's Kit.Base whose only declaration is a test fixture gets no edge while a test class's lands on it.

  • npx vitest run __tests__/swift-qualified-inheritance.test.ts, wasm arm: 14 tests pass. With the source files returned to current main, 6 of the ten vector tests fail (the three nested landings, the extension-owned Base check, the unrelated-Runner check and the extraction check). With the source files of the previous revision of this PR, 2 of the four tests in the new blocks fail (the same-file Kit.Base and the test-fixture check).
  • kernel arm and kernel-swift-parity.test.ts (skip without a staged kernel): not run by the author, who could build the kernel (npm run build:kernel compiles) but not load the result.
  • Verified against upstream main 47a9c7d.
  • Platform: tested on macOS only; not run on Linux or Windows.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T17:46:53.509961Z aacc6d2 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 03bff86026

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

export function resolveSwiftQualifiedSupertype(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
const name = ref.referenceName;
const from = context.getNodeById?.(ref.fromNodeId) ?? null;
const decl = declarationFor(name.slice(name.lastIndexOf('.') + 1), name, ref, from, context);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match the full extension owner before selecting a supertype

When a Swift file declares Base inside extension A.Inner and then inside extension B.Inner, class Derived: B.Inner.Base {} resolves here to A.Inner.Base. Both extractors index these declarations as Inner::Base; declarationFor accepts that suffix for both candidates and collapses them to the first declaration per file. gateSwiftTypeTarget accepts the same suffix, so the incorrect superclass edge survives and pollutes inheritance and impact queries. Reconstruct the extension owner's full path, as ownerPath already does for type-path calls, before selecting the supertype, and add coverage for colliding extension-owned types.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed: with Base declared in both extension Left.Box and extension Right.Box, class FromRight: Right.Box.Base landed on Left's Base. A qualified inheritance entry now also matches a candidate's path read through the extension it is declared in, the way type-path calls already read it, so each entry lands on its own extension's Base. Test j in swift-qualified-inheritance.test.ts covers both directions.

@bob-codedaptive
bob-codedaptive force-pushed the pr/swift-qualified-inheritance branch from 03bff86 to 775ce87 Compare November 3, 2026 03:04

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 775ce87527

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/resolution/swift-type-visibility.ts Outdated
Comment on lines +299 to +300
if (fitsPath(node, path, context) && (!inheritance || fitsPath(withExtendedPath(node, context), path, context))) {
scored.push({ node, depth: 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prefer exact type paths over same-file fallback matches

When a Swift file contains class Base {}, enum Kit { class Base {} }, and then class Child: Kit.Base {}, this lookup selects the top-level Base instead of Kit::Base. fitsPath admits the top-level declaration merely because it shares a file with Kit, both candidates receive depth 0, and the per-file collapse keeps the first. gateSwiftTypeTarget accepts the same fallback, so the incorrect superclass edge survives and pollutes inheritance and impact queries. Prefer the exact reconstructed path before considering suffix or grammar-recovery matches, and cover same-file name collisions.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed: with class Base {} declared before enum Kit { class Base {} } in the same file, class Child: Kit.Base landed on the top-level Base, because the same-file fallback admitted it at the same rank as the nested class. The lookup now ranks a declaration whose qualified name is the written path above one that fits only through a fallback, so the edge lands on Kit::Base. This is pinned in swift-qualified-inheritance.test.ts ('lands Kit.Base on the nested class, not on the top-level Base declared first in the file'), with a guard that a type the grammar indexes at the top level still lands through its file.

Comment thread src/resolution/index.ts Outdated
Comment on lines +1767 to +1768
if (ref.language === 'swift' && isInheritanceRef(ref) && ref.referenceName.includes('.')) {
return this.gateLanguage(resolveSwiftQualifiedSupertype(ref, this.context), ref);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve test-suite isolation in qualified supertype lookup

When an indexed test fixture is the only project declaration of Kit::Base, production class Child: Kit.Base {} now resolves to that test-only type—even when the actual superclass comes from an external dependency. This early return bypasses the isVisibleAcrossFiles check in the normal name-matching path, while neither declarationFor nor the subsequent gates applies its test-suite exclusion. The result is a production superclass edge into Tests/Fixture.swift, contaminating inheritance and impact results. Apply the cross-file eligibility check before accepting a qualified-supertype candidate.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed: a production class Child: Kit.Base {} got an extends edge into Tests/AppTests/Fixture.swift when that fixture held the only Kit.Base, because the qualified-supertype path skipped the cross-file visibility check that name matching applies. That path now keeps its target only when the reference's file can see it, so a test-suite type (or another file's private type) is no production supertype, while a test file's Kit.Base still lands on its fixture. Both are pinned in swift-qualified-inheritance.test.ts ('gives no supertype edge from production code to a nested type that only a test-suite file declares' and 'lands a test class's Kit.Base on the nested type its test-suite fixture declares').

class QC: Kit.Runner was extracted as Kit, its first segment, so its supertype
edge landed on the namespace. Both extractors now record an inheritance entry as
its full dotted path, generic arguments dropped, at the entry's position, and the
resolver places it on the type that path names. A qualifier that names no project
type (Swift.Error, Foundation.Operation) is an external module's type and gets no
edge, so it never lands on a project type sharing its last segment.
@bob-codedaptive
bob-codedaptive force-pushed the pr/swift-qualified-inheritance branch from 775ce87 to aacc6d2 Compare November 3, 2026 17:37

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aacc6d2882

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +365 to +366
const qualifier = written.split('.').slice(0, -1);
if (!qualifier.some((seg) => context.getNodesByName(seg).some((n) => n.language === 'swift' && TYPE_KINDS.has(n.kind)))) return null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the root of module-qualified supertype paths

With a local enum Kit { class Base {} } and an imported dependency ThirdParty, class Child: ThirdParty.Kit.Base {} resolves to the local Kit::Base. fitsPath accepts that suffix, and this check passes because the intermediate Kit segment names a project type, even though ThirdParty does not. The resulting superclass edge incorrectly pulls an external dependency into the project's inheritance and impact graph. Validate the qualifier's root and complete owner path rather than accepting any matching intermediate segment.

Useful? React with 👍 / 👎.

Comment on lines +304 to +305
const exact = read.qualifiedName === path || read.qualifiedName.endsWith(`::${path}`);
scored.push({ node, depth: exact ? 1 : 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resolve qualified supertypes relative to their lexical scope

When one file declares top-level Kit.Base before Outer.Kit.Base, and Outer.Child inherits Kit.Base, both candidates receive depth 1 here: one matches the complete string and the other matches its suffix. The per-file collapse consequently selects top-level Kit::Base, although Swift resolves the qualifier inside Outer.Child to Outer.Kit. Resolve the qualifier against the source type's lexical scope before ranking suffix matches; otherwise declaration order changes the superclass edge for valid, unambiguous Swift.

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
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.

1 participant