Skip to content

Commit 380c641

Browse files
committed
Correct behavior of diags in shouldMarkClassAsObjC
1 parent 1f134c1 commit 380c641

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,13 @@ static Optional<ObjCReason> shouldMarkClassAsObjC(const ClassDecl *CD) {
11411141
auto ancestry = CD->checkAncestry();
11421142

11431143
if (auto attr = CD->getAttrs().getAttribute<ObjCAttr>()) {
1144+
auto reason = objCReasonForObjCAttr(attr);
1145+
auto behavior = behaviorLimitForObjCReason(reason, ctx);
1146+
1147+
SourceLoc attrLoc = attr->getLocation();
1148+
if (attrLoc.isInvalid())
1149+
attrLoc = CD->getLoc();
1150+
11441151
if (ancestry.contains(AncestryFlags::Generic)) {
11451152
if (attr->hasName() && !CD->isGenericContext()) {
11461153
// @objc with a name on a non-generic subclass of a generic class is
@@ -1150,8 +1157,9 @@ static Optional<ObjCReason> shouldMarkClassAsObjC(const ClassDecl *CD) {
11501157
return None;
11511158
}
11521159

1153-
ctx.Diags.diagnose(attr->getLocation(), diag::objc_for_generic_class)
1154-
.fixItRemove(attr->getRangeWithAt());
1160+
ctx.Diags.diagnose(attrLoc, diag::objc_for_generic_class)
1161+
.fixItRemove(attr->getRangeWithAt())
1162+
.limitBehavior(behavior);
11551163
}
11561164

11571165
// If the class has resilient ancestry, @objc just controls the runtime
@@ -1170,43 +1178,48 @@ static Optional<ObjCReason> shouldMarkClassAsObjC(const ClassDecl *CD) {
11701178
auto platform = prettyPlatformString(targetPlatform(ctx.LangOpts));
11711179
auto range = getMinOSVersionForClassStubs(target);
11721180
auto *ancestor = getResilientAncestor(CD->getParentModule(), CD);
1173-
ctx.Diags.diagnose(attr->getLocation(),
1181+
ctx.Diags.diagnose(attrLoc,
11741182
diag::objc_for_resilient_class,
11751183
ancestor->getName(),
11761184
platform,
11771185
range.getLowerEndpoint())
1178-
.fixItRemove(attr->getRangeWithAt());
1186+
.fixItRemove(attr->getRangeWithAt())
1187+
.limitBehavior(behavior);
11791188
}
11801189

11811190
// Only allow ObjC-rooted classes to be @objc.
11821191
// (Leave a hole for test cases.)
11831192
if (ancestry.contains(AncestryFlags::ObjC) &&
11841193
!ancestry.contains(AncestryFlags::ClangImported)) {
11851194
if (ctx.LangOpts.EnableObjCAttrRequiresFoundation) {
1186-
ctx.Diags.diagnose(attr->getLocation(),
1195+
ctx.Diags.diagnose(attrLoc,
11871196
diag::invalid_objc_swift_rooted_class)
1188-
.fixItRemove(attr->getRangeWithAt());
1197+
.fixItRemove(attr->getRangeWithAt())
1198+
.limitBehavior(behavior);
11891199
// If the user has not spelled out a superclass, offer to insert
11901200
// 'NSObject'. We could also offer to replace the existing superclass,
11911201
// but that's a touch aggressive.
11921202
if (CD->getInherited().empty()) {
11931203
auto nameEndLoc = Lexer::getLocForEndOfToken(ctx.SourceMgr,
11941204
CD->getNameLoc());
11951205
CD->diagnose(diag::invalid_objc_swift_root_class_insert_nsobject)
1196-
.fixItInsert(nameEndLoc, ": NSObject");
1206+
.fixItInsert(nameEndLoc, ": NSObject")
1207+
.limitBehavior(behavior);
11971208
} else if (CD->getSuperclass().isNull()) {
11981209
CD->diagnose(diag::invalid_objc_swift_root_class_insert_nsobject)
1199-
.fixItInsert(CD->getInherited().front().getLoc(), "NSObject, ");
1210+
.fixItInsert(CD->getInherited().front().getLoc(), "NSObject, ")
1211+
.limitBehavior(behavior);
12001212
}
12011213
}
12021214

12031215
if (!ctx.LangOpts.EnableObjCInterop) {
1204-
ctx.Diags.diagnose(attr->getLocation(), diag::objc_interop_disabled)
1205-
.fixItRemove(attr->getRangeWithAt());
1216+
ctx.Diags.diagnose(attrLoc, diag::objc_interop_disabled)
1217+
.fixItRemove(attr->getRangeWithAt())
1218+
.limitBehavior(behavior);
12061219
}
12071220
}
12081221

1209-
return objCReasonForObjCAttr(CD->getAttrs().getAttribute<ObjCAttr>());
1222+
return reason;
12101223
}
12111224

12121225
if (ancestry.contains(AncestryFlags::ObjC)) {

0 commit comments

Comments
 (0)