Skip to content

Commit 52e3952

Browse files
committed
[AST] Add a new implicit conversion to model unsafe casts
`UnsafeCastExpr` - A special kind of conversion that performs an unsafe bitcast from one type to the other. Note that this is an unsafe operation and type-checker is allowed to use this only in a limited number of cases like: `any Sendable` -> `Any` conversions in some positions, covariant conversions of function and function result types. (cherry picked from commit 1a5f00b)
1 parent 004083d commit 52e3952

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

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)

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

0 commit comments

Comments
 (0)