Skip to content

Commit aaec49e

Browse files
committed
[cxx-interop] Fix anonymous enum issue for swift-named computed properties.
1 parent 590caf9 commit aaec49e

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5705,14 +5705,35 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
57055705
if (dc->isTypeContext() && !getterName.getSelfIndex())
57065706
isStatic = true;
57075707

5708-
// Compute the property type.
5709-
bool isFromSystemModule = isInSystemModule(dc);
5710-
auto importedType = Impl.importType(
5711-
propertyType, ImportTypeKind::Property,
5712-
ImportDiagnosticAdder(Impl, getter, getter->getLocation()),
5713-
Impl.shouldAllowNSUIntegerAsInt(isFromSystemModule, getter),
5714-
Bridgeability::Full, getImportTypeAttrs(accessor),
5715-
OTK_ImplicitlyUnwrappedOptional);
5708+
ImportedType importedType;
5709+
5710+
// Sometimes we import unavailable typedefs as enums. If that's the case,
5711+
// use the enum, not the typedef here.
5712+
if (auto typedefType = dyn_cast<clang::TypedefType>(propertyType.getTypePtr())) {
5713+
if (Impl.isUnavailableInSwift(typedefType->getDecl())) {
5714+
if (auto clangEnum = findAnonymousEnumForTypedef(Impl.SwiftContext, typedefType)) {
5715+
// If this fails, it means that we need a stronger predicate for
5716+
// determining the relationship between an enum and typedef.
5717+
assert(clangEnum.getValue()->getIntegerType()->getCanonicalTypeInternal() ==
5718+
typedefType->getCanonicalTypeInternal());
5719+
if (auto swiftEnum = Impl.importDecl(*clangEnum, Impl.CurrentVersion)) {
5720+
importedType = {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
5721+
}
5722+
}
5723+
}
5724+
}
5725+
5726+
if (!importedType) {
5727+
// Compute the property type.
5728+
bool isFromSystemModule = isInSystemModule(dc);
5729+
importedType = Impl.importType(
5730+
propertyType, ImportTypeKind::Property,
5731+
ImportDiagnosticAdder(Impl, getter, getter->getLocation()),
5732+
Impl.shouldAllowNSUIntegerAsInt(isFromSystemModule, getter),
5733+
Bridgeability::Full, getImportTypeAttrs(accessor),
5734+
OTK_ImplicitlyUnwrappedOptional);
5735+
}
5736+
57165737
if (!importedType)
57175738
return nullptr;
57185739

test/Interop/Cxx/enum/Inputs/anonymous-with-swift-name.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ inline CFColorMask renameCFColorMask(ParentStruct parent)
2828
__attribute__((swift_name("ParentStruct.childFn(self:)")))
2929
{ return kSOColorMaskRed; }
3030

31+
inline CFColorMask getCFColorMask(ParentStruct parent)
32+
__attribute__((swift_name("getter:ParentStruct.colorProp(self:)")))
33+
{ return kSOColorMaskRed; }
34+
35+
inline void getCFColorMask(ParentStruct parent, CFColorMask newValue)
36+
__attribute__((swift_name("setter:ParentStruct.colorProp(self:newValue:)")))
37+
{ }
38+
3139
typedef __attribute__((availability(swift, unavailable))) __attribute__((swift_name("ParentStruct.NewName"))) unsigned OldName;
3240

3341
enum __attribute__((flag_enum,enum_extensibility(open))) : OldName {

test/Interop/Cxx/enum/anonymous-with-swift-name.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ AnonymousEnumsTestSuite.test("Parameter types") {
4444
expectEqual(all, .all)
4545
}
4646

47+
AnonymousEnumsTestSuite.test("Computed properties") {
48+
let parent = ParentStruct()
49+
50+
expectEqual(parent.colorProp, useCFColorMask(.red))
51+
parent.colorProp = .green
52+
expectEqual(parent.colorProp, useCFColorMask(.red))
53+
}
54+
4755
runAllTests()

0 commit comments

Comments
 (0)