Skip to content

Commit 5f2f440

Browse files
authored
Merge pull request #14849 from DougGregor/lookup-conformance-via-superclass-bound
2 parents a62647f + 46e623a commit 5f2f440

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/AST/SubstitutionMap.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,19 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
192192

193193
auto genericSig = getGenericSignature();
194194

195-
// If the type doesn't conform to this protocol, fail.
196-
if (!genericSig->conformsToProtocol(type, proto))
195+
// If the type doesn't conform to this protocol, the result isn't formed
196+
// from these requirements.
197+
if (!genericSig->conformsToProtocol(type, proto)) {
198+
// Check whether the superclass conforms.
199+
if (auto superclass = genericSig->getSuperclassBound(type)) {
200+
return LookUpConformanceInSignature(*getGenericSignature())(
201+
type->getCanonicalType(),
202+
superclass,
203+
proto->getDeclaredType());
204+
}
205+
197206
return None;
207+
}
198208

199209
auto accessPath =
200210
genericSig->getConformanceAccessPath(type, proto);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-swift-frontend %s -emit-sil -o - | %FileCheck %s
2+
3+
public final class GenClass<Element: Cl> {
4+
public subscript(index: Int) -> Element {
5+
get { return unsafeBitCast(0, to: Element.self) }
6+
}
7+
}
8+
9+
public protocol Proto { }
10+
11+
public struct Iter<Element: Proto>: IteratorProtocol {
12+
public mutating func next() -> Element? { return nil }
13+
}
14+
15+
extension GenClass: RandomAccessCollection {
16+
public func makeIterator() -> Iter<Element> { return Iter() }
17+
public var startIndex: Int { return 0 }
18+
public var endIndex: Int { return 0 }
19+
}
20+
21+
open class Cl: Proto { }
22+
23+
class Bar: Cl {
24+
var x: Int?
25+
}
26+
27+
// CHECK-LABEL: sil hidden @$S4main5crash4barsSbAA8GenClassCyAA3BarCG_tF
28+
func crash(bars: GenClass<Bar>) -> Bool {
29+
// CHECK: apply [[FN:%.*]]<Bar, [Bar]>
30+
return Array(bars.filter { $0.x == nil }).isEmpty
31+
}

0 commit comments

Comments
 (0)