@@ -28,7 +28,34 @@ using namespace swift;
28
28
using namespace swift ::objc_translation;
29
29
30
30
static void deriveBodyBridgedNSError_enum_nsErrorDomain (
31
- AbstractFunctionDecl *domainDecl, void *) {
31
+ AbstractFunctionDecl *domainDecl, void *) {
32
+ // enum SomeEnum {
33
+ // @derived
34
+ // static var _nsErrorDomain: String {
35
+ // return _typeName(self, qualified: true)
36
+ // }
37
+ // }
38
+
39
+ auto M = domainDecl->getParentModule ();
40
+ auto &C = M->getASTContext ();
41
+ auto self = domainDecl->getImplicitSelfDecl ();
42
+
43
+ auto selfRef = new (C) DeclRefExpr (self, DeclNameLoc (), /* implicit*/ true );
44
+ auto stringType = TypeExpr::createForDecl (SourceLoc (), C.getStringDecl (),
45
+ domainDecl, /* implicit*/ true );
46
+ auto initReflectingCall =
47
+ CallExpr::createImplicit (C, stringType,
48
+ { selfRef }, { C.getIdentifier (" reflecting" ) });
49
+ auto ret =
50
+ new (C) ReturnStmt (SourceLoc (), initReflectingCall, /* implicit*/ true );
51
+
52
+ auto body = BraceStmt::create (C, SourceLoc (), ASTNode (ret), SourceLoc ());
53
+
54
+ domainDecl->setBody (body);
55
+ }
56
+
57
+ static void deriveBodyBridgedNSError_printAsObjCEnum_nsErrorDomain (
58
+ AbstractFunctionDecl *domainDecl, void *) {
32
59
// enum SomeEnum {
33
60
// @derived
34
61
// static var _nsErrorDomain: String {
@@ -52,11 +79,12 @@ static void deriveBodyBridgedNSError_enum_nsErrorDomain(
52
79
}
53
80
54
81
static ValueDecl *
55
- deriveBridgedNSError_enum_nsErrorDomain (DerivedConformance &derived) {
82
+ deriveBridgedNSError_enum_nsErrorDomain (DerivedConformance &derived,
83
+ void (*synthesizer)(AbstractFunctionDecl *, void *)) {
56
84
// enum SomeEnum {
57
85
// @derived
58
86
// static var _nsErrorDomain: String {
59
- // return "ModuleName.SomeEnum"
87
+ // ...
60
88
// }
61
89
// }
62
90
@@ -74,7 +102,7 @@ deriveBridgedNSError_enum_nsErrorDomain(DerivedConformance &derived) {
74
102
// Define the getter.
75
103
auto getterDecl = derived.addGetterToReadOnlyDerivedProperty (
76
104
derived.TC , propDecl, stringTy);
77
- getterDecl->setBodySynthesizer (&deriveBodyBridgedNSError_enum_nsErrorDomain );
105
+ getterDecl->setBodySynthesizer (synthesizer );
78
106
79
107
derived.addMembersToConformanceContext ({getterDecl, propDecl, pbDecl});
80
108
@@ -85,8 +113,17 @@ ValueDecl *DerivedConformance::deriveBridgedNSError(ValueDecl *requirement) {
85
113
if (!isa<EnumDecl>(Nominal))
86
114
return nullptr ;
87
115
88
- if (requirement->getBaseName () == TC.Context .Id_nsErrorDomain )
89
- return deriveBridgedNSError_enum_nsErrorDomain (*this );
116
+ if (requirement->getBaseName () == TC.Context .Id_nsErrorDomain ) {
117
+ auto synthesizer = deriveBodyBridgedNSError_enum_nsErrorDomain;
118
+
119
+ auto scope = Nominal->getFormalAccessScope (Nominal->getModuleScopeContext ());
120
+ if (scope.isPublic () || scope.isInternal ())
121
+ // PrintAsObjC may print this domain, so we should make sure we use the
122
+ // same string it will.
123
+ synthesizer = deriveBodyBridgedNSError_printAsObjCEnum_nsErrorDomain;
124
+
125
+ return deriveBridgedNSError_enum_nsErrorDomain (*this , synthesizer);
126
+ }
90
127
91
128
TC.diagnose (requirement->getLoc (), diag::broken_errortype_requirement);
92
129
return nullptr ;
0 commit comments