Skip to content

Commit 9514caa

Browse files
committed
NFC: Reorganize LifetimeDependence utils
1 parent 7fd1392 commit 9514caa

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

include/swift/AST/LifetimeDependence.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ class LifetimeDependenceInfo {
138138
unsigned index,
139139
LifetimeDependenceKind kind);
140140

141+
/// Builds LifetimeDependenceInfo from a swift decl
142+
static std::optional<LifetimeDependenceInfo>
143+
fromTypeRepr(AbstractFunctionDecl *afd);
144+
145+
/// Infer LifetimeDependenceInfo
146+
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd);
147+
141148
public:
142149
LifetimeDependenceInfo()
143150
: inheritLifetimeParamIndices(nullptr),
@@ -186,26 +193,17 @@ class LifetimeDependenceInfo {
186193
/// Builds LifetimeDependenceInfo from a swift decl, either from the explicit
187194
/// lifetime dependence specifiers or by inference based on types and
188195
/// ownership modifiers.
189-
static std::optional<LifetimeDependenceInfo>
190-
get(AbstractFunctionDecl *decl, Type resultType, bool allowIndex = false);
196+
static std::optional<LifetimeDependenceInfo> get(AbstractFunctionDecl *decl);
191197

192198
/// Builds LifetimeDependenceInfo from the bitvectors passes as parameters.
193199
static LifetimeDependenceInfo
194200
get(ASTContext &ctx, const SmallBitVector &inheritLifetimeIndices,
195201
const SmallBitVector &scopeLifetimeIndices);
196202

197-
/// Builds LifetimeDependenceInfo from a swift decl
198-
static std::optional<LifetimeDependenceInfo>
199-
fromTypeRepr(AbstractFunctionDecl *afd, Type resultType, bool allowIndex);
200-
201203
/// Builds LifetimeDependenceInfo from SIL
202204
static std::optional<LifetimeDependenceInfo>
203205
fromTypeRepr(LifetimeDependentReturnTypeRepr *lifetimeDependentRepr,
204206
SmallVectorImpl<SILParameterInfo> &params, DeclContext *dc);
205-
206-
/// Infer LifetimeDependenceInfo
207-
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd,
208-
Type resultType);
209207
};
210208

211209
} // namespace swift

lib/Sema/LifetimeDependence.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,23 @@ void LifetimeDependenceInfo::getConcatenatedData(
140140
}
141141
}
142142

