Skip to content

Commit ca50986

Browse files
authored
Merge pull request #83308 from slavapestov/fix-rdar156095800
Embedded Swift: Fix === and !== with existential
2 parents ba88078 + b7766b6 commit ca50986

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,17 @@ bool MissingConformanceFailure::diagnoseAsError() {
671671
};
672672

673673
// Limit this to `Equatable` and `Comparable` protocols for now.
674-
auto *protocol = getRHS()->castTo<ProtocolType>()->getDecl();
675-
if (isEnumWithAssociatedValues(getLHS()) &&
676-
(protocol->isSpecificProtocol(KnownProtocolKind::Equatable) ||
677-
protocol->isSpecificProtocol(KnownProtocolKind::Comparable))) {
678-
if (RequirementFailure::diagnoseAsError()) {
679-
auto opName = getOperatorName(expr);
680-
emitDiagnostic(diag::no_binary_op_overload_for_enum_with_payload,
681-
opName->str());
682-
return true;
674+
if (auto *protocolTy = getRHS()->getAs<ProtocolType>()) {
675+
auto *protocol = protocolTy->getDecl();
676+
if (isEnumWithAssociatedValues(getLHS()) &&
677+
(protocol->isSpecificProtocol(KnownProtocolKind::Equatable) ||
678+
protocol->isSpecificProtocol(KnownProtocolKind::Comparable))) {
679+
if (RequirementFailure::diagnoseAsError()) {
680+
auto opName = getOperatorName(expr);
681+
emitDiagnostic(diag::no_binary_op_overload_for_enum_with_payload,
682+
opName->str());
683+
return true;
684+
}
683685
}
684686
}
685687
}

stdlib/public/core/Equatable.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool {
271271
#else
272272
@inlinable // trivial-implementation
273273
@safe
274-
public func ===<T: AnyObject, U: AnyObject>(lhs: T?, rhs: U?) -> Bool {
274+
public func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool {
275275
switch (lhs, rhs) {
276276
case let (l?, r?):
277277
return Builtin.bridgeToRawPointer(l) == Builtin.bridgeToRawPointer(r)
@@ -293,14 +293,7 @@ public func ===<T: AnyObject, U: AnyObject>(lhs: T?, rhs: U?) -> Bool {
293293
/// - Parameters:
294294
/// - lhs: A reference to compare.
295295
/// - rhs: Another reference to compare.
296-
#if !$Embedded
297296
@inlinable // trivial-implementation
298297
public func !== (lhs: AnyObject?, rhs: AnyObject?) -> Bool {
299298
return !(lhs === rhs)
300299
}
301-
#else
302-
@inlinable // trivial-implementation
303-
public func !==<T: AnyObject, U: AnyObject>(lhs: T, rhs: U) -> Bool {
304-
return !(lhs === rhs)
305-
}
306-
#endif

test/embedded/anyobject.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -swift-version 5) | %FileCheck %s
2+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -O -swift-version 5) | %FileCheck %s
3+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -Osize -swift-version 5) | %FileCheck %s
4+
5+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -swift-version 6) | %FileCheck %s
6+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -O -swift-version 6) | %FileCheck %s
7+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -Osize -swift-version 6) | %FileCheck %s
8+
9+
// REQUIRES: swift_in_compiler
10+
// REQUIRES: executable_test
11+
// REQUIRES: optimized_stdlib
12+
// REQUIRES: swift_feature_Embedded
13+
14+
protocol P: AnyObject {}
15+
16+
class C: P {}
17+
18+
@main
19+
struct Main {
20+
static func main() {
21+
let p1: any P = C()
22+
let p2: any P = C()
23+
let p3 = p1
24+
25+
// CHECK: false
26+
print(p1 === p2)
27+
print(p2 === p1)
28+
// CHECK: true
29+
print(p1 === p3)
30+
print(p3 === p1)
31+
// CHECK: false
32+
print(p2 === p3)
33+
print(p3 === p2)
34+
}
35+
}
36+

0 commit comments

Comments
 (0)