@@ -2243,7 +2243,10 @@ getImportTypeKindForParam(const clang::ParmVarDecl *param) {
2243
2243
2244
2244
Optional<swift::Type> ClangImporter::Implementation::importParameterType (
2245
2245
const clang::ParmVarDecl *param, OptionalTypeKind OptionalityOfParam,
2246
- bool allowNSUIntegerAsInt, ArrayRef<GenericTypeParamDecl *> genericParams,
2246
+ bool allowNSUIntegerAsInt, bool isNSDictionarySubscriptGetter,
2247
+ bool paramIsError, bool paramIsCompletionHandler,
2248
+ Optional<unsigned > completionHandlerErrorParamIndex,
2249
+ ArrayRef<GenericTypeParamDecl *> genericParams,
2247
2250
llvm::function_ref<void (Diagnostic &&)> paramAddDiag, bool &isInOut,
2248
2251
bool &isParamTypeImplicitlyUnwrapped) {
2249
2252
auto paramTy = param->getType ();
@@ -2306,10 +2309,41 @@ Optional<swift::Type> ClangImporter::Implementation::importParameterType(
2306
2309
isInOut = true ;
2307
2310
}
2308
2311
2312
+ // Special case for NSDictionary's subscript.
2313
+ if (isNSDictionarySubscriptGetter && paramTy->isObjCIdType ()) {
2314
+ // Not using `getImportTypeAttrs()` is unprincipled but OK for this hack.
2315
+ auto nsCopying = SwiftContext.getNSCopyingType ();
2316
+ if (!nsCopying)
2317
+ return None;
2318
+
2319
+ swiftParamTy = ExistentialType::get (nsCopying);
2320
+ if (OptionalityOfParam != OTK_None)
2321
+ swiftParamTy = OptionalType::get (swiftParamTy);
2322
+
2323
+ isParamTypeImplicitlyUnwrapped =
2324
+ OptionalityOfParam == OTK_ImplicitlyUnwrappedOptional;
2325
+ }
2326
+
2309
2327
if (!swiftParamTy) {
2328
+ bool sendableByDefault =
2329
+ paramIsCompletionHandler &&
2330
+ SwiftContext.LangOpts .hasFeature (Feature::SendableCompletionHandlers);
2331
+
2332
+ auto attrs = getImportTypeAttrs (param, /* isParam=*/ true , sendableByDefault);
2333
+
2334
+ // If this is the throws error parameter, we don't need to convert any
2335
+ // NSError** arguments to the sugared NSErrorPointer typealias form,
2336
+ // because all that is done with it is retrieving the canonical
2337
+ // type. Avoiding the sugar breaks a loop in Foundation caused by method
2338
+ // on NSString that has an error parameter. FIXME: This is a work-around
2339
+ // for the specific case when the throws conversion works, but is not
2340
+ // sufficient if it fails. (The correct, overarching fix is ClangImporter
2341
+ // being lazier.)
2310
2342
auto importedType =
2311
2343
importType (paramTy, importKind, paramAddDiag, allowNSUIntegerAsInt,
2312
- Bridgeability::Full, attrs, OptionalityOfParam);
2344
+ Bridgeability::Full, attrs, OptionalityOfParam,
2345
+ /* resugarNSErrorPointer=*/ !paramIsError,
2346
+ completionHandlerErrorParamIndex);
2313
2347
if (!importedType)
2314
2348
return None;
2315
2349
@@ -2378,8 +2412,12 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
2378
2412
bool isParamTypeImplicitlyUnwrapped = false ;
2379
2413
2380
2414
auto swiftParamTyOpt = importParameterType (
2381
- param, OptionalityOfParam, importKind, allowNSUIntegerAsInt,
2382
- genericParams, paramAddDiag, isInOut, isParamTypeImplicitlyUnwrapped);
2415
+ param, OptionalityOfParam, allowNSUIntegerAsInt,
2416
+ /* isNSDictionarySubscriptGetter=*/ false ,
2417
+ /* paramIsError=*/ false ,
2418
+ /* paramIsCompletionHandler=*/ false ,
2419
+ /* completionHandlerErrorParamIndex=*/ None, genericParams, paramAddDiag,
2420
+ isInOut, isParamTypeImplicitlyUnwrapped);
2383
2421
if (!swiftParamTyOpt) {
2384
2422
addImportDiagnostic (param,
2385
2423
Diagnostic (diag::parameter_type_not_imported, param),
@@ -2931,8 +2969,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
2931
2969
OptionalTypeKind optionalityOfParam =
2932
2970
getParamOptionality (param, knownNonNull);
2933
2971
2934
- // Import the parameter type into Swift.
2935
-
2936
2972
bool allowNSUIntegerAsIntInParam = isFromSystemModule;
2937
2973
if (allowNSUIntegerAsIntInParam) {
2938
2974
StringRef name;
@@ -2945,68 +2981,22 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
2945
2981
allowNSUIntegerAsIntInParam = !nameContainsUnsigned (name);
2946
2982
}
2947
2983
2948
- ImportTypeKind importKind = getImportTypeKindForParam (param);
2949
2984
ImportDiagnosticAdder paramAddDiag (*this , clangDecl, param->getLocation ());
2950
2985
2951
2986
bool isInOut = false ;
2952
2987
bool paramIsIUO = false ;
2953
-
2954
- Type swiftParamTy;
2955
- if (auto typedefType = dyn_cast<clang::TypedefType>(paramTy.getTypePtr ())) {
2956
- if (isUnavailableInSwift (typedefType->getDecl ())) {
2957
- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2958
- // If this fails, it means that we need a stronger predicate for
2959
- // determining the relationship between an enum and typedef.
2960
- assert (clangEnum.getValue ()->getIntegerType ()->getCanonicalTypeInternal () ==
2961
- typedefType->getCanonicalTypeInternal ());
2962
- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2963
- swiftParamTy = cast<NominalTypeDecl>(swiftEnum)->getDeclaredType ();
2964
- }
2965
- }
2966
- }
2967
- }
2968
-
2969
- // Special case for NSDictionary's subscript.
2970
- if (kind == SpecialMethodKind::NSDictionarySubscriptGetter &&
2971
- paramTy->isObjCIdType ()) {
2972
- // Not using `getImportTypeAttrs()` is unprincipled but OK for this hack.
2973
- auto nsCopying = SwiftContext.getNSCopyingType ();
2974
- if (!nsCopying)
2975
- return {Type (), false };
2976
- swiftParamTy = ExistentialType::get (nsCopying);
2977
- if (optionalityOfParam != OTK_None)
2978
- swiftParamTy = OptionalType::get (swiftParamTy);
2979
-
2980
- paramIsIUO = optionalityOfParam == OTK_ImplicitlyUnwrappedOptional;
2981
- } else if (!swiftParamTy) {
2982
- bool sendableByDefault = paramIsCompletionHandler &&
2983
- SwiftContext.LangOpts .hasFeature (Feature::SendableCompletionHandlers);
2984
-
2985
- // If this is the throws error parameter, we don't need to convert any
2986
- // NSError** arguments to the sugared NSErrorPointer typealias form,
2987
- // because all that is done with it is retrieving the canonical
2988
- // type. Avoiding the sugar breaks a loop in Foundation caused by method
2989
- // on NSString that has an error parameter. FIXME: This is a work-around
2990
- // for the specific case when the throws conversion works, but is not
2991
- // sufficient if it fails. (The correct, overarching fix is ClangImporter
2992
- // being lazier.)
2993
- auto importedParamType =
2994
- importType (paramTy, importKind, paramAddDiag,
2995
- allowNSUIntegerAsIntInParam, Bridgeability::Full,
2996
- getImportTypeAttrs (param, /* isParam=*/ true ,
2997
- sendableByDefault),
2998
- optionalityOfParam,
2999
- /* resugarNSErrorPointer=*/ !paramIsError,
3000
- completionHandlerErrorParamIndex);
3001
- paramIsIUO = importedParamType.isImplicitlyUnwrapped ();
3002
- swiftParamTy = importedParamType.getType ();
3003
- }
3004
- if (!swiftParamTy) {
2988
+ auto swiftParamTyOpt = importParameterType (
2989
+ param, optionalityOfParam, allowNSUIntegerAsIntInParam,
2990
+ kind == SpecialMethodKind::NSDictionarySubscriptGetter, paramIsError,
2991
+ paramIsCompletionHandler, completionHandlerErrorParamIndex,
2992
+ ArrayRef<GenericTypeParamDecl *>(), paramAddDiag, isInOut, paramIsIUO);
2993
+ if (!swiftParamTyOpt) {
3005
2994
addImportDiagnostic (param,
3006
2995
Diagnostic (diag::parameter_type_not_imported, param),
3007
2996
param->getSourceRange ().getBegin ());
3008
2997
return {Type (), false };
3009
2998
}
2999
+ auto swiftParamTy = *swiftParamTyOpt;
3010
3000
3011
3001
swiftParamTy = mapGenericArgs (origDC, dc, swiftParamTy);
3012
3002
@@ -3030,6 +3020,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3030
3020
decomposeCompletionHandlerType (swiftParamTy, *asyncInfo)) {
3031
3021
swiftResultTy = replacedSwiftResultTy;
3032
3022
3023
+ ImportTypeKind importKind = getImportTypeKindForParam (param);
3024
+
3033
3025
// Import the original completion handler type without adjustments.
3034
3026
Type origSwiftParamTy = importType (
3035
3027
paramTy, importKind, paramAddDiag, allowNSUIntegerAsIntInParam,
0 commit comments