Skip to content

Commit bb79e9c

Browse files
authored
Merge pull request #34647 from CodaFi/ancestry-dot-NeXT
2 parents 87f57f9 + b9b30a7 commit bb79e9c

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,6 +4531,9 @@ ERROR(invalid_objc_decl,none,
45314531
"properties, and subscript declarations can be declared @objc", ())
45324532
ERROR(invalid_objc_swift_rooted_class,none,
45334533
"only classes that inherit from NSObject can be declared @objc", ())
4534+
NOTE(invalid_objc_swift_root_class_insert_nsobject,none,
4535+
"inherit from 'NSObject' to silence this error", ())
4536+
45344537
ERROR(invalid_nonobjc_decl,none,
45354538
"only class members and extensions of classes can be declared @nonobjc", ())
45364539
ERROR(invalid_nonobjc_extension,none,

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,13 +1155,28 @@ static Optional<ObjCReason> shouldMarkClassAsObjC(const ClassDecl *CD) {
11551155
// (Leave a hole for test cases.)
11561156
if (ancestry.contains(AncestryFlags::ObjC) &&
11571157
!ancestry.contains(AncestryFlags::ClangImported)) {
1158-
if (ctx.LangOpts.EnableObjCAttrRequiresFoundation)
1158+
if (ctx.LangOpts.EnableObjCAttrRequiresFoundation) {
11591159
ctx.Diags.diagnose(attr->getLocation(),
11601160
diag::invalid_objc_swift_rooted_class)
11611161
.fixItRemove(attr->getRangeWithAt());
1162-
if (!ctx.LangOpts.EnableObjCInterop)
1162+
// If the user has not spelled out a superclass, offer to insert
1163+
// 'NSObject'. We could also offer to replace the existing superclass,
1164+
// but that's a touch aggressive.
1165+
if (CD->getInherited().empty()) {
1166+
auto nameEndLoc = Lexer::getLocForEndOfToken(ctx.SourceMgr,
1167+
CD->getNameLoc());
1168+
CD->diagnose(diag::invalid_objc_swift_root_class_insert_nsobject)
1169+
.fixItInsert(nameEndLoc, ": NSObject");
1170+
} else if (CD->getSuperclass().isNull()) {
1171+
CD->diagnose(diag::invalid_objc_swift_root_class_insert_nsobject)
1172+
.fixItInsert(CD->getInherited().front().getLoc(), "NSObject, ");
1173+
}
1174+
}
1175+
1176+
if (!ctx.LangOpts.EnableObjCInterop) {
11631177
ctx.Diags.diagnose(attr->getLocation(), diag::objc_interop_disabled)
11641178
.fixItRemove(attr->getRangeWithAt());
1179+
}
11651180
}
11661181

11671182
return ObjCReason(ObjCReason::ExplicitlyObjC);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend -typecheck -verify -enable-objc-interop %s
2+
// RUN: %target-swift-frontend -typecheck -verify -enable-objc-interop -parse-as-library %s
3+
4+
// REQUIRES: objc_interop
5+
6+
import Foundation
7+
8+
class NonObjCBase {}
9+
protocol NonObjCProto {}
10+
11+
@objc class AddSuper {} // expected-error {{only classes that inherit from NSObject can be declared @objc}} {{1-7=}}
12+
// expected-note@-1 {{inherit from 'NSObject' to silence this error}} {{21-21=: NSObject}}
13+
14+
@objc class InsertSuper: NonObjCProto {} // expected-error {{only classes that inherit from NSObject can be declared @objc}} {{1-7=}}
15+
// expected-note@-1 {{inherit from 'NSObject' to silence this error}} {{26-26=NSObject, }}
16+
17+
@objc class NoNote: NonObjCBase {} // expected-error {{only classes that inherit from NSObject can be declared @objc}} {{1-7=}}

test/Sema/objc_attr_requires_module_1.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// this test cover both cases.
77

88
@objc class Foo {} // expected-error {{@objc attribute used without importing module 'Foundation'}} expected-error {{only classes that inherit from NSObject can be declared @objc}} {{1-7=}}
9+
// expected-note@-1 {{inherit from 'NSObject' to silence this error}} {{16-16=: NSObject}}
910

1011
#if false
1112
#endif

test/Sema/objc_attr_requires_module_2.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ class Foo {}
44
_ = Foo()
55

66
@objc class NSFoo {} // expected-error {{@objc attribute used without importing module 'Foundation'}} expected-error {{only classes that inherit from NSObject can be declared @objc}} {{1-7=}}
7+
// expected-note@-1 {{inherit from 'NSObject' to silence this error}} {{18-18=: NSObject}}

0 commit comments

Comments
 (0)