Skip to content

Commit 27b27a4

Browse files
committed
[Profiler] Fix autoclosure property wrapper crash
We need to query the top-level expression, not the sub-expression of a potential function conversion. rdar://99449154
1 parent 7e141f4 commit 27b27a4

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
981981

982982
void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
983983
bool EmitProfilerIncrement) {
984+
auto *const topLevelValue = value;
984985
auto *dc = function.getDecl()->getInnermostDeclContext();
985986
MagicFunctionName = SILGenModule::getMagicFunctionName(function);
986987

@@ -1034,8 +1035,12 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
10341035
auto interfaceType = value->getType()->mapTypeOutOfContext();
10351036
emitProlog(captureInfo, params, /*selfParam=*/nullptr,
10361037
dc, interfaceType, /*throws=*/false, SourceLoc());
1037-
if (EmitProfilerIncrement)
1038-
emitProfilerIncrement(value);
1038+
if (EmitProfilerIncrement) {
1039+
// Emit a profiler increment for the top-level value, not looking through
1040+
// any function conversions. This is necessary as the counter would have
1041+
// been recorded for this expression, not the sub-expression.
1042+
emitProfilerIncrement(topLevelValue);
1043+
}
10391044
prepareEpilog(interfaceType, false, CleanupLocation(Loc));
10401045

10411046
{

test/Profiler/coverage_var_init.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ final class VarInit {
3636
// CHECK-NEXT: [[@LINE+1]]:4 -> [[@LINE+1]]:42 : 0
3737
@Wrapper var initializedWrapperInit = 2
3838

39+
// CHECK: sil_coverage_map {{.*}} // variable initialization expression of coverage_var_init.VarInit.(_autoclosureWrapperInit
40+
// CHECK-NEXT: [[@LINE+1]]:52 -> [[@LINE+1]]:53
41+
@AutoClosureWrapper var autoclosureWrapperInit = 3
42+
3943
// CHECK: sil_coverage_map {{.*}} "$s17coverage_var_init7VarInitC04lazydE033_49373CB2DFB47C8DC62FA963604688DFLLSSvgSSyXEfU_"
4044
// CHECK-NEXT: [[@LINE+1]]:42 -> [[@LINE+3]]:4 : 0
4145
private lazy var lazyVarInit: String = {
@@ -67,4 +71,12 @@ final class VarInit {
6771
var wrappedValue: Int { 1 }
6872
}
6973

74+
@propertyWrapper
75+
struct AutoClosureWrapper<T> {
76+
var wrappedValue: T
77+
init(wrappedValue: @autoclosure () -> T) {
78+
self.wrappedValue = wrappedValue()
79+
}
80+
}
81+
7082
VarInit().coverageFunction()

0 commit comments

Comments
 (0)