Skip to content

Sema: Coerce the type of the index expression in a key path component to match the subscript decl's index type. #12301

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
merged 1 commit into from
Oct 6, 2017
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 @@ -4071,9 +4071,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 @@ -4082,9 +4087,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 @@ -417,6 +417,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