@@ -1214,6 +1214,78 @@ AbstractionPattern AbstractionPattern::getFunctionResultType() const {
1214
1214
llvm_unreachable (" bad kind" );
1215
1215
}
1216
1216
1217
+ llvm::Optional<AbstractionPattern>
1218
+ AbstractionPattern::getFunctionThrownErrorType () const {
1219
+ switch (getKind ()) {
1220
+ case Kind::Invalid:
1221
+ llvm_unreachable (" querying invalid abstraction pattern!" );
1222
+ case Kind::ObjCCompletionHandlerArgumentsType:
1223
+ case Kind::Tuple:
1224
+ llvm_unreachable (" abstraction pattern for tuple cannot be function" );
1225
+ case Kind::Opaque:
1226
+ return *this ;
1227
+ case Kind::Type: {
1228
+ if (isTypeParameterOrOpaqueArchetype ())
1229
+ return getOpaque ();
1230
+
1231
+ if (auto errorType = cast<AnyFunctionType>(getType ())->getEffectiveThrownErrorType ()) {
1232
+ return AbstractionPattern (getGenericSubstitutions (),
1233
+ getGenericSignatureForFunctionComponent (),
1234
+ (*errorType)->getCanonicalType ());
1235
+ }
1236
+
1237
+ return llvm::None;
1238
+ }
1239
+ case Kind::Discard:
1240
+ llvm_unreachable (" don't need to discard function abstractions yet" );
1241
+ case Kind::ClangType:
1242
+ case Kind::CFunctionAsMethodType:
1243
+ case Kind::PartialCurriedCFunctionAsMethodType:
1244
+ case Kind::CXXMethodType:
1245
+ case Kind::PartialCurriedCXXMethodType:
1246
+ case Kind::CurriedObjCMethodType:
1247
+ case Kind::CurriedCFunctionAsMethodType:
1248
+ case Kind::CurriedCXXMethodType:
1249
+ case Kind::PartialCurriedObjCMethodType:
1250
+ case Kind::ObjCMethodType:
1251
+ llvm_unreachable (" implement me" );
1252
+ case Kind::OpaqueFunction:
1253
+ case Kind::OpaqueDerivativeFunction:
1254
+ return llvm::None;
1255
+ }
1256
+ llvm_unreachable (" bad kind" );
1257
+ }
1258
+
1259
+ llvm::Optional<std::pair<AbstractionPattern, CanType>>
1260
+ AbstractionPattern::getFunctionThrownErrorType (
1261
+ CanAnyFunctionType substFnInterfaceType) const {
1262
+ auto optOrigErrorType = getFunctionThrownErrorType ();
1263
+ if (!optOrigErrorType)
1264
+ return llvm::None;
1265
+
1266
+ auto &ctx = substFnInterfaceType->getASTContext ();
1267
+ auto optErrorType = substFnInterfaceType->getEffectiveThrownErrorType ();
1268
+
1269
+ if (isTypeParameterOrOpaqueArchetype ()) {
1270
+ if (!optErrorType)
1271
+ return llvm::None;
1272
+
1273
+ if (!(*optErrorType)->isEqual (ctx.getErrorExistentialType ())) {
1274
+ llvm::errs () << " unsupported reabstraction\n " ;
1275
+ abort ();
1276
+ }
1277
+
1278
+ return std::make_pair (AbstractionPattern (*optErrorType),
1279
+ (*optErrorType)->getCanonicalType ());
1280
+ }
1281
+
1282
+ if (!optErrorType)
1283
+ optErrorType = ctx.getErrorExistentialType ();
1284
+
1285
+ return std::make_pair (*optOrigErrorType,
1286
+ (*optErrorType)->getCanonicalType ());
1287
+ }
1288
+
1217
1289
AbstractionPattern
1218
1290
AbstractionPattern::getObjCMethodAsyncCompletionHandlerType (
1219
1291
CanType swiftCompletionHandlerType) const {
@@ -1939,7 +2011,7 @@ AbstractionPattern::getParameterConvention(TypeConverter &TC) const {
1939
2011
case Kind::Opaque:
1940
2012
// Maximally abstracted values are always passed indirectly.
1941
2013
return Indirect;
1942
-
2014
+
1943
2015
case Kind::OpaqueFunction:
1944
2016
case Kind::OpaqueDerivativeFunction:
1945
2017
case Kind::PartialCurriedObjCMethodType:
@@ -1953,16 +2025,57 @@ AbstractionPattern::getParameterConvention(TypeConverter &TC) const {
1953
2025
case Kind::PartialCurriedCXXMethodType:
1954
2026
// Function types are always passed directly
1955
2027
return Direct;
1956
-
2028
+
1957
2029
case Kind::ClangType:
1958
2030
case Kind::Type:
1959
2031
case Kind::Discard:
1960
2032
// Pass according to the formal type.
1961
2033
return SILType::isFormallyPassedIndirectly (getType (),
1962
- TC,
1963
- getGenericSignatureOrNull ())
2034
+ TC,
2035
+ getGenericSignatureOrNull ())
1964
2036
? Indirect : Direct;
1965
-
2037
+
2038
+ case Kind::Invalid:
2039
+ case Kind::Tuple:
2040
+ case Kind::ObjCCompletionHandlerArgumentsType:
2041
+ llvm_unreachable (" should not get here" );
2042
+ }
2043
+ }
2044
+
2045
+ AbstractionPattern::CallingConventionKind
2046
+ AbstractionPattern::getErrorConvention (TypeConverter &TC) const {
2047
+ // Tuples should be destructured.
2048
+ if (isTuple ()) {
2049
+ return Destructured;
2050
+ }
2051
+ switch (getKind ()) {
2052
+ case Kind::Opaque:
2053
+ // Maximally abstracted values are always thrown indirectly.
2054
+ return Indirect;
2055
+
2056
+ case Kind::OpaqueFunction:
2057
+ case Kind::OpaqueDerivativeFunction:
2058
+ case Kind::PartialCurriedObjCMethodType:
2059
+ case Kind::CurriedObjCMethodType:
2060
+ case Kind::PartialCurriedCFunctionAsMethodType:
2061
+ case Kind::CurriedCFunctionAsMethodType:
2062
+ case Kind::CFunctionAsMethodType:
2063
+ case Kind::ObjCMethodType:
2064
+ case Kind::CXXMethodType:
2065
+ case Kind::CurriedCXXMethodType:
2066
+ case Kind::PartialCurriedCXXMethodType:
2067
+ // Function types are always thrown directly
2068
+ return Direct;
2069
+
2070
+ case Kind::ClangType:
2071
+ case Kind::Type:
2072
+ case Kind::Discard:
2073
+ // Pass according to the formal type.
2074
+ return SILType::isFormallyThrownIndirectly (getType (),
2075
+ TC,
2076
+ getGenericSignatureOrNull ())
2077
+ ? Indirect : Direct;
2078
+
1966
2079
case Kind::Invalid:
1967
2080
case Kind::Tuple:
1968
2081
case Kind::ObjCCompletionHandlerArgumentsType:
@@ -2514,14 +2627,28 @@ class SubstFunctionTypePatternVisitor
2514
2627
if (yieldType) {
2515
2628
substYieldType = visit (yieldType, yieldPattern);
2516
2629
}
2517
-
2630
+
2631
+ CanType newErrorType;
2632
+
2633
+ if (auto optPair = pattern.getFunctionThrownErrorType (func)) {
2634
+ auto errorPattern = optPair->first ;
2635
+ auto errorType = optPair->second ;
2636
+ newErrorType = visit (errorType, errorPattern);
2637
+ }
2638
+
2518
2639
auto newResultTy = visit (func.getResult (),
2519
2640
pattern.getFunctionResultType ());
2520
2641
2521
2642
llvm::Optional<FunctionType::ExtInfo> extInfo;
2522
2643
if (func->hasExtInfo ())
2523
2644
extInfo = func->getExtInfo ();
2524
-
2645
+
2646
+ if (newErrorType) {
2647
+ if (!extInfo)
2648
+ extInfo = FunctionType::ExtInfo ();
2649
+ extInfo = extInfo->withThrows (true , newErrorType);
2650
+ }
2651
+
2525
2652
return CanFunctionType::get (FunctionType::CanParamArrayRef (newParams),
2526
2653
newResultTy, extInfo);
2527
2654
}
@@ -2562,7 +2689,7 @@ const {
2562
2689
auto substTy = visitor.handleUnabstractedFunctionType (substType, *this ,
2563
2690
substYieldType,
2564
2691
origYieldType);
2565
-
2692
+
2566
2693
auto substSig = buildGenericSignature (TC.Context , GenericSignature (),
2567
2694
std::move (visitor.substGenericParams ),
2568
2695
std::move (visitor.substRequirements ))
0 commit comments