Skip to content

Commit d04675a

Browse files
authored
Merge pull request #59613 from apple/egorzhdan/cxx-typecheck-iuo
[cxx-interop] Allow conforming C++ APIs that return IUO to Swift protocols
2 parents 36476e4 + 546accb commit d04675a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,9 @@ swift::matchWitness(
754754
OptionalAdjustment(std::get<2>(types)));
755755
}
756756

757-
if (!req->isObjC() && reqTypeIsIUO != witnessTypeIsIUO)
757+
if (!req->isObjC() &&
758+
!isa_and_nonnull<clang::CXXMethodDecl>(witness->getClangDecl()) &&
759+
reqTypeIsIUO != witnessTypeIsIUO)
758760
return RequirementMatch(witness, MatchKind::TypeConflict, witnessType);
759761

760762
if (auto result = matchTypes(std::get<0>(types), std::get<1>(types))) {

test/Interop/Cxx/class/Inputs/protocol-conformance.h

+10
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ struct Trivial {
2828
char test3(int, unsigned) { return 42; }
2929
};
3030

31+
struct ReturnsNullableValue {
32+
const int *returnPointer() { return nullptr; }
33+
};
34+
35+
struct ReturnsNonNullValue {
36+
const int *returnPointer() __attribute__((returns_nonnull)) {
37+
return (int *)this;
38+
}
39+
};
40+
3141
#endif // TEST_INTEROP_CXX_CLASS_INPUTS_PROTOCOL_CONFORMANCE_H

test/Interop/Cxx/class/protocol-conformance-typechecker.swift

+15
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,18 @@ protocol HasReturn42 {
1111
extension ConformsToProtocol : HasReturn42 {}
1212

1313
extension DoesNotConformToProtocol : HasReturn42 {} // expected-error {{'DoesNotConformToProtocol' does not conform to protocol}}
14+
15+
16+
protocol HasReturnNullable {
17+
mutating func returnPointer() -> UnsafePointer<Int32>?
18+
}
19+
20+
// HasReturnNullable's returnNullable returns an implicitly unwrapped optional:
21+
// mutating func returnPointer() -> UnsafePointer<Int32>!
22+
extension ReturnsNullableValue: HasReturnNullable {}
23+
24+
protocol HasReturnNonNull {
25+
mutating func returnPointer() -> UnsafePointer<Int32>
26+
}
27+
28+
extension ReturnsNonNullValue: HasReturnNonNull {}

0 commit comments

Comments
 (0)