24
24
#include " swift/AST/DiagnosticsSIL.h"
25
25
#include " swift/AST/ForeignInfo.h"
26
26
#include " swift/AST/GenericEnvironment.h"
27
+ #include " swift/AST/LocalArchetypeRequirementCollector.h"
27
28
#include " swift/AST/Module.h"
28
29
#include " swift/AST/ModuleLoader.h"
29
30
#include " swift/AST/TypeCheckRequests.h"
@@ -1896,11 +1897,7 @@ void updateResultTypeForForeignInfo(
1896
1897
llvm_unreachable (" unhandled kind" );
1897
1898
}
1898
1899
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.
1904
1901
static void
1905
1902
lowerCaptureContextParameters (TypeConverter &TC, SILDeclRef function,
1906
1903
CanGenericSignature genericSig,
@@ -1924,28 +1921,38 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
1924
1921
// signature from the AST for that.
1925
1922
auto origGenericSig = function.getAnyFunctionRef ()->getGenericSignature ();
1926
1923
auto loweredCaptures = TC.getLoweredLocalCaptures (function);
1924
+ auto capturedEnvs = loweredCaptures.getGenericEnvironments ();
1927
1925
auto *isolatedParam = loweredCaptures.getIsolatedParamCapture ();
1928
1926
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
+
1929
1938
for (auto capture : loweredCaptures.getCaptures ()) {
1930
1939
if (capture.isDynamicSelfMetadata ()) {
1931
1940
ParameterConvention convention = ParameterConvention::Direct_Unowned;
1932
1941
auto dynamicSelfInterfaceType =
1933
- loweredCaptures.getDynamicSelfType ()-> mapTypeOutOfContext ( );
1942
+ mapTypeOutOfContext ( loweredCaptures.getDynamicSelfType ());
1934
1943
1935
- auto selfMetatype = MetatypeType ::get (dynamicSelfInterfaceType,
1936
- MetatypeRepresentation::Thick);
1944
+ auto selfMetatype = CanMetatypeType ::get (dynamicSelfInterfaceType,
1945
+ MetatypeRepresentation::Thick);
1937
1946
1938
- auto canSelfMetatype = selfMetatype->getReducedType (origGenericSig);
1939
- SILParameterInfo param (canSelfMetatype, convention);
1947
+ SILParameterInfo param (selfMetatype, convention);
1940
1948
inputs.push_back (param);
1941
1949
1942
1950
continue ;
1943
1951
}
1944
1952
1945
1953
if (capture.isOpaqueValue ()) {
1946
1954
OpaqueValueExpr *opaqueValue = capture.getOpaqueValue ();
1947
- auto canType = opaqueValue->getType ()->mapTypeOutOfContext ()
1948
- ->getReducedType (origGenericSig);
1955
+ auto canType = mapTypeOutOfContext (opaqueValue->getType ());
1949
1956
auto &loweredTL =
1950
1957
TC.getTypeLowering (AbstractionPattern (genericSig, canType),
1951
1958
canType, expansion);
@@ -1963,9 +1970,16 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
1963
1970
continue ;
1964
1971
}
1965
1972
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);
1969
1983
1970
1984
auto options = SILParameterInfo::Options ();
1971
1985
if (isolatedParam == varDecl) {
0 commit comments