143-
static bool hasEscapableResultOrYield(AbstractFunctionDecl *afd,
144-
Type resultType) {
145-
Type resultTypeInContext = afd->mapTypeIntoContext(resultType);
146-
std::optional<Type> yieldTyInContext;
147-
143+
static bool hasEscapableResultOrYield(AbstractFunctionDecl *afd) {
148144
if (auto *accessor = dyn_cast<AccessorDecl>(afd)) {
149145
if (accessor->isCoroutine()) {
150-
yieldTyInContext = accessor->getStorage()->getValueInterfaceType();
151-
yieldTyInContext = accessor->mapTypeIntoContext(*yieldTyInContext);
146+
auto yieldTyInContext = accessor->mapTypeIntoContext(
147+
accessor->getStorage()->getValueInterfaceType());
148+
return yieldTyInContext->isEscapable();
152149
}
153150
}
154-
if (resultTypeInContext->isEscapable() &&
155-
(!yieldTyInContext.has_value() || (*yieldTyInContext)->isEscapable())) {
156-
return true;
151+
152+
Type resultType;
153+
if (auto fn = dyn_cast<FuncDecl>(afd)) {
154+
resultType = fn->getResultInterfaceType();
155+
} else {
156+
auto ctor = cast<ConstructorDecl>(afd);
157+
resultType = ctor->getResultInterfaceType();
157158
}
158-
return false;
159+
return afd->mapTypeIntoContext(resultType)->isEscapable();
159160
}
160161

161162
static LifetimeDependenceKind getLifetimeDependenceKindFromDecl(
@@ -172,8 +173,7 @@ static LifetimeDependenceKind getLifetimeDependenceKindFromDecl(
172173
}
173174

174175
std::optional<LifetimeDependenceInfo>
175-
LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
176-
bool allowIndex) {
176+
LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd) {
177177
auto *dc = afd->getDeclContext();
178178
auto &ctx = dc->getASTContext();
179179
auto *mod = afd->getModuleContext();
@@ -184,7 +184,7 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
184184
auto lifetimeDependentRepr =
185185
cast<LifetimeDependentReturnTypeRepr>(afd->getResultTypeRepr());
186186

187-
if (hasEscapableResultOrYield(afd, resultType)) {
187+
if (hasEscapableResultOrYield(afd)) {
188188
diags.diagnose(lifetimeDependentRepr->getLoc(),
189189
diag::lifetime_dependence_invalid_return_type);
190190
return std::nullopt;
@@ -389,7 +389,7 @@ std::optional<LifetimeDependenceInfo> LifetimeDependenceInfo::fromTypeRepr(
389389
}
390390

391391
std::optional<LifetimeDependenceInfo>
392-
LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
392+
LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
393393
auto *dc = afd->getDeclContext();
394394
auto &ctx = dc->getASTContext();
395395
auto *mod = afd->getModuleContext();
@@ -403,7 +403,7 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
403403
return std::nullopt;
404404
}
405405

406-
if (hasEscapableResultOrYield(afd, resultType)) {
406+
if (hasEscapableResultOrYield(afd)) {
407407
return std::nullopt;
408408
}
409409

@@ -423,7 +423,7 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
423423
}
424424
}
425425

426-
if (afd->getKind() != DeclKind::Constructor && afd->hasImplicitSelfDecl()) {
426+
if (!cd && afd->hasImplicitSelfDecl()) {
427427
Type selfTypeInContext = dc->getSelfTypeInContext();
428428
if (selfTypeInContext->isEscapable()) {
429429
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
@@ -475,7 +475,7 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
475475
continue;
476476
}
477477
if (candidateParam) {
478-
if (afd->getKind() == DeclKind::Constructor && afd->isImplicit()) {
478+
if (cd && afd->isImplicit()) {
479479
diags.diagnose(
480480
returnLoc,
481481
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
@@ -493,7 +493,7 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
493493
}
494494

495495
if (!candidateParam && !hasParamError) {
496-
if (afd->getKind() == DeclKind::Constructor && afd->isImplicit()) {
496+
if (cd && afd->isImplicit()) {
497497
diags.diagnose(returnLoc,
498498
diag::lifetime_dependence_cannot_infer_no_candidates,
499499
"on implicit initializer");
@@ -508,13 +508,13 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
508508
}
509509

510510
std::optional<LifetimeDependenceInfo>
511-
LifetimeDependenceInfo::get(AbstractFunctionDecl *afd, Type resultType,
512-
bool allowIndex) {
511+
LifetimeDependenceInfo::get(AbstractFunctionDecl *afd) {
512+
assert(isa<FuncDecl>(afd) || isa<ConstructorDecl>(afd));
513513
auto *returnTypeRepr = afd->getResultTypeRepr();
514514
if (isa_and_nonnull<LifetimeDependentReturnTypeRepr>(returnTypeRepr)) {
515-
return LifetimeDependenceInfo::fromTypeRepr(afd, resultType, allowIndex);
515+
return LifetimeDependenceInfo::fromTypeRepr(afd);
516516
}
517-
return LifetimeDependenceInfo::infer(afd, resultType);
517+
return LifetimeDependenceInfo::infer(afd);
518518
}
519519

520520
LifetimeDependenceInfo

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,14 +3101,5 @@ ImplicitKnownProtocolConformanceRequest::evaluate(Evaluator &evaluator,
31013101
std::optional<LifetimeDependenceInfo>
31023102
LifetimeDependenceInfoRequest::evaluate(Evaluator &evaluator,
31033103
AbstractFunctionDecl *decl) const {
3104-
Type resultTy;
3105-
if (auto fn = dyn_cast<FuncDecl>(decl)) {
3106-
resultTy = fn->getResultInterfaceType();
3107-
} else if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
3108-
resultTy = ctor->getResultInterfaceType();
3109-
} else {
3110-
return std::nullopt;
3111-
}
3112-
3113-
return LifetimeDependenceInfo::get(decl, resultTy);
3104+
return LifetimeDependenceInfo::get(decl);
31143105
}

0 commit comments

Comments
 (0)