@@ -3974,10 +3974,6 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
39743974 // so we can build our singular PatternBindingDecl at the end.
39753975 SmallVector<PatternBindingEntry, 4 > PBDEntries;
39763976
3977- // The pattern binding initializer contexts, which have to be set up at the
3978- // end.
3979- SmallVector<PatternBindingInitializer *, 4 > PBDInitContexts;
3980-
39813977 // No matter what error path we take, make sure the
39823978 // PatternBindingDecl/TopLevel code block are added.
39833979 SWIFT_DEFER {
@@ -3992,10 +3988,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
39923988 VarLoc, PBDEntries, CurDeclContext);
39933989
39943990 // Wire up any initializer contexts we needed.
3995- assert (PBDInitContexts.size () == PBDEntries.size ());
3996- for (unsigned i : indices (PBDInitContexts)) {
3997- auto initContext = PBDInitContexts[i];
3998- if (initContext) initContext->setBinding (PBD, i);
3991+ for (unsigned i : indices (PBDEntries)) {
3992+ if (auto initContext = PBDEntries[i].getInitContext ())
3993+ cast<PatternBindingInitializer>(initContext)->setBinding (PBD, i);
39993994 }
40003995
40013996 // If we're setting up a TopLevelCodeDecl, configure it by setting up the
@@ -4041,8 +4036,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
40414036
40424037 // Remember this pattern/init pair for our ultimate PatternBindingDecl. The
40434038 // Initializer will be added later when/if it is parsed.
4044- PBDEntries.push_back ({pattern, nullptr });
4045- PBDInitContexts.push_back (nullptr );
4039+ PBDEntries.push_back ({pattern, nullptr , nullptr });
40464040
40474041 Expr *PatternInit = nullptr ;
40484042
@@ -4052,7 +4046,6 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
40524046 // into (should we have one). This happens for properties and global
40534047 // variables in libraries.
40544048 PatternBindingInitializer *initContext = nullptr ;
4055- bool usedInitContext = false ;
40564049
40574050 // Record the variables that we're trying to initialize. This allows us
40584051 // to cleanly reject "var x = x" when "x" isn't bound to an enclosing
@@ -4072,7 +4065,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
40724065 // for the PBD we'll eventually create. This allows us to have reasonable
40734066 // DeclContexts for any closures that may live inside of initializers.
40744067 if (!CurDeclContext->isLocalContext () && !topLevelDecl && !initContext)
4075- initContext = Context. createPatternBindingContext (CurDeclContext);
4068+ initContext = new ( Context) PatternBindingInitializer (CurDeclContext);
40764069
40774070 // If we're using a local context (either a TopLevelCodeDecl or a
40784071 // PatternBindingContext) install it now so that CurDeclContext is set
@@ -4109,21 +4102,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
41094102 PatternInit = init.getPtrOrNull ();
41104103 PBDEntries.back ().setInit (PatternInit);
41114104
4112- // If we allocated an initContext and the expression had a closure in it,
4113- // we'll need to keep the initContext around.
4114- if (initContext)
4115- usedInitContext |= initParser->hasClosures ();
4116-
41174105 // If we set up an initialization context for a property or module-level
4118- // global, check to see if we needed it and wind it down.
4119- if (initContext) {
4120- // If we didn't need the context, "destroy" it, which recycles it for
4121- // the next user.
4122- if (!usedInitContext)
4123- Context.destroyPatternBindingContext (initContext);
4124- else
4125- PBDInitContexts.back () = initContext;
4126- }
4106+ // global, record it.
4107+ PBDEntries.back ().setInitContext (initContext);
41274108
41284109 // If we are doing second pass of code completion, we don't want to
41294110 // suddenly cut off parsing and throw away the declaration.
@@ -4158,7 +4139,6 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
41584139 // into (should we have one). This happens for properties and global
41594140 // variables in libraries.
41604141 PatternBindingInitializer *initContext = nullptr ;
4161- bool usedInitContext = false ;
41624142
41634143 // Record the variables that we're trying to set up. This allows us
41644144 // to cleanly reject "var x = x" when "x" isn't bound to an enclosing
@@ -4178,7 +4158,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
41784158 // This will be recontextualized to a method we synthesize during
41794159 // type checking.
41804160 if (!CurDeclContext->isLocalContext () && !topLevelDecl && !initContext)
4181- initContext = Context. createPatternBindingContext (CurDeclContext);
4161+ initContext = new ( Context) PatternBindingInitializer (CurDeclContext);
41824162 Optional<ParseFunctionBody> initParser;
41834163 Optional<ContextChange> topLevelParser;
41844164 if (topLevelDecl)
@@ -4188,10 +4168,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
41884168 initParser.emplace (*this , initContext);
41894169
41904170 auto closure = parseExprClosure ();
4191- if (initContext) {
4192- usedInitContext = true ;
4193- PBDInitContexts.back () = initContext;
4194- }
4171+ PBDEntries.back ().setInitContext (initContext);
41954172
41964173 if (closure.isParseError ())
41974174 return makeParserError ();
0 commit comments