Skip to content

Commit 341bc34

Browse files
authored
Merge pull request #60076 from DougGregor/existential-var-opening
2 parents 4e1a1b8 + 6ce2454 commit 341bc34

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5935,7 +5935,8 @@ ArgumentList *ExprRewriter::coerceCallArguments(
59355935

59365936
// If the argument is an existential type that has been opened, perform
59375937
// the open operation.
5938-
if (argType->getInOutObjectType()->isAnyExistentialType() &&
5938+
if (argType->getRValueType()->getInOutObjectType()
5939+
->isAnyExistentialType() &&
59395940
paramType->hasOpenedExistential()) {
59405941
// FIXME: Look for an opened existential and use it. We need to
59415942
// know how far out we need to go to close the existentials. Huh.

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 0 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,6 +1542,12 @@ 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();
@@ -1944,6 +1951,9 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
19441951
std::tie(argTy, opened) = cs.openExistentialType(
19451952
existentialType, cs.getConstraintLocator(loc));
19461953

1954+
if (adjustments.contains(OpenedExistentialAdjustmentFlags::LValue))
1955+
argTy = LValueType::get(argTy);
1956+
19471957
if (adjustments.contains(OpenedExistentialAdjustmentFlags::InOut))
19481958
argTy = InOutType::get(argTy);
19491959

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)