Skip to content
Draft
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
1 change: 1 addition & 0 deletions Runtimes/Core/Concurrency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_library(swift_Concurrency
"${PROJECT_SOURCE_DIR}/CompatibilityOverride/CompatibilityOverride.cpp"
"./linker-support/magic-symbols-for-install-name.c"
Actor.swift
Actor+Send.swift
AsyncCompactMapSequence.swift
AsyncDropFirstSequence.swift
AsyncDropWhileSequence.swift
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,11 @@ BridgedUnsafeExpr BridgedUnsafeExpr_createParsed(BridgedASTContext cContext,
swift::SourceLoc unsafeLoc,
BridgedExpr cSubExpr);

SWIFT_NAME("BridgedSendExpr.createParsed(_:sendLoc:subExpr:)")
BridgedSendExpr BridgedSendExpr_createParsed(BridgedASTContext cContext,
swift::SourceLoc sendLoc,
BridgedExpr cSubExpr);

SWIFT_NAME("BridgedInOutExpr.createParsed(_:loc:subExpr:)")
BridgedInOutExpr BridgedInOutExpr_createParsed(BridgedASTContext cContext,
swift::SourceLoc loc,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ ERROR(expected_expr_after_move, none,
"expected expression after 'consume'", ())
ERROR(expected_expr_after_copy, none,
"expected expression after 'copy'", ())
ERROR(expected_expr_after_send, none,
"expected function call after 'send'", ())
ERROR(expected_expr_after_borrow, none,
"expected expression after '_borrow'", ())
ERROR(expected_expr_after_each, none,
Expand Down
41 changes: 41 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -6810,6 +6810,47 @@ ERROR(distributed_witness_typed_throws_requirement,none,
"method can throw any error",
(const ValueDecl *, const ValueDecl *, Type))

ERROR(send_requires_call,none,
"'send' can only be applied to a function call", ())
ERROR(send_requires_actor_isolated_callee,none,
"'send' can only be applied to calls of actor isolated methods", ())
ERROR(send_property_access,none,
"'send' cannot be applied to property access", ())
ERROR(send_property_assignment,none,
"'send' cannot be applied to property assignment", ())
ERROR(send_subscript_access,none,
"'send' cannot be applied to subscript access", ())
ERROR(send_call_discards_result,none,
"'send' cannot discard the result of a call%select{| to %1}0",
(bool, const ValueDecl *))
ERROR(send_combined_with_await,none,
"%select{'await' cannot be combined with 'send'|"
"'send' cannot be combined with 'await'}0; an operation cannot be both "
"awaited on and sent without waiting for its result", (bool))
ERROR(send_call_can_throw,none,
"'send' cannot discard error thrown by call%select{| to %1}0",
(bool, const ValueDecl *))
ERROR(send_call_inout_argument,none,
"'send' cannot be applied to a call with 'inout' arguments", ())
ERROR(send_call_nonescapable_argument,none,
"'send' cannot be applied to a call with non-escapable argument of "
"type %0", (Type))
ERROR(send_call_pack_expansion_argument,none,
"'send' cannot be applied to a call with a pack expansion argument", ())
ERROR(send_call_mutating_method,none,
"'send' cannot be applied to a call of a mutating method", ())
ERROR(send_call_unsupported,none,
"'send' cannot be applied to this call; its receiver and arguments "
"could not be evaluated at the call site", ())
ERROR(send_requires_newer_concurrency_library,none,
"'send' requires newer _Concurrency library", ())
ERROR(send_only_version_newer,none,
"'send' is only available in %0 %1 or newer",
(AvailabilityDomain, AvailabilityRange))
ERROR(send_distributed_not_supported,none,
"'send' of %kind0 is not supported yet",
(const ValueDecl *))

ERROR(concurrency_lib_missing,none,
"missing '%0' declaration, probably because the '_Concurrency' "
"module was not imported", (StringRef))
Expand Down
52 changes: 50 additions & 2 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
Kind : 2
);

SWIFT_INLINE_BITFIELD(ClosureExpr, AbstractClosureExpr, 1+1+1+1+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(ClosureExpr, AbstractClosureExpr, 1+1+1+1+1+1+1+1+1+1+1,
/// True if closure parameters were synthesized from anonymous closure
/// variables.
HasAnonymousClosureVars : 1,
Expand Down Expand Up @@ -311,7 +311,11 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
/// parameter of a `nonisolated(nonsending)` call and doesn't leave isolation of
/// the parent context. It's easier to work with statically known isolation than
/// make closure dynamically isolated via assuming `nonisolated(nonsending)`.
BehavesLikeNonisolatedNonsending: 1
BehavesLikeNonisolatedNonsending: 1,

/// True if this is the operation closure which pre-checking synthesizes
/// for the operand of a 'send'
IsOnewaySendOperation : 1
);

SWIFT_INLINE_BITFIELD_FULL(BindOptionalExpr, Expr, 16,
Expand Down Expand Up @@ -2213,6 +2217,41 @@ class AwaitExpr final : public IdentityExpr {
}
};

/// SendExpr - A 'send' surrounding a Void-returning, non-throwing call,
/// marking that the call should be dispatched fire-and-forget rather than
/// awaited
///
/// Pre-checking wraps the call in an implicit '{ try await <call> }' operation
/// closure of type '() async throws -> Void', which SILGen runs in a new
/// discarding task. CSApply replaces the closure with a synchronous
/// expression for a synchronous callee which is isolated to an actor, which
/// enqueues the call
class SendExpr final : public Expr {
Expr *SubExpr;
SourceLoc SendLoc;

public:
SendExpr(SourceLoc sendLoc, Expr *sub, bool implicit = false)
: Expr(ExprKind::Send, implicit), SubExpr(sub), SendLoc(sendLoc) {}

/// Create an implicit 'send', which is always of type 'Void'
static SendExpr *createImplicit(ASTContext &ctx, SourceLoc sendLoc,
Expr *sub);

SourceLoc getLoc() const { return SendLoc; }
SourceLoc getSendLoc() const { return SendLoc; }

Expr *getSubExpr() const { return SubExpr; }
void setSubExpr(Expr *E) { SubExpr = E; }

SourceLoc getStartLoc() const { return SendLoc; }
SourceLoc getEndLoc() const { return getSubExpr()->getEndLoc(); }

static bool classof(const Expr *e) {
return e->getKind() == ExprKind::Send;
}
};

/// UnsafeExpr - An 'unsafe' surrounding an expression, marking that the
/// expression contains uses of unsafe declarations.
///
Expand Down Expand Up @@ -4350,6 +4389,7 @@ class ClosureExpr : public AbstractClosureExpr {
Bits.ClosureExpr.RequiresDynamicIsolationChecking = false;
Bits.ClosureExpr.IsMacroArgument = false;
Bits.ClosureExpr.BehavesLikeNonisolatedNonsending = false;
Bits.ClosureExpr.IsOnewaySendOperation = false;
}

SourceRange getSourceRange() const;
Expand Down Expand Up @@ -4475,6 +4515,14 @@ class ClosureExpr : public AbstractClosureExpr {
Bits.ClosureExpr.BehavesLikeNonisolatedNonsending = value;
}

/// Whether this is the operation closure which pre-checking synthesizes for
/// the operand of a 'send', whose body is the user's call
bool isOnewaySendOperation() const { return Bits.ClosureExpr.IsOnewaySendOperation; }

void setIsOnewaySendOperation(bool value = true) {
Bits.ClosureExpr.IsOnewaySendOperation = value;
}

/// Determine whether this closure expression has an
/// explicitly-specified result type.
bool hasExplicitResultType() const { return ArrowLoc.isValid(); }
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/ExprNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ ABSTRACT_EXPR(Identity, Expr)
EXPR(Borrow, IdentityExpr)
EXPR(UnresolvedMemberChainResult, IdentityExpr)
EXPR_RANGE(Identity, Paren, UnresolvedMemberChainResult)
EXPR(Send, Expr)
EXPR(Copy, Expr)
EXPR(Consume, Expr)
ABSTRACT_EXPR(AnyTry, Expr)
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ EXPERIMENTAL_FEATURE(ExtendedSpecializedAttribute, false)
/// Builtin.createSynchronousDiscardingTask
LANGUAGE_FEATURE(BuiltinCreateSynchronousDiscardingTask, 0, "Builtin.createSynchronousDiscardingTask")

/// The 'send' fire-and-forget call expression.
EXPERIMENTAL_FEATURE(SendCalls, false)

#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3864,6 +3864,11 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
printRec(E->getSubExpr(), Label::optional("sub_expr"));
printFoot();
}
void visitSendExpr(SendExpr *E, Label label) {
printCommon(E, "send_expr", label);
printRec(E->getSubExpr(), Label::optional("sub_expr"));
printFoot();
}
void visitBorrowExpr(BorrowExpr *E, Label label) {
printCommon(E, "borrow_expr", label);
printRec(E->getSubExpr(), Label::optional("sub_expr"));
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5579,6 +5579,11 @@ void PrintAST::visitConsumeExpr(ConsumeExpr *expr) {
visit(expr->getSubExpr());
}

void PrintAST::visitSendExpr(SendExpr *expr) {
Printer << "send ";
visit(expr->getSubExpr());
}

void PrintAST::visitCopyExpr(CopyExpr *expr) {
Printer << "copy ";
visit(expr->getSubExpr());
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,14 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return nullptr;
}

Expr *visitSendExpr(SendExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}

Expr *visitTupleExpr(TupleExpr *E) {
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i)
if (E->getElement(i)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/Bridging/ExprBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ BridgedUnsafeExpr BridgedUnsafeExpr_createParsed(BridgedASTContext cContext,
return new (cContext.unbridged()) UnsafeExpr(unsafeLoc, cSubExpr.unbridged());
}

BridgedSendExpr BridgedSendExpr_createParsed(BridgedASTContext cContext,
SourceLoc sendLoc,
BridgedExpr cSubExpr) {
return new (cContext.unbridged()) SendExpr(sendLoc, cSubExpr.unbridged());
}

BridgedInOutExpr BridgedInOutExpr_createParsed(BridgedASTContext cContext,
SourceLoc loc,
BridgedExpr cSubExpr) {
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ DeclRefExpr *Expr::getMemberOperatorRef() {
return operatorRef;
}

SendExpr *SendExpr::createImplicit(ASTContext &ctx, SourceLoc sendLoc,
Expr *sub) {
auto *send = new (ctx) SendExpr(sendLoc, sub, /*implicit=*/true);
send->setType(ctx.TheEmptyTupleType);
return send;
}

ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
switch (getKind()) {
// No declaration reference.
Expand Down Expand Up @@ -366,6 +373,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
PASS_THROUGH_REFERENCE(Unsafe, getSubExpr);
PASS_THROUGH_REFERENCE(Consume, getSubExpr);
PASS_THROUGH_REFERENCE(Copy, getSubExpr);
PASS_THROUGH_REFERENCE(Send, getSubExpr);
PASS_THROUGH_REFERENCE(Borrow, getSubExpr);
PASS_THROUGH_REFERENCE(Try, getSubExpr);
PASS_THROUGH_REFERENCE(ForceTry, getSubExpr);
Expand Down Expand Up @@ -741,6 +749,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
return true;

case ExprKind::Await:
case ExprKind::Send:
case ExprKind::Unsafe:
case ExprKind::Consume:
case ExprKind::Copy:
Expand Down Expand Up @@ -959,6 +968,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
case ExprKind::Sequence:
case ExprKind::Paren:
case ExprKind::Await:
case ExprKind::Send:
case ExprKind::Unsafe:
case ExprKind::Consume:
case ExprKind::Copy:
Expand Down
38 changes: 38 additions & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/Pattern.h"
Expand Down Expand Up @@ -614,6 +615,43 @@ static bool usesFeatureNonexhaustiveAttribute(Decl *decl) {
return decl->getAttrs().hasAttribute<NonexhaustiveAttr>();
}

static bool usesFeatureSendCalls(Decl *decl) {
// Whether the body of 'afd' is printed into a module interface, because it
// is inlinable, and contains a 'send' call
auto hasInlinableSendCall = [](AbstractFunctionDecl *afd) {
if (afd->getResilienceExpansion() != ResilienceExpansion::Minimal ||
!afd->hasInlinableBodyText())
return false;
auto *body = afd->getBody(/*canSynthesize=*/false);
if (!body)
return false;

class SendCallFinder : public ASTWalker {
public:
bool found = false;

PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
// The body is printed as written, so only an explicit 'send' matters
if (isa<SendExpr>(E) && !E->isImplicit()) {
found = true;
return Action::Stop();
}
return Action::Continue(E);
}
};

SendCallFinder finder;
body->walk(finder);
return finder.found;
};

if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl))
return hasInlinableSendCall(afd);
if (auto *storage = dyn_cast<AbstractStorageDecl>(decl))
return llvm::any_of(storage->getAllAccessors(), hasInlinableSendCall);
return false;
}

static bool usesFeatureAlwaysInheritActorContext(Decl *decl) {
auto *VD = dyn_cast<ValueDecl>(decl);
if (!VD)
Expand Down
10 changes: 10 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ extension ASTGenVisitor {
return self.generate(prefixOperatorExpr: node)
case .regexLiteralExpr(let node):
return self.generate(regexLiteralExpr: node).asExpr
case .sendExpr(let node):
return self.generate(sendExpr: node).asExpr
case .sequenceExpr(let node):
return self.generate(sequenceExpr: node)
case .simpleStringLiteralExpr:
Expand Down Expand Up @@ -1294,6 +1296,14 @@ extension ASTGenVisitor {
)
}

func generate(sendExpr node: SendExprSyntax) -> BridgedSendExpr {
return .createParsed(
self.ctx,
sendLoc: self.generateSourceLoc(node.sendKeyword),
subExpr: self.generate(expr: node.expression)
)
}

// NOTE: When implementing new `generate(expr:)`, please update `isExprMigrated(_:)`.
}

Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/Sources/ASTGen/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ extension Parser.LanguageFeatures {
mapFeature(.OnceFunctions, to: .onceFunctions)
mapFeature(.CoroutineFunctions, to: .coroutineFunctions)
mapFeature(.ExtendedSpecializedAttribute, to: .extendedSpecializedAttribute)
mapFeature(.SendCalls, to: .sendCalls)
}
}

Expand Down
15 changes: 15 additions & 0 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,21 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
return sub;
}

if (Context.LangOpts.hasFeature(Feature::SendCalls) &&
Tok.isContextualKeyword("send") &&
peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
tok::code_complete) &&
!peekToken().isAtStartOfLine()) {
Tok.setKind(tok::contextual_keyword);
SourceLoc sendLoc = consumeToken();
ParserResult<Expr> sub =
parseExprSequenceElement(diag::expected_expr_after_send, isExprBasic);
if (!sub.hasCodeCompletion() && !sub.isNull()) {
sub = makeParserResult(new (Context) SendExpr(sendLoc, sub.get()));
}
return sub;
}

if (Context.LangOpts.hasFeature(Feature::OldOwnershipOperatorSpellings)) {
if (Tok.isContextualKeyword("_move")) {
Tok.setKind(tok::contextual_keyword);
Expand Down
Loading