diff --git a/lib/Macros/Sources/SwiftMacros/CMakeLists.txt b/lib/Macros/Sources/SwiftMacros/CMakeLists.txt index cde6d3f4f9491..6c6a9784000d9 100644 --- a/lib/Macros/Sources/SwiftMacros/CMakeLists.txt +++ b/lib/Macros/Sources/SwiftMacros/CMakeLists.txt @@ -14,7 +14,6 @@ add_swift_macro_library(SwiftMacros OptionSetMacro.swift DebugDescriptionMacro.swift DistributedResolvableMacro.swift - TaskMacro.swift SyntaxExtensions.swift TaskLocalMacro.swift SwiftifyImportMacro.swift diff --git a/lib/Macros/Sources/SwiftMacros/TaskMacro.swift b/lib/Macros/Sources/SwiftMacros/TaskMacro.swift deleted file mode 100644 index 53baf1fd13b11..0000000000000 --- a/lib/Macros/Sources/SwiftMacros/TaskMacro.swift +++ /dev/null @@ -1,126 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2025 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -import SwiftDiagnostics -import SwiftParser -import SwiftSyntax -import SwiftSyntaxBuilder -import SwiftSyntaxMacros - -extension MacroExpansionContext { - func diagnose( - _ diag: TaskMacroDiagnostic, - at node: some SyntaxProtocol - ) { - diagnose(Diagnostic( - node: Syntax(node), - message: diag - )) - } -} - -enum TaskMacroDiagnostic: String, DiagnosticMessage { - case noImplementation - = "'@Task' macro can only be used on functions with an implementation" - case unsupportedGlobalActor - = "'@Task' global actor must be written 'GlobalActorType'.shared" - - var message: String { rawValue } - - var severity: DiagnosticSeverity { .error } - - var diagnosticID: MessageID { - MessageID(domain: "_Concurrency", id: "TaskMacro.\(self)") - } -} - - -public struct TaskMacro: BodyMacro { - public static func expansion( - of node: AttributeSyntax, - statements: CodeBlockItemListSyntax, - in context: some MacroExpansionContext - ) throws -> [CodeBlockItemSyntax] { - var globalActor: TokenSyntax? = nil - var argumentList: LabeledExprListSyntax = [] - if case .argumentList(let arguments) = node.arguments { - if let actor = arguments.first(labeled: "on") { - guard let member = actor.expression.as(MemberAccessExprSyntax.self), - let declRef = member.base?.as(DeclReferenceExprSyntax.self) else { - context.diagnose(.unsupportedGlobalActor, at: actor) - return [] - } - - argumentList = LabeledExprListSyntax(arguments.dropFirst()) - globalActor = declRef.baseName - } else { - argumentList = arguments - } - } - - let signature: ClosureSignatureSyntax? = - if let globalActor { - .init(attributes: "@\(globalActor) ") - } else { - nil - } - - let parens: (left: TokenSyntax, right: TokenSyntax)? = - if !argumentList.isEmpty { - (.leftParenToken(), .rightParenToken()) - } else { - nil - } - - let taskInit = FunctionCallExprSyntax( - calledExpression: DeclReferenceExprSyntax( - baseName: "Task" - ), - leftParen: parens?.left, - arguments: argumentList, - rightParen: parens?.right, - trailingClosure: ClosureExprSyntax( - signature: signature, - statements: statements - ) - ) - - return ["\(taskInit)"] - } - - public static func expansion( - of node: AttributeSyntax, - providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax, - in context: some MacroExpansionContext - ) throws -> [CodeBlockItemSyntax] { - guard let taskBody = declaration.body else { - context.diagnose(.noImplementation, at: node) - return [] - } - - return try expansion( - of: node, - statements: taskBody.statements, - in: context) - } - - public static func expansion( - of node: AttributeSyntax, - providingBodyFor closure: ClosureExprSyntax, - in context: some MacroExpansionContext - ) throws -> [CodeBlockItemSyntax] { - try expansion( - of: node, - statements: closure.statements, - in: context) - } -} diff --git a/lib/Sema/TypeCheckMacros.cpp b/lib/Sema/TypeCheckMacros.cpp index cb8b74d140238..73b8bb1c93b59 100644 --- a/lib/Sema/TypeCheckMacros.cpp +++ b/lib/Sema/TypeCheckMacros.cpp @@ -1902,18 +1902,6 @@ ExpandBodyMacroRequest::evaluate(Evaluator &evaluator, if (bufferID) return; - // '@Task' is gated behind the 'ConcurrencySyntaxSugar' - // experimental feature. - if (macro->getParentModule()->getName().is("_Concurrency") && - macro->getBaseIdentifier().is("Task") && - !ctx.LangOpts.hasFeature(Feature::ConcurrencySyntaxSugar)) { - ctx.Diags.diagnose( - customAttr->getLocation(), - diag::experimental_macro, - macro->getName()); - return; - } - SourceFile * macroSourceFile = nullptr; if (auto *fnDecl = fn.getAbstractFunctionDecl()) { macroSourceFile = ::evaluateAttachedMacro( diff --git a/stdlib/public/Concurrency/Actor.swift b/stdlib/public/Concurrency/Actor.swift index 263783305006e..026b1766456e5 100644 --- a/stdlib/public/Concurrency/Actor.swift +++ b/stdlib/public/Concurrency/Actor.swift @@ -99,27 +99,6 @@ internal func _enqueueOnMain(_ job: UnownedJob) @freestanding(expression) public macro isolation() -> T = Builtin.IsolationMacro -/// Wrap the function body in a new top-level task on behalf of the -/// given actor. -@available(SwiftStdlib 5.1, *) -@attached(body) -public macro Task( - on actor: any GlobalActor, - name: String? = nil, - priority: TaskPriority? = nil -) = - #externalMacro(module: "SwiftMacros", type: "TaskMacro") - -/// Wrap the function body in a new top-level task on behalf of the -/// current actor. -@available(SwiftStdlib 5.1, *) -@attached(body) -public macro Task( - name: String? = nil, - priority: TaskPriority? = nil -) = - #externalMacro(module: "SwiftMacros", type: "TaskMacro") - #endif #if $IsolatedAny diff --git a/test/Macros/task_macro.swift b/test/Macros/task_macro.swift deleted file mode 100644 index b13d4091a0229..0000000000000 --- a/test/Macros/task_macro.swift +++ /dev/null @@ -1,157 +0,0 @@ -// REQUIRES: swift_swift_parser, swift_feature_ConcurrencySyntaxSugar, swift_feature_ClosureBodyMacro - -// RUN: %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -enable-experimental-feature ConcurrencySyntaxSugar -enable-experimental-feature ClosureBodyMacro -language-mode 6 %s -dump-macro-expansions -disable-availability-checking 2>&1 | %FileCheck %s - -func f() async {} - -// CHECK-LABEL: @__swiftmacro_10task_macro4sync4TaskfMb_.swift -// CHECK: Task { -// CHECK: await f() -// CHECK: } - -@Task -func sync() { - await f() -} - -func takeClosure( - _ closure: @escaping @Sendable () -> Void, - v: Int = 42 -) { - closure() -} - -func multipleClosures( - a: @escaping @Sendable () -> Void, - b: @escaping @Sendable () -> Void) { -} - -func onClosure() { - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX35_16_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await f() - // CHECK: } - // CHECK: } - takeClosure { @Task in - await f() - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX45_16_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await f() - // CHECK: } - // CHECK: } - takeClosure({ @Task in - await f() - }, v: 0) - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX55_21_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await f() - // CHECK: } - // CHECK: } - multipleClosures { @Task in - await f() - } b: { - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX68_9_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await f() - // CHECK: } - // CHECK: } - multipleClosures { - _ = 42 - } b: { @Task in - await f() - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX86_4_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: _ = 42 - // CHECK: } - // CHECK: } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX88_9_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await f() - // CHECK: } - // CHECK: } - multipleClosures { - @Task in - _ = 42 - } b: { @Task in - await f() - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX100_12_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await f() - // CHECK: } - // CHECK: } - let _ = { - func test() { - _ = { @Task in await f() } - } - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX114_54_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { - // CHECK: await test() - // CHECK: } - // CHECK: } - let _ = { - let y = 42 - func test() async { print(y) } - - if case let (_, closure) = (otherValue: 42, fn: { @Task in - await test() - }) { - let _: Task<(), Never> = closure() - } - } -} - -func onClosureWithArguments() { - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX129_16_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task { @MainActor in - // CHECK: await f() - // CHECK: } - // CHECK: } - takeClosure { @Task(on: MainActor.shared) in - await f() - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX139_16_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task(name: "MyTask") { @MainActor in - // CHECK: await f() - // CHECK: } - // CHECK: } - takeClosure { @Task(on: MainActor.shared, name: "MyTask") in - await f() - } - - // CHECK-LABEL: @__swiftmacro_10task_macro0021task_macroswift_IfFDefMX149_16_33_39E2B7F186C0B70273105736DD2F3721Ll4TaskfMb_.swift - // CHECK: { - // CHECK: Task(name: "MyTask", priority: .high) { - // CHECK: await f() - // CHECK: } - // CHECK: } - takeClosure { @Task(name: "MyTask", priority: .high) in - await f() - } -} - -// FIXME: Remove `-disable-availability-checking` from the run line after -// SE-0469 review wraps up. This comment is at the end of the file because -// closure body macro mangling includes source locations.