Skip to content

[SubstitutionMap] Handle conformance lookup via superclass requirements. #14870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,19 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {

auto genericSig = getGenericSignature();

// If the type doesn't conform to this protocol, fail.
if (!genericSig->conformsToProtocol(type, proto))
// If the type doesn't conform to this protocol, the result isn't formed
// from these requirements.
if (!genericSig->conformsToProtocol(type, proto)) {
// Check whether the superclass conforms.
if (auto superclass = genericSig->getSuperclassBound(type)) {
return LookUpConformanceInSignature(*getGenericSignature())(
type->getCanonicalType(),
superclass,
proto->getDeclaredType());
}

return None;
}

auto accessPath =
genericSig->getConformanceAccessPath(type, proto);
Expand Down
31 changes: 31 additions & 0 deletions validation-test/compiler_crashers_2_fixed/0144-sr7072.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-swift-frontend %s -emit-sil -o - | %FileCheck %s

public final class GenClass<Element: Cl> {
public subscript(index: Int) -> Element {
get { return unsafeBitCast(0, to: Element.self) }
}
}

public protocol Proto { }

public struct Iter<Element: Proto>: IteratorProtocol {
public mutating func next() -> Element? { return nil }
}

extension GenClass: RandomAccessCollection {
public func makeIterator() -> Iter<Element> { return Iter() }
public var startIndex: Int { return 0 }
public var endIndex: Int { return 0 }
}

open class Cl: Proto { }

class Bar: Cl {
var x: Int?
}

// CHECK-LABEL: sil hidden @$S4main5crash4barsSbAA8GenClassCyAA3BarCG_tF
func crash(bars: GenClass<Bar>) -> Bool {
// CHECK: apply [[FN:%.*]]<Bar, [Bar]>
return Array(bars.filter { $0.x == nil }).isEmpty
}