Skip to content

Commit 91c6425

Browse files
authored
Merge pull request swiftlang#78272 from xedin/rdar-140300022-6.1
[6.1][TypeChecker/SILGen] Allow `any Sendable` to match `Any` while matching generic arguments
2 parents 99a9d61 + fde6f50 commit 91c6425

20 files changed

+725
-20
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,9 @@ ERROR(types_not_inherited_decl,none,
27892789
ERROR(types_not_inherited_in_decl_ref,none,
27902790
"referencing %kind0 on %1 requires that %2 inherit from %3",
27912791
(const ValueDecl *, Type, Type, Type))
2792+
ERROR(cannot_reference_conditional_member_on_base_multiple_mismatches,none,
2793+
"cannot reference %kind0 on %1",
2794+
(const ValueDecl *, Type))
27922795
NOTE(where_requirement_failure_one_subst,none,
27932796
"where %0 = %1", (Type, Type))
27942797
NOTE(where_requirement_failure_both_subst,none,

include/swift/AST/Expr.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,6 +3615,24 @@ class ActorIsolationErasureExpr : public ImplicitConversionExpr {
36153615
}
36163616
};
36173617

3618+
/// UnsafeCastExpr - A special kind of conversion that performs an unsafe
3619+
/// bitcast from one type to the other.
3620+
///
3621+
/// Note that this is an unsafe operation and type-checker is allowed to
3622+
/// use this only in a limited number of cases like: `any Sendable` -> `Any`
3623+
/// conversions in some positions, covariant conversions of function and
3624+
/// function result types.
3625+
class UnsafeCastExpr : public ImplicitConversionExpr {
3626+
public:
3627+
UnsafeCastExpr(Expr *subExpr, Type type)
3628+
: ImplicitConversionExpr(ExprKind::UnsafeCast, subExpr, type) {
3629+
}
3630+
3631+
static bool classof(const Expr *E) {
3632+
return E->getKind() == ExprKind::UnsafeCast;
3633+
}
3634+
};
3635+
36183636
/// Extracts the isolation of a dynamically isolated function value.
36193637
class ExtractFunctionIsolationExpr : public Expr {
36203638
/// The function value expression from which to extract the

include/swift/AST/ExprNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ ABSTRACT_EXPR(ImplicitConversion, Expr)
191191
EXPR(LinearFunctionExtractOriginal, ImplicitConversionExpr)
192192
EXPR(LinearToDifferentiableFunction, ImplicitConversionExpr)
193193
EXPR(ActorIsolationErasure, ImplicitConversionExpr)
194-
EXPR_RANGE(ImplicitConversion, Load, ActorIsolationErasure)
194+
EXPR(UnsafeCast, ImplicitConversionExpr)
195+
EXPR_RANGE(ImplicitConversion, Load, UnsafeCast)
195196
ABSTRACT_EXPR(ExplicitCast, Expr)
196197
ABSTRACT_EXPR(CheckedCast, ExplicitCastExpr)
197198
EXPR(ForcedCheckedCast, CheckedCastExpr)

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
674674
/// Is this an existential containing only marker protocols?
675675
bool isMarkerExistential();
676676

677+
/// Is this `any Sendable` type?
678+
bool isSendableExistential();
679+
677680
bool isPlaceholder();
678681

679682
/// Returns true if this contextual type does not satisfy a conformance to

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,12 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
27272727
printFoot();
27282728
}
27292729

2730+
void visitUnsafeCastExpr(UnsafeCastExpr *E, StringRef label) {
2731+
printCommon(E, "unsafe_cast_expr", label);
2732+
printRec(E->getSubExpr());
2733+
printFoot();
2734+
}
2735+
27302736
void visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
27312737
StringRef label) {
27322738
printCommon(E, "extract_function_isolation", label);

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5346,6 +5346,9 @@ void PrintAST::visitLinearToDifferentiableFunctionExpr(swift::LinearToDifferenti
53465346
void PrintAST::visitActorIsolationErasureExpr(ActorIsolationErasureExpr *expr) {
53475347
}
53485348

5349+
void PrintAST::visitUnsafeCastExpr(UnsafeCastExpr *expr) {
5350+
}
5351+
53495352
void PrintAST::visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *expr) {
53505353
visit(expr->getFunctionExpr());
53515354
Printer << ".isolation";

lib/AST/Expr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
459459
PASS_THROUGH_REFERENCE(UnderlyingToOpaque, getSubExpr);
460460
PASS_THROUGH_REFERENCE(Unreachable, getSubExpr);
461461
PASS_THROUGH_REFERENCE(ActorIsolationErasure, getSubExpr);
462+
PASS_THROUGH_REFERENCE(UnsafeCast, getSubExpr);
462463
NO_REFERENCE(Coerce);
463464
NO_REFERENCE(ForcedCheckedCast);
464465
NO_REFERENCE(ConditionalCheckedCast);
@@ -828,6 +829,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
828829
case ExprKind::UnderlyingToOpaque:
829830
case ExprKind::Unreachable:
830831
case ExprKind::ActorIsolationErasure:
832+
case ExprKind::UnsafeCast:
831833
case ExprKind::TypeValue:
832834
// Implicit conversion nodes have no syntax of their own; defer to the
833835
// subexpression.
@@ -1058,6 +1060,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
10581060
case ExprKind::CurrentContextIsolation:
10591061
case ExprKind::ActorIsolationErasure:
10601062
case ExprKind::ExtractFunctionIsolation:
1063+
case ExprKind::UnsafeCast:
10611064
return false;
10621065
}
10631066

lib/AST/Type.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ bool TypeBase::isMarkerExistential() {
163163
return true;
164164
}
165165

166+
bool TypeBase::isSendableExistential() {
167+
Type constraint = this;
168+
if (auto existential = constraint->getAs<ExistentialType>())
169+
constraint = existential->getConstraintType();
170+
171+
if (!constraint->isConstraintType())
172+
return false;
173+
174+
return constraint->getKnownProtocol() == KnownProtocolKind::Sendable;
175+
}
176+
166177
bool TypeBase::isPlaceholder() {
167178
return is<PlaceholderType>();
168179
}

lib/SILGen/SILGenBuilder.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,31 @@ ManagedValue SILGenBuilder::createUncheckedBitCast(SILLocation loc,
757757
return cloner.clone(cast);
758758
}
759759

760+
ManagedValue SILGenBuilder::createUncheckedForwardingCast(SILLocation loc,
761+
ManagedValue value,
762+
SILType type) {
763+
CleanupCloner cloner(*this, value);
764+
SILValue cast = createUncheckedForwardingCast(loc, value.getValue(), type);
765+
766+
// Currently createUncheckedBitCast only produces these
767+
// instructions. We assert here to make sure if this changes, this code is
768+
// updated.
769+
assert((isa<UncheckedTrivialBitCastInst>(cast) ||
770+
isa<UncheckedRefCastInst>(cast) ||
771+
isa<UncheckedValueCastInst>(cast) ||
772+
isa<ConvertFunctionInst>(cast)) &&
773+
"SILGenBuilder is out of sync with SILBuilder.");
774+
775+
// If we have a trivial inst, just return early.
776+
if (isa<UncheckedTrivialBitCastInst>(cast))
777+
return ManagedValue::forObjectRValueWithoutOwnership(cast);
778+
779+
// Otherwise, we forward the cleanup of the input value and place the cleanup
780+
// on the cast value since unchecked_ref_cast is "forwarding".
781+
value.forward(SGF);
782+
return cloner.clone(cast);
783+
}
784+
760785
ManagedValue SILGenBuilder::createOpenExistentialRef(SILLocation loc,
761786
ManagedValue original,
762787
SILType type) {

lib/SILGen/SILGenBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ class SILGenBuilder : public SILBuilder {
340340
ManagedValue createUncheckedBitCast(SILLocation loc, ManagedValue original,
341341
SILType type);
342342

343+
using SILBuilder::createUncheckedForwardingCast;
344+
ManagedValue createUncheckedForwardingCast(SILLocation loc,
345+
ManagedValue original,
346+
SILType type);
347+
343348
using SILBuilder::createOpenExistentialRef;
344349
ManagedValue createOpenExistentialRef(SILLocation loc, ManagedValue arg,
345350
SILType openedType);

0 commit comments

Comments
 (0)