Skip to content

Commit b20d037

Browse files
committed
SILGen: Enclose formal evaluation of self in a type(of:) expression during an init delegation in a formal scope.
Fixes rdar://problem/36889295.
1 parent 02fcdf1 commit b20d037

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,12 +2773,13 @@ static SILValue emitMetatypeOfDelegatingInitExclusivelyBorrowedSelf(
27732773
auto *vd = cast<ParamDecl>(dre->getDecl());
27742774
ManagedValue selfValue;
27752775

2776+
Scope S(SGF, loc);
2777+
Optional<FormalEvaluationScope> FES;
27762778
// If we have not exclusively borrowed self, we need to do so now.
27772779
if (SGF.SelfInitDelegationState == SILGenFunction::WillExclusiveBorrowSelf) {
27782780
// We need to use a full scope here to ensure that any underlying
27792781
// "normal cleanup" borrows are cleaned up.
2780-
Scope S(SGF, loc);
2781-
selfValue = S.popPreservingValue(SGF.emitRValueAsSingleValue(dre));
2782+
selfValue = SGF.emitRValueAsSingleValue(dre);
27822783
} else {
27832784
// If we already exclusively borrowed self, then we need to emit self
27842785
// using formal evaluation primitives.
@@ -2788,6 +2789,7 @@ static SILValue emitMetatypeOfDelegatingInitExclusivelyBorrowedSelf(
27882789
// This needs to be inlined since there is a Formal Evaluation Scope
27892790
// in emitRValueForDecl that causing any borrow for this LValue to be
27902791
// popped too soon.
2792+
FES.emplace(SGF);
27912793
selfValue =
27922794
SGF.emitLValueForDecl(dre, vd, dre->getType()->getCanonicalType(),
27932795
AccessKind::Read, dre->getAccessSemantics());
@@ -2796,9 +2798,7 @@ static SILValue emitMetatypeOfDelegatingInitExclusivelyBorrowedSelf(
27962798
selfValue.getLValueAddress(), ctx)
27972799
.getAsSingleValue(SGF, loc);
27982800
}
2799-
assert(selfValue && !selfValue.hasCleanup());
28002801

2801-
// Check if we need to perform a conversion here.
28022802
return SGF.B.createValueMetatype(loc, metaTy, selfValue.getValue());
28032803
}
28042804

@@ -2814,7 +2814,6 @@ SILValue SILGenFunction::emitMetatypeOfValue(SILLocation loc, Expr *baseExpr) {
28142814
SGFContext::AllowImmediatePlusZero).getValue();
28152815
return B.createExistentialMetatype(loc, metaTy, base);
28162816
}
2817-
28182817
SILType metaTy = getLoweredLoadableType(CanMetatypeType::get(baseTy));
28192818
// If the lowered metatype has a thick representation, we need to derive it
28202819
// dynamically from the instance.
@@ -2836,7 +2835,6 @@ SILValue SILGenFunction::emitMetatypeOfValue(SILLocation loc, Expr *baseExpr) {
28362835
return S.popPreservingValue(B.createValueMetatype(loc, metaTy, base))
28372836
.getValue();
28382837
}
2839-
28402838
// Otherwise, ignore the base and return the static thin metatype.
28412839
emitIgnoredExpr(baseExpr);
28422840
return B.createMetatype(loc, metaTy);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-silgen -verify %s
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
import CoreData
6+
7+
@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
8+
class Foo : NSManagedObject {
9+
convenience init(context: NSManagedObjectContext, value: Int) {
10+
self.init(entity: type(of: self).entity(), insertInto: context)
11+
}
12+
}

0 commit comments

Comments
 (0)