Skip to content

Commit 0e89efa

Browse files
committed
Bridge types during import only if we are in a fully-bridgeable context.
Somehow the logic had slipped so that we were basing this decision purely on the ImportTypeKind and not on whether the broader context is bridgeable. This was allowing us to use bridged types when e.g. importing the results and parameters of C function pointer types, which is really bad. Also, when importing a reference to a typedef of block type, do not use the typedef in a non-bridgeable context. We import typedefs of block type as fully-bridged types, but this means that it is invalid to import a type using the typedef in a context where the original C type must be used. Similarly, make sure we use a properly-imported underlying type of the typedef when the typedef itself is unavailable. Also, extend the special behavior of block typedefs to abstract-function typedefs, which seems to be consistent with the expected behavior of the tests. Finally, I changed importType to take a new Bridgeability enum instead of a raw canFullyBridgeTypes bool. At the time, I was doing that because I was going to make it tri-valued; that turned out to be unnecessary, but I think it's an improvement anyway.
1 parent 72c8d6a commit 0e89efa

File tree

9 files changed

+118
-57
lines changed

9 files changed

+118
-57
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ namespace {
23032303
SwiftType = Impl.importType(ClangType,
23042304
ImportTypeKind::Typedef,
23052305
isInSystemModule(DC),
2306-
ClangType->isBlockPointerType(),
2306+
getTypedefBridgeability(ClangType),
23072307
OTK_Optional);
23082308
}
23092309

@@ -2410,7 +2410,7 @@ namespace {
24102410
auto underlyingType = Impl.importType(decl->getIntegerType(),
24112411
ImportTypeKind::Enum,
24122412
isInSystemModule(dc),
2413-
/*isFullyBridgeable*/false);
2413+
Bridgeability::None);
24142414
if (!underlyingType)
24152415
return nullptr;
24162416

@@ -2447,7 +2447,7 @@ namespace {
24472447
// Compute the underlying type.
24482448
auto underlyingType = Impl.importType(
24492449
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
2450-
/*isFullyBridgeable*/ false);
2450+
Bridgeability::None);
24512451
if (!underlyingType)
24522452
return nullptr;
24532453

@@ -3063,7 +3063,7 @@ namespace {
30633063
auto type = Impl.importType(clangContext.getTagDeclType(clangEnum),
30643064
ImportTypeKind::Value,
30653065
isInSystemModule(dc),
3066-
/*isFullyBridgeable*/false);
3066+
Bridgeability::None);
30673067
if (!type)
30683068
return nullptr;
30693069
// FIXME: Importing the type will recursively revisit this same
@@ -3101,7 +3101,7 @@ namespace {
31013101
Impl.getClangASTContext().getTagDeclType(clangEnum),
31023102
ImportTypeKind::Value,
31033103
isInSystemModule(dc),
3104-
/*isFullyBridgeable*/false);
3104+
Bridgeability::None);
31053105
if (!enumType)
31063106
return nullptr;
31073107

@@ -3162,7 +3162,7 @@ namespace {
31623162
auto type = Impl.importType(decl->getType(),
31633163
ImportTypeKind::Variable,
31643164
isInSystemModule(dc),
3165-
/*isFullyBridgeable*/false);
3165+
Bridgeability::None);
31663166
if (!type)
31673167
return nullptr;
31683168

@@ -3385,7 +3385,7 @@ namespace {
33853385
auto type = Impl.importType(decl->getType(),
33863386
ImportTypeKind::RecordField,
33873387
isInSystemModule(dc),
3388-
/*isFullyBridgeable*/false);
3388+
Bridgeability::None);
33893389
if (!type)
33903390
return nullptr;
33913391

@@ -3454,7 +3454,7 @@ namespace {
34543454
(isAudited ? ImportTypeKind::AuditedVariable
34553455
: ImportTypeKind::Variable),
34563456
isInSystemModule(dc),
3457-
/*isFullyBridgeable*/false);
3457+
Bridgeability::None);
34583458

