Skip to content

Commit 68b6c7e

Browse files
authored
Merge pull request #74492 from xedin/rdar-127520993
[ClangImporter] Always add `AnyObject` constraint to generic parameters
2 parents b8b6395 + 4cc69b8 commit 68b6c7e

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7358,10 +7358,10 @@ std::optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
73587358
TypeLoc::withoutLoc(proto->getDeclaredInterfaceType()));
73597359
}
73607360
}
7361-
if (inherited.empty()) {
7362-
inherited.push_back(
7363-
TypeLoc::withoutLoc(Impl.SwiftContext.getAnyObjectConstraint()));
7364-
}
7361+
7362+
inherited.push_back(
7363+
TypeLoc::withoutLoc(Impl.SwiftContext.getAnyObjectConstraint()));
7364+
73657365
genericParamDecl->setInherited(Impl.SwiftContext.AllocateCopy(inherited));
73667366

73677367
genericParams.push_back(genericParamDecl);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -emit-silgen %t/main.swift -import-objc-header %t/Test.h | %FileCheck %t/main.swift
5+
6+
// REQUIRES: objc_interop
7+
// REQUIRES: concurrency
8+
9+
10+
// rdar://127520993
11+
12+
//--- Test.h
13+
#import <Foundation/Foundation.h>
14+
15+
#define SWIFT_SENDABLE __attribute__((__swift_attr__("@Sendable")))
16+
17+
@interface Test<N : id SWIFT_SENDABLE> : NSObject
18+
- (void)luckWithNumber:(nullable N)number;
19+
@end
20+
21+
//--- main.swift
22+
import Foundation
23+
24+
Test<NSNumber>().luck(withNumber: 5)
25+
// CHECK-LABEL: sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32
26+
// CHECK: [[NS_NUMBER_INIT:%.*]] = function_ref @$sSo8NSNumberC10FoundationE14integerLiteralABSi_tcfC : $@convention(method) (Int, @thick NSNumber.Type) -> @owned NSNumber
27+
// CHECK-NEXT: [[NS_NUMBER:%.*]] = apply [[NS_NUMBER_INIT]]({{.*}}) : $@convention(method) (Int, @thick NSNumber.Type) -> @owned NSNumber
28+
// CHECK-NEXT: [[OPT_NS_NUMBER:%.*]] = enum $Optional<NSNumber>, #Optional.some!enumelt, [[NS_NUMBER]] : $NSNumber
29+
// CHECK-NEXT: [[LUCK_METHOD_REF:%.*]] = objc_method %4 : $Test<NSNumber>, #Test.luck!foreign : <N where N : AnyObject, N : Sendable> (Test<N>) -> (N?) -> (), $@convention(objc_method) @pseudogeneric <τ_0_0 where τ_0_0 : AnyObject, τ_0_0 : Sendable> (Optional<τ_0_0>, Test<τ_0_0>) -> ()
30+
// CHECK-NEXT: %14 = apply [[LUCK_METHOD_REF]]<NSNumber>([[OPT_NS_NUMBER]], {{.*}}) : $@convention(objc_method) @pseudogeneric <τ_0_0 where τ_0_0 : AnyObject, τ_0_0 : Sendable> (Optional<τ_0_0>, Test<τ_0_0>) -> ()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-clang %t/Test.m -c -o %t/Test.o
5+
// RUN: %target-build-swift %t/main.swift -import-objc-header %t/Test.h %t/Test.o -o %t/main
6+
// RUN: %target-codesign %t/main
7+
// RUN: %target-run %t/main | %FileCheck %t/main.swift
8+
9+
// REQUIRES: executable_test
10+
// REQUIRES: objc_interop
11+
// REQUIRES: concurrency
12+
// REQUIRES: OS=macosx
13+
14+
// rdar://127520993
15+
16+
//--- Test.h
17+
#import <Foundation/Foundation.h>
18+
19+
#define SWIFT_SENDABLE __attribute__((__swift_attr__("@Sendable")))
20+
21+
@interface Test<N : id SWIFT_SENDABLE> : NSObject
22+
- (void)luckWithNumber:(nullable N)number;
23+
@end
24+
25+
//--- Test.m
26+
#import "Test.h"
27+
28+
static void NSPrint(NSString *format, ...)
29+
{
30+
va_list args;
31+
32+
va_start(args, format);
33+
NSString *string = [[NSString alloc] initWithFormat:format arguments:args];
34+
va_end(args);
35+
36+
fprintf(stdout, "%s\n", [string UTF8String]);
37+
38+
#if !__has_feature(objc_arc)
39+
[string release];
40+
#endif
41+
}
42+
43+
@implementation Test
44+
45+
- (void)luckWithNumber:(id)number
46+
{
47+
NSPrint(@"Lucky number: %@", number);
48+
}
49+
50+
@end
51+
52+
//--- main.swift
53+
Test<NSNumber>().luck(withNumber: 5)
54+
// CHECK: Lucky number: 5

0 commit comments

Comments
 (0)