Skip to content

Commit 6ceb47b

Browse files
committed
[Type checker] Provide actual ObjC runtime name in @NSKeyedArchiveLegacy Fix-Its.
1 parent 243b91a commit 6ceb47b

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ NOTE(unstable_mangled_name_add_nskeyedarchivelegacy,none,
12311231
"for compatibility with existing archives, use '@NSKeyedArchiveLegacy' "
12321232
"to record the Swift 3 mangled name", ())
12331233
NOTE(add_nskeyedarchivesubclassesonly_attr,none,
1234-
"generic classes should not be archived directly; "
1234+
"generic class %0 should not be archived directly; "
12351235
"add @NSKeyedArchiveSubclassesOnly "
12361236
"and only archive specific subclasses of this class", (Type))
12371237

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/AccessScope.h"
2424
#include "swift/AST/GenericSignatureBuilder.h"
2525
#include "swift/AST/ASTContext.h"
26+
#include "swift/AST/ASTMangler.h"
2627
#include "swift/AST/ASTPrinter.h"
2728
#include "swift/AST/Decl.h"
2829
#include "swift/AST/ExistentialLayout.h"
@@ -6048,13 +6049,16 @@ void TypeChecker::checkConformancesInContext(DeclContext *dc,
60486049
auto insertionLoc =
60496050
classDecl->getAttributeInsertionLoc(/*forModifier=*/false);
60506051
if (isFixable) {
6052+
Mangle::ASTMangler mangler;
60516053
diagnose(classDecl, diag::unstable_mangled_name_add_objc)
60526054
.fixItInsert(insertionLoc,
60536055
"@objc(<#Objective-C class name#>)");
60546056
diagnose(classDecl,
60556057
diag::unstable_mangled_name_add_nskeyedarchivelegacy)
60566058
.fixItInsert(insertionLoc,
6057-
"@NSKeyedArchiveLegacy(\"<#class archival name#>\")");
6059+
"@NSKeyedArchiveLegacy(\"" +
6060+
mangler.mangleObjCRuntimeName(classDecl) +
6061+
"\")");
60586062
} else {
60596063
diagnose(classDecl, diag::add_nskeyedarchivesubclassesonly_attr,
60606064
classDecl->getDeclaredInterfaceType())

test/decl/protocol/conforms/nscoding.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class CodingA : NSObject, NSCoding {
1818
extension CodingA {
1919
class NestedA : NSObject, NSCoding { // expected-error{{nested class 'CodingA.NestedA' has an unstable name when archiving via 'NSCoding'}}
2020
// expected-note@-1{{for new classes, add '@objc' to specify a unique, prefixed Objective-C runtime name}}{{3-3=@objc(<#Objective-C class name#>)}}
21-
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{3-3=@NSKeyedArchiveLegacy("<#class archival name#>")}}
21+
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{3-3=@NSKeyedArchiveLegacy("_TtCC8nscoding7CodingA7NestedA")}}
2222
required init(coder: NSCoder) { }
2323
func encode(coder: NSCoder) { }
2424
}
2525

2626
class NestedB : NSObject {
2727
// expected-note@-1{{for new classes, add '@objc' to specify a unique, prefixed Objective-C runtime name}}{{3-3=@objc(<#Objective-C class name#>)}}
28-
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{3-3=@NSKeyedArchiveLegacy("<#class archival name#>")}}
28+
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{3-3=@NSKeyedArchiveLegacy("_TtCC8nscoding7CodingA7NestedB")}}
2929
required init(coder: NSCoder) { }
3030
func encode(coder: NSCoder) { }
3131
}
@@ -51,14 +51,14 @@ extension CodingA.NestedD: NSCoding { // okay
5151

5252
// Generic classes
5353
class CodingB<T> : NSObject, NSCoding { // expected-error{{generic class 'CodingB<T>' has an unstable name when archiving via 'NSCoding'}}
54-
// expected-note@-1{{generic classes should not be archived directly}}{{1-1=@NSKeyedArchiveSubclassesOnly}}
54+
// expected-note@-1{{generic class 'CodingB<T>' should not be archived directly}}{{1-1=@NSKeyedArchiveSubclassesOnly}}
5555
required init(coder: NSCoder) { }
5656
func encode(coder: NSCoder) { }
5757
}
5858

5959
extension CodingB {
6060
class NestedA : NSObject, NSCoding { // expected-error{{generic class 'CodingB<T>.NestedA' has an unstable name when archiving via 'NSCoding'}}
61-
// expected-note@-1{{generic classes should not be archived directly}}{{3-3=@NSKeyedArchiveSubclassesOnly}}
61+
// expected-note@-1{{generic class 'CodingB<T>.NestedA' should not be archived directly}}{{3-3=@NSKeyedArchiveSubclassesOnly}}
6262
required init(coder: NSCoder) { }
6363
func encode(coder: NSCoder) { }
6464
}
@@ -67,15 +67,15 @@ extension CodingB {
6767
// Fileprivate classes.
6868
fileprivate class CodingC : NSObject, NSCoding { // expected-error{{fileprivate class 'CodingC' has an unstable name when archiving via 'NSCoding'}}
6969
// expected-note@-1{{for new classes, add '@objc' to specify a unique, prefixed Objective-C runtime name}}{{1-1=@objc(<#Objective-C class name#>)}}
70-
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{1-1=@NSKeyedArchiveLegacy("<#class archival name#>")}}
70+
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{1-1=@NSKeyedArchiveLegacy("_TtC8nscodingP33_0B4E7641C0BD1F170280EEDD0D0C1F6C7CodingC")}}
7171
required init(coder: NSCoder) { }
7272
func encode(coder: NSCoder) { }
7373
}
7474

7575
// Private classes
7676
private class CodingD : NSObject, NSCoding { // expected-error{{private class 'CodingD' has an unstable name when archiving via 'NSCoding'}}
7777
// expected-note@-1{{for new classes, add '@objc' to specify a unique, prefixed Objective-C runtime name}}{{1-1=@objc(<#Objective-C class name#>)}}
78-
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{1-1=@NSKeyedArchiveLegacy("<#class archival name#>")}}
78+
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{1-1=@NSKeyedArchiveLegacy("_TtC8nscodingP33_0B4E7641C0BD1F170280EEDD0D0C1F6C7CodingD")}}
7979
required init(coder: NSCoder) { }
8080
func encode(coder: NSCoder) { }
8181
}
@@ -84,15 +84,15 @@ private class CodingD : NSObject, NSCoding { // expected-error{{private cl
8484
func someFunction() {
8585
class LocalCoding : NSObject, NSCoding { // expected-error{{local class 'LocalCoding' has an unstable name when archiving via 'NSCoding'}}
8686
// expected-note@-1{{for new classes, add '@objc' to specify a unique, prefixed Objective-C runtime name}}{{3-3=@objc(<#Objective-C class name#>)}}
87-
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{3-3=@NSKeyedArchiveLegacy("<#class archival name#>")}}
87+
// expected-note@-2{{for compatibility with existing archives, use '@NSKeyedArchiveLegacy' to record the Swift 3 mangled name}}{{3-3=@NSKeyedArchiveLegacy("_TtCF8nscoding12someFunctionFT_T_L_11LocalCoding")}}
8888
required init(coder: NSCoder) { }
8989
func encode(coder: NSCoder) { }
9090
}
9191
}
9292

9393
// Inherited conformances.
9494
class CodingE<T> : CodingB<T> { // expected-error{{generic class 'CodingE<T>' has an unstable name when archiving via 'NSCoding'}}
95-
// expected-note@-1{{generic classes should not be archived directly}}{{1-1=@NSKeyedArchiveSubclassesOnly}}
95+
// expected-note@-1{{generic class 'CodingE<T>' should not be archived directly}}{{1-1=@NSKeyedArchiveSubclassesOnly}}
9696
required init(coder: NSCoder) { super.init(coder: coder) }
9797
override func encode(coder: NSCoder) { }
9898
}

0 commit comments

Comments
 (0)