Repository navigation
Don't rewrite composition/existential types to Self in prefer_self_in_static_references - #6749
Conversation
`prefer_self_in_static_references` treats an extension as class-like and rewrites references to the extended type into `Self`. It also did this when the type appeared inside a protocol composition (`any A & B`) or as the constraint of an existential/opaque type (`any A`, `some A`), where `Self` is not interchangeable with the named type — changing the code's meaning and, for existentials, producing code that does not compile. Skip these positions in both the identifier and member-type handlers, and add non-triggering examples covering them. Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates prefer_self_in_static_references to avoid unsafe rewrites to Self in Swift type contexts where Self is not equivalent to a named type (protocol compositions and any/some constraints).
Changes:
- Skip violations when a referenced type is part of a protocol composition (
A & B/any A & B). - Skip violations when a referenced type is the constraint of
any/some. - Add non-triggering examples and document the behavior change in the changelog.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift | Adds early-returns to prevent rewriting in composition and any/some constraint contexts. |
| Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift | Adds non-triggering examples covering any and composition cases. |
| CHANGELOG.md | Records the behavioral change for the rule. |
- Extract the composition / existential-or-opaque skip logic into a single `isNonSubstitutableTypeContext(_:)` helper, removing the duplication between the `IdentifierTypeSyntax` and `MemberTypeSyntax` visitors. - Disable `file_length` on the rule file and `type_body_length` on the examples enum, matching the convention used by other rules (e.g. `NoMagicNumbersRule`), since `swiftlint lint --strict` in CI would otherwise fail on the additions. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Generated by 🚫 Danger |
SimplyDanny
left a comment
There was a problem hiding this comment.
Thanks for the prompt fix!
Replace the parent-context checks in the `visitPost` handlers (and the `isNonSubstitutableTypeContext` helper) with `visit(_:) -> .skipChildren` overrides for `SomeOrAnyTypeSyntax` and `CompositionTypeSyntax` in class-like scopes, matching how the rest of the visitor excludes contexts (generic lists, parameter clauses, etc.). `SomeOrAnyTypeSyntax` covers `any A`, `some A`, and `any A & B`; `CompositionTypeSyntax` additionally covers a bare `A & B` composition written without `any` (e.g. a class-and-protocol existential in Swift 5 mode), as exercised by the `Outer.Inner & B` example. Co-Authored-By: Claude Opus 4.8 <[email protected]>
An empirical sweep of `any`/`some`/`&`/metatype edge cases (verified against
the corrector and swiftc) surfaced one more miscorrection: in a protocol
extension, `P.Protocol` was rewritten to the invalid `Self.Protocol`
("cannot use 'Protocol' with non-protocol type 'Self'").
Skip the base of a `.Protocol` existential metatype in class-like scopes via
`visit(MetatypeTypeSyntax)`. `.Type` metatypes stay correctable, since
`Self.Type` is valid.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
| // The surrounding type must not be rewritten to `Self` when it appears in | ||
| // a composition (`A & B`, `any A & B`) or as the constraint of an | ||
| // existential or opaque type (`any A`, `some A`): `Self` is not | ||
| // interchangeable with the named type in a composition, and | ||
| // `any Self`/`some Self` is not valid. Skip these whole type constructs | ||
| // so neither their identifier nor member-type components are flagged. | ||
| override func visit(_: CompositionTypeSyntax) -> SyntaxVisitorContinueKind { | ||
| if case .likeClass = parentDeclScopes.peek() { | ||
| return .skipChildren | ||
| } | ||
| return .visitChildren | ||
| } | ||
|
|
||
| override func visit(_: SomeOrAnyTypeSyntax) -> SyntaxVisitorContinueKind { | ||
| if case .likeClass = parentDeclScopes.peek() { | ||
| return .skipChildren | ||
| } | ||
| return .visitChildren | ||
| } |
There was a problem hiding this comment.
I tried to turn this into a concrete false-negative sample before changing the visitor. The cases I could think of either are already intentionally skipped by the older generic-argument handling (any P<Self>), or produce invalid Swift when rewritten: Self & B, any Self & B, any Self, any Self.Type, and Self.P/any Self.P. So I do not currently have a troublesome code sample that would justify moving away from the subtree skip. @SimplyDanny, can you think of any other case Copilot might be referring to here?
There was a problem hiding this comment.
I think it refers to something like
class S {
protocol P {}
}
class T: S.P {}
extension S {
func f() -> any S.P { T() } // `any Self.P` would be an error
}which would be invalid either. Skipping the entire type and all children should thus be fine.
- Move the existential/opaque half of the shared comment onto `visit(SomeOrAnyTypeSyntax)`; keep the composition half on `visit(CompositionTypeSyntax)`. - Add a triggering + correction example for `.Type` metatypes (`A.Type` -> `Self.Type`), confirming `.Type` stays correctable while `.Protocol` is skipped. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Summary
prefer_self_in_static_referencestreats anextensionas class-like and rewrites references to the extended type intoSelf. It also did this when the type appeared inside a protocol composition (any A & B) or as the constraint of an existential/opaque type (any A,some A), whereSelfis not interchangeable with the named type — this changes the code's meaning and, for existentials, produces code that does not compile.For example, inside a protocol extension:
Changes
visitPost(IdentifierTypeSyntax)andvisitPost(MemberTypeSyntax)by bailing out when the parent is aCompositionTypeElementSyntaxorSomeOrAnyTypeSyntax.any A,any A & B, and the member-type compositionOuter.Inner & B. (Non-triggering examples are verified both for linting and for being unchanged by the corrector.)Genuine static references in the same extension (e.g.
K.i→Self.i) continue to be corrected.Fixes #6748
🤖 Generated with Claude Code