|
| 1 | +//===-- Sema/AsyncCallerExecutionMigration.cpp ------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// This file implements code migration support for the `AsyncCallerExecution` |
| 15 | +/// feature. |
| 16 | +/// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#include "AsyncCallerExecutionMigration.h" |
| 20 | +#include "swift/AST/ASTContext.h" |
| 21 | +#include "swift/AST/Decl.h" |
| 22 | +#include "swift/AST/DiagnosticsSema.h" |
| 23 | +#include "swift/AST/Expr.h" |
| 24 | +#include "swift/AST/TypeRepr.h" |
| 25 | +#include "swift/Basic/Assertions.h" |
| 26 | +#include "swift/Basic/Feature.h" |
| 27 | +#include "swift/Basic/TaggedUnion.h" |
| 28 | +#include "llvm/ADT/PointerUnion.h" |
| 29 | + |
| 30 | +using namespace swift; |
| 31 | + |
| 32 | +namespace { |
| 33 | +class AsyncCallerExecutionMigrationTarget { |
| 34 | + ASTContext &ctx; |
| 35 | + PointerUnion<ValueDecl *, AbstractClosureExpr *, FunctionTypeRepr *> node; |
| 36 | + TaggedUnion<ActorIsolation, FunctionTypeIsolation> isolation; |
| 37 | + |
| 38 | +public: |
| 39 | + AsyncCallerExecutionMigrationTarget(ASTContext &ctx, ValueDecl *decl, |
| 40 | + ActorIsolation isolation) |
| 41 | + : ctx(ctx), node(decl), isolation(isolation) {} |
| 42 | + |
| 43 | + AsyncCallerExecutionMigrationTarget(ASTContext &ctx, |
| 44 | + AbstractClosureExpr *closure, |
| 45 | + ActorIsolation isolation) |
| 46 | + : ctx(ctx), node(closure), isolation(isolation) {} |
| 47 | + |
| 48 | + AsyncCallerExecutionMigrationTarget(ASTContext &ctx, FunctionTypeRepr *repr, |
| 49 | + FunctionTypeIsolation isolation) |
| 50 | + : ctx(ctx), node(repr), isolation(isolation) {} |
| 51 | + |
| 52 | + /// Warns that the behavior of nonisolated async functions will change under |
| 53 | + /// `AsyncCallerExecution` and suggests `@execution(concurrent)` to preserve |
| 54 | + /// the current behavior. |
| 55 | + void diagnose() const; |
| 56 | +}; |
| 57 | +} // end anonymous namespace |
| 58 | + |
| 59 | +void AsyncCallerExecutionMigrationTarget::diagnose() const { |
| 60 | + const auto feature = Feature::AsyncCallerExecution; |
| 61 | + |
| 62 | + ASSERT(node); |
| 63 | + ASSERT(ctx.LangOpts.getFeatureState(feature).isEnabledForAdoption()); |
| 64 | + |
| 65 | + AbstractFunctionDecl *functionDecl = nullptr; |
| 66 | + ClosureExpr *closure = nullptr; |
| 67 | + FunctionTypeRepr *functionRepr = nullptr; |
| 68 | + |
| 69 | + if (auto *decl = node.dyn_cast<ValueDecl *>()) { |
| 70 | + // Diagnose only explicit nodes. |
| 71 | + if (decl->isImplicit()) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // Diagnose only functions. |
| 76 | + functionDecl = dyn_cast<AbstractFunctionDecl>(decl); |
| 77 | + if (!functionDecl) { |
| 78 | + return; |
| 79 | + } |
| 80 | + } else if (auto *anyClosure = node.dyn_cast<AbstractClosureExpr *>()) { |
| 81 | + // Diagnose only explicit nodes. |
| 82 | + if (anyClosure->isImplicit()) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // The only subclass that can be explicit is this one. |
| 87 | + closure = cast<ClosureExpr>(anyClosure); |
| 88 | + } else { |
| 89 | + functionRepr = node.get<FunctionTypeRepr *>(); |
| 90 | + } |
| 91 | + |
| 92 | + // The execution behavior changes only for nonisolated functions. |
| 93 | + { |
| 94 | + bool isNonisolated; |
| 95 | + if (functionRepr) { |
| 96 | + isNonisolated = isolation.get<FunctionTypeIsolation>().isNonIsolated(); |
| 97 | + } else { |
| 98 | + auto isolation = this->isolation.get<ActorIsolation>(); |
| 99 | + isNonisolated = isolation.isNonisolated() || isolation.isUnspecified(); |
| 100 | + } |
| 101 | + |
| 102 | + if (!isNonisolated) { |
| 103 | + return; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // If the intended behavior is specified explicitly, don't diagnose. |
| 108 | + { |
| 109 | + const DeclAttributes *attrs = nullptr; |
| 110 | + if (functionDecl) { |
| 111 | + attrs = &functionDecl->getAttrs(); |
| 112 | + } else if (closure) { |
| 113 | + attrs = &closure->getAttrs(); |
| 114 | + } |
| 115 | + |
| 116 | + if (attrs && attrs->hasAttribute<ExecutionAttr>()) { |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // The execution behavior changes only for async functions. |
| 122 | + { |
| 123 | + bool isAsync = false; |
| 124 | + if (functionDecl) { |
| 125 | + isAsync = functionDecl->hasAsync(); |
| 126 | + } else if (closure) { |
| 127 | + isAsync = closure->isBodyAsync(); |
| 128 | + } else { |
| 129 | + isAsync = functionRepr->isAsync(); |
| 130 | + } |
| 131 | + |
| 132 | + if (!isAsync) { |
| 133 | + return; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + const ExecutionAttr attr(ExecutionKind::Concurrent, /*implicit=*/true); |
| 138 | + |
| 139 | + const auto featureName = getFeatureName(feature); |
| 140 | + if (functionDecl) { |
| 141 | + ctx.Diags |
| 142 | + .diagnose(functionDecl->getLoc(), |
| 143 | + diag::attr_execution_nonisolated_behavior_will_change_decl, |
| 144 | + featureName, functionDecl, &attr) |
| 145 | + .fixItInsertAttribute( |
| 146 | + functionDecl->getAttributeInsertionLoc(/*forModifier=*/false), |
| 147 | + &attr); |
| 148 | + } else if (closure) { |
| 149 | + ctx.Diags |
| 150 | + .diagnose(closure->getLoc(), |
| 151 | + diag::attr_execution_nonisolated_behavior_will_change_closure, |
| 152 | + featureName, &attr) |
| 153 | + .fixItAddAttribute(&attr, closure); |
| 154 | + } else { |
| 155 | + ctx.Diags |
| 156 | + .diagnose( |
| 157 | + functionRepr->getStartLoc(), |
| 158 | + diag::attr_execution_nonisolated_behavior_will_change_typerepr, |
| 159 | + featureName, &attr) |
| 160 | + .fixItInsertAttribute(functionRepr->getStartLoc(), &attr); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void swift::warnAboutNewNonisolatedAsyncExecutionBehavior( |
| 165 | + ASTContext &ctx, FunctionTypeRepr *repr, FunctionTypeIsolation isolation) { |
| 166 | + AsyncCallerExecutionMigrationTarget(ctx, repr, isolation).diagnose(); |
| 167 | +} |
| 168 | + |
| 169 | +void swift::warnAboutNewNonisolatedAsyncExecutionBehavior( |
| 170 | + ASTContext &ctx, ValueDecl *decl, ActorIsolation isolation) { |
| 171 | + AsyncCallerExecutionMigrationTarget(ctx, decl, isolation).diagnose(); |
| 172 | +} |
| 173 | + |
| 174 | +void swift::warnAboutNewNonisolatedAsyncExecutionBehavior( |
| 175 | + ASTContext &ctx, AbstractClosureExpr *closure, ActorIsolation isolation) { |
| 176 | + AsyncCallerExecutionMigrationTarget(ctx, closure, isolation).diagnose(); |
| 177 | +} |
0 commit comments