Skip to content

Sema: Subscript called with opened existential cannot produce lvalue if result is type-erased #77892

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 1 commit into from
Dec 18, 2024
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
10 changes: 8 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,14 @@ namespace {
auto *env = record.Archetype->getGenericEnvironment();

if (resultTy->hasLocalArchetypeFromEnvironment(env)) {
Type erasedTy = typeEraseOpenedArchetypesFromEnvironment(
resultTy, env);
Type erasedTy = typeEraseOpenedArchetypesFromEnvironment(resultTy, env);
ASSERT(erasedTy.getPointer() != resultTy.getPointer());

// We currently cannot keep lvalueness if the object type changed.
if (auto *lvalueTy = dyn_cast<LValueType>(erasedTy.getPointer())) {
erasedTy = lvalueTy->getObjectType();
}

auto range = result->getSourceRange();
result = coerceToType(result, erasedTy, locator);
// FIXME: Implement missing tuple-to-tuple conversion
Expand Down
14 changes: 12 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13270,9 +13270,19 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
Type result2 = func2->getResult();
if (result2->hasTypeVariable() && !openedExistentials.empty()) {
for (const auto &opened : openedExistentials) {
result2 = typeEraseOpenedExistentialReference(
result2, opened.second->getExistentialType(), opened.first,
auto originalTy = result2;
if (auto *lvalueTy = dyn_cast<LValueType>(originalTy.getPointer())) {
originalTy = lvalueTy->getObjectType();
}

const auto erasedTy = typeEraseOpenedExistentialReference(
originalTy, opened.second->getExistentialType(), opened.first,
TypePosition::Covariant);

if (originalTy.getPointer() != erasedTy.getPointer()) {
// We currently cannot keep lvalueness if the object type changed.
result2 = erasedTy;
}
}
}

Expand Down
14 changes: 0 additions & 14 deletions lib/Sema/OpenedExistentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,20 +800,6 @@ static Type typeEraseExistentialSelfReferences(
return parameterized->getBaseType();
}
}
/*
if (auto lvalue = dyn_cast<LValueType>(t)) {
auto objTy = lvalue->getObjectType();
auto erasedTy =
typeEraseExistentialSelfReferences(
objTy, currPos,
containsFn, predicateFn, eraseFn);

if (erasedTy.getPointer() == objTy.getPointer())
return Type(lvalue);

return erasedTy;
}
*/

if (!predicateFn(t)) {
// Recurse.
Expand Down
32 changes: 15 additions & 17 deletions test/Sema/open_existential_lvalue.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// RUN: %target-typecheck-verify-swift

// rdar://121214563
// https://github.com/swiftlang/swift/issues/60619
// REQUIRES: rdar121214563

protocol Q {}
do {
protocol P {
associatedtype A
}
struct S {
subscript<T: P>(_: T) -> T {
get {} set {}
}
}

func takesAny(x: any Q) {}
func takesRValue(x: any Q) {}
func takesInOut(x: inout any Q) {}
func takesInOut(_: inout any P) {}

struct S {
subscript<T: Q>(_ ct: T.Type) -> T {
get { fatalError() }
set { fatalError() }
}
}
var s: S
let p: any P

func f(s: inout S, t: any Q.Type) -> (any Q) {
takesAny(x: s[t])
takesRValue(x: s[t])
takesInOut(x: &s[t]) // expected-error {{cannot pass immutable value as inout argument: 's' is immutable}}
let _ = s[p]
s[p] = p // expected-error {{cannot assign through subscript: 's' is immutable}}
takesInOut(&s[p]) // expected-error {{cannot pass immutable value as inout argument: 's' is immutable}}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-typecheck-verify-swift -target %target-swift-5.9-abi-triple

// rdar://121214563
// https://github.com/apple/swift/issues/62219

// nonmutating get mutating set
Expand Down