Skip to content

Commit d65ff34

Browse files
committed
Sema: Fix type mixup with unbound method references
When applying a solution containing an unbound reference to an instance method of a class, the type of the new expression did not match the type in the constraint system. Usually this was papered over because CSApply is sprinkled with coerceToType() calls, but we would crash when passing an unbound reference to a generic function, or type(of:).
1 parent db3c74c commit d65ff34

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,8 @@ ConstraintSystem::getTypeOfMemberReference(
12821282
} else {
12831283
// For an unbound instance method reference, replace the 'Self'
12841284
// parameter with the base type.
1285-
type = openedFnType->replaceSelfParameterType(baseObjTy);
1285+
openedType = openedFnType->replaceSelfParameterType(baseObjTy);
1286+
type = openedType;
12861287
}
12871288

12881289
// 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)