Skip to content

🍒 Diagnose conformances on @objcImpl extensions #66649

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
Jun 15, 2023
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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,11 @@ ERROR(attr_objc_implementation_category_not_found,none,
NOTE(attr_objc_implementation_fixit_remove_category_name,none,
"remove arguments to implement the main '@interface' for this class",
())
ERROR(attr_objc_implementation_no_conformance,none,
"'@_objcImplementation' extension cannot add conformance to %0; "
"add this conformance %select{with an ordinary extension|"
"in the Objective-C header}1",
(Type, bool))

ERROR(member_of_objc_implementation_not_objc_or_final,none,
"%0 %1 does not match any %0 declared in the headers for %2; did you use "
Expand Down
12 changes: 12 additions & 0 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,18 @@ class ObjCImplementationChecker {
{
assert(!ext->hasClangNode() && "passed interface, not impl, to checker");

// Conformances are declared exclusively in the interface, so diagnose any
// in the implementation right away.
for (auto &inherited : ext->getInherited()) {
bool isImportedProtocol = false;
if (auto protoNominal = inherited.getType()->getAnyNominal())
isImportedProtocol = protoNominal->hasClangNode();

diagnose(inherited.getLoc(),
diag::attr_objc_implementation_no_conformance,
inherited.getType(), isImportedProtocol);
}

// Did we actually match this extension to an interface? (In invalid code,
// we might not have.)
auto interfaceDecl = ext->getImplementedObjCDecl();
Expand Down
3 changes: 3 additions & 0 deletions test/decl/ext/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@
struct ObjCStruct {
int foo;
};

@protocol EmptyObjCProto
@end
6 changes: 5 additions & 1 deletion test/decl/ext/objc_implementation.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// RUN: %target-typecheck-verify-swift -import-objc-header %S/Inputs/objc_implementation.h
// REQUIRES: objc_interop

@_objcImplementation extension ObjCClass {
protocol EmptySwiftProto {}

@_objcImplementation extension ObjCClass: EmptySwiftProto, EmptyObjCProto {
// expected-note@-1 {{previously implemented by extension here}}
// expected-warning@-2 {{extension for main class interface should provide implementation for instance method 'method(fromHeader4:)'; this will become an error before '@_objcImplementation' is stabilized}}
// expected-warning@-3 {{extension for main class interface should provide implementation for property 'propertyFromHeader9'; this will become an error before '@_objcImplementation' is stabilized}}
// FIXME: give better diagnostic expected-warning@-4 {{extension for main class interface should provide implementation for property 'propertyFromHeader8'; this will become an error before '@_objcImplementation' is stabilized}}
// FIXME: give better diagnostic expected-warning@-5 {{extension for main class interface should provide implementation for property 'propertyFromHeader7'; this will become an error before '@_objcImplementation' is stabilized}}
// FIXME: give better diagnostic expected-warning@-6 {{extension for main class interface should provide implementation for instance method 'method(fromHeader3:)'; this will become an error before '@_objcImplementation' is stabilized}}
// expected-warning@-7 {{'@_objcImplementation' extension cannot add conformance to 'EmptySwiftProto'; add this conformance with an ordinary extension}}
// expected-warning@-8 {{'@_objcImplementation' extension cannot add conformance to 'EmptyObjCProto'; add this conformance in the Objective-C header}}

func method(fromHeader1: CInt) {
// OK, provides an implementation for the header's method.
Expand Down