Skip to content

Commit 1f84641

Browse files
authored
Merge pull request #12386 from slavapestov/most-definitely-self
DI fixes for resilience
2 parents 01fa606 + d8153b3 commit 1f84641

17 files changed

+969
-773
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,36 +151,35 @@ ERROR(ivar_not_initialized_at_implicit_superinit,none,
151151
(StringRef, bool))
152152

153153
ERROR(self_use_before_fully_init,none,
154-
"use of 'self' in %select{method call|property access}1 %0 before "
154+
"'self' used in %select{method call|property access}1 %0 before "
155155
"%select{all stored properties are initialized|"
156-
"super.init initializes self|"
157-
"self.init initializes self}2", (DeclBaseName, bool, unsigned))
156+
"'super.init' call|"
157+
"'self.init' call}2", (DeclBaseName, bool, unsigned))
158158
ERROR(use_of_self_before_fully_init,none,
159159
"'self' used before all stored properties are initialized", ())
160-
ERROR(use_of_self_before_fully_init_protocol,none,
161-
"'self' used before chaining to another self.init requirement", ())
162160

163161

164162
NOTE(stored_property_not_initialized,none,
165163
"'%0' not initialized", (StringRef))
166164

167165
ERROR(selfinit_multiple_times,none,
168-
"%select{super|self}0.init called multiple times in initializer",
166+
"'%select{super|self}0.init' called multiple times in initializer",
169167
(unsigned))
170168
ERROR(superselfinit_not_called_before_return,none,
171-
"%select{super|self}0.init isn't called on all paths before returning "
169+
"'%select{super|self}0.init' isn't called on all paths before returning "
172170
"from initializer", (unsigned))
173-
ERROR(self_before_superselfinit,none,
174-
"'self' used before %select{super|self}0.init call",
175-
(unsigned))
171+
ERROR(self_before_superinit,none,
172+
"'self' used before 'super.init' call", ())
173+
ERROR(self_before_selfinit,none,
174+
"'self' used before 'self.init' call", ())
175+
ERROR(self_before_selfinit_value_type,none,
176+
"'self' used before 'self.init' call or assignment to 'self'", ())
176177
ERROR(self_inside_catch_superselfinit,none,
177178
"'self' used inside 'catch' block reachable from "
178179
"%select{super|self}0.init call",
179180
(unsigned))
180181
ERROR(return_from_init_without_initing_self,none,
181-
"return from enum initializer method without storing to 'self'", ())
182-
ERROR(return_from_protocol_init_without_initing_self,none,
183-
"protocol extension initializer never chained to 'self.init'", ())
182+
"return from initializer before 'self.init' call or assignment to 'self'", ())
184183
ERROR(return_from_init_without_initing_stored_properties,none,
185184
"return from initializer without initializing all"
186185
" stored properties", ())

lib/AST/Decl.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5306,6 +5306,29 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
53065306
// get the kind out of the finder.
53075307
auto Kind = finder.Kind;
53085308

5309+
auto *NTD = getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
5310+
5311+
// Protocol extension and enum initializers are always delegating.
5312+
if (Kind == BodyInitKind::None) {
5313+
if (isa<ProtocolDecl>(NTD) || isa<EnumDecl>(NTD)) {
5314+
Kind = BodyInitKind::Delegating;
5315+
}
5316+
}
5317+
5318+
// Struct initializers that cannot see the layout of the struct type are
5319+
// always delegating. This occurs if the struct type is not fixed layout,
5320+
// and the constructor is either inlinable or defined in another module.
5321+
//
5322+
// FIXME: Figure out the right condition to use here that does not depend
5323+
// on the -enable-resilience flag, and make it conditional on
5324+
// -swift-version 5 instead, once the "disallow memberwise cross-module
5325+
// initializer" proposal lands.
5326+
if (Kind == BodyInitKind::None) {
5327+
if (isa<StructDecl>(NTD) &&
5328+
!NTD->hasFixedLayout(getParentModule(), getResilienceExpansion())) {
5329+
Kind = BodyInitKind::Delegating;
5330+
}
5331+
}
53095332

53105333
// If we didn't find any delegating or chained initializers, check whether
53115334
// the initializer was explicitly marked 'convenience'.

0 commit comments

Comments
 (0)