@@ -103,6 +103,24 @@ void LifetimeDependenceInfo::getConcatenatedData(
103103 }
104104}
105105
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+
106124std::optional<LifetimeDependenceInfo>
107125LifetimeDependenceInfo::fromTypeRepr (AbstractFunctionDecl *afd, Type resultType,
108126 bool allowIndex) {
@@ -114,6 +132,12 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
114132 auto lifetimeDependentRepr =
115133 cast<LifetimeDependentReturnTypeRepr>(afd->getResultTypeRepr ());
116134
135+ if (hasEscapableResultOrYield (afd, resultType)) {
136+ diags.diagnose (lifetimeDependentRepr->getLoc (),
137+ diag::lifetime_dependence_invalid_return_type);
138+ return std::nullopt ;
139+ }
140+
117141 SmallBitVector inheritLifetimeParamIndices (capacity);
118142 SmallBitVector scopeLifetimeParamIndices (capacity);
119143
@@ -122,11 +146,6 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
122146 ValueOwnership ownership) {
123147 auto loc = specifier.getLoc ();
124148 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- }
130149
131150 if (ownership == ValueOwnership::Default) {
132151 diags.diagnose (loc, diag::lifetime_dependence_missing_ownership_modifier);
@@ -274,19 +293,11 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
274293 auto &diags = ctx.Diags ;
275294 auto returnTypeRepr = afd->getResultTypeRepr ();
276295 auto returnLoc = returnTypeRepr ? returnTypeRepr->getLoc () : afd->getLoc ();
277- Type returnTyInContext = afd->mapTypeIntoContext (resultType);
278- std::optional<Type> yieldTyInContext;
279296
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)) {
288298 return std::nullopt ;
289299 }
300+
290301 if (afd->getAttrs ().hasAttribute <UnsafeNonEscapableResultAttr>()) {
291302 return std::nullopt ;
292303 }
0 commit comments