File: EffectfulExpressionHandling.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (235 lines) | stat: -rw-r--r-- 7,924 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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 Swift project authors
//

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

// MARK: - Finding effect keywords and expressions

/// Get the effect keyword corresponding to a given syntax node, if any.
///
/// - Parameters:
/// 	- expr: The syntax node that may represent an effectful expression.
///
/// - Returns: The effect keyword corresponding to `expr`, if any.
private func _effectKeyword(for expr: ExprSyntax) -> Keyword? {
  switch expr.kind {
  case .tryExpr:
    return .try
  case .awaitExpr:
    return .await
  case .consumeExpr:
    return .consume
  case .borrowExpr:
    return .borrow
  case .unsafeExpr:
    return .unsafe
  default:
    return nil
  }
}

/// Determine how to descend further into a syntax node tree from a given node.
///
/// - Parameters:
///   - node: The syntax node currently being walked.
///
/// - Returns: Whether or not to descend into `node` and visit its children.
private func _continueKind(for node: Syntax) -> SyntaxVisitorContinueKind {
  switch node.kind {
  case .tryExpr, .awaitExpr, .consumeExpr, .borrowExpr, .unsafeExpr:
    // If this node represents an effectful expression, look inside it for
    // additional such expressions.
    return .visitChildren
  case .closureExpr, .functionDecl:
    // Do not delve into closures or function declarations.
    return .skipChildren
  case .variableDecl:
    // Delve into variable declarations.
    return .visitChildren
  default:
    // Do not delve into declarations other than variables.
    if node.isProtocol((any DeclSyntaxProtocol).self) {
      return .skipChildren
    }
  }

  // Recurse into everything else.
  return .visitChildren
}

/// A syntax visitor class that looks for effectful keywords in a given
/// expression.
private final class _EffectFinder: SyntaxAnyVisitor {
  /// The effect keywords discovered so far.
  var effectKeywords: Set<Keyword> = []

  override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
    if let expr = node.as(ExprSyntax.self), let keyword = _effectKeyword(for: expr) {
      effectKeywords.insert(keyword)
    }

    return _continueKind(for: node)
  }
}

/// Find effectful keywords in a syntax node.
///
/// - Parameters:
///   - node: The node to inspect.
///
/// - Returns: A set of effectful keywords such as `await` that are present in
///   `node`.
///
/// This function does not descend into function declarations or closure
/// expressions because they represent distinct lexical contexts and their
/// effects are uninteresting in the context of `node` unless they are called.
func findEffectKeywords(in node: some SyntaxProtocol) -> Set<Keyword> {
  let effectFinder = _EffectFinder(viewMode: .sourceAccurate)
  effectFinder.walk(node)
  return effectFinder.effectKeywords
}

/// Find effectful keywords in a macro's lexical context.
///
/// - Parameters:
///   - context: The macro context in which the expression is being parsed.
///
/// - Returns: A set of effectful keywords such as `await` that are present in
///   `context` and would apply to an expression macro during its expansion.
func findEffectKeywords(in context: some MacroExpansionContext) -> Set<Keyword> {
  let result = context.lexicalContext.reversed().lazy
    .prefix { _continueKind(for: $0) == .visitChildren }
    .compactMap { $0.as(ExprSyntax.self) }
    .compactMap(_effectKeyword(for:))
  return Set(result)
}

extension BidirectionalCollection<Syntax> {
  /// The suffix of syntax nodes in this collection which are effectful
  /// expressions, such as those for `try` or `await`.
  var trailingEffectExpressions: some Collection<Syntax> {
    reversed()
      .prefix { node in
        // This could be simplified if/when swift-syntax introduces a protocol
        // which all effectful expression syntax node types conform to.
        // See https://github.com/swiftlang/swift-syntax/issues/3040
        node.is(TryExprSyntax.self) || node.is(AwaitExprSyntax.self) || node.is(UnsafeExprSyntax.self)
      }
      .reversed()
  }
}

