Skip to content

Commit aeb7456

Browse files
johnno1962DougGregor
authored andcommitted
Cater for opening var existential followup.
(cherry picked from commit d72b9a4)
1 parent 64989d1 commit aeb7456

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ namespace {
14681468
enum class OpenedExistentialAdjustmentFlags {
14691469
/// The argument should be made inout after opening.
14701470
InOut = 0x01,
1471+
LValue = 0x02,
14711472
};
14721473

14731474
using OpenedExistentialAdjustments =
@@ -1541,16 +1542,18 @@ shouldOpenExistentialCallArgument(
15411542

15421543
OpenedExistentialAdjustments adjustments;
15431544

1545+
// The argument may be a "var" instead of a "let".
1546+
if (auto lv = argTy->getAs<LValueType>()) {
1547+
argTy = lv->getObjectType();
1548+
adjustments |= OpenedExistentialAdjustmentFlags::LValue;
1549+
}
1550+
15441551
// If the argument is inout, strip it off and we can add it back.
15451552
if (auto inOutArg = argTy->getAs<InOutType>()) {
15461553
argTy = inOutArg->getObjectType();
15471554
adjustments |= OpenedExistentialAdjustmentFlags::InOut;
15481555
}
15491556

1550-
// The argument may be a "var" instead of a "let".
1551-
if (auto lv = dyn_cast<LValueType>(argTy->getCanonicalType()))
1552-
argTy = lv->getObjectType();
1553-
15541557
// The argument type needs to be an existential type or metatype thereof.
15551558
if (!argTy->isAnyExistentialType())
15561559
return None;
@@ -1950,6 +1953,9 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
19501953
std::tie(argTy, opened) = cs.openExistentialType(
19511954
existentialType, cs.getConstraintLocator(loc));
19521955

1956+
if (adjustments.contains(OpenedExistentialAdjustmentFlags::LValue))
1957+
argTy = LValueType::get(argTy);
1958+
19531959
if (adjustments.contains(OpenedExistentialAdjustmentFlags::InOut))
19541960
argTy = InOutType::get(argTy);
19551961

test/Constraints/opened_existentials.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func testSimpleExistentialOpening(p: any P, pq: any P & Q, c: any Collection) {
2525
let pa = acceptGeneric(p)
2626
let _: Int = pa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
2727

28+
var vp = p
29+
let vpa = acceptGeneric(vp)
30+
let _: Int = vpa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
31+
2832
let pqa = acceptGeneric(pq)
2933
let _: Int = pqa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
3034

0 commit comments

Comments
 (0)