Skip to content

Commit 67d2224

Browse files
authored
Merge pull request #7756 from jrose-apple/small-multi-name-import-fixes
Several small fixes for importing alternate names
2 parents e128c24 + ee0dd49 commit 67d2224

File tree

10 files changed

+108
-35
lines changed

10 files changed

+108
-35
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ class ClangImporter final : public ClangModuleLoader {
260260
/// The return value may be an empty identifier, in which case the enum would
261261
/// not be imported.
262262
///
263-
/// This is mostly an implementation detail of the importer, but is also
264-
/// used by the debugger.
263+
/// This is not used by the importer itself, but is used by the debugger.
265264
Identifier getEnumConstantName(const clang::EnumConstantDecl *enumConstant);
266265

267266
/// Writes the mangled name of \p clangDecl to \p os.

lib/ClangImporter/ImportDecl.cpp

Lines changed: 27 additions & 25 deletions
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
}
@@ -6516,7 +6518,7 @@ getSwiftNameFromClangName(StringRef replacement) {
65166518
if (!clangDecl)
65176519
return "";
65186520

6519-
auto importedName = importFullName(clangDecl, ImportNameVersion::Swift3);
6521+
auto importedName = importFullName(clangDecl, CurrentVersion);
65206522
if (!importedName)
65216523
return "";
65226524

@@ -6758,7 +6760,7 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
67586760
Result = converter.Visit(ClangDecl);
67596761
HadForwardDeclaration = converter.hadForwardDeclaration();
67606762
}
6761-
if (!Result && version > ImportNameVersion::Swift2) {
6763+
if (!Result && version == CurrentVersion) {
67626764
// If we couldn't import this Objective-C entity, determine
67636765
// whether it was a required member of a protocol.
67646766
bool hasMissingRequiredMember = false;
@@ -7568,7 +7570,7 @@ ClangImporter::Implementation::getSpecialTypedefKind(clang::TypedefNameDecl *dec
75687570

75697571
Identifier
75707572
ClangImporter::getEnumConstantName(const clang::EnumConstantDecl *enumConstant){
7571-
return Impl.importFullName(enumConstant, ImportNameVersion::Swift3)
7573+
return Impl.importFullName(enumConstant, Impl.CurrentVersion)
75727574
.getDeclName()
75737575
.getBaseName();
75747576
}

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
16261626
}
16271627

16281628
// Figure out the name for this parameter.
1629-
Identifier bodyName = importFullName(param, ImportNameVersion::Swift3)
1629+
Identifier bodyName = importFullName(param, CurrentVersion)
16301630
.getDeclName()
16311631
.getBaseName();
16321632

@@ -2037,7 +2037,7 @@ Type ClangImporter::Implementation::importMethodType(
20372037
}
20382038

20392039
// Figure out the name for this parameter.
2040-
Identifier bodyName = importFullName(param, ImportNameVersion::Swift3)
2040+
Identifier bodyName = importFullName(param, CurrentVersion)
20412041
.getDeclName()
20422042
.getBaseName();
20432043

@@ -2194,7 +2194,7 @@ Type ClangImporter::Implementation::importAccessorMethodType(
21942194

21952195
} else {
21962196
const clang::ParmVarDecl *param = clangDecl->parameters().front();
2197-
ImportedName fullBodyName = importFullName(param,ImportNameVersion::Swift3);
2197+
ImportedName fullBodyName = importFullName(param, CurrentVersion);
21982198
Identifier bodyName = fullBodyName.getDeclName().getBaseName();
21992199
SourceLoc nameLoc = importSourceLoc(param->getLocation());
22002200
Identifier argLabel = functionName.getDeclName().getArgumentNames().front();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ SwiftVersions:
7979
- Name: accessorsOnlyRenamedRetypedClass
8080
PropertyKind: Class
8181
SwiftImportAsAccessors: true
82+
Protocols:
83+
- Name: ProtoWithVersionedUnavailableMember
84+
Methods:
85+
- Selector: requirement
86+
MethodKind: Instance
87+
ResultType: 'ForwardClass * _Nullable'
8288
Functions:
8389
- Name: acceptDoublePointer
8490
SwiftName: 'acceptPointer(_:)'
8591
Nullability: [ O ]
92+
Tags:
93+
- Name: SomeCStruct
94+
SwiftName: ImportantCStruct

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ void jumpToLocation(double x, double y, double z);
22

33
void acceptDoublePointer(double* _Nonnull ptr) __attribute__((swift_name("accept(_:)")));
44

5+
void oldAcceptDoublePointer(double* _Nonnull ptr) __attribute__((availability(swift, unavailable, replacement="acceptDoublePointer")));
6+
57
#ifdef __OBJC__
68

79
__attribute__((objc_root_class))
@@ -13,5 +15,8 @@ __attribute__((objc_root_class))
1315
-(nonnull id)methodWithA:(nonnull id)a;
1416
@end
1517

18+
#endif // __OBJC__
19+
1620
#import <APINotesFrameworkTest/Properties.h>
17-
#endif
21+
#import <APINotesFrameworkTest/Protocols.h>
22+
#import <APINotesFrameworkTest/Types.h>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef __OBJC__
12
#pragma clang assume_nonnull begin
23

34
__attribute__((objc_root_class))
@@ -33,3 +34,4 @@ __attribute__((objc_root_class))
3334
@end
3435

3536
#pragma clang assume_nonnull end
37+
#endif // __OBJC__
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifdef __OBJC__
2+
#pragma clang assume_nonnull begin
3+
4+
@class ForwardClass; // used by API notes
5+
6+
@protocol ProtoWithVersionedUnavailableMember
7+
- (nullable id)requirement;
8+
@end
9+
10+
#pragma clang assume_nonnull end
11+
#endif // __OBJC__
Lines changed: 7 additions & 0 deletions
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-objc.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
3+
// RUN: not %target-swift-frontend -typecheck -F %S/Inputs/custom-frameworks -swift-version 4 %s 2>&1 | %FileCheck -check-prefix=CHECK-DIAGS -check-prefix=CHECK-DIAGS-4 %s
4+
// RUN: not %target-swift-frontend -typecheck -F %S/Inputs/custom-frameworks -swift-version 3 %s 2>&1 | %FileCheck -check-prefix=CHECK-DIAGS -check-prefix=CHECK-DIAGS-3 %s
5+
6+
// REQUIRES: objc_interop
7+
8+
import APINotesFrameworkTest
9+
10+
// CHECK-DIAGS-4-NOT: versioned-objc.swift:[[@LINE-1]]:
11+
class ProtoWithVersionedUnavailableMemberImpl: ProtoWithVersionedUnavailableMember {
12+
// CHECK-DIAGS-3: versioned-objc.swift:[[@LINE-1]]:7: error: type 'ProtoWithVersionedUnavailableMemberImpl' cannot conform to protocol 'ProtoWithVersionedUnavailableMember' because it has requirements that cannot be satisfied
13+
func requirement() -> Any? { return nil }
14+
}
15+
16+
let unrelatedDiagnostic: Int = nil

test/APINotes/versioned.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,41 @@ import APINotesFrameworkTest
1818
func testRenamedTopLevel() {
1919
var value = 0.0
2020

21-
// CHECK-DIAGS-4-NOT: versioned.swift:[[@LINE+1]]
21+
// CHECK-DIAGS-4-NOT: versioned.swift:[[@LINE+1]]:
2222
accept(&value)
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
36+
37+
oldAcceptDoublePointer(&value)
38+
// CHECK-DIAGS: versioned.swift:[[@LINE-1]]:3: error: 'oldAcceptDoublePointer' has been renamed to
39+
// CHECK-DIAGS-4-SAME: 'accept(_:)'
40+
// CHECK-DIAGS-3-SAME: 'acceptPointer(_:)'
41+
// 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
3658
}

0 commit comments

Comments
 (0)