Skip to content

Commit fcebcd3

Browse files
Merge pull request #81058 from nate-chandler/rdar149782365
[SILGen] Make eagerMove class init's self lexical.
2 parents d827c6f + 96eb85c commit fcebcd3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/SILGen/SILGenConstructor.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,15 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
11141114
/*ignored parameters*/ 1);
11151115

11161116
SILType selfTy = getLoweredLoadableType(selfDecl->getTypeInContext());
1117-
ManagedValue selfArg = B.createInputFunctionArgument(selfTy, selfDecl);
1118-
1117+
// Force a lexical lifetime for the self argument of an eagerMove class' init
1118+
// to ensure that the body of its deinit always runs after the body of that
1119+
// init.
1120+
LifetimeAnnotation annotation = selfTy.getLifetime(F).isEagerMove()
1121+
? LifetimeAnnotation::Lexical
1122+
: LifetimeAnnotation::None;
1123+
ManagedValue selfArg = B.createInputFunctionArgument(
1124+
selfTy, selfDecl, /*isNoImplicitCopy=*/false, annotation);
1125+
11191126
// is this a designated initializer for a distributed actor?
11201127
const bool isDesignatedDistActorInit =
11211128
selfClassDecl->isDistributedActor() && !isDelegating;

test/Interpreter/rdar149782365.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-run-simple-swift | %FileCheck %s
2+
3+
@_silgen_name("start")
4+
func start() {
5+
print("init C")
6+
}
7+
@_silgen_name("barrier")
8+
func barrier() {
9+
print("nothing uses C anymore")
10+
}
11+
@_silgen_name("end")
12+
func end() {
13+
print("deinit C")
14+
}
15+
16+
@_eagerMove class C {
17+
init() { start() }
18+
deinit { end() }
19+
}
20+
21+
@_silgen_name("doit")
22+
public func main() {
23+
C()
24+
barrier()
25+
}
26+
27+
main()
28+
29+
// CHECK: init C
30+
// CHECK: deinit C
31+
// CHECK: nothing uses C anymore

0 commit comments

Comments
 (0)