Skip to content

Commit 6966b7f

Browse files
committed
[TypeChecker] Ignore mismatches on sendability in @objc @implementation witnesses unless in strict concurrency mode
The requirement is `@preconcurrency` in this case and the checking should abide by the rules.
1 parent 0bccec6 commit 6966b7f

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,12 +3889,27 @@ class ObjCImplementationChecker {
38893889
diagnoseVTableUse(cand);
38903890
return;
38913891

3892-
case MatchOutcome::WrongSendability:
3892+
case MatchOutcome::WrongSendability: {
3893+
auto concurrencyLevel = req->getASTContext().LangOpts.StrictConcurrencyLevel;
3894+
3895+
DiagnosticBehavior behavior;
3896+
switch (concurrencyLevel) {
3897+
case StrictConcurrency::Complete:
3898+
behavior = DiagnosticBehavior::Warning;
3899+
break;
3900+
3901+
case StrictConcurrency::Targeted:
3902+
case StrictConcurrency::Minimal:
3903+
behavior = DiagnosticBehavior::Ignore;
3904+
break;
3905+
}
3906+
38933907
diagnose(cand, diag::objc_implementation_sendability_mismatch, cand,
38943908
getMemberType(cand), getMemberType(req))
3895-
.limitBehaviorWithPreconcurrency(DiagnosticBehavior::Warning,
3909+
.limitBehaviorWithPreconcurrency(behavior,
38963910
/*preconcurrency*/ true);
38973911
return;
3912+
}
38983913

38993914
case MatchOutcome::WrongImplicitObjCName:
39003915
case MatchOutcome::WrongExplicitObjCName: {

test/Concurrency/sendable_objc_attr_in_type_context_swift5.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ class TestConformanceWithoutStripping : InnerSendableTypes {
172172
@objc public required init(callback: @escaping () -> Void) {}
173173
// expected-error@-1 {{initializer 'init(callback:)' should not be 'required' to match initializer declared by the header}}
174174

175-
@objc func compute(completionHandler: @escaping () -> Void) {}
176-
// expected-warning@-1 {{sendability of function types in instance method 'compute(completionHandler:)' of type '(@escaping () -> Void) -> ()' does not match type '(@escaping @Sendable () -> Void) -> Void' declared by the header}}
175+
@objc func compute(completionHandler: @escaping () -> Void) {} // Ok (no warnings with minimal checking)
177176
}
178177

179178
// Methods deliberately has no `@Sendable` to make sure that

test/Concurrency/sendable_objc_attr_in_type_context_swift5_strict.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ void doSomethingConcurrently(__attribute__((noescape)) void SWIFT_SENDABLE (^blo
6565
-(void) testWithCallback:(NSString *)name handler:(MAIN_ACTOR void (^)(NSDictionary<NSString *, SWIFT_SENDABLE id> *, NSError * _Nullable))handler;
6666
@end
6767

68+
@interface SwiftImpl : NSObject
69+
-(id)initWithCallback: (void (^ SWIFT_SENDABLE)(void)) callback;
70+
-(void)computeWithCompletionHandler: (void (^)(void)) handler;
71+
@end
72+
6873
#pragma clang assume_nonnull end
6974

7075
//--- main.swift
@@ -153,3 +158,11 @@ class TestConformanceWithoutStripping : InnerSendableTypes {
153158
func test(withCallback name: String, handler: @escaping @MainActor ([String : any Sendable], (any Error)?) -> Void) { // Ok
154159
}
155160
}
161+
162+
@objc @implementation extension SwiftImpl {
163+
@objc public required init(callback: @escaping () -> Void) {}
164+
// expected-error@-1 {{initializer 'init(callback:)' should not be 'required' to match initializer declared by the header}}
165+
166+
@objc func compute(completionHandler: @escaping () -> Void) {}
167+
// expected-warning@-1 {{sendability of function types in instance method 'compute(completionHandler:)' of type '(@escaping () -> Void) -> ()' does not match type '(@escaping @Sendable () -> Void) -> Void' declared by the header}}
168+
}

0 commit comments

Comments
 (0)