Skip to content

Commit c472c22

Browse files
authored
Merge pull request #83268 from slavapestov/fix-rdar156454697
Sema: Handle ArchetypeType and DynamicSelfType in determineBestChoicesInContext()
2 parents 40e372f + 30c7f4a commit c472c22

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,16 @@ static void determineBestChoicesInContext(
11591159
});
11601160
}
11611161

1162+
if (auto *selfType = candidateType->getAs<DynamicSelfType>()) {
1163+
candidateType = selfType->getSelfType();
1164+
}
1165+
1166+
if (auto *archetypeType = candidateType->getAs<ArchetypeType>()) {
1167+
candidateType = archetypeType->getSuperclass();
1168+
if (!candidateType)
1169+
return false;
1170+
}
1171+
11621172
auto *subclassDecl = candidateType->getClassOrBoundGenericClass();
11631173
auto *superclassDecl = superclassType->getClassOrBoundGenericClass();
11641174

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
2+
3+
// rdar://156454697
4+
5+
// The appearance of DynamicSelfType as an argument type to ==
6+
// would lead us to choose the Base.== overload, which is less
7+
// "specific" than Derived.==. There was a similar bug with
8+
// archetypes.
9+
//
10+
// This is a SILGen test, to check the overload choice.
11+
12+
public class Base: Equatable {
13+
public static func ==(lhs: Base, rhs: Base) -> Bool {
14+
return false
15+
}
16+
}
17+
18+
public class Derived: Base {
19+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2f1SbyF : $@convention(method) (@guaranteed Derived) -> Bool {
20+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
21+
// CHECK: return
22+
public func f1() -> Bool {
23+
return self == self
24+
}
25+
26+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2f2SbyF : $@convention(method) (@guaranteed Derived) -> Bool {
27+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
28+
// CHECK: return
29+
public func f2() -> Bool {
30+
let c = self as! Self
31+
return self == c
32+
}
33+
34+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2f3SbyF : $@convention(method) (@guaranteed Derived) -> Bool {
35+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
36+
// CHECK: return
37+
public func f3() -> Bool {
38+
let c = self as! Self
39+
return c == self
40+
}
41+
42+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2f4SbyF : $@convention(method) (@guaranteed Derived) -> Bool {
43+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
44+
// CHECK: return
45+
public func f4() -> Bool {
46+
let c = self as! Self
47+
return c == c
48+
}
49+
50+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2g1ySbxACRbzlF : $@convention(method) <T where T : Derived> (@guaranteed T, @guaranteed Derived) -> Bool {
51+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
52+
// CHECK: return
53+
public func g1<T: Derived>(_ c: T) -> Bool {
54+
return self == c
55+
}
56+
57+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2g2ySbxACRbzlF : $@convention(method) <T where T : Derived> (@guaranteed T, @guaranteed Derived) -> Bool {
58+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
59+
// CHECK: return
60+
public func g2<T: Derived>(_ c: T) -> Bool {
61+
return c == self
62+
}
63+
64+
// CHECK-LABEL: sil [ossa] @$s32subclass_shadows_operator_equals7DerivedC2g3ySbxACRbzlF : $@convention(method) <T where T : Derived> (@guaranteed T, @guaranteed Derived) -> Bool {
65+
// CHECK: function_ref @$s32subclass_shadows_operator_equals7DerivedC2eeoiySbAC_ACtFZ : $@convention(method) (@guaranteed Derived, @guaranteed Derived, @thick Derived.Type) -> Bool
66+
// CHECK: return
67+
public func g3<T: Derived>(_ c: T) -> Bool {
68+
return c == c
69+
}
70+
71+
public static func ==(lhs: Derived, rhs: Derived) -> Bool {
72+
return false
73+
}
74+
}

0 commit comments

Comments
 (0)