Retain a class's sole designated initializer when it has stored state#1134
Open
danwood wants to merge 2 commits into
Open
Retain a class's sole designated initializer when it has stored state#1134danwood wants to merge 2 commits into
danwood wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
valuehas no default, soinit(value:)is the only way to build aPage. Structs don't hit this because they synthesize a memberwise initializer, so this is class-specific.Root cause
DefaultConstructorReferenceBuildersynthesizes a retaining reference only for constructors wherename == "init()"orisImplicit. A class's sole parameterized designated initializer is neither, so nothing retains it and it's flagged unused.Fix
I extended
DefaultConstructorReferenceBuilderwith a second pass that retains a class's initializer, but only when all of the following hold:.classand share the same structural justification — stored state that must be initialized — so they're intentionally covered too.)varInstancethat 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:
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.