@@ -297,7 +297,13 @@ class DeallocateUninitializedLocalVariable : public Cleanup {
297297
298298 void emit (SILGenFunction &SGF, CleanupLocation l,
299299 ForUnwind_t forUnwind) override {
300- SGF.B .createDeallocBox (l, Box);
300+ auto box = Box;
301+ if (SGF.getASTContext ().SILOpts .supportsLexicalLifetimes (SGF.getModule ())) {
302+ auto *bbi = cast<BeginBorrowInst>(box);
303+ SGF.B .createEndBorrow (l, bbi);
304+ box = bbi->getOperand ();
305+ }
306+ SGF.B .createDeallocBox (l, box);
301307 }
302308
303309 void dump (SILGenFunction &) const override {
@@ -360,6 +366,10 @@ class LocalVariableInitialization : public SingleBufferInitialization {
360366 if (kind)
361367 Box = SGF.B .createMarkUninitialized (decl, Box, kind.getValue ());
362368
369+ if (SGF.getASTContext ().SILOpts .supportsLexicalLifetimes (SGF.getModule ())) {
370+ Box = SGF.B .createBeginBorrow (decl, Box, /* isLexical=*/ true );
371+ }
372+
363373 Addr = SGF.B .createProjectBox (decl, Box, 0 );
364374
365375 // Push a cleanup to destroy the local variable. This has to be
@@ -1751,7 +1761,15 @@ void SILGenFunction::destroyLocalVariable(SILLocation silLoc, VarDecl *vd) {
17511761 // For a heap variable, the box is responsible for the value. We just need
17521762 // to give up our retain count on it.
17531763 if (loc.box ) {
1754- B.emitDestroyValueOperation (silLoc, loc.box );
1764+ if (!getASTContext ().SILOpts .supportsLexicalLifetimes (getModule ())) {
1765+ B.emitDestroyValueOperation (silLoc, loc.box );
1766+ return ;
1767+ }
1768+
1769+ auto *bbi = cast<BeginBorrowInst>(loc.box );
1770+ B.createEndBorrow (silLoc, bbi);
1771+ B.emitDestroyValueOperation (silLoc, bbi->getOperand ());
1772+
17551773 return ;
17561774 }
17571775
0 commit comments