Skip to content

Commit 5adc3b4

Browse files
committed
[Mangler] Mangle compatibility aliases as decls synthesized by ClangImporter
1 parent fe26400 commit 5adc3b4

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,14 @@ ASTMangler::getSpecialManglingContext(const ValueDecl *decl,
25232523
if (auto file = dyn_cast<FileUnit>(decl->getDeclContext())) {
25242524
if (file->getKind() == FileUnitKind::ClangModule ||
25252525
file->getKind() == FileUnitKind::DWARFModule) {
2526+
2527+
// Use ClangImporterContext for compatibility aliases to avoid conflicting
2528+
// with the mangled name of the actual declaration.
2529+
if (auto *aliasDecl = dyn_cast<TypeAliasDecl>(decl)) {
2530+
if (aliasDecl->isCompatibilityAlias())
2531+
return ASTMangler::ClangImporterContext;
2532+
}
2533+
25262534
if (decl->getClangDecl())
25272535
return ASTMangler::ObjCContext;
25282536
return ASTMangler::ClangImporterContext;
@@ -3116,6 +3124,13 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl,
31163124
// Always use Clang names for imported Clang declarations, unless they don't
31173125
// have one.
31183126
auto tryAppendClangName = [this, decl]() -> bool {
3127+
// We use the Swift name rather than the Clang name for compatibility
3128+
// aliases to avoid multiple declarations having the same mangled name.
3129+
if (auto *aliasDecl = dyn_cast<TypeAliasDecl>(decl)) {
3130+
if (aliasDecl->isCompatibilityAlias())
3131+
return false;
3132+
}
3133+
31193134
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
31203135
auto namedDecl = getClangDeclForMangling(decl);
31213136
if (!namedDecl) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,6 +3551,17 @@ void ClangImporter::lookupTypeDecl(
35513551
if (auto ext = dyn_cast_or_null<ExtensionDecl>(imported)) {
35523552
imported = ext->getExtendedNominal();
35533553
}
3554+
3555+
// Look through compatibility aliases since we must have mangled the
3556+
// underlying type (see ASTMangler::getSpecialManglingContext).
3557+
if (auto *alias = dyn_cast_or_null<TypeAliasDecl>(imported)) {
3558+
if (alias->isCompatibilityAlias()) {
3559+
imported = alias->getUnderlyingType()->getAnyNominal();
3560+
assert(imported != nullptr &&
3561+
"No underlying decl for a compatibility typealias");
3562+
}
3563+
}
3564+
35543565
if (auto *importedType = dyn_cast_or_null<TypeDecl>(imported)) {
35553566
foundViaClang = true;
35563567
receiver(importedType);

test/APINotes/versioned-test-mangling.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import APINotesFrameworkTest
1010

11-
// CHECK-TOP-ALIAS-4: typealias ImportantCStruct = VeryImportantCStruct
11+
// CHECK-TOP-ALIAS-4: struct VeryImportantCStruct {
1212
// CHECK-TOP-ALIAS-5: struct VeryImportantCStruct {
13-
// CHECK-NESTED-ALIAS-4: typealias InnerInSwift5 = Outer.Inner
13+
// CHECK-NESTED-ALIAS-4: struct Inner {
1414
// CHECK-NESTED-ALIAS-5: struct Inner {

0 commit comments

Comments
 (0)