// MARK: - Inserting effect keywords/thunks

/// Whether or not the `unsafe` expression keyword is supported.
var isUnsafeKeywordSupported: Bool {
  // The 'unsafe' keyword was introduced in 6.2 as part of SE-0458. Older
  // toolchains are not aware of it.
#if compiler(>=6.2)
  true
#else
  false
#endif
}

/// Make a function call expression to an effectful thunk function provided by
/// the testing library.
///
/// - Parameters:
///   - thunkName: The unqualified name of the thunk function to call. This
///     token must be the name of a function in the `Testing` module.
///   - expr: The expression to thunk.
///
/// - Returns: An expression representing a call to the function named
///   `thunkName`, passing `expr`.
private func _makeCallToEffectfulThunk(_ thunkName: TokenSyntax, passing expr: some ExprSyntaxProtocol) -> ExprSyntax {
  ExprSyntax(
    FunctionCallExprSyntax(
      calledExpression: MemberAccessExprSyntax(
        base: DeclReferenceExprSyntax(baseName: .identifier("Testing")),
        declName: DeclReferenceExprSyntax(baseName: thunkName)
      ),
      leftParen: .leftParenToken(),
      rightParen: .rightParenToken()
    ) {
      LabeledExprSyntax(expression: expr.trimmed)
    }
  )
}

/// Apply the given effectful keywords (i.e. `try` and `await`) to an expression
/// using thunk functions provided by the testing library.
///
/// - Parameters:
///   - effectfulKeywords: The effectful keywords to apply.
///   - expr: The expression to apply the keywords and thunk functions to.
///   - insertThunkCalls: Whether or not to also insert calls to thunks to
///   	ensure the inserted keywords do not generate warnings. If you aren't
///     sure whether thunk calls are needed, pass `true`.
///
/// - Returns: A copy of `expr` if no changes are needed, or an expression that
///   adds the keywords in `effectfulKeywords` to `expr`.
func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some ExprSyntaxProtocol, insertThunkCalls: Bool = true) -> ExprSyntax {
  let originalExpr = expr
  var expr = ExprSyntax(expr.trimmed)

  let needAwait = effectfulKeywords.contains(.await) && !expr.is(AwaitExprSyntax.self)
  let needTry = effectfulKeywords.contains(.try) && !expr.is(TryExprSyntax.self)

  let needUnsafe = isUnsafeKeywordSupported && effectfulKeywords.contains(.unsafe) && !expr.is(UnsafeExprSyntax.self)

  // First, add thunk function calls.
  if insertThunkCalls {
    if needAwait {
      expr = _makeCallToEffectfulThunk(.identifier("__requiringAwait"), passing: expr)
    }
    if needTry {
      expr = _makeCallToEffectfulThunk(.identifier("__requiringTry"), passing: expr)
    }
    if needUnsafe {
      expr = _makeCallToEffectfulThunk(.identifier("__requiringUnsafe"), passing: expr)
    }
  }

  // Then add keyword expressions. (We do this separately so we end up writing
  // `try await __r(__r(self))` instead of `try __r(await __r(self))` which is
  // less accepted by the compiler.)
  if needAwait {
    expr = ExprSyntax(
      AwaitExprSyntax(
        awaitKeyword: .keyword(.await, trailingTrivia: .space),
        expression: expr
      )
    )
  }
  if needTry {
    expr = ExprSyntax(
      TryExprSyntax(
        tryKeyword: .keyword(.try, trailingTrivia: .space),
        expression: expr
      )
    )
  }
  if needUnsafe {
    expr = ExprSyntax(
      UnsafeExprSyntax(
        unsafeKeyword: .keyword(.unsafe, trailingTrivia: .space),
        expression: expr
      )
    )
  }

  expr.leadingTrivia = originalExpr.leadingTrivia
  expr.trailingTrivia = originalExpr.trailingTrivia

  return expr
}