Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include try and await expressions which are parents of a freestanding expression macro in lexicalContext #2724

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/SwiftSyntaxMacros/Syntax+LexicalContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ extension SyntaxProtocol {
case let freestandingMacro as FreestandingMacroExpansionSyntax:
return Syntax(freestandingMacro.detached) as Syntax

// Try and await are preserved: A freestanding expression macro preceded
// by try or await may need to know whether those keywords are present so it
// can propagate them to any expressions in its expansion which were passed
// as arguments to the macro. The expression of the try or await is replaced
// with a trivial placeholder, though.
case var tryExpr as TryExprSyntax:
tryExpr = tryExpr.detached
tryExpr.expression = ExprSyntax(TypeExprSyntax(type: IdentifierTypeSyntax(name: .wildcardToken())))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wildcard token was chosen arbitrarily here. We don't necessarily have to reassign the expression at all, but I figured it shouldn't be necessary since the macro implementation already has it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably we do need to reassign it, otherwise in an expression like this one:

await f(a, b, c, #d)

d is able to see a, b, and c.

return Syntax(tryExpr)
case var awaitExpr as AwaitExprSyntax:
awaitExpr = awaitExpr.detached
awaitExpr.expression = ExprSyntax(TypeExprSyntax(type: IdentifierTypeSyntax(name: .wildcardToken())))
return Syntax(awaitExpr)

default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ final class LexicalContextTests: XCTestCase {
struct S {
let arg: C
var contextDescription: String {
#lexicalContextDescription
try await #lexicalContextDescription
}
}
return S(arg: c)
Expand All @@ -542,7 +542,9 @@ final class LexicalContextTests: XCTestCase {
struct S {
let arg: C
var contextDescription: String {
"""
try await """
await _
try _
contextDescription: String
struct S {}
{ c in
Expand All @@ -551,7 +553,7 @@ final class LexicalContextTests: XCTestCase {
struct S {
let arg: C
var contextDescription: String {
#lexicalContextDescription
try await #lexicalContextDescription
}
}
return S(arg: c)
Expand Down