Repository navigation
fix(swift): a qualified inheritance entry lands on the type it names - #3201
bob-codedaptive wants to merge 1 commit into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
03bff86 to
775ce87
Compare
There was a problem hiding this comment.
💡 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".
| if (fitsPath(node, path, context) && (!inheritance || fitsPath(withExtendedPath(node, context), path, context))) { | ||
| scored.push({ node, depth: 0 }); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| if (ref.language === 'swift' && isInheritanceRef(ref) && ref.referenceName.includes('.')) { | ||
| return this.gateLanguage(resolveSwiftQualifiedSupertype(ref, this.context), ref); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
775ce87 to
aacc6d2
Compare
There was a problem hiding this comment.
💡 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".
| 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; |
There was a problem hiding this comment.
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 👍 / 👎.
| const exact = read.qualifiedName === path || read.qualifiedName.endsWith(`::${path}`); | ||
| scored.push({ node, depth: exact ? 1 : 0 }); |
There was a problem hiding this comment.
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 👍 / 👎.
What was wrong
class QC: Kit.Runner {}was extracted as anextendsreference namedKit, the first segment of the entry, so the supertype edge landed on the namespaceenum Kitinstead of onKit.Runner. Anything that walks supertypes (callers, impact, the Swift conformance pass) then sawQCinheriting from the wrong type. The qualifier can also name an external module, as instruct Y: Swift.Error; such an entry has no project target, and must not land on a projectenum Errorthat merely shares the last segment.What this changes
TreeSitterExtractorfor the wasm path,extract_inheritancein the native kernel) record an inheritance entry as the dotted path of itstype_identifierchildren,Kit.RunnerorKit.Inner.Deep, with generic arguments dropped (Kit.Runner<Int>isKit.Runner), at the position of the entry'suser_type. An unqualified entry (: B) is unchanged.resolveSwiftQualifiedSupertypeplaces such a reference on the declaration the path names, with no fallthrough to a name match on the last segment (class QC: Kit.Runnernever lands on an unrelated top-levelRunner). A type declared inside an extension is matched by the extension's written path, soRight.Box.Baselands on theBaseinextension Right.Boxand not on a twin inextension 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 withclass Base {}andenum Kit { class Base {} }in one file,Kit.Baselands on the nested class. A type declared in a test-suite file, or another file'sprivatetype, is no target for a supertype written in a file that cannot see it, as for any other cross-file name match.gateSwiftTypeTargetgives 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.__tests__/fixtures/kernel-parity/torture.swiftgains 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>andKit.Inner.Deepland on the nested protocol; not on an unrelatedRunner;A: Bunchanged;Foundation.NSObject,Swift.ErrorandFoundation.Operationget no edge;Right.Box.BaseandLeft.Box.Baseland on theBaseof their own extension; and the extracted reference name and position. Two more blocks, on the wasm path only:Kit.Basebeside a top-levelBasedeclared 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'sKit.Basewhose 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-ownedBasecheck, the unrelated-Runnercheck 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-fileKit.Baseand the test-fixture check).kernel-swift-parity.test.ts(skip without a staged kernel): not run by the author, who could build the kernel (npm run build:kernelcompiles) but not load the result.🤖 Generated with Claude Code