Skip to content

Commit f018901

Browse files
authored
Merge pull request #60553 from zoecarver/enum-fix-for-computed-props
[cxx-interop] Fix anonymous enum issue for swift-named computed prope…
2 parents 7693167 + aaec49e commit f018901

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
@@ -5706,14 +5706,35 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
57065706
if (dc->isTypeContext() && !getterName.getSelfIndex())
57075707
isStatic = true;
57085708

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

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)