Skip to content

Ban @objcMembers Without Importing Foundation #60612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5238,7 +5238,7 @@ ERROR(objc_extension_not_class,none,
"'@objc' can only be applied to an extension of a class", ())

// If you change this, also change enum ObjCReason
#define OBJC_ATTR_SELECT "select{marked @_cdecl|marked dynamic|marked @objc|marked @IBOutlet|marked @IBAction|marked @IBSegueAction|marked @NSManaged|a member of an @objc protocol|implicitly @objc|an @objc override|an implementation of an @objc requirement|marked @IBInspectable|marked @GKInspectable|in an @objc extension of a class (without @nonobjc)|marked @objc by an access note}"
#define OBJC_ATTR_SELECT "select{marked @_cdecl|marked dynamic|marked @objc|marked @objcMembers|marked @IBOutlet|marked @IBAction|marked @IBSegueAction|marked @NSManaged|a member of an @objc protocol|implicitly @objc|an @objc override|an implementation of an @objc requirement|marked @IBInspectable|marked @GKInspectable|in an @objc extension of a class (without @nonobjc)|marked @objc by an access note}"

WARNING(attribute_meaningless_when_nonobjc,none,
"'@%0' attribute is meaningless on a property that cannot be "
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,11 @@ static bool checkObjCDeclContext(Decl *D) {
return false;
}

static void diagnoseObjCAttrWithoutFoundation(ObjCAttr *attr, Decl *decl,
static void diagnoseObjCAttrWithoutFoundation(DeclAttribute *attr, Decl *decl,
ObjCReason reason,
DiagnosticBehavior behavior) {
assert(attr->getKind() == DeclAttrKind::DAK_ObjC ||
attr->getKind() == DeclAttrKind::DAK_ObjCMembers);
auto *SF = decl->getDeclContext()->getParentSourceFile();
assert(SF);

Expand Down Expand Up @@ -1369,6 +1371,10 @@ void AttributeChecker::visitNonObjCAttr(NonObjCAttr *attr) {
void AttributeChecker::visitObjCMembersAttr(ObjCMembersAttr *attr) {
if (!isa<ClassDecl>(D))
diagnoseAndRemoveAttr(attr, diag::objcmembers_attribute_nonclass);

auto reason = ObjCReason(ObjCReason::ExplicitlyObjCMembers, attr);
auto behavior = behaviorLimitForObjCReason(reason, Ctx);
diagnoseObjCAttrWithoutFoundation(attr, D, reason, behavior);
}

void AttributeChecker::visitOptionalAttr(OptionalAttr *attr) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ swift::behaviorLimitForObjCReason(ObjCReason reason, ASTContext &ctx) {
case ObjCReason::ExplicitlyCDecl:
case ObjCReason::ExplicitlyDynamic:
case ObjCReason::ExplicitlyObjC:
case ObjCReason::ExplicitlyObjCMembers:
case ObjCReason::ExplicitlyIBOutlet:
case ObjCReason::ExplicitlyIBAction:
case ObjCReason::ExplicitlyIBSegueAction:
Expand Down Expand Up @@ -71,6 +72,7 @@ unsigned swift::getObjCDiagnosticAttrKind(ObjCReason reason) {
case ObjCReason::ExplicitlyCDecl:
case ObjCReason::ExplicitlyDynamic:
case ObjCReason::ExplicitlyObjC:
case ObjCReason::ExplicitlyObjCMembers:
case ObjCReason::ExplicitlyIBOutlet:
case ObjCReason::ExplicitlyIBAction:
case ObjCReason::ExplicitlyIBSegueAction:
Expand Down Expand Up @@ -128,6 +130,7 @@ void ObjCReason::describe(const Decl *D) const {
case ObjCReason::ExplicitlyCDecl:
case ObjCReason::ExplicitlyDynamic:
case ObjCReason::ExplicitlyObjC:
case ObjCReason::ExplicitlyObjCMembers:
case ObjCReason::ExplicitlyIBOutlet:
case ObjCReason::ExplicitlyIBAction:
case ObjCReason::ExplicitlyIBSegueAction:
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/TypeCheckObjC.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ObjCReason {
ExplicitlyDynamic,
/// Has an explicit '@objc' attribute.
ExplicitlyObjC,
/// Has an explicit '@objcmembers' attribute.
ExplicitlyObjCMembers,
/// Has an explicit '@IBOutlet' attribute.
ExplicitlyIBOutlet,
/// Has an explicit '@IBAction' attribute.
Expand Down Expand Up @@ -101,6 +103,7 @@ class ObjCReason {
case ExplicitlyCDecl:
case ExplicitlyDynamic:
case ExplicitlyObjC:
case ExplicitlyObjCMembers:
case ExplicitlyIBOutlet:
case ExplicitlyIBAction:
case ExplicitlyIBSegueAction:
Expand Down
6 changes: 6 additions & 0 deletions test/Sema/objcmembers_attr_requires_module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-swift-frontend -typecheck -verify -enable-objc-interop %s
// RUN: %target-swift-frontend -typecheck -verify -enable-objc-interop %s -parse-as-library

@objcMembers class Oof {
// expected-error@-1 {{@objcMembers attribute used without importing module 'Foundation'}}
}