diff --git a/lib/AST/ASTScopeCreation.cpp b/lib/AST/ASTScopeCreation.cpp index a8c979b05ad91..7f610aeb68895 100644 --- a/lib/AST/ASTScopeCreation.cpp +++ b/lib/AST/ASTScopeCreation.cpp @@ -290,7 +290,11 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF, switch (*macroRole) { case MacroRole::Expression: case MacroRole::Declaration: - case MacroRole::CodeItem: + case MacroRole::CodeItem: { + parentLoc = SF->getMacroInsertionRange().Start; + break; + } + case MacroRole::Accessor: case MacroRole::MemberAttribute: case MacroRole::Conformance: diff --git a/test/Macros/Inputs/syntax_macro_definitions.swift b/test/Macros/Inputs/syntax_macro_definitions.swift index b21f7c6236565..1fa363f5a9454 100644 --- a/test/Macros/Inputs/syntax_macro_definitions.swift +++ b/test/Macros/Inputs/syntax_macro_definitions.swift @@ -105,6 +105,20 @@ public struct StringifyAndTryMacro: ExpressionMacro { } } +public struct TryCallThrowingFuncMacro: ExpressionMacro { + public static func expansion( + of macro: some FreestandingMacroExpansionSyntax, + in context: some MacroExpansionContext + ) -> ExprSyntax { + return """ + try await { + print("let's throw") + return try await throwingFunc() + }() + """ + } +} + struct SimpleDiagnosticMessage: DiagnosticMessage { let message: String let diagnosticID: MessageID diff --git a/test/Macros/macro_expand.swift b/test/Macros/macro_expand.swift index 51822706b3a2c..c2fdceb373501 100644 --- a/test/Macros/macro_expand.swift +++ b/test/Macros/macro_expand.swift @@ -308,6 +308,18 @@ func testStringifyWithThrows() throws { _ = #stringifyAndTry(maybeThrowing()) } +@available(SwiftStdlib 5.1, *) +func throwingFunc() async throws -> Int { 5 } + +@freestanding(expression) macro callThrowingFunc(_ body: () -> T) -> T = #externalMacro(module: "MacroDefinition", type: "TryCallThrowingFuncMacro") + +@available(SwiftStdlib 5.1, *) +func testThrowingCall() async throws -> Int { + #callThrowingFunc { + [1, 2, 3, 4, 5].map { $0 + 1 }.first! + } +} + func testStringifyWithLocalType() throws { _ = #stringify({ struct QuailError: Error {}