Skip to content

[ConstraintSystem] Look through l-value while checking whether dynami… #30040

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 2 commits into from
Feb 26, 2020
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: 13 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5546,7 +5546,15 @@ static bool isSelfRecursiveKeyPathDynamicMemberLookup(
return baseDecl == keyPathRootDecl;
}

if (baseTy->isEqual(keyPathRootTy))
// Previous base type could be r-value because that could be
// a base type of subscript "as written" for which we attempt
// a dynamic member lookup.
auto baseTy1 = baseTy->getRValueType();
// Root type of key path is always wrapped in an l-value
// before lookup is performed, so we need to unwrap that.
auto baseTy2 = keyPathRootTy->getRValueType();

if (baseTy1->isEqual(baseTy2))
return true;
}

Expand Down Expand Up @@ -5848,6 +5856,10 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
using KPDynamicMemberElt = LocatorPathElt::KeyPathDynamicMember;
if (auto kpElt = memberLocator->getLastElementAs<KPDynamicMemberElt>()) {
auto *keyPath = kpElt->getKeyPathDecl();
if (isSelfRecursiveKeyPathDynamicMemberLookup(*this, baseTy,
memberLocator))
return;

if (auto *storage = dyn_cast<AbstractStorageDecl>(decl)) {
// If this is an attempt to access read-only member via
// writable key path, let's fail this choice early.
Expand Down
25 changes: 25 additions & 0 deletions test/Constraints/keypath_dynamic_member_lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,28 @@ func testDynamicMemberWithDefault(_ x: SR_11933) {
// CHECK: [[OUTER_KP:%[0-9]+]] = keypath $KeyPath<SR_11933, Int>, (root $SR_11933; gettable_property $Int, id @$s29keypath_dynamic_member_lookup8SR_11933V0B6MemberSis7KeyPathCyAA21HasDefaultedSubscriptVSiG_tcig : $@convention(method) (@guaranteed KeyPath<HasDefaultedSubscript, Int>, SR_11933) -> Int, getter @$s29keypath_dynamic_member_lookup8SR_11933V0B6MemberSis7KeyPathCyAA21HasDefaultedSubscriptVSiG_tcipACTK : $@convention(thin) (@in_guaranteed SR_11933, UnsafeRawPointer) -> @out Int, indices [%$0 : $KeyPath<HasDefaultedSubscript, Int> : $KeyPath<HasDefaultedSubscript, Int>], indices_equals @$ss7KeyPathCy29keypath_dynamic_member_lookup21HasDefaultedSubscriptVSiGTH : $@convention(thin) (UnsafeRawPointer, UnsafeRawPointer) -> Bool, indices_hash @$ss7KeyPathCy29keypath_dynamic_member_lookup21HasDefaultedSubscriptVSiGTh : $@convention(thin) (UnsafeRawPointer) -> Int) ([[INNER_KP]])
_ = \SR_11933.[]
}

// SR-11743 - KeyPath Dynamic Member Lookup crash
@dynamicMemberLookup
protocol SR_11743_P {
subscript(dynamicMember member: KeyPath<Self, Any>) -> Any { get }
}

extension SR_11743_P {
subscript(dynamicMember member: KeyPath<Self, Any>) -> Any {
self[keyPath: member] // Ok
// CHECK: function_ref @swift_getAtKeyPath
// CHECK-NEXT: apply %{{.*}}<Self, Any>({{.*}})
}
}

@dynamicMemberLookup
struct SR_11743_Struct {
let value: Int

subscript<T>(dynamicMember member: KeyPath<Self, T>) -> T {
return self[keyPath: member]
// CHECK: function_ref @swift_getAtKeyPath
// CHECK-NEXT: apply %{{.*}}<SR_11743_Struct, T>({{.*}})
}
}