@@ -103,6 +103,24 @@ void LifetimeDependenceInfo::getConcatenatedData(
103
103
}
104
104
}
105
105
106
+ static bool hasEscapableResultOrYield (AbstractFunctionDecl *afd,
107
+ Type resultType) {
108
+ Type resultTypeInContext = afd->mapTypeIntoContext (resultType);
109
+ std::optional<Type> yieldTyInContext;
110
+
111
+ if (auto *accessor = dyn_cast<AccessorDecl>(afd)) {
112
+ if (accessor->isCoroutine ()) {
113
+ yieldTyInContext = accessor->getStorage ()->getValueInterfaceType ();
114
+ yieldTyInContext = accessor->mapTypeIntoContext (*yieldTyInContext);
115
+ }
116
+ }
117
+ if (resultTypeInContext->isEscapable () &&
118
+ (!yieldTyInContext.has_value () || (*yieldTyInContext)->isEscapable ())) {
119
+ return true ;
120
+ }
121
+ return false ;
122
+ }
123
+
106
124
std::optional<LifetimeDependenceInfo>
107
125
LifetimeDependenceInfo::fromTypeRepr (AbstractFunctionDecl *afd, Type resultType,
108
126
bool allowIndex) {
@@ -114,6 +132,12 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
114
132
auto lifetimeDependentRepr =
115
133
cast<LifetimeDependentReturnTypeRepr>(afd->getResultTypeRepr ());
116
134
135
+ if (hasEscapableResultOrYield (afd, resultType)) {
136
+ diags.diagnose (lifetimeDependentRepr->getLoc (),
137
+ diag::lifetime_dependence_invalid_return_type);
138
+ return std::nullopt;
139
+ }
140
+
117
141
SmallBitVector inheritLifetimeParamIndices (capacity);
118
142
SmallBitVector scopeLifetimeParamIndices (capacity);
119
143
@@ -122,11 +146,6 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
122
146
ValueOwnership ownership) {
123
147
auto loc = specifier.getLoc ();
124
148
auto kind = specifier.getLifetimeDependenceKind ();
125
- Type resultTypeInContext = afd->mapTypeIntoContext (resultType);
126
- if (resultTypeInContext->isEscapable ()) {
127
- diags.diagnose (loc, diag::lifetime_dependence_invalid_return_type);
128
- return true ;
129
- }
130
149
131
150
if (ownership == ValueOwnership::Default) {
132
151
diags.diagnose (loc, diag::lifetime_dependence_missing_ownership_modifier);
@@ -274,19 +293,11 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
274
293
auto &diags = ctx.Diags ;
275
294
auto returnTypeRepr = afd->getResultTypeRepr ();
276
295
auto returnLoc = returnTypeRepr ? returnTypeRepr->getLoc () : afd->getLoc ();
277
- Type returnTyInContext = afd->mapTypeIntoContext (resultType);
278
- std::optional<Type> yieldTyInContext;
279
296
280
- if (auto *accessor = dyn_cast<AccessorDecl>(afd)) {
281
- if (accessor->isCoroutine ()) {
282
- yieldTyInContext = accessor->getStorage ()->getValueInterfaceType ();
283
- yieldTyInContext = accessor->mapTypeIntoContext (*yieldTyInContext);
284
- }
285
- }
286
- if (returnTyInContext->isEscapable () &&
287
- (!yieldTyInContext.has_value () || (*yieldTyInContext)->isEscapable ())) {
297
+ if (hasEscapableResultOrYield (afd, resultType)) {
288
298
return std::nullopt;
289
299
}
300
+
290
301
if (afd->getAttrs ().hasAttribute <UnsafeNonEscapableResultAttr>()) {
291
302
return std::nullopt;
292
303
}
0 commit comments