Skip to content

Commit 0441436

Browse files
authored
Merge pull request swiftlang#74925 from DougGregor/astscope-insertion-loc-freestanding
[Macros] Put the insertion location for freestanding macros at the beginning
2 parents 268a785 + 7ea1599 commit 0441436

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
@@ -290,7 +290,11 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
290290
switch (*macroRole) {
291291
case MacroRole::Expression:
292292
case MacroRole::Declaration:
293-
case MacroRole::CodeItem:
293+
case MacroRole::CodeItem: {
294+
parentLoc = SF->getMacroInsertionRange().Start;
295+
break;
296+
}
297+
294298
case MacroRole::Accessor:
295299
case MacroRole::MemberAttribute:
296300
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)