Skip to content

Commit 0c44bbd

Browse files
authored
Merge pull request #11308 from slavapestov/unbound-method-reference-wrong-type
Fix for unbound method references getting the wrong type
2 parents efb3c50 + d65ff34 commit 0c44bbd

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,16 +1279,11 @@ ConstraintSystem::getTypeOfMemberReference(
12791279
// For a static member referenced through a metatype or an instance
12801280
// member referenced through an instance, strip off the 'self'.
12811281
type = openedFnType->getResult();
1282-
} else if (isDynamicResult && isa<AbstractFunctionDecl>(value)) {
1283-
// For a dynamic result referring to an instance function through
1284-
// an object of metatype type, replace the 'Self' parameter with
1285-
// a AnyObject member.
1286-
auto anyObjectTy = TC.Context.getAnyObjectType();
1287-
type = openedFnType->replaceSelfParameterType(anyObjectTy);
12881282
} else {
12891283
// For an unbound instance method reference, replace the 'Self'
12901284
// parameter with the base type.
1291-
type = openedFnType->replaceSelfParameterType(baseObjTy);
1285+
openedType = openedFnType->replaceSelfParameterType(baseObjTy);
1286+
type = openedType;
12921287
}
12931288

12941289
// When accessing protocol members with an existential base, replace

test/SILGen/decls.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,25 @@ struct StructWithStaticVar {
167167
init() {
168168
}
169169
}
170+
171+
// Make sure unbound method references on class hierarchies are
172+
// properly represented in the AST
173+
174+
class Base {
175+
func method1() -> Self { return self }
176+
func method2() -> Self { return self }
177+
}
178+
179+
class Derived : Base {
180+
override func method2() -> Self { return self }
181+
}
182+
183+
func generic<T>(arg: T) { }
184+
185+
func unboundMethodReferences() {
186+
generic(arg: Derived.method1)
187+
generic(arg: Derived.method2)
188+
189+
_ = type(of: Derived.method1)
190+
_ = type(of: Derived.method2)
191+
}

0 commit comments

Comments
 (0)