30
30
#include " swift/AST/ForeignErrorConvention.h"
31
31
#include " swift/AST/GenericEnvironment.h"
32
32
#include " swift/AST/GenericSignatureBuilder.h"
33
+ #include " swift/AST/Initializer.h"
33
34
#include " swift/AST/NameLookup.h"
34
35
#include " swift/AST/PrettyStackTrace.h"
35
36
#include " swift/AST/ProtocolConformance.h"
@@ -2236,6 +2237,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2236
2237
}
2237
2238
2238
2239
void visitPatternBindingDecl (PatternBindingDecl *PBD) {
2240
+ DeclContext *DC = PBD->getDeclContext ();
2241
+
2239
2242
// Check all the pattern/init pairs in the PBD.
2240
2243
validatePatternBindingEntries (TC, PBD);
2241
2244
@@ -2296,9 +2299,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2296
2299
}
2297
2300
2298
2301
bool isInSILMode = false ;
2299
- if (auto sourceFile = PBD-> getDeclContext () ->getParentSourceFile ())
2302
+ if (auto sourceFile = DC ->getParentSourceFile ())
2300
2303
isInSILMode = sourceFile->Kind == SourceFileKind::SIL;
2301
- bool isTypeContext = PBD-> getDeclContext () ->isTypeContext ();
2304
+ bool isTypeContext = DC ->isTypeContext ();
2302
2305
2303
2306
// If this is a declaration without an initializer, reject code if
2304
2307
// uninitialized vars are not allowed.
@@ -2315,8 +2318,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2315
2318
if (var->isInvalid () || PBD->isInvalid ())
2316
2319
return ;
2317
2320
2318
- auto *varDC = var->getDeclContext ();
2319
-
2320
2321
auto markVarAndPBDInvalid = [PBD, var] {
2321
2322
PBD->setInvalid ();
2322
2323
var->setInvalid ();
@@ -2334,9 +2335,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2334
2335
2335
2336
// Static/class declarations require an initializer unless in a
2336
2337
// protocol.
2337
- if (var->isStatic () && !isa<ProtocolDecl>(varDC )) {
2338
+ if (var->isStatic () && !isa<ProtocolDecl>(DC )) {
2338
2339
// ...but don't enforce this for SIL or parseable interface files.
2339
- switch (varDC ->getParentSourceFile ()->Kind ) {
2340
+ switch (DC ->getParentSourceFile ()->Kind ) {
2340
2341
case SourceFileKind::Interface:
2341
2342
case SourceFileKind::SIL:
2342
2343
return ;
@@ -2353,8 +2354,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2353
2354
}
2354
2355
2355
2356
// Global variables require an initializer in normal source files.
2356
- if (varDC ->isModuleScopeContext ()) {
2357
- switch (varDC ->getParentSourceFile ()->Kind ) {
2357
+ if (DC ->isModuleScopeContext ()) {
2358
+ switch (DC ->getParentSourceFile ()->Kind ) {
2358
2359
case SourceFileKind::Main:
2359
2360
case SourceFileKind::REPL:
2360
2361
case SourceFileKind::Interface:
@@ -2378,8 +2379,35 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2378
2379
2379
2380
// If the initializers in the PBD aren't checked yet, do so now.
2380
2381
for (unsigned i = 0 , e = PBD->getNumPatternEntries (); i != e; ++i) {
2381
- if (!PBD->isInitializerChecked (i) && PBD->getInit (i))
2382
+ if (!PBD->getInit (i))
2383
+ continue ;
2384
+
2385
+ if (!PBD->isInitializerChecked (i))
2382
2386
TC.typeCheckPatternBinding (PBD, i);
2387
+
2388
+ if (!PBD->isInvalid ()) {
2389
+ auto &entry = PBD->getPatternList ()[i];
2390
+ auto *init = PBD->getInit (i);
2391
+
2392
+ // If we're performing an binding to a weak or unowned variable from a
2393
+ // constructor call, emit a warning that the instance will be immediately
2394
+ // deallocated.
2395
+ diagnoseUnownedImmediateDeallocation (TC, PBD->getPattern (i),
2396
+ entry.getEqualLoc (),
2397
+ init);
2398
+
2399
+ // If we entered an initializer context, contextualize any
2400
+ // auto-closures we might have created.
2401
+ if (!DC->isLocalContext ()) {
2402
+ auto *initContext = cast_or_null<PatternBindingInitializer>(
2403
+ entry.getInitContext ());
2404
+ if (initContext) {
2405
+ // Check safety of error-handling in the declaration, too.
2406
+ TC.checkInitializerErrorHandling (initContext, init);
2407
+ (void ) TC.contextualizeInitializer (initContext, init);
2408
+ }
2409
+ }
2410
+ }
2383
2411
}
2384
2412
}
2385
2413
0 commit comments