@@ -28,7 +28,34 @@ using namespace swift;
2828using namespace swift ::objc_translation;
2929
3030static 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 *) {
3259 // enum SomeEnum {
3360 // @derived
3461 // static var _nsErrorDomain: String {
@@ -52,11 +79,12 @@ static void deriveBodyBridgedNSError_enum_nsErrorDomain(
5279}
5380
5481static ValueDecl *
55- deriveBridgedNSError_enum_nsErrorDomain (DerivedConformance &derived) {
82+ deriveBridgedNSError_enum_nsErrorDomain (DerivedConformance &derived,
83+ void (*synthesizer)(AbstractFunctionDecl *, void *)) {
5684 // enum SomeEnum {
5785 // @derived
5886 // static var _nsErrorDomain: String {
59- // return "ModuleName.SomeEnum"
87+ // ...
6088 // }
6189 // }
6290
@@ -74,7 +102,7 @@ deriveBridgedNSError_enum_nsErrorDomain(DerivedConformance &derived) {
74102 // Define the getter.
75103 auto getterDecl = derived.addGetterToReadOnlyDerivedProperty (
76104 derived.TC , propDecl, stringTy);
77- getterDecl->setBodySynthesizer (&deriveBodyBridgedNSError_enum_nsErrorDomain );
105+ getterDecl->setBodySynthesizer (synthesizer );
78106
79107 derived.addMembersToConformanceContext ({getterDecl, propDecl, pbDecl});
80108
@@ -85,8 +113,17 @@ ValueDecl *DerivedConformance::deriveBridgedNSError(ValueDecl *requirement) {
85113 if (!isa<EnumDecl>(Nominal))
86114 return nullptr ;
87115
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+ }
90127
91128 TC.diagnose (requirement->getLoc (), diag::broken_errortype_requirement);
92129 return nullptr ;
0 commit comments