Skip to content

Commit 3b08349

Browse files
committed
SIL: Support element archetypes inside lowerCaptureContextParameters()
1 parent a5d127e commit 3b08349

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/DiagnosticsSIL.h"
2525
#include "swift/AST/ForeignInfo.h"
2626
#include "swift/AST/GenericEnvironment.h"
27+
#include "swift/AST/LocalArchetypeRequirementCollector.h"
2728
#include "swift/AST/Module.h"
2829
#include "swift/AST/ModuleLoader.h"
2930
#include "swift/AST/TypeCheckRequests.h"
@@ -1896,11 +1897,7 @@ void updateResultTypeForForeignInfo(
18961897
llvm_unreachable("unhandled kind");
18971898
}
18981899

1899-
/// Lower any/all capture context parameters.
1900-
///
1901-
/// *NOTE* Currently default arg generators can not capture anything.
1902-
/// If we ever add that ability, it will be a different capture list
1903-
/// from the function to which the argument is attached.
1900+
/// Captured values become SIL function parameters in this function.
19041901
static void
19051902
lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19061903
CanGenericSignature genericSig,
@@ -1924,28 +1921,38 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19241921
// signature from the AST for that.
19251922
auto origGenericSig = function.getAnyFunctionRef()->getGenericSignature();
19261923
auto loweredCaptures = TC.getLoweredLocalCaptures(function);
1924+
auto capturedEnvs = loweredCaptures.getGenericEnvironments();
19271925
auto *isolatedParam = loweredCaptures.getIsolatedParamCapture();
19281926

1927+
auto mapTypeOutOfContext = [&](Type t) -> CanType {
1928+
LLVM_DEBUG(llvm::dbgs() << "-- capture with contextual type " << t << "\n");
1929+
1930+
t = t.subst(MapLocalArchetypesOutOfContext(origGenericSig, capturedEnvs),
1931+
MakeAbstractConformanceForGenericType(),
1932+
SubstFlags::PreservePackExpansionLevel);
1933+
1934+
LLVM_DEBUG(llvm::dbgs() << "-- maps to " << t->getCanonicalType() << "\n");
1935+
return t->getCanonicalType();
1936+
};
1937+
19291938
for (auto capture : loweredCaptures.getCaptures()) {
19301939
if (capture.isDynamicSelfMetadata()) {
19311940
ParameterConvention convention = ParameterConvention::Direct_Unowned;
19321941
auto dynamicSelfInterfaceType =
1933-
loweredCaptures.getDynamicSelfType()->mapTypeOutOfContext();
1942+
mapTypeOutOfContext(loweredCaptures.getDynamicSelfType());
19341943

1935-
auto selfMetatype = MetatypeType::get(dynamicSelfInterfaceType,
1936-
MetatypeRepresentation::Thick);
1944+
auto selfMetatype = CanMetatypeType::get(dynamicSelfInterfaceType,
1945+
MetatypeRepresentation::Thick);
19371946

1938-
auto canSelfMetatype = selfMetatype->getReducedType(origGenericSig);
1939-
SILParameterInfo param(canSelfMetatype, convention);
1947+
SILParameterInfo param(selfMetatype, convention);
19401948
inputs.push_back(param);
19411949

19421950
continue;
19431951
}
19441952

19451953
if (capture.isOpaqueValue()) {
19461954
OpaqueValueExpr *opaqueValue = capture.getOpaqueValue();
1947-
auto canType = opaqueValue->getType()->mapTypeOutOfContext()
1948-
->getReducedType(origGenericSig);
1955+
auto canType = mapTypeOutOfContext(opaqueValue->getType());
19491956
auto &loweredTL =
19501957
TC.getTypeLowering(AbstractionPattern(genericSig, canType),
19511958
canType, expansion);
@@ -1963,9 +1970,16 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19631970
continue;
19641971
}
19651972

1966-
auto *varDecl = capture.getDecl();
1967-
auto type = varDecl->getInterfaceType();
1968-
auto canType = type->getReducedType(origGenericSig);
1973+
auto *varDecl = cast<VarDecl>(capture.getDecl());
1974+
1975+
auto type = varDecl->getTypeInContext();
1976+
assert(!type->hasLocalArchetype() ||
1977+
(genericSig && origGenericSig &&
1978+
!genericSig->isEqual(origGenericSig)));
1979+
type = mapTypeOutOfContext(type);
1980+
1981+
auto canType = type->getReducedType(
1982+
genericSig ? genericSig : origGenericSig);
19691983

19701984
auto options = SILParameterInfo::Options();
19711985
if (isolatedParam == varDecl) {

0 commit comments

Comments
 (0)