Skip to content

Commit 636525e

Browse files
authored
Merge pull request swiftlang#78171 from xedin/rdar-140300022
[TypeChecker/SILGen] Allow `any Sendable` to match `Any` while matching generic arguments
2 parents 51cce0d + a2f711c commit 636525e

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
@@ -2798,6 +2798,9 @@ ERROR(types_not_inherited_decl,none,
27982798
ERROR(types_not_inherited_in_decl_ref,none,
27992799
"referencing %kind0 on %1 requires that %2 inherit from %3",
28002800
(const ValueDecl *, Type, Type, Type))
2801+
ERROR(cannot_reference_conditional_member_on_base_multiple_mismatches,none,
2802+
"cannot reference %kind0 on %1",
2803+
(const ValueDecl *, Type))
28012804
NOTE(where_requirement_failure_one_subst,none,
28022805
"where %0 = %1", (Type, Type))
28032806
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
@@ -3600,6 +3600,24 @@ class ActorIsolationErasureExpr : public ImplicitConversionExpr {
36003600
}
36013601
};
36023602

3603+
/// UnsafeCastExpr - A special kind of conversion that performs an unsafe
3604+
/// bitcast from one type to the other.
3605+
///
3606+
/// Note that this is an unsafe operation and type-checker is allowed to
3607+
/// use this only in a limited number of cases like: `any Sendable` -> `Any`
3608+
/// conversions in some positions, covariant conversions of function and
3609+
/// function result types.
3610+
class UnsafeCastExpr : public ImplicitConversionExpr {
3611+
public:
3612+
UnsafeCastExpr(Expr *subExpr, Type type)
3613+
: ImplicitConversionExpr(ExprKind::UnsafeCast, subExpr, type) {
3614+
}
3615+
3616+
static bool classof(const Expr *E) {
3617+
return E->getKind() == ExprKind::UnsafeCast;
3618+
}
3619+
};
3620+
36033621
/// Extracts the isolation of a dynamically isolated function value.
36043622
class ExtractFunctionIsolationExpr : public Expr {
36053623
/// 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
@@ -675,6 +675,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
675675
/// Is this an existential containing only marker protocols?
676676
bool isMarkerExistential();
677677

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

680683
/// 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
@@ -2834,6 +2834,12 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
28342834
printFoot();
28352835
}
28362836

2837+
void visitUnsafeCastExpr(UnsafeCastExpr *E, StringRef label) {
2838+
printCommon(E, "unsafe_cast_expr", label);
2839+
printRec(E->getSubExpr());
2840+
printFoot();
2841+
}
2842+
28372843
void visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
28382844
StringRef label) {
28392845
printCommon(E, "extract_function_isolation", label);

lib/AST/ASTPrinter.cpp

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

5346+
void PrintAST::visitUnsafeCastExpr(UnsafeCastExpr *expr) {
5347+
}
5348+
53465349
void PrintAST::visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *expr) {
53475350
visit(expr->getFunctionExpr());
53485351
Printer << ".isolation";

lib/AST/Expr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
444444
PASS_THROUGH_REFERENCE(UnderlyingToOpaque, getSubExpr);
445445
PASS_THROUGH_REFERENCE(Unreachable, getSubExpr);
446446
PASS_THROUGH_REFERENCE(ActorIsolationErasure, getSubExpr);
447+
PASS_THROUGH_REFERENCE(UnsafeCast, getSubExpr);
447448
NO_REFERENCE(Coerce);
448449
NO_REFERENCE(ForcedCheckedCast);
449450
NO_REFERENCE(ConditionalCheckedCast);
@@ -813,6 +814,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
813814
case ExprKind::UnderlyingToOpaque:
814815
case ExprKind::Unreachable:
815816
case ExprKind::ActorIsolationErasure:
817+
case ExprKind::UnsafeCast:
816818
case ExprKind::TypeValue:
817819
// Implicit conversion nodes have no syntax of their own; defer to the
818820
// subexpression.
@@ -1043,6 +1045,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
10431045
case ExprKind::CurrentContextIsolation:
10441046
case ExprKind::ActorIsolationErasure:
10451047
case ExprKind::ExtractFunctionIsolation:
1048+
case ExprKind::UnsafeCast:
10461049
return false;
10471050
}
10481051

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)