Skip to content

Commit c402030

Browse files
committed
[ClangImporter] Give compatibility typealiases the correct version.
Unlike values, we can't import multiple copies of types under different names and get good results. Instead, we make a typealias that points back to the original type. Make sure this typealias is flagged with whatever version is appropriate, rather than always using "Swift 2".
1 parent 79ed26f commit c402030

File tree

5 files changed

+56
-27
lines changed

5 files changed

+56
-27
lines changed

lib/ClangImporter/ImportDecl.cpp

+24-22
Original file line numberDiff line numberDiff line change
@@ -2041,10 +2041,11 @@ namespace {
20412041
decl->setImplicit();
20422042
}
20432043

2044-
/// Create a typealias for the Swift 2 name of a Clang type declaration.
2045-
Decl *importSwift2TypeAlias(const clang::NamedDecl *decl,
2046-
ImportedName swift2Name,
2047-
ImportedName correctSwiftName);
2044+
/// Create a typealias for the name of a Clang type declaration in an
2045+
/// alternate version of Swift.
2046+
Decl *importCompatibilityTypeAlias(const clang::NamedDecl *decl,
2047+
ImportedName compatibilityName,
2048+
ImportedName correctSwiftName);
20482049

20492050
/// Create a swift_newtype struct corresponding to a typedef. Returns
20502051
/// nullptr if unable.
@@ -2062,7 +2063,8 @@ namespace {
20622063
// If we've been asked to produce a Swift 2 stub, handle it via a
20632064
// typealias.
20642065
if (correctSwiftName)
2065-
return importSwift2TypeAlias(Decl, importedName, *correctSwiftName);
2066+
return importCompatibilityTypeAlias(Decl, importedName,
2067+
*correctSwiftName);
20662068

20672069
Type SwiftType;
20682070
if (Decl->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
@@ -2277,7 +2279,8 @@ namespace {
22772279
// If we've been asked to produce a Swift 2 stub, handle it via a
22782280
// typealias.
22792281
if (correctSwiftName)
2280-
return importSwift2TypeAlias(decl, importedName, *correctSwiftName);
2282+
return importCompatibilityTypeAlias(decl, importedName,
2283+
*correctSwiftName);
22812284

22822285
auto dc =
22832286
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
@@ -2693,7 +2696,8 @@ namespace {
26932696
// If we've been asked to produce a Swift 2 stub, handle it via a
26942697
// typealias.
26952698
if (correctSwiftName)
2696-
return importSwift2TypeAlias(decl, importedName, *correctSwiftName);
2699+
return importCompatibilityTypeAlias(decl, importedName,
2700+
*correctSwiftName);
26972701

26982702
auto dc =
26992703
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
@@ -3957,7 +3961,8 @@ namespace {
39573961
// If we've been asked to produce a Swift 2 stub, handle it via a
39583962
// typealias.
39593963
if (correctSwiftName)
3960-
return importSwift2TypeAlias(decl, importedName, *correctSwiftName);
3964+
return importCompatibilityTypeAlias(decl, importedName,
3965+
*correctSwiftName);
39613966

39623967
Identifier name = importedName.getDeclName().getBaseName();
39633968

@@ -4092,7 +4097,8 @@ namespace {
40924097
// If we've been asked to produce a Swift 2 stub, handle it via a
40934098
// typealias.
40944099
if (correctSwiftName)
4095-
return importSwift2TypeAlias(decl, importedName, *correctSwiftName);
4100+
return importCompatibilityTypeAlias(decl, importedName,
4101+
*correctSwiftName);
40964102

40974103
auto name = importedName.getDeclName().getBaseName();
40984104

@@ -4630,9 +4636,10 @@ SwiftDeclConverter::importCFClassType(const clang::TypedefNameDecl *decl,
46304636
return theClass;
46314637
}
46324638

4633-
Decl *SwiftDeclConverter::importSwift2TypeAlias(const clang::NamedDecl *decl,
4634-
ImportedName swift2Name,
4635-
ImportedName correctSwiftName) {
4639+
Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
4640+
const clang::NamedDecl *decl,
4641+
ImportedName compatibilityName,
4642+
ImportedName correctSwiftName) {
46364643
// Import the referenced declaration. If it doesn't come in as a type,
46374644
// we don't care.
46384645
auto importedDecl = Impl.importDecl(decl, getActiveSwiftVersion());
@@ -4665,19 +4672,14 @@ Decl *SwiftDeclConverter::importSwift2TypeAlias(const clang::NamedDecl *decl,
46654672

46664673
// Create the type alias.
46674674
auto alias = Impl.createDeclWithClangNode<TypeAliasDecl>(
4668-
decl,
4669-
Accessibility::Public, Impl.importSourceLoc(decl->getLocStart()),
4670-
SourceLoc(), swift2Name.getDeclName().getBaseName(),
4671-
Impl.importSourceLoc(decl->getLocation()),
4672-
genericParams, dc);
4675+
decl, Accessibility::Public, Impl.importSourceLoc(decl->getLocStart()),
4676+
SourceLoc(), compatibilityName.getDeclName().getBaseName(),
4677+
Impl.importSourceLoc(decl->getLocation()), genericParams, dc);
46734678
alias->setUnderlyingType(underlyingType);
46744679
alias->setGenericEnvironment(genericEnv);
46754680

4676-
// Record that this is the Swift 2 version of this declaration.
4677-
Impl.ImportedDecls[{decl->getCanonicalDecl(), ImportNameVersion::Swift2}] =
4678-
alias;
4679-
4680-
// Mark it as the Swift 2 variant.
4681+
// Record that this is the official version of this declaration.
4682+
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = alias;
46814683
markAsVariant(alias, correctSwiftName);
46824684
return alias;
46834685
}

test/APINotes/Inputs/custom-frameworks/APINotesFrameworkTest.framework/Headers/APINotesFrameworkTest.apinotes

+3
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,6 @@ SwiftVersions:
8989
- Name: acceptDoublePointer
9090
SwiftName: 'acceptPointer(_:)'
9191
Nullability: [ O ]
92+
Tags:
93+
- Name: SomeCStruct
94+
SwiftName: ImportantCStruct

test/APINotes/Inputs/custom-frameworks/APINotesFrameworkTest.framework/Headers/APINotesFrameworkTest.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ __attribute__((objc_root_class))
1919

2020
#import <APINotesFrameworkTest/Properties.h>
2121
#import <APINotesFrameworkTest/Protocols.h>
22+
#import <APINotesFrameworkTest/Types.h>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma clang assume_nonnull begin
2+
3+
struct __attribute__((swift_name("VeryImportantCStruct"))) SomeCStruct {
4+
int field;
5+
};
6+
7+
#pragma clang assume_nonnull end

test/APINotes/versioned.swift

+21-5
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,36 @@ func testRenamedTopLevel() {
2323
// CHECK-DIAGS-3: versioned.swift:[[@LINE-1]]:3: error: 'accept' has been renamed to 'acceptPointer(_:)'
2424
// CHECK-DIAGS-3: note: 'accept' was introduced in Swift 4
2525

26-
// CHECK-DIAGS-4-NOT: versioned.swift:[[@LINE+1]]:
26+
// CHECK-DIAGS-3-NOT: versioned.swift:[[@LINE+1]]:
2727
acceptPointer(&value)
2828
// CHECK-DIAGS-4: versioned.swift:[[@LINE-1]]:3: error: 'acceptPointer' has been renamed to 'accept(_:)'
2929
// CHECK-DIAGS-4: note: 'acceptPointer' was obsoleted in Swift 4
3030

3131
acceptDoublePointer(&value)
3232
// CHECK-DIAGS: versioned.swift:[[@LINE-1]]:3: error: 'acceptDoublePointer' has been renamed to
33-
// CHECK-DIAGS-4: 'accept(_:)'
34-
// CHECK-DIAGS-3: 'acceptPointer(_:)'
33+
// CHECK-DIAGS-4-SAME: 'accept(_:)'
34+
// CHECK-DIAGS-3-SAME: 'acceptPointer(_:)'
3535
// CHECK-DIAGS: note: 'acceptDoublePointer' was obsoleted in Swift 3
3636

3737
oldAcceptDoublePointer(&value)
3838
// CHECK-DIAGS: versioned.swift:[[@LINE-1]]:3: error: 'oldAcceptDoublePointer' has been renamed to
39-
// CHECK-DIAGS-4: 'accept(_:)'
40-
// CHECK-DIAGS-3: 'acceptPointer(_:)'
39+
// CHECK-DIAGS-4-SAME: 'accept(_:)'
40+
// CHECK-DIAGS-3-SAME: 'acceptPointer(_:)'
4141
// CHECK-DIAGS: note: 'oldAcceptDoublePointer' has been explicitly marked unavailable here
42+
43+
_ = SomeCStruct()
44+
// CHECK-DIAGS: versioned.swift:[[@LINE-1]]:7: error: 'SomeCStruct' has been renamed to
45+
// CHECK-DIAGS-4-SAME: 'VeryImportantCStruct'
46+
// CHECK-DIAGS-3-SAME: 'ImportantCStruct'
47+
// CHECK-DIAGS: note: 'SomeCStruct' was obsoleted in Swift 3
48+
49+
// CHECK-DIAGS-3-NOT: versioned.swift:[[@LINE+1]]:
50+
_ = ImportantCStruct()
51+
// CHECK-DIAGS-4: versioned.swift:[[@LINE-1]]:7: error: 'ImportantCStruct' has been renamed to 'VeryImportantCStruct'
52+
// CHECK-DIAGS-4: note: 'ImportantCStruct' was obsoleted in Swift 4
53+
54+
// CHECK-DIAGS-4-NOT: versioned.swift:[[@LINE+1]]:
55+
_ = VeryImportantCStruct()
56+
// CHECK-DIAGS-3: versioned.swift:[[@LINE-1]]:7: error: 'VeryImportantCStruct' has been renamed to 'ImportantCStruct'
57+
// CHECK-DIAGS-3: note: 'VeryImportantCStruct' was introduced in Swift 4
4258
}

0 commit comments

Comments
 (0)