Skip to content

[CSApply] Allow marker existential to superclass conversions #75628

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
Aug 2, 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
14 changes: 14 additions & 0 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7806,6 +7806,20 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
llvm_unreachable("BuiltinTupleType should not show up here");
}

// Allow existential-to-supertype conversion if all protocol
// bounds are marker protocols. Normally this requires a
// conversion restriction but there are situations related
// to `@preconcurrency` where the `& Sendable` would be stripped
// transparently to the solver.
if (auto *existential = fromType->getAs<ExistentialType>()) {
if (auto *PCT = existential->getConstraintType()
->getAs<ProtocolCompositionType>()) {
if (PCT->withoutMarkerProtocols()->isEqual(toType)) {
return coerceSuperclass(expr, toType);
}
}
}

// Unresolved types come up in diagnostics for lvalue and inout types.
if (fromType->hasUnresolvedType() || toType->hasUnresolvedType())
return cs.cacheType(new (ctx) UnresolvedTypeConversionExpr(expr, toType));
Expand Down
30 changes: 30 additions & 0 deletions test/Concurrency/predates_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,33 @@ do {
}
}
}

// rdar://132700409 - coercion PartialKeyPath & Sendable -> PartialKeyPath crashes in CSApply
do {
struct Test {
enum KeyPath {
static var member: PartialKeyPath<Test> {
fatalError()
}
}
}

struct KeyPathComparator<Compared> {
@preconcurrency public let keyPath: any PartialKeyPath<Compared> & Sendable

func testDirect() {
switch keyPath { // Ok
case Test.KeyPath.member: break // Ok
default: break
}
}

func testErasure() {
let kp: PartialKeyPath<Compared> = keyPath
switch kp { // Ok
case Test.KeyPath.member: break // Ok
default: break
}
}
}
}