Skip to content

Commit 0692203

Browse files
committed
[TypeChecker] TypeWrappers: Inject _storage variable into initializer body
1 parent 412915e commit 0692203

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,19 +2129,35 @@ TypeCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
21292129
// what the type of the expression is. Take the inserted return back out.
21302130
body->setLastElement(func->getSingleExpressionBody());
21312131
}
2132-
} else if (isa<ConstructorDecl>(AFD) &&
2133-
(body->empty() ||
2134-
!isKnownEndOfConstructor(body->getLastElement()))) {
2135-
// For constructors, we make sure that the body ends with a "return" stmt,
2136-
// which we either implicitly synthesize, or the user can write. This
2137-
// simplifies SILGen.
2138-
SmallVector<ASTNode, 8> Elts(body->getElements().begin(),
2139-
body->getElements().end());
2140-
Elts.push_back(new (ctx) ReturnStmt(body->getRBraceLoc(),
2141-
/*value*/nullptr,
2142-
/*implicit*/true));
2143-
body = BraceStmt::create(ctx, body->getLBraceLoc(), Elts,
2144-
body->getRBraceLoc(), body->isImplicit());
2132+
} else if (auto *ctor = dyn_cast<ConstructorDecl>(AFD)) {
2133+
// If this is user-defined constructor that requires `_storage`
2134+
// variable injection, do so now so now.
2135+
if (auto *storageVar = ctor->getLocalTypeWrapperStorageVar()) {
2136+
SmallVector<ASTNode, 8> Elts;
2137+
2138+
Elts.push_back(storageVar->getParentPatternBinding());
2139+
Elts.push_back(storageVar);
2140+
2141+
Elts.append(body->getElements().begin(),
2142+
body->getElements().end());
2143+
2144+
body = BraceStmt::create(ctx, body->getLBraceLoc(), Elts,
2145+
body->getRBraceLoc(), body->isImplicit());
2146+
}
2147+
2148+
if (body->empty() ||
2149+
!isKnownEndOfConstructor(body->getLastElement())) {
2150+
// For constructors, we make sure that the body ends with a "return" stmt,
2151+
// which we either implicitly synthesize, or the user can write. This
2152+
// simplifies SILGen.
2153+
SmallVector<ASTNode, 8> Elts(body->getElements().begin(),
2154+
body->getElements().end());
2155+
Elts.push_back(new (ctx) ReturnStmt(body->getRBraceLoc(),
2156+
/*value*/nullptr,
2157+
/*implicit*/true));
2158+
body = BraceStmt::create(ctx, body->getLBraceLoc(), Elts,
2159+
body->getRBraceLoc(), body->isImplicit());
2160+
}
21452161
}
21462162

21472163
// Typechecking, in particular ApplySolution is going to replace closures

0 commit comments

Comments
 (0)