Skip to content

Commit 428fa9c

Browse files
committed
CSApply: Handle unbound references to methods found via dynamic lookup
1 parent 183f7b6 commit 428fa9c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/Sema/CSApply.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -903,11 +903,6 @@ namespace {
903903
bool baseIsInstance) {
904904
ValueDecl *member = choice.getDecl();
905905

906-
// FIXME: We should finish plumbing this through for dynamic
907-
// lookup as well.
908-
if (choice.getKind() == OverloadChoiceKind::DeclViaDynamic)
909-
return false;
910-
911906
// If we're inside a selector expression, don't build the thunk.
912907
// Were not actually going to emit the member reference, just
913908
// look at the AST.
@@ -921,9 +916,11 @@ namespace {
921916
if (!baseIsInstance && member->isInstanceMember())
922917
return true;
923918

924-
// Bound optional method references are represented via
925-
// DynamicMemberRefExpr instead of a curry thunk.
926-
if (member->getAttrs().hasAttribute<OptionalAttr>())
919+
// Bound member references that are '@objc optional' or found via dynamic
920+
// lookup are always represented via DynamicMemberRefExpr instead of a
921+
// curry thunk.
922+
if (member->getAttrs().hasAttribute<OptionalAttr>() ||
923+
choice.getKind() == OverloadChoiceKind::DeclViaDynamic)
927924
return false;
928925

929926
// Figure out how many argument lists we need.
@@ -1590,8 +1587,8 @@ namespace {
15901587
}
15911588

15921589
// Handle dynamic references.
1593-
if (isDynamic || (!needsCurryThunk &&
1594-
member->getAttrs().hasAttribute<OptionalAttr>())) {
1590+
if (!needsCurryThunk &&
1591+
(isDynamic || member->getAttrs().hasAttribute<OptionalAttr>())) {
15951592
base = cs.coerceToRValue(base);
15961593
Expr *ref = new (context) DynamicMemberRefExpr(base, dotLoc, memberRef,
15971594
memberLoc);

test/SILGen/dynamic_lookup.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ func opt_to_class(_ obj: AnyObject) {
117117
// CHECK: return [[RESULT]] : $()
118118
}
119119

120+
// CHECK-LABEL: sil hidden [ossa] @$s14dynamic_lookup20opt_to_class_unboundyyF : $@convention(thin) () -> () {
121+
// CHECK: bb0:
122+
// CHECK: metatype $@thin AnyObject.Protocol
123+
// CHECK: function_ref @$[[THUNK_NAME:[_a-zA-Z0-9]+]]
124+
// CHECK: } // end sil function '$s14dynamic_lookup20opt_to_class_unboundyyF'
125+
//
126+
// CHECK: sil private [ossa] @$[[THUNK_NAME]] : $@convention(thin) (@guaranteed AnyObject) -> @owned Optional<@callee_guaranteed () -> ()> {
127+
// CHECK: bb0(%0 : @guaranteed $AnyObject):
128+
// CHECK: [[OPENED:%[0-9]+]] = open_existential_ref %0 : $AnyObject to $[[OPENED_TY:@opened\("[-A-F0-9]+"\) AnyObject]]
129+
// CHECK: [[OPENED_COPY:%[0-9]+]] = copy_value [[OPENED]]
130+
// CHECK: alloc_stack $Optional<@callee_guaranteed () -> ()>
131+
// CHECK: dynamic_method_br [[OPENED_COPY]] : $[[OPENED_TY]], #X.f!foreign, bb1, bb2
132+
// CHECK: } // end sil function '$[[THUNK_NAME]]'
133+
func opt_to_class_unbound() {
134+
let f = AnyObject.f
135+
}
136+
120137
// CHECK-LABEL: sil hidden [ossa] @$s14dynamic_lookup20forced_without_outer{{[_0-9a-zA-Z]*}}F
121138
func forced_without_outer(_ obj: AnyObject) {
122139
// CHECK: dynamic_method_br

0 commit comments

Comments
 (0)