Skip to content

Retain nested types referenced only by sibling method signatures#1137

Open
danwood wants to merge 2 commits into
peripheryapp:masterfrom
danwood:fix-used-marking-propagation
Open

Retain nested types referenced only by sibling method signatures#1137
danwood wants to merge 2 commits into
peripheryapp:masterfrom
danwood:fix-used-marking-propagation

Conversation

@danwood

@danwood danwood commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Symptom

A nested type that is used only as the return type or a parameter type of a sibling method in the same enclosing type is incorrectly reported as unused, even when the enclosing type is reachable. For example:

public struct Container {
    public init() {}

    func makeResult() -> Payload { Payload() }   // Payload flagged unused
    func consume(_: Mode) {}                      // Mode flagged unused

    struct Payload {}
    enum Mode { case one }
}

Payload and Mode are reported unused despite appearing in the method signatures.

Root cause

The Swift index store records the type reference for a return/parameter type against the method declaration; it does not emit a standalone reference occurrence for the type itself. UsedDeclarationMarker.markUsed(_:) follows a declaration's own references and related, but it does not descend into the signature-type references held by the declaration's child functions. So when the enclosing type is marked used, a nested type reachable only through a sibling method's signature has no incoming reference to follow and is left unmarked.

Fix

When a declaration is marked used, also walk the returnType and parameterType references of its child function declarations and mark the referenced declarations used. This retains nested types that appear only in sibling method signatures, without affecting types reachable through ordinary references.

Tests

Adds testRetainsNestedTypeUsedAsSiblingMethodSignatureType, with a fixture containing a nested struct used as a method return type and a nested enum used as a method parameter type, reachable only through those signatures. The test fails before the change (both nested types reported unused) and passes after. The full existing suite continues to pass with no regressions.

A nested type used solely as the return type or a parameter type of a
sibling method was incorrectly reported as unused. The index store records
the type reference against the method declaration rather than emitting a
standalone reference occurrence for the type, so when the enclosing type
was marked used the nested type had no incoming reference to follow.

UsedDeclarationMarker now walks the return-type and parameter-type
references of a used declaration's child functions, ensuring such nested
types are retained. Adds a retention test covering a nested struct and
nested enum reachable only through a sibling method's signature.
@danwood danwood marked this pull request as ready for review June 15, 2026 01:43
The improved used-marking now reports the individual members of
SetupSelection (its cases and selectedValues) as unused on Linux, where
XcodeSupport — the sole consumer — is not compiled. Previously only the
enum type itself was reported and baselined. Replace the type entry with
the three member USRs in the Linux and Linux-Bazel baselines.
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