Skip to content

Retain a class's sole designated initializer when it has stored state#1134

Open
danwood wants to merge 2 commits into
peripheryapp:masterfrom
danwood:fix-sole-class-init
Open

Retain a class's sole designated initializer when it has stored state#1134
danwood wants to merge 2 commits into
peripheryapp:masterfrom
danwood:fix-sole-class-init

Conversation

@danwood

@danwood danwood commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Symptom

A class whose only initializer is an explicit parameterized designated initializer is reported as unused when that initializer has no external callers, even though removing it would make the class unconstructable:

public class Page {
    private var value: Int

    init(value: Int) {   // reported unused
        self.value = value
    }
}

value has no default, so init(value:) is the only way to build a Page. Structs don't hit this because they synthesize a memberwise initializer, so this is class-specific.

Root cause

DefaultConstructorReferenceBuilder synthesizes a retaining reference only for constructors where name == "init()" or isImplicit. A class's sole parameterized designated initializer is neither, so nothing retains it and it's flagged unused.

Fix

I extended DefaultConstructorReferenceBuilder with a second pass that retains a class's initializer, but only when all of the following hold:

  • the parent is a class — structs and enums synthesize their own initializers, so an unused explicit init on those is genuinely dead code and must stay reportable. (Actors are indexed as .class and share the same structural justification — stored state that must be initialized — so they're intentionally covered too.)
  • it is the sole non-implicit initializer of that class — if a class declares two or more explicit initializers, the user has alternatives, so none are retained and they all stay reportable.
  • the class declares at least one stored instance property — this is the structural reason the initializer has to exist. "Stored" is detected as a varInstance that is not a computed property (a computed property has a getter accessor and no setter; a stored property has either no accessors or a synthesized get/set pair).

The existing init()/implicit behavior is unchanged; this only adds the new narrow case.

Why this doesn't mask dead code (re #1058)

#1058 raised the concern that broadly retaining unused initializers can hide dead code. This rule is deliberately narrow to avoid that:

  • It never fires when a class has multiple explicit inits, so it can't keep an unused secondary init alive.
  • It requires stored state, so it doesn't retain inits on stateless classes that have no construction obligation.
  • It does not condition on whether the class itself is used (which isn't determinable at this pipeline stage and isn't needed): if the class is itself unused, it folds away entirely and the init goes with it, so retaining the init can't keep a dead class alive.

Tests

  • testRetainsSoleRequiredClassInitializer — the repro; sole class init with stored state is now retained.
  • testDoesNotRetainClassInitializersWhenMultipleDeclared — class with two explicit inits; both stay reportable.
  • testDoesNotRetainSoleStructInitializer — sole struct init stays reportable (unchanged upstream behavior).
  • testRetainsUsedSoleClassInitializer — sanity: a used sole class init remains referenced.

Full suite passes (312 tests), self-scan reports no unused code, swiftformat and swiftlint are clean.

danwood added 2 commits June 12, 2026 22:02
A class retained only as a property type, whose single explicit initializer
takes parameters and has no external callers, has that initializer reported
as unused. Removing it would leave the class's non-default stored property
un-initializable. DefaultConstructorReferenceBuilder only retains init() and
implicit initializers, so a parameterized sole designated initializer is not
covered. This test pins the behavior for inspection.
A struct synthesizes a memberwise initializer, so an explicit struct init
that's never called is genuinely dead code. A class has no such synthesis:
when a class declares a stored property without a default value, the explicit
initializer is the only way to construct an instance, and removing it would
make the class unconstructable. DefaultConstructorReferenceBuilder previously
only retained init() and implicit initializers, so a class's sole parameterized
designated initializer with no external callers was reported as unused.

Retain that initializer, but narrowly. It fires only when the parent is a class
with exactly one non-implicit initializer and at least one stored instance
property. If a class declares multiple explicit initializers there are
alternatives, so none are retained. Structs and enums are excluded because they
synthesize their own initializers. This keeps the change from masking the dead
init cases discussed in issue peripheryapp#1058.

Also adds scope tests: multiple class inits stay reportable, a sole struct init
stays reportable, and a used sole class init remains referenced.
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