Skip to content

Commit 96c9456

Browse files
committed
Sema: Still diagnose '@objc' even if Foundation was imported
We checked for an import of a module named 'Foundation' before checking if Objective-C interoperability was enabled, which would lead to hilarious results. Fixes <https://bugs.swift.org/browse/SR-13713> / <rdar://problem/70140319>.
1 parent 9e4bdb6 commit 96c9456

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,26 +1051,26 @@ static void diagnoseObjCAttrWithoutFoundation(ObjCAttr *attr, Decl *decl) {
10511051
return;
10521052

10531053
auto &ctx = SF->getASTContext();
1054-
if (ctx.LangOpts.EnableObjCInterop) {
1055-
// Don't diagnose in a SIL file.
1056-
if (SF->Kind == SourceFileKind::SIL)
1057-
return;
10581054

1059-
// Don't diagnose for -disable-objc-attr-requires-foundation-module.
1060-
if (!ctx.LangOpts.EnableObjCAttrRequiresFoundation)
1061-
return;
1055+
if (!ctx.LangOpts.EnableObjCInterop) {
1056+
ctx.Diags.diagnose(attr->getLocation(), diag::objc_interop_disabled)
1057+
.fixItRemove(attr->getRangeWithAt());
1058+
return;
10621059
}
10631060

1061+
// Don't diagnose in a SIL file.
1062+
if (SF->Kind == SourceFileKind::SIL)
1063+
return;
1064+
1065+
// Don't diagnose for -disable-objc-attr-requires-foundation-module.
1066+
if (!ctx.LangOpts.EnableObjCAttrRequiresFoundation)
1067+
return;
1068+
10641069
// If we have the Foundation module, @objc is okay.
10651070
auto *foundation = ctx.getLoadedModule(ctx.Id_Foundation);
10661071
if (foundation && ctx.getImportCache().isImportedBy(foundation, SF))
10671072
return;
10681073

1069-
if (!ctx.LangOpts.EnableObjCInterop) {
1070-
ctx.Diags.diagnose(attr->getLocation(), diag::objc_interop_disabled)
1071-
.fixItRemove(attr->getRangeWithAt());
1072-
}
1073-
10741074
ctx.Diags.diagnose(attr->getLocation(),
10751075
diag::attr_used_without_required_module, attr,
10761076
ctx.Id_Foundation)

test/SourceKit/Sema/objc_attr_without_import.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %FileCheck -input-file=%t.response %s
33
// This tests that we are not crashing in SILGen.
44

5-
// CHECK: @objc attribute used without importing module
5+
// CHECK: {{Objective-C interoperability is disabled|@objc attribute used without importing module}}
66
@objc protocol Communicate {
77
var name: String { get }
88
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift -disable-objc-interop -module-name Foundation
2+
3+
// Make sure we diagnose this even if the current module is named 'Foundation':
4+
@objc protocol Horse { // expected-error {{Objective-C interoperability is disabled}}
5+
func ride()
6+
}
7+
8+
func rideHorse(_ horse: Horse) {
9+
horse.ride()
10+
}

0 commit comments

Comments
 (0)