Skip to content
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
13 changes: 11 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4172,9 +4172,14 @@ namespace {

auto dc = subscript->getInnermostDeclContext();
SmallVector<Substitution, 4> subs;
SubstitutionMap subMap;
auto indexType = subscript->getIndicesInterfaceType();

if (auto sig = dc->getGenericSignatureOfContext()) {
// Compute substitutions to refer to the member.
solution.computeSubstitutions(sig, locator, subs);
subMap = sig->getSubstitutionMap(subs);
indexType = indexType.subst(subMap);
}

auto resolvedTy = foundDecl->openedType->castTo<AnyFunctionType>()
Expand All @@ -4183,9 +4188,13 @@ namespace {

auto ref = ConcreteDeclRef(cs.getASTContext(), subscript, subs);

// Coerce the indices to the type the subscript expects.
auto indexExpr = coerceToType(origComponent.getIndexExpr(),
indexType,
locator);

component = KeyPathExpr::Component
::forSubscriptWithPrebuiltIndexExpr(ref,
origComponent.getIndexExpr(),
::forSubscriptWithPrebuiltIndexExpr(ref, indexExpr,
origComponent.getSubscriptLabels(),
resolvedTy,
origComponent.getLoc(),
Expand Down
14 changes: 14 additions & 0 deletions test/SILGen/keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ func iuoKeyPaths() {
_ = \IUOProperty.iuo!.x
}

class Bass: Hashable {
static func ==(_: Bass, _: Bass) -> Bool { return false }
var hashValue: Int { return 0 }
}

class Treble: Bass { }

struct Subscripts<T> {
subscript() -> T {
get { fatalError() }
Expand All @@ -292,6 +299,10 @@ struct Subscripts<T> {
get { fatalError() }
set { fatalError() }
}
subscript(bass: Bass) -> Bass {
get { return bass }
set { }
}
}

// CHECK-LABEL: sil hidden @{{.*}}10subscripts
Expand Down Expand Up @@ -321,4 +332,7 @@ func subscripts<T: Hashable, U: Hashable>(x: T, y: U, s: String) {
_ = \Subscripts<String>.[subGeneric: y]

_ = \Subscripts<T>.[s, s].count

_ = \Subscripts<T>.[Bass()]
_ = \Subscripts<T>.[Treble()]
}
17 changes: 17 additions & 0 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,23 @@ func testStaticKeyPathComponent() {
_ = \X.Type.b // expected-error{{cannot refer to static member}}
}

class Bass: Hashable {
static func ==(_: Bass, _: Bass) -> Bool { return false }
var hashValue: Int { return 0 }
}

class Treble: Bass { }

struct BassSubscript {
subscript(_: Bass) -> Int { fatalError() }
subscript(_: @autoclosure () -> String) -> Int { fatalError() }
}

func testImplicitConversionInSubscriptIndex() {
_ = \BassSubscript.[Treble()]
_ = \BassSubscript.["hello"] // expected-error{{must be Hashable}}
}

func testSyntaxErrors() { // expected-note{{}}
_ = \. ; // expected-error{{expected member name following '.'}}
_ = \.a ;
Expand Down