@@ -2244,6 +2244,9 @@ getImportTypeKindForParam(const clang::ParmVarDecl *param) {
2244
2244
Optional<swift::Type> ClangImporter::Implementation::importParameterType (
2245
2245
const clang::ParmVarDecl *param, OptionalTypeKind OptionalityOfParam,
2246
2246
ImportTypeKind importKind, bool allowNSUIntegerAsInt,
2247
+ bool isNSDictionarySubscriptGetter, bool paramIsError,
2248
+ bool paramIsCompletionHandler,
2249
+ Optional<unsigned > completionHandlerErrorParamIndex,
2247
2250
ArrayRef<GenericTypeParamDecl *> genericParams,
2248
2251
llvm::function_ref<void (Diagnostic &&)> paramAddDiag, bool &isInOut,
2249
2252
bool &isParamTypeImplicitlyUnwrapped) {
@@ -2305,10 +2308,41 @@ Optional<swift::Type> ClangImporter::Implementation::importParameterType(
2305
2308
isInOut = true ;
2306
2309
}
2307
2310
2311
+ // Special case for NSDictionary's subscript.
2312
+ if (isNSDictionarySubscriptGetter && paramTy->isObjCIdType ()) {
2313
+ // Not using `getImportTypeAttrs()` is unprincipled but OK for this hack.
2314
+ auto nsCopying = SwiftContext.getNSCopyingType ();
2315
+ if (!nsCopying)
2316
+ return None;
2317
+
2318
+ swiftParamTy = ExistentialType::get (nsCopying);
2319
+ if (OptionalityOfParam != OTK_None)
2320
+ swiftParamTy = OptionalType::get (swiftParamTy);
2321
+
2322
+ isParamTypeImplicitlyUnwrapped =
2323
+ OptionalityOfParam == OTK_ImplicitlyUnwrappedOptional;
2324
+ }
2325
+
2308
2326
if (!swiftParamTy) {
2327
+ bool sendableByDefault =
2328
+ paramIsCompletionHandler &&
2329
+ SwiftContext.LangOpts .hasFeature (Feature::SendableCompletionHandlers);
2330
+
2331
+ auto attrs = getImportTypeAttrs (param, /* isParam=*/ true , sendableByDefault);
2332
+
2333
+ // If this is the throws error parameter, we don't need to convert any
2334
+ // NSError** arguments to the sugared NSErrorPointer typealias form,
2335
+ // because all that is done with it is retrieving the canonical
2336
+ // type. Avoiding the sugar breaks a loop in Foundation caused by method
2337
+ // on NSString that has an error parameter. FIXME: This is a work-around
2338
+ // for the specific case when the throws conversion works, but is not
2339
+ // sufficient if it fails. (The correct, overarching fix is ClangImporter
2340
+ // being lazier.)
2309
2341
auto importedType =
2310
2342
importType (paramTy, importKind, paramAddDiag, allowNSUIntegerAsInt,
2311
- Bridgeability::Full, attrs, OptionalityOfParam);
2343
+ Bridgeability::Full, attrs, OptionalityOfParam,
2344
+ /* resugarNSErrorPointer=*/ !paramIsError,
2345
+ completionHandlerErrorParamIndex);
2312
2346
if (!importedType)
2313
2347
return None;
2314
2348
@@ -2380,7 +2414,11 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
2380
2414
2381
2415
auto swiftParamTy = importParameterType (
2382
2416
param, OptionalityOfParam, importKind, allowNSUIntegerAsInt,
2383
- genericParams, paramAddDiag, isInOut, isParamTypeImplicitlyUnwrapped);
2417
+ /* isNSDictionarySubscriptGetter=*/ false ,
2418
+ /* paramIsError=*/ false ,
2419
+ /* paramIsCompletionHandler=*/ false ,
2420
+ /* completionHandlerErrorParamIndex=*/ None, genericParams, paramAddDiag,
2421
+ isInOut, isParamTypeImplicitlyUnwrapped);
2384
2422
if (!swiftParamTy) {
2385
2423
addImportDiagnostic (param,
2386
2424
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;
@@ -2950,70 +2986,24 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
2950
2986
2951
2987
bool isInOut = false ;
2952
2988
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
- }
2989
+ auto swiftParamTy = importParameterType (
2990
+ param, optionalityOfParam, importKind, allowNSUIntegerAsIntInParam,
2991
+ kind == SpecialMethodKind::NSDictionarySubscriptGetter, paramIsError,
2992
+ paramIsCompletionHandler, completionHandlerErrorParamIndex,
2993
+ ArrayRef<GenericTypeParamDecl *>(), paramAddDiag, isInOut, paramIsIUO);
3004
2994
if (!swiftParamTy) {
3005
2995
addImportDiagnostic (param,
3006
2996
Diagnostic (diag::parameter_type_not_imported, param),
3007
2997
param->getSourceRange ().getBegin ());
3008
2998
return {Type (), false };
3009
2999
}
3010
3000
3011
- swiftParamTy = mapGenericArgs (origDC, dc, swiftParamTy);
3001
+ swiftParamTy = mapGenericArgs (origDC, dc, * swiftParamTy);
3012
3002
3013
3003
// If this is the error parameter, remember it, but don't build it
3014
3004
// into the parameter type.
3015
3005
if (paramIsError) {
3016
- errorParamType = swiftParamTy->getCanonicalType ();
3006
+ errorParamType = (* swiftParamTy) ->getCanonicalType ();
3017
3007
3018
3008
// ...unless we're supposed to replace it with ().
3019
3009
if (errorInfo->ErrorParameterIsReplaced ) {
@@ -3027,7 +3017,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3027
3017
// type but don't build it into the parameter type.
3028
3018
if (isAsync && paramIsCompletionHandler) {
3029
3019
if (Type replacedSwiftResultTy =
3030
- decomposeCompletionHandlerType (swiftParamTy, *asyncInfo)) {
3020
+ decomposeCompletionHandlerType (* swiftParamTy, *asyncInfo)) {
3031
3021
swiftResultTy = replacedSwiftResultTy;
3032
3022
3033
3023
// Import the original completion handler type without adjustments.
@@ -3052,7 +3042,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3052
3042
3053
3043
// Set up the parameter info
3054
3044
auto paramInfo =
3055
- getParameterInfo (this , param, name, swiftParamTy, isInOut, paramIsIUO);
3045
+ getParameterInfo (this , param, name, * swiftParamTy, isInOut, paramIsIUO);
3056
3046
3057
3047
// Determine whether we have a default argument.
3058
3048
if (kind == SpecialMethodKind::Regular ||
0 commit comments