Skip to content

Enable opening of an existential var #60108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5929,7 +5929,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(

// If the argument is an existential type that has been opened, perform
// the open operation.
if (argType->getInOutObjectType()->isAnyExistentialType() &&
if (argType->getWithoutSpecifierType()->isAnyExistentialType() &&
paramType->hasOpenedExistential()) {
// FIXME: Look for an opened existential and use it. We need to
// know how far out we need to go to close the existentials. Huh.
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ namespace {
enum class OpenedExistentialAdjustmentFlags {
/// The argument should be made inout after opening.
InOut = 0x01,
LValue = 0x02,
};

using OpenedExistentialAdjustments =
Expand Down Expand Up @@ -1541,6 +1542,12 @@ shouldOpenExistentialCallArgument(

OpenedExistentialAdjustments adjustments;

// The argument may be a "var" instead of a "let".
if (auto lv = argTy->getAs<LValueType>()) {
argTy = lv->getObjectType();
adjustments |= OpenedExistentialAdjustmentFlags::LValue;
}

// If the argument is inout, strip it off and we can add it back.
if (auto inOutArg = argTy->getAs<InOutType>()) {
argTy = inOutArg->getObjectType();
Expand Down Expand Up @@ -1946,6 +1953,9 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
std::tie(argTy, opened) = cs.openExistentialType(
existentialType, cs.getConstraintLocator(loc));

if (adjustments.contains(OpenedExistentialAdjustmentFlags::LValue))
argTy = LValueType::get(argTy);

if (adjustments.contains(OpenedExistentialAdjustmentFlags::InOut))
argTy = InOutType::get(argTy);

Expand Down
4 changes: 4 additions & 0 deletions test/Constraints/opened_existentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func testSimpleExistentialOpening(p: any P, pq: any P & Q, c: any Collection) {
let pa = acceptGeneric(p)
let _: Int = pa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}

var vp = p
let vpa = acceptGeneric(vp)
let _: Int = vpa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}

let pqa = acceptGeneric(pq)
let _: Int = pqa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}

Expand Down