Skip to content

Commit 9a58455

Browse files
committed
[ConstraintSystem] Ignore optionals when looking for a concrete base of a static member ref on protocol metatype
1 parent a2ee7f2 commit 9a58455

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,10 @@ ConstraintSystem::getTypeOfMemberReference(
15911591
baseOpenedTy = funcTy->getResult();
15921592
else
15931593
baseOpenedTy = refTy;
1594+
1595+
// It should be possible to form optional chains which start
1596+
// from a protocol metatype.
1597+
baseOpenedTy = baseOpenedTy->lookThroughAllOptionalTypes();
15941598
}
15951599
} else if (baseObjTy->isExistentialType()) {
15961600
auto openedArchetype = OpenedArchetypeType::get(baseObjTy);

test/Constraints/static_members_on_protocol_metatype.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ struct G<T> : P {
1414
extension P {
1515
static var property: S { S() }
1616

17+
static var iuoProp: S! { S() }
18+
static var optProp: S? { S() }
19+
1720
static var fnProp: () -> S {
1821
{ S() }
1922
}
@@ -37,6 +40,10 @@ extension P {
3740

3841
_ = P.property // Ok
3942
_ = P.property.other // Ok
43+
_ = P.iuoProp // Ok
44+
_ = P.iuoProp.other // Ok
45+
_ = P.optProp // Ok
46+
_ = P.optProp?.other // Ok
4047
_ = P.fnProp // Ok
4148
_ = P.fnProp() // Ok
4249
_ = P.fnProp().other // Ok
@@ -75,6 +82,14 @@ func test<T: P>(_: T) {}
7582

7683
test(.property) // Ok, base is inferred as Style.Type
7784
test(.property.other) // Ok
85+
test(.iuoProp) // Ok
86+
test(.iuoProp.other) // Ok
87+
test(.optProp!) // Ok
88+
test(.optProp)
89+
// expected-error@-1 {{value of optional type 'S?' must be unwrapped to a value of type 'S'}}
90+
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
91+
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
92+
test(.optProp!.other) // Ok
7893
test(.fnProp()) // Ok
7994
test(.fnProp().other) // Ok
8095
test(.method()) // Ok, static method call on the metatype

0 commit comments

Comments
 (0)