Skip to content

Commit fb14414

Browse files
committed
Fix assertion failure importing NS_OPTION with differing Swift name across versions
At the call sites of `findAnonymousEnumForTypedef` we often wish to import the returned enum declaration and return the type the declaration creates. Prior to this patch, we assumed that the enum declaration would be imported as a `NominalTypeDecl`. This is not always the case. For whatever reason, sometimes in typechecking we import a declaration for various different naming versions. If the Swift name for an imported enum differs between the canonical name version, and currently requested name version, we import the enum as a `TypeAliasDecl` instead. Prior to this patch, this meant we would hit asserts importing some components of UIKit. This patch relaxes the assumption that the import is a `NominalTypeDecl` to just a `TypeDecl`, which as of yet, seems to be true.
1 parent 9f92220 commit fb14414

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5756,7 +5756,8 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
57565756
assert(clangEnum.value()->getIntegerType()->getCanonicalTypeInternal() ==
57575757
typedefType->getCanonicalTypeInternal());
57585758
if (auto swiftEnum = Impl.importDecl(*clangEnum, Impl.CurrentVersion)) {
5759-
importedType = {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
5759+
importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType(),
5760+
false};
57605761
}
57615762
}
57625763
}

lib/ClangImporter/ImportType.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
21342134
assert(clangEnum.value()->getIntegerType()->getCanonicalTypeInternal() ==
21352135
typedefType->getCanonicalTypeInternal());
21362136
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2137-
return {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
2137+
return {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType(), false};
21382138
}
21392139
}
21402140
}
@@ -2200,7 +2200,8 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
22002200
assert(clangEnum.value()->getIntegerType()->getCanonicalTypeInternal() ==
22012201
typedefType->getCanonicalTypeInternal());
22022202
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2203-
importedType = {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
2203+
importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType(),
2204+
false};
22042205
}
22052206
}
22062207
}
@@ -2301,7 +2302,7 @@ ClangImporter::Implementation::importParameterType(
23012302
->getCanonicalTypeInternal() ==
23022303
typedefType->getCanonicalTypeInternal());
23032304
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2304-
swiftParamTy = cast<NominalTypeDecl>(swiftEnum)->getDeclaredType();
2305+
swiftParamTy = cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType();
23052306
}
23062307
}
23072308
}
@@ -2892,7 +2893,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
28922893
assert(clangEnum.value()->getIntegerType()->getCanonicalTypeInternal() ==
28932894
typedefType->getCanonicalTypeInternal());
28942895
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2895-
importedType = {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
2896+
importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType(),
2897+
false};
28962898
}
28972899
}
28982900
}

test/Interop/Cxx/enum/Inputs/CenumsNSOptions.apinotes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ Enumerators:
1111
Tags:
1212
- Name: UIPrinterJobTypes
1313
SwiftName: UIPrinter.JobTypes
14+
- Name: Baz
15+
SwiftName: CurrentBazVersion
1416
SwiftVersions:
1517
- Version: 4
1618
Tags:
1719
- Name: UIPrinterJobTypes
1820
SwiftName: UIPrinterJobTypes
21+
- Name: Baz
22+
SwiftName: BazVersion4

test/Interop/Cxx/enum/Inputs/c-enums-NS_OPTIONS.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ typedef NS_OPTIONS(NSUInteger, Bar) {
7878
API_NOTES_NAMED_OptionFour = API_NOTES_NAMED_OptionOne |
7979
API_NOTES_NAMED_OptionTwo
8080
};
81+
82+
typedef NS_OPTIONS(NSUInteger, Baz) { Baz1, Baz2 };
83+
84+
Baz CFunctionReturningNSOption();
85+
void CFunctionTakingNSOption(Baz);
86+
87+
@interface NSOptionTypeCheckTest
88+
+ (Baz)methodReturningNSOption;
89+
+ (void)methodTakingNSOption:(Baz)baz;
90+
@end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -I %S/Inputs -enable-experimental-cxx-interop -typecheck %s
2+
// REQUIRES: objc_interop
3+
4+
import CenumsNSOptions
5+
6+
let foo = CFunctionReturningNSOption()
7+
CFunctionTakingNSOption(foo)
8+
let foo2 = NSOptionTypeCheckTest.methodReturningNSOption()
9+
NSOptionTypeCheckTest.methodTakingNSOption(foo)

0 commit comments

Comments
 (0)