34593459
if (!type)
34603460
return nullptr;
@@ -4332,7 +4332,7 @@ namespace {
43324332
superclassType = Impl.importType(clangSuperclassType,
43334333
ImportTypeKind::Abstract,
43344334
isInSystemModule(dc),
4335-
/*isFullyBridgeable*/false);
4335+
Bridgeability::None);
43364336
if (superclassType) {
43374337
assert(superclassType->is<ClassType>() ||
43384338
superclassType->is<BoundGenericClassType>());
@@ -4938,7 +4938,7 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
49384938
// Import the type of the underlying storage
49394939
auto storedUnderlyingType = Impl.importType(
49404940
decl->getUnderlyingType(), ImportTypeKind::Value, isInSystemModule(dc),
4941-
decl->getUnderlyingType()->isBlockPointerType(), OTK_None);
4941+
Bridgeability::None, OTK_None);
49424942

49434943
if (auto objTy = storedUnderlyingType->getAnyOptionalObjectType())
49444944
storedUnderlyingType = objTy;
@@ -4956,7 +4956,7 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
49564956
// Find a bridged type, which may be different
49574957
auto computedPropertyUnderlyingType = Impl.importType(
49584958
decl->getUnderlyingType(), ImportTypeKind::Property, isInSystemModule(dc),
4959-
decl->getUnderlyingType()->isBlockPointerType(), OTK_None);
4959+
Bridgeability::Full, OTK_None);
49604960
if (auto objTy = computedPropertyUnderlyingType->getAnyOptionalObjectType())
49614961
computedPropertyUnderlyingType = objTy;
49624962

@@ -5173,7 +5173,7 @@ SwiftDeclConverter::importAsOptionSetType(DeclContext *dc, Identifier name,
51735173
// Compute the underlying type.
51745174
auto underlyingType = Impl.importType(
51755175
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
5176-
/*isFullyBridgeable*/ false);
5176+
Bridgeability::None);
51775177
if (!underlyingType)
51785178
return nullptr;
51795179

@@ -5492,7 +5492,7 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
54925492
Type swiftPropertyType = Impl.importType(
54935493
propertyType, ImportTypeKind::Property,
54945494
Impl.shouldAllowNSUIntegerAsInt(isFromSystemModule, getter),
5495-
/*isFullyBridgeable*/ true, OTK_ImplicitlyUnwrappedOptional);
5495+
Bridgeability::Full, OTK_ImplicitlyUnwrappedOptional);
54965496
if (!swiftPropertyType)
54975497
return nullptr;
54985498

@@ -6406,7 +6406,8 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
64066406
clangBound->stripObjCKindOfTypeAndQuals(Impl.getClangASTContext());
64076407
Type superclassType =
64086408
Impl.importType(clang::QualType(unqualifiedClangBound, 0),
6409-
ImportTypeKind::Abstract, false, false);
6409+
ImportTypeKind::Abstract, false,
6410+
Bridgeability::None);
64106411
if (!superclassType) {
64116412
return None;
64126413
}

lib/ClangImporter/ImportMacro.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
113113
auto clangTy = parsed->getType();
114114
auto literalType = Impl.importType(clangTy, ImportTypeKind::Value,
115115
isInSystemModule(DC),
116-
/*isFullyBridgeable*/false);
116+
Bridgeability::None);
117117
if (!literalType)
118118
return nullptr;
119119

@@ -123,7 +123,7 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
123123
} else {
124124
constantType = Impl.importType(castType, ImportTypeKind::Value,
125125
isInSystemModule(DC),
126-
/*isFullyBridgeable*/false);
126+
Bridgeability::None);
127127
if (!constantType)
128128
return nullptr;
129129
}
@@ -300,7 +300,7 @@ static Optional<std::pair<llvm::APSInt, Type>>
300300
auto type = impl.importType(literal->getType(),
301301
ImportTypeKind::Value,
302302
isInSystemModule(DC),
303-
/*isFullyBridgeable*/false);
303+
Bridgeability::None);
304304
return {{ value, type }};
305305
}
306306

0 commit comments

Comments
 (0)