@@ -2236,12 +2236,14 @@ namespace {
2236
2236
MoveOnlyAttr (/* Implicit=*/ true ));
2237
2237
}
2238
2238
2239
+ bool isNonEscapable = false ;
2239
2240
if (evaluateOrDefault (
2240
2241
Impl.SwiftContext .evaluator ,
2241
2242
ClangTypeEscapability ({decl->getTypeForDecl (), Impl}),
2242
2243
CxxEscapability::Unknown) == CxxEscapability::NonEscapable) {
2243
2244
result->getAttrs ().add (new (Impl.SwiftContext )
2244
2245
NonEscapableAttr (/* Implicit=*/ true ));
2246
+ isNonEscapable = true ;
2245
2247
}
2246
2248
2247
2249
// FIXME: Figure out what to do with superclasses in C++. One possible
@@ -2386,6 +2388,19 @@ namespace {
2386
2388
synthesizer.createValueConstructor (result, member,
2387
2389
/* want param names*/ true ,
2388
2390
/* wantBody=*/ true );
2391
+
2392
+ if (Impl.SwiftContext .LangOpts .hasFeature (
2393
+ Feature::LifetimeDependence) &&
2394
+ isNonEscapable) {
2395
+ valueCtor->getAttrs ().add (
2396
+ new (Impl.SwiftContext )
2397
+ UnsafeNonEscapableResultAttr (/* Implicit=*/ true ));
2398
+ if (Impl.SwiftContext .LangOpts .hasFeature (Feature::SafeInterop) &&
2399
+ Impl.SwiftContext .LangOpts .hasFeature (
2400
+ Feature::AllowUnsafeAttribute))
2401
+ valueCtor->getAttrs ().add (new (Impl.SwiftContext )
2402
+ UnsafeAttr (/* Implicit=*/ true ));
2403
+ }
2389
2404
ctors.push_back (valueCtor);
2390
2405
}
2391
2406
// TODO: we have a problem lazily looking up members of an unnamed
@@ -2410,7 +2425,13 @@ namespace {
2410
2425
(!cxxRecordDecl->hasDefaultConstructor () ||
2411
2426
cxxRecordDecl->ctors ().empty ());
2412
2427
}
2413
- if (hasZeroInitializableStorage && needsEmptyInitializer) {
2428
+
2429
+ // TODO: builtin "zeroInitializer" does not work with non-escapable
2430
+ // types yet. Don't generate an initializer.
2431
+ if (hasZeroInitializableStorage && needsEmptyInitializer &&
2432
+ (!Impl.SwiftContext .LangOpts .hasFeature (
2433
+ Feature::LifetimeDependence) ||
2434
+ !isNonEscapable)) {
2414
2435
// Add default constructor for the struct if compiling in C mode.
2415
2436
// If we're compiling for C++:
2416
2437
// 1. If a default constructor is declared, don't synthesize one.
@@ -2423,7 +2444,6 @@ namespace {
2423
2444
// constructor available in Swift.
2424
2445
ConstructorDecl *defaultCtor =
2425
2446
synthesizer.createDefaultConstructor (result);
2426
- ctors.push_back (defaultCtor);
2427
2447
if (cxxRecordDecl) {
2428
2448
auto attr = AvailableAttr::createUniversallyDeprecated (
2429
2449
defaultCtor->getASTContext (),
@@ -2433,6 +2453,7 @@ namespace {
2433
2453
" " );
2434
2454
defaultCtor->getAttrs ().add (attr);
2435
2455
}
2456
+ ctors.push_back (defaultCtor);
2436
2457
}
2437
2458
2438
2459
bool forceMemberwiseInitializer = false ;
@@ -2462,6 +2483,19 @@ namespace {
2462
2483
if (!hasUnreferenceableStorage)
2463
2484
valueCtor->setIsMemberwiseInitializer ();
2464
2485
2486
+ if (Impl.SwiftContext .LangOpts .hasFeature (
2487
+ Feature::LifetimeDependence) &&
2488
+ isNonEscapable) {
2489
+ valueCtor->getAttrs ().add (
2490
+ new (Impl.SwiftContext )
2491
+ UnsafeNonEscapableResultAttr (/* Implicit=*/ true ));
2492
+ if (Impl.SwiftContext .LangOpts .hasFeature (Feature::SafeInterop) &&
2493
+ Impl.SwiftContext .LangOpts .hasFeature (
2494
+ Feature::AllowUnsafeAttribute))
2495
+ valueCtor->getAttrs ().add (new (Impl.SwiftContext )
2496
+ UnsafeAttr (/* Implicit=*/ true ));
2497
+ }
2498
+
2465
2499
ctors.push_back (valueCtor);
2466
2500
}
2467
2501
0 commit comments