@@ -286,6 +286,12 @@ class LifetimeDependenceChecker {
286
286
}
287
287
}
288
288
289
+ LifetimeDependenceChecker (EnumElementDecl *eed)
290
+ : decl(eed), dc(eed->getDeclContext ()), ctx(dc->getASTContext ()) {
291
+ auto *paramList = eed->getParameterList ();
292
+ resultIndex = paramList ? eed->getParameterList ()->size () + 1 : 1 ;
293
+ }
294
+
289
295
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
290
296
currentDependencies () const {
291
297
if (lifetimeDependencies.empty ()) {
@@ -351,6 +357,45 @@ class LifetimeDependenceChecker {
351
357
return currentDependencies ();
352
358
}
353
359
360
+ std::optional<llvm::ArrayRef<LifetimeDependenceInfo>> checkEnumElementDecl () {
361
+ auto *eed = cast<EnumElementDecl>(decl);
362
+ auto enumType = eed->getParentEnum ()->mapTypeIntoContext (
363
+ eed->getParentEnum ()->getDeclaredInterfaceType ());
364
+ // Escapable enum, bailout.
365
+ if (!isDiagnosedNonEscapable (enumType)) {
366
+ return std::nullopt ;
367
+ }
368
+ auto *params = eed->getParameterList ();
369
+ // No payload, bailout.
370
+ if (!params) {
371
+ return std::nullopt ;
372
+ }
373
+
374
+ auto resultIndex = params->size () + /* selfType*/ 1 ;
375
+ auto capacity = resultIndex + 1 ;
376
+ SmallBitVector inheritIndices (capacity);
377
+ SmallVector<LifetimeDependenceInfo, 1 > lifetimeDependencies;
378
+
379
+ // Add all indices of ~Escapable parameters as lifetime dependence sources.
380
+ for (size_t i = 0 ; i < params->size (); i++) {
381
+ auto paramType = params->get (i)->getTypeInContext ();
382
+ if (!isDiagnosedNonEscapable (paramType)) {
383
+ continue ;
384
+ }
385
+ inheritIndices.set (i);
386
+ }
387
+ if (inheritIndices.none ()) {
388
+ return std::nullopt ;
389
+ }
390
+ auto lifetimeDependenceInfo = LifetimeDependenceInfo (
391
+ IndexSubset::get (eed->getASTContext (), inheritIndices), nullptr ,
392
+ resultIndex,
393
+ /* isImmortal*/ false );
394
+ lifetimeDependencies.push_back (lifetimeDependenceInfo);
395
+
396
+ return eed->getASTContext ().AllocateCopy (lifetimeDependencies);
397
+ }
398
+
354
399
protected:
355
400
template <typename ...ArgTypes>
356
401
InFlightDiagnostic diagnose (
@@ -1417,8 +1462,12 @@ class LifetimeDependenceChecker {
1417
1462
};
1418
1463
1419
1464
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
1420
- LifetimeDependenceInfo::get (AbstractFunctionDecl *afd) {
1421
- return LifetimeDependenceChecker (afd).checkFuncDecl ();
1465
+ LifetimeDependenceInfo::get (ValueDecl *decl) {
1466
+ if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
1467
+ return LifetimeDependenceChecker (afd).checkFuncDecl ();
1468
+ }
1469
+ auto *eed = cast<EnumElementDecl>(decl);
1470
+ return LifetimeDependenceChecker (eed).checkEnumElementDecl ();
1422
1471
}
1423
1472
1424
1473
// This implements the logic for SIL type descriptors similar to source-level
0 commit comments