Skip to content

Commit ef2320c

Browse files
authored
Merge pull request #74927 from DougGregor/astscope-insertion-loc-freestanding-6.0
[6.0] [Macros] Put the insertion location for freestanding macros at the beginning
2 parents 2de093b + 3c9bc4f commit ef2320c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
289289
switch (*macroRole) {
290290
case MacroRole::Expression:
291291
case MacroRole::Declaration:
292-
case MacroRole::CodeItem:
292+
case MacroRole::CodeItem: {
293+
parentLoc = SF->getMacroInsertionRange().Start;
294+
break;
295+
}
296+
293297
case MacroRole::Accessor:
294298
case MacroRole::MemberAttribute:
295299
case MacroRole::Conformance:

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ public struct StringifyAndTryMacro: ExpressionMacro {
105105
}
106106
}
107107

108+
public struct TryCallThrowingFuncMacro: ExpressionMacro {
109+
public static func expansion(
110+
of macro: some FreestandingMacroExpansionSyntax,
111+
in context: some MacroExpansionContext
112+
) -> ExprSyntax {
113+
return """
114+
try await {
115+
print("let's throw")
116+
return try await throwingFunc()
117+
}()
118+
"""
119+
}
120+
}
121+
108122
struct SimpleDiagnosticMessage: DiagnosticMessage {
109123
let message: String
110124
let diagnosticID: MessageID

test/Macros/macro_expand.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ func testStringifyWithThrows() throws {
308308
_ = #stringifyAndTry(maybeThrowing())
309309
}
310310

311+
@available(SwiftStdlib 5.1, *)
312+
func throwingFunc() async throws -> Int { 5 }
313+
314+
@freestanding(expression) macro callThrowingFunc<T>(_ body: () -> T) -> T = #externalMacro(module: "MacroDefinition", type: "TryCallThrowingFuncMacro")
315+
316+
@available(SwiftStdlib 5.1, *)
317+
func testThrowingCall() async throws -> Int {
318+
#callThrowingFunc {
319+
[1, 2, 3, 4, 5].map { $0 + 1 }.first!
320+
}
321+
}
322+
311323
func testStringifyWithLocalType() throws {
312324
_ = #stringify({
313325
struct QuailError: Error {}

0 commit comments

Comments
 (0)