Skip to content

Commit eafba9b

Browse files
Merge pull request #62149 from nate-chandler/opaque-values/1/20221116
[AddressLowering] Fix synthesized ParamDecl.
2 parents c5a7eff + 80e0bfc commit eafba9b

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
#define DEBUG_TYPE "address-lowering"
136136

137137
#include "PhiStorageOptimizer.h"
138+
#include "swift/AST/Decl.h"
138139
#include "swift/Basic/BlotSetVector.h"
139140
#include "swift/Basic/Range.h"
140141
#include "swift/SIL/BasicBlockUtils.h"
@@ -552,13 +553,20 @@ static unsigned insertIndirectReturnArgs(AddressLoweringState &pass) {
552553
auto &astCtx = pass.getModule()->getASTContext();
553554
auto typeCtx = pass.function->getTypeExpansionContext();
554555
auto *declCtx = pass.function->getDeclContext();
556+
if (!declCtx) {
557+
// Fall back to using the module as the decl context if the function
558+
// doesn't have one. The can happen with default argument getters, for
559+
// example.
560+
declCtx = pass.function->getModule().getSwiftModule();
561+
}
555562

556563
unsigned argIdx = 0;
557564
for (auto resultTy : pass.loweredFnConv.getIndirectSILResultTypes(typeCtx)) {
558565
auto bodyResultTy = pass.function->mapTypeIntoContext(resultTy);
559566
auto var = new (astCtx) ParamDecl(
560567
SourceLoc(), SourceLoc(), astCtx.getIdentifier("$return_value"),
561568
SourceLoc(), astCtx.getIdentifier("$return_value"), declCtx);
569+
var->setSpecifier(ParamSpecifier::InOut);
562570

563571
SILFunctionArgument *funcArg =
564572
pass.function->begin()->insertFunctionArgument(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-frontend -enable-sil-opaque-values -parse-as-library -emit-sil -O %s | %FileCheck %s
2+
3+
// Verify the arguments. When AccessPathVerification runs, it will be checked
4+
// that the ParamDecl that AddressLowering synthesizes has a specifier
5+
// (ParamSpecifier) set.
6+
// CHECK-LABEL: sil @$s15opaque_values_O3minyxx_xtSLRzlF : {{.*}} {
7+
// CHECK: bb0(%0 : $*T, %1 : $*T, %2 : $*T):
8+
// CHECK-LABEL: } // end sil function '$s15opaque_values_O3minyxx_xtSLRzlF'
9+
public func min<T: Comparable>(_ x: T, _ y: T) -> T {
10+
return y < x ? y : x
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-frontend -enable-sil-opaque-values -parse-as-library -emit-sil -O -enable-library-evolution %s | %FileCheck %s
2+
3+
public struct Gadget {
4+
// Verify the default accessor's arguments. When AccessPathVerification runs,
5+
// it will be checked that the ParamDecl that AddressLowering synthesizes has a
6+
// non-null decl context.
7+
// CHECK-LABEL: sil @$s25opaque_values_O_resilient6GadgetV5whichA2C5WhichO_tcfC : {{.*}} {
8+
// CHECK: bb0(%0 : $*Gadget, %1 : $*Gadget.Which, %2 : $@thin Gadget.Type):
9+
// CHECK-LABEL: } // end sil function '$s25opaque_values_O_resilient6GadgetV5whichA2C5WhichO_tcfC'
10+
public init(which: Which = .that) {
11+
fatalError()
12+
}
13+
14+
public enum Which {
15+
case that
16+
case this(() -> Gadget)
17+
case theOther
18+
}
19+
}

0 commit comments

Comments
 (0)