Skip to content

Conservative Dynamic Casts to/from Parameterized Protocols #41926

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
Mar 21, 2022
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
17 changes: 17 additions & 0 deletions lib/SIL/Utils/DynamicCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ static bool canClassOrSuperclassesHaveUnknownSubclasses(ClassDecl *CD,
return false;
}

static CanType unwrapExistential(CanType e) {
assert(e.isExistentialType());

if (auto et = dyn_cast<ExistentialType>(e))
return et.getConstraintType();

return e;
}

/// Try to classify a conversion from non-existential type
/// into an existential type by performing a static check
/// of protocol conformances if it is possible.
Expand All @@ -94,6 +103,14 @@ classifyDynamicCastToProtocol(ModuleDecl *M, CanType source, CanType target,
if (!TargetProtocol)
return DynamicCastFeasibility::MaySucceed;

// If the target is a parameterized protocol type, conformsToProtocol
// is insufficient to prove the feasibility of the cast as it does not
// check the additional requirements.
// FIXME: This is a weak predicate that doesn't take into account
// class compositions - since any C & P<T> doesn't work yet anyways.
if (isa<ParameterizedProtocolType>(unwrapExistential(target)))
return DynamicCastFeasibility::MaySucceed;

// If conformsToProtocol returns a valid conformance, then all requirements
// were proven by the type checker.
if (M->conformsToProtocol(source, TargetProtocol))
Expand Down
81 changes: 81 additions & 0 deletions test/SILOptimizer/cast_folding_parameterized_protocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// RUN: %target-swift-frontend %s -emit-sil -enable-parameterized-protocol-types -O -o - | %FileCheck %s

public protocol P<T> {}
public protocol Q: P where T == Int {}
public struct X: Q {
public typealias T = Int
}
public struct Y<T>: P {}
extension Y: Q where T == Int {}

@_optimize(none)
func use<T>(_ t: T) {}

// CHECK-LABEL: sil @$s35cast_folding_parameterized_protocol23concrete_to_existentialyyAA1XV_AA1YVyxGAFySiGtlF : $@convention(thin) <T> (X, Y<T>, Y<Int>) -> ()
public func concrete_to_existential<T>(_ x: X, _ yt: Y<T>, _ yi: Y<Int>) {
// CHECK:{{%.*}} = init_existential_addr %6 : $*P, $X
use(x as! any P)
// CHECK: unconditional_checked_cast_addr X in {{%.*}} : $*X to P<T> in {{%.*}} : $*P<T>
use(x as! any P<T>)
// CHECK: unconditional_checked_cast_addr X in {{%.*}} : $*X to P<Int> in {{%.*}} : $*P<Int>
use(x as! any P<Int>)
// CHECK: unconditional_checked_cast_addr X in {{%.*}} : $*X to P<String> in {{%.*}} : $*P<String>
use(x as! any P<String>)
// CHECK: {{%.*}} = init_existential_addr {{%.*}} : $*Q, $X
use(x as! any Q)

// CHECK: {{%.*}} = init_existential_addr {{%.*}} : $*P, $Y<T>
use(yt as! any P)
// CHECK: unconditional_checked_cast_addr Y<T> in {{%.*}} : $*Y<T> to P<T> in {{%.*}} : $*P<T>
use(yt as! any P<T>)
// CHECK: unconditional_checked_cast_addr Y<T> in {{%.*}} : $*Y<T> to P<Int> in {{%.*}} : $*P<Int>
use(yt as! any P<Int>)
// CHECK: unconditional_checked_cast_addr Y<T> in {{%.*}} : $*Y<T> to P<String> in {{%.*}} : $*P<String>
use(yt as! any P<String>)
// CHECK: unconditional_checked_cast_addr Y<T> in {{%.*}} : $*Y<T> to Q in {{%.*}} : $*Q
use(yt as! any Q)

// CHECK: {{%.*}} = init_existential_addr {{%.*}} : $*P, $Y<Int>
use(yi as! any P)
// CHECK: unconditional_checked_cast_addr Y<Int> in {{%.*}} : $*Y<Int> to P<T> in {{%.*}} : $*P<T>
use(yi as! any P<T>)
// CHECK: unconditional_checked_cast_addr Y<Int> in {{%.*}} : $*Y<Int> to P<Int> in {{%.*}} : $*P<Int>
use(yi as! any P<Int>)
// CHECK: unconditional_checked_cast_addr Y<Int> in {{%.*}} : $*Y<Int> to P<String> in {{%.*}} : $*P<String>
use(yi as! any P<String>)
// CHECK: {{%.*}} = init_existential_addr {{%.*}} : $*Q, $Y<Int>
use(yi as! any Q)
}

// CHECK-LABEL: sil @$s35cast_folding_parameterized_protocol23existential_to_concreteyyxm_AA1P_pyxXPtlF : $@convention(thin) <T> (@thick T.Type, @in_guaranteed P<T>) -> ()
public func existential_to_concrete<T>(_: T.Type, _ p: any P<T>) {
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to X in {{%.*}} : $*X
_ = p as! X
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to Y<T> in {{%.*}} : $*Y<T>
_ = p as! Y<T>
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to Y<Int> in {{%.*}} : $*Y<Int>
_ = p as! Y<Int>
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to Y<String> in {{%.*}} : $*Y<String>
_ = p as! Y<String>
}

// CHECK-LABEL: sil @$s35cast_folding_parameterized_protocol015existential_to_E0yyAA1P_pyxXP_AA1Q_ptlF : $@convention(thin) <T> (@in_guaranteed P<T>, @in_guaranteed Q) -> ()
public func existential_to_existential<T>(_ p: any P<T>, _ q: any Q) {
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to Q in {{%.*}} : $*Q
_ = p as! any Q
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to P in {{%.*}} : $*P
_ = p as! any P
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to P<Int> in {{%.*}} : $*P<Int>
_ = p as! any P<Int>
// CHECK: unconditional_checked_cast_addr P<T> in {{%.*}} : $*P<T> to P<String> in {{%.*}} : $*P<String>
_ = p as! any P<String>

// CHECK: unconditional_checked_cast_addr Q in {{%.*}} : $*Q to P in {{%.*}} : $*P
_ = q as! any P
// CHECK: unconditional_checked_cast_addr Q in {{%.*}} : $*Q to P<T> in {{%.*}} : $*P<T>
_ = q as! any P<T>
// CHECK: unconditional_checked_cast_addr Q in {{%.*}} : $*Q to P<Int> in {{%.*}} : $*P<Int>
_ = q as! any P<Int>
// CHECK: unconditional_checked_cast_addr Q in {{%.*}} : $*Q to P<String> in {{%.*}} : $*P<String>
_ = q as! any P<String>
}