Skip to content

Fix two objcImpl resyntaxing bugs #74135

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 2 commits into from
Jun 6, 2024
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
8 changes: 8 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,14 @@ void PrintAST::printAttributes(const Decl *D) {
}
}

// If we are suppressing @implementation, also suppress @objc on extensions.
if (auto ED = dyn_cast<ExtensionDecl>(D)) {
if (ED->isObjCImplementation() &&
Options.excludeAttrKind(DeclAttrKind::ObjCImplementation)) {
Options.ExcludeAttrList.push_back(DeclAttrKind::ObjC);
}
}

// We will handle ownership specifiers separately.
if (isa<FuncDecl>(D)) {
Options.ExcludeAttrList.push_back(DeclAttrKind::Mutating);
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ using namespace swift;
DiagnosticBehavior
swift::behaviorLimitForObjCReason(ObjCReason reason, ASTContext &ctx) {
switch(reason) {
case ObjCReason::MemberOfObjCImplementationExtension:
// If they're using the old syntax, soften to a warning.
if (cast<ObjCImplementationAttr>(reason.getAttr())->isEarlyAdopter())
return DiagnosticBehavior::Warning;

LLVM_FALLTHROUGH;

case ObjCReason::ExplicitlyCDecl:
case ObjCReason::ExplicitlyDynamic:
case ObjCReason::ExplicitlyObjC:
Expand All @@ -51,7 +58,6 @@ swift::behaviorLimitForObjCReason(ObjCReason reason, ASTContext &ctx) {
case ObjCReason::WitnessToObjC:
case ObjCReason::ImplicitlyObjC:
case ObjCReason::MemberOfObjCExtension:
case ObjCReason::MemberOfObjCImplementationExtension:
return DiagnosticBehavior::Unspecified;

case ObjCReason::ExplicitlyIBInspectable:
Expand Down
4 changes: 4 additions & 0 deletions test/ModuleInterface/objc_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import Foundation

// We should never see @_objcImplementation in the header
// NEGATIVE-NOT: @_objcImplementation
// NEGATIVE-NOT: @implementation

// @objc should be omitted on extensions
// NEGATIVE-NOT: @objc{{.*}} extension

//
// @_objcImplementation class
Expand Down
5 changes: 5 additions & 0 deletions test/decl/ext/objc_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ protocol EmptySwiftProto {}
// rdar://122280735 - crash when the parameter of a block property needs @escaping
let rdar122280735: (() -> ()) -> Void = { _ in }
// expected-error@-1 {{property 'rdar122280735' of type '(() -> ()) -> Void' does not match type '(@escaping () -> Void) -> Void' declared by the header}}

private func privateNonObjCMethod(_: EmptySwiftProto) {
// expected-error@-1 {{method cannot be in an @objc @implementation extension of a class (without final or @nonobjc) because the type of the parameter cannot be represented in Objective-C}}
// expected-note@-2 {{protocol-constrained type containing protocol 'EmptySwiftProto' cannot be represented in Objective-C}}
}
}

@objc(PresentAdditions) @implementation extension ObjCClass {
Expand Down
9 changes: 7 additions & 2 deletions test/decl/ext/objc_implementation_early_adopter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ protocol EmptySwiftProto {}
// rdar://122280735 - crash when the parameter of a block property needs @escaping
let rdar122280735: (() -> ()) -> Void = { _ in }
// expected-warning@-1 {{property 'rdar122280735' of type '(() -> ()) -> Void' does not match type '(@escaping () -> Void) -> Void' declared by the header}}

private func privateNonObjCMethod(_: EmptySwiftProto) {
// expected-warning@-1 {{method cannot be in an @objc @implementation extension of a class (without final or @nonobjc) because the type of the parameter cannot be represented in Objective-C}}
// expected-note@-2 {{protocol-constrained type containing protocol 'EmptySwiftProto' cannot be represented in Objective-C}}
}
}

@_objcImplementation(PresentAdditions) extension ObjCClass {
Expand Down Expand Up @@ -428,8 +433,8 @@ protocol EmptySwiftProto {}
func nullableResult() -> Any { fatalError() } // expected-warning {{instance method 'nullableResult()' of type '() -> Any' does not match type '() -> Any?' declared by the header}}
func nullableArgument(_: Any) {} // expected-warning {{instance method 'nullableArgument' of type '(Any) -> ()' does not match type '(Any?) -> Void' declared by the header}}

func nonPointerResult() -> CInt! { fatalError() } // expected-error{{method cannot be in an @objc @implementation extension of a class (without final or @nonobjc) because its result type cannot be represented in Objective-C}}
func nonPointerArgument(_: CInt!) {} // expected-error {{method cannot be in an @objc @implementation extension of a class (without final or @nonobjc) because the type of the parameter cannot be represented in Objective-C}}
func nonPointerResult() -> CInt! { fatalError() } // expected-warning{{method cannot be in an @objc @implementation extension of a class (without final or @nonobjc) because its result type cannot be represented in Objective-C}}
func nonPointerArgument(_: CInt!) {} // expected-warning {{method cannot be in an @objc @implementation extension of a class (without final or @nonobjc) because the type of the parameter cannot be represented in Objective-C}}
}

// Intentionally using `@_objcImplementation` for this test; do not upgrade!
Expand Down