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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 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 SwiftParser
public import SwiftSyntax
import SwiftSyntaxBuilder
public import SwiftSyntaxMacros
#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand #expect(processExitsWith:)")
#endif
/// A protocol containing the common implementation for the expansions of the
/// `#expect()` and `#require()` macros.
///
/// This type is used to implement the `#expect()` and `#require()` macros. Do
/// not use it directly.
///
/// ## Macro arguments
///
/// Overloads of these macros that evaluate an expression take exactly three
/// arguments: a "condition" argument, a `Comment?`-typed argument with no
/// label, and an optional `SourceLocation`-typed argument with the label
/// `sourceLocation`. The "condition" argument may be expanded into additional
/// arguments based on its representation in the syntax tree.
///
/// ## Macro arguments with trailing closures
///
/// Overloads of these macros that take trailing closures are expected to use
/// the label `"performing"` to identify the (first) trailing closure. By using
/// a consistent label, we can eliminate ambiguity during parsing.
///
/// The `__check()` function that implements expansions of these macros must
/// take any developer-supplied arguments _before_ the ones inserted during
/// macro expansion (starting with the `"expression"` argument.) The `isolation`
/// argument (if present) and `sourceLocation` argument are placed at the end of
/// the generated function call's argument list.
public protocol ConditionMacro: ExpressionMacro, Sendable {
/// Whether or not the macro's expansion may throw an error.
static var isThrowing: Bool { get }
}
// MARK: -
/// The token used as the label of the argument passed to `#expect()` and
/// `#require()` and used for actor isolation.
private var _isolationLabel: TokenSyntax { .identifier("isolation") }
/// The token used as the label of the source location argument passed to
/// `#expect()` and `#require()`.
private var _sourceLocationLabel: TokenSyntax { .identifier("sourceLocation") }
/// The token used as a mandatory label on any (first) trailing closure used
/// with `#expect()` or `#require()`.
private var _trailingClosureLabel: TokenSyntax { .identifier("performing") }
extension ConditionMacro {
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
return try expansion(of: macro, primaryExpression: nil, in: context)
}
public static var formatMode: FormatMode {
.disabled
}
/// Perform the expansion of this condition macro.
///
/// - Parameters:
/// - macro: The macro to expand.
/// - primaryExpression: The expression to use for source code capture, or
/// `nil` to infer it from `macro`.
/// - context: The macro context in which the expression is being parsed.
///
/// - Returns: The expanded form of `macro`.
///
/// - Throws: Any error preventing expansion of `macro`.
static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
primaryExpression: ExprSyntax?,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
// Reconstruct an argument list that includes any trailing closures.
let macroArguments = argumentList(of: macro, in: context)
// Figure out important argument indices.
let trailingClosureIndex = macroArguments.firstIndex { $0.label?.tokenKind == _trailingClosureLabel.tokenKind }
var commentIndex: [Argument].Index?
if let trailingClosureIndex {
// Assume that the comment, if present is the last argument in the
// argument list prior to the trailing closure that has no label.
#if SWT_FIXED_154221449
commentIndex = macroArguments[..<trailingClosureIndex].lastIndex { $0.label == nil }
#else
commentIndex = macroArguments[..<trailingClosureIndex].lastIndex { argument in
guard argument.label == nil else {
return false
}
if let expr = argument.expression.as(MacroExpansionExprSyntax.self),
expr.macroName.tokenKind == .identifier("__capturedValue") {
return false
}
return true
}
#endif
} else if macroArguments.count > 1 {
// If there is no trailing closure argument and there is more than one
// argument, then the comment is the last argument with no label (and also
// never the first argument.)
commentIndex = macroArguments.dropFirst().lastIndex { $0.label == nil }
}
let isolationArgumentIndex = macroArguments.lazy
.compactMap(\.label)
.firstIndex { $0.tokenKind == _isolationLabel.tokenKind }
let sourceLocationArgumentIndex = macroArguments.lazy
.compactMap(\.label)
.firstIndex { $0.tokenKind == _sourceLocationLabel.tokenKind }
// Construct the argument list to __check().
let expandedFunctionName: TokenSyntax
var checkArguments = [Argument]()
do {
if let trailingClosureIndex {
// Include all arguments other than the "comment" and "sourceLocation"
// arguments here.
checkArguments += macroArguments.indices.lazy
.filter { $0 != commentIndex }
.filter { $0 != isolationArgumentIndex }
.filter { $0 != sourceLocationArgumentIndex }
.map { macroArguments[$0] }
// The trailing closure should be the focus of the source code capture.
let primaryExpression = primaryExpression ?? macroArguments[trailingClosureIndex].expression
let sourceCode = parseCondition(from: primaryExpression, for: macro, in: context).expression
checkArguments.append(Argument(label: "expression", expression: sourceCode))
expandedFunctionName = .identifier("__checkClosureCall")
} else {
// Get the condition expression and extract its parsed form and source
// code. The first argument is always the condition argument if there is
// no trailing closure argument.
let conditionArgument = parseCondition(from: macroArguments.first!.expression, for: macro, in: context)
checkArguments += conditionArgument.arguments
// Include all arguments other than the "condition", "comment", and
// "sourceLocation" arguments here.
checkArguments += macroArguments.dropFirst().indices.lazy
.filter { $0 != commentIndex }
.filter { $0 != isolationArgumentIndex }
.filter { $0 != sourceLocationArgumentIndex }
.map { macroArguments[$0] }
if let primaryExpression {
let sourceCode = parseCondition(from: primaryExpression, for: macro, in: context).expression
checkArguments.append(Argument(label: "expression", expression: sourceCode))
} else {
checkArguments.append(Argument(label: "expression", expression: conditionArgument.expression))
}
expandedFunctionName = conditionArgument.expandedFunctionName
}
// Capture any comments as well -- either in source, preceding the
// expression macro or one of its lexical context nodes, or as an argument
// to the macro.
let commentsArrayExpr = ArrayExprSyntax {
// Lexical context is ordered innermost-to-outermost, so reverse it to
// maintain the expected order.
for lexicalSyntaxNode in context.lexicalContext.trailingEffectExpressions.reversed() {
for commentTraitExpr in createCommentTraitExprs(for: lexicalSyntaxNode) {
ArrayElementSyntax(expression: commentTraitExpr)
}
}
for commentTraitExpr in createCommentTraitExprs(for: macro) {
ArrayElementSyntax(expression: commentTraitExpr)
}
if let commentIndex {
ArrayElementSyntax(expression: macroArguments[commentIndex].expression.trimmed)
}
}
if let commentIndex, !macroArguments[commentIndex].expression.is(StringLiteralExprSyntax.self) {
// The developer supplied a comment argument that isn't a string
// literal. It might be nil, so explicitly filter out nil values from
// the resulting comment array.
checkArguments.append(Argument(
label: "comments",
expression: #"(\#(commentsArrayExpr) as [Comment?]).compactMap(\.self)"#
))
} else {
checkArguments.append(Argument(label: "comments", expression: commentsArrayExpr))
}
checkArguments.append(Argument(label: "isRequired", expression: BooleanLiteralExprSyntax(isThrowing)))
if let isolationArgumentIndex {
checkArguments.append(macroArguments[isolationArgumentIndex])
}
if let sourceLocationArgumentIndex {
checkArguments.append(macroArguments[sourceLocationArgumentIndex])
} else {
checkArguments.append(Argument(label: _sourceLocationLabel, expression: createSourceLocationExpr(of: macro, context: context)))
}
}
// Construct and return the call to __check().
let call: ExprSyntax = "Testing.\(expandedFunctionName)(\(LabeledExprListSyntax(checkArguments)))"
if isThrowing {
return "\(call).__required()"
}
return "\(call).__expected()"
}
/// Get the complete argument list for a given macro, including any trailing
/// closures.
///
/// - Parameters:
/// - macro: The macro being inspected.
/// - context: The macro context in which the expression is being parsed.
///
/// - Returns: A copy of the argument list of `macro` with trailing closures
/// included.
static func argumentList(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> [Argument] {
var result = [Argument]()
// NOTE: An #expect() or #require() invocation can have a condition argument
// OR a trailing closure, but not both. If it has both, then that makes it
// ambiguous to parse an invocation with a comment argument such as:
//
// #expect(x) { ... }
//
// Since we do not have enough context during macro expansion to know if the
// "x" argument is the condition or a comment. Additional labelled arguments
// are allowed.
// Include the original arguments first.
result += macro.arguments.lazy.map(Argument.init)
if let trailingClosure = macro.trailingClosure {
// Since a trailing closure does not (syntactically) include a label, we
// assume that the argument has the mandatory label described above.
result.append(Argument(label: _trailingClosureLabel, expression: trailingClosure))
}
// Include any additional trailing closures. These trailing closures will
// always have labels.
result += macro.additionalTrailingClosures.lazy.map(Argument.init)
return result
}
}
// MARK: -
/// A type describing the expansion of the `#expect()` macro.
public struct ExpectMacro: ConditionMacro {
public static var isThrowing: Bool {
false
}
}
// MARK: -
/// A type describing the expansion of the `#require()` macro.
public struct RequireMacro: ConditionMacro {
public static var isThrowing: Bool {
true
}
}
/// A protocol that can be used to create a condition macro that refines the
/// behavior of another previously-defined condition macro.
public protocol RefinedConditionMacro: ConditionMacro {
associatedtype Base: ConditionMacro
}
extension RefinedConditionMacro {
public static var isThrowing: Bool {
Base.isThrowing
}
}
// MARK: - Diagnostics-emitting condition macros
/// A type describing the expansion of the `#require()` macro when it is
/// ambiguous whether it refers to a boolean check or optional unwrapping.
///
/// This type is otherwise exactly equivalent to ``RequireMacro``.
public struct AmbiguousRequireMacro: RefinedConditionMacro {
public typealias Base = RequireMacro
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
if let argument = macro.arguments.first {
_checkAmbiguousArgument(argument.expression, in: context)
}
// Perform the normal macro expansion for #require().
return try RequireMacro.expansion(of: macro, in: context)
}
/// Check for an ambiguous argument to the `#require()` macro and emit the
/// appropriate diagnostics.
///
/// - Parameters:
/// - argument: The ambiguous argument.
/// - context: The macro context in which the expression is being parsed.
private static func _checkAmbiguousArgument(_ argument: ExprSyntax, in context: some MacroExpansionContext) {
// If the argument is wrapped in parentheses, strip them before continuing.
if let argumentWithoutParentheses = removeParentheses(from: argument) {
return _checkAmbiguousArgument(argumentWithoutParentheses, in: context)
}
// If the argument is explicitly an as? cast already, do not diagnose.
if argument.is(AsExprSyntax.self) {
return
}
// If we reach this point, then the argument appears to be an ambiguous
// expression and we aren't sure if the developer intended to unwrap a Bool?
// or check the value of the wrapped Bool.
context.diagnose(.optionalBoolExprIsAmbiguous(argument))
}
}
/// A type describing the expansion of the `#require()` macro when it is passed
/// a non-optional, non-`Bool` value.
///
/// This type is otherwise exactly equivalent to ``RequireMacro``.
public struct NonOptionalRequireMacro: RefinedConditionMacro {
public typealias Base = RequireMacro
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
if let argument = macro.arguments.first {
#if !SWT_FIXED_137943258
// Silence this warning if we see a token (`?`, `nil`, or "Optional") that
// might indicate the test author expects the expression is optional.
let tokenKindsIndicatingOptionality: [TokenKind] = [
.infixQuestionMark,
.postfixQuestionMark,
.keyword(.nil),
.identifier("Optional")
]
let looksOptional = argument.tokens(viewMode: .sourceAccurate).lazy
.map(\.tokenKind)
.contains(where: tokenKindsIndicatingOptionality.contains)
if !looksOptional {
context.diagnose(.nonOptionalRequireIsRedundant(argument.expression, in: macro))
}
#else
context.diagnose(.nonOptionalRequireIsRedundant(argument.expression, in: macro))
#endif
}
// Perform the normal macro expansion for #require().
return try RequireMacro.expansion(of: macro, in: context)
}
}
/// A type describing the expansion of the `#require(throws:)` macro.
///
/// This macro makes a best effort to check if the type argument is `Never.self`
/// (as we only have the syntax tree here) and diagnoses it as redundant if so.
/// See also ``RequireThrowsNeverMacro`` which is used when full type checking
/// is contextually available.
///
/// This type is otherwise exactly equivalent to ``RequireMacro``.
public struct RequireThrowsMacro: RefinedConditionMacro {
public typealias Base = RequireMacro
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
if let argument = macro.arguments.first {
let argumentTokens: [String] = argument.expression.tokens(viewMode: .fixedUp).lazy
.filter { $0.tokenKind != .period }
.map(\.textWithoutBackticks)
if argumentTokens == ["Swift", "Never", "self"] || argumentTokens == ["Never", "self"] {
context.diagnose(.requireThrowsNeverIsRedundant(argument.expression, in: macro))
}
}
// Perform the normal macro expansion for #require().
return try RequireMacro.expansion(of: macro, in: context)
}
}
/// A type describing the expansion of the `#require(throws:)` macro when it is
/// passed `Never.self`, which is redundant.
///
/// This type is otherwise exactly equivalent to ``RequireMacro``.
public struct RequireThrowsNeverMacro: RefinedConditionMacro {
public typealias Base = RequireMacro
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
if let argument = macro.arguments.first {
context.diagnose(.requireThrowsNeverIsRedundant(argument.expression, in: macro))
}
// Perform the normal macro expansion for #require().
return try RequireMacro.expansion(of: macro, in: context)
}
}
// MARK: - Exit test condition macros
public protocol ExitTestConditionMacro: RefinedConditionMacro {}
extension ExitTestConditionMacro {
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
// Perform the normal macro expansion for the macro with the standard set
// of arguments. This gives us any relevant diagnostics for the body of the
// exit test. We then discard the result (because we haven't performed any
// additional transformations) and then re-run the macro with the
// substituted trailing closure argument.
_ = try Base.expansion(of: macro, in: context)
var arguments = argumentList(of: macro, in: context)
let trailingClosureIndex = arguments.firstIndex { $0.label?.tokenKind == _trailingClosureLabel.tokenKind }
guard let trailingClosureIndex else {
fatalError("Could not find the body argument to this exit test. Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new")
}
var bodyArgumentExpr = arguments[trailingClosureIndex].expression
bodyArgumentExpr = removeParentheses(from: bodyArgumentExpr) ?? bodyArgumentExpr
// Before building the macro expansion, look for any problems and return
// early if found.
guard _diagnoseIssues(with: macro, body: bodyArgumentExpr, in: context) else {
if Self.isThrowing {
return #"{ () async throws -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
} else {
return #"{ () async -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
}
}
// Find any captured values and extract them from the trailing closure.
var capturedValues = [CapturedValueInfo]()
if var closureExpr = bodyArgumentExpr.as(ClosureExprSyntax.self),
let captureList = closureExpr.signature?.capture?.items {
closureExpr.signature?.capture = ClosureCaptureClauseSyntax(items: [], trailingTrivia: .space)
capturedValues = captureList.map { CapturedValueInfo($0, in: context) }
bodyArgumentExpr = ExprSyntax(closureExpr)
}
// Generate a unique identifier for this exit test.
let idExpr = _makeExitTestIDExpr(for: macro, in: context)
var decls = [DeclSyntax]()
// Implement the body of the exit test outside the enum we're declaring so
// that `Self` resolves to the type containing the exit test, not the enum.
let bodyThunkName = context.makeUniqueName("")
let bodyThunkParameterList = FunctionParameterListSyntax {
for capturedValue in capturedValues {
FunctionParameterSyntax(
firstName: .wildcardToken(trailingTrivia: .space),
secondName: capturedValue.name.trimmed,
colon: .colonToken(trailingTrivia: .space),
type: capturedValue.type.trimmed
)
}
}
decls.append(
"""
@Sendable func \(bodyThunkName)(\(bodyThunkParameterList)) async throws {
_ = \(applyEffectfulKeywords([.try, .await, .unsafe], to: bodyArgumentExpr))()
}
"""
)
// Create a local type that can be discovered at runtime and which contains
// the exit test body.
let enumName = context.makeUniqueName("")
do {
// Create the test content record.
let testContentRecordDecl = makeTestContentRecordDecl(
named: .identifier("testContentRecord"),
in: TypeSyntax(IdentifierTypeSyntax(name: enumName)),
ofKind: .exitTest,
accessingWith: .identifier("accessor")
)
// Create another local type for legacy test discovery.
var recordDecl: DeclSyntax?
#if !SWT_NO_LEGACY_TEST_DISCOVERY
let legacyEnumName = context.makeUniqueName("__🟡$")
let unsafeKeyword: TokenSyntax? = isUnsafeKeywordSupported ? .keyword(.unsafe, trailingTrivia: .space) : nil
recordDecl = """
enum \(legacyEnumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(unsafeKeyword)\(enumName).testContentRecord
}
}
"""
#endif
decls.append(
"""
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName) {
private nonisolated static let accessor: Testing.__TestContentRecordAccessor = { outValue, type, hint, _ in
Testing.ExitTest.__store(
\(idExpr),
\(bodyThunkName),
into: outValue,
asTypeAt: type,
withHintAt: hint
)
}
\(testContentRecordDecl)
\(recordDecl)
}
"""
)
}
arguments[trailingClosureIndex].expression = ExprSyntax(
ClosureExprSyntax {
for decl in decls {
CodeBlockItemSyntax(
leadingTrivia: .newline,
item: .decl(decl),
trailingTrivia: .newline
)
}
}
)
// Insert additional arguments at the beginning of the argument list. Note
// that this will invalidate all indices into `arguments`!
var leadingArguments = [
Argument(label: "identifiedBy", expression: idExpr),
]
#if SWT_FIXED_154221449
if !capturedValues.isEmpty {
leadingArguments.append(
Argument(
label: "encodingCapturedValues",
expression: TupleExprSyntax {
for capturedValue in capturedValues {
LabeledExprSyntax(expression: capturedValue.typeCheckedExpression)
}
}
)
)
}
#else
if let firstCapturedValue = capturedValues.first {
leadingArguments.append(
Argument(
label: "encodingCapturedValues",
expression: firstCapturedValue.typeCheckedExpression
)
)
leadingArguments += capturedValues.dropFirst()
.map(\.typeCheckedExpression)
.map { Argument(expression: $0) }
}
#endif
arguments = leadingArguments + arguments
// Replace the exit test body (as an argument to the macro) with a stub
// closure that hosts the type we created above.
var macro = macro
macro.arguments = LabeledExprListSyntax(arguments)
macro.trailingClosure = nil
macro.additionalTrailingClosures = MultipleTrailingClosureElementListSyntax()
return try Base.expansion(of: macro, primaryExpression: bodyArgumentExpr, in: context)
}
/// Make an expression representing an exit test ID that can be passed to the
/// `ExitTest.__store()` function at runtime.
///
/// - Parameters:
/// - macro: The exit test macro being inspected.
/// - context: The macro context in which the expression is being parsed.
///
/// - Returns: An expression representing the exit test's unique ID.
private static func _makeExitTestIDExpr(
for macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
withUnsafeTemporaryAllocation(of: UInt64.self, capacity: 4) { exitTestID in
if let sourceLocation = context.location(of: macro, at: .afterLeadingTrivia, filePathMode: .fileID),
let fileID = sourceLocation.file.as(StringLiteralExprSyntax.self)?.representedLiteralValue,
let line = sourceLocation.line.as(IntegerLiteralExprSyntax.self)?.representedLiteralValue,
let column = sourceLocation.column.as(IntegerLiteralExprSyntax.self)?.representedLiteralValue {
// Hash the entire source location and store the entire hash in the
// resulting ID.
let stringValue = "\(fileID):\(line):\(column)"
exitTestID.withMemoryRebound(to: UInt8.self) { exitTestID in
_ = exitTestID.initialize(from: SHA256.hash(stringValue.utf8))
}
} else {
// This branch is dead code in production, but is used when we expand a
// macro in our own unit tests because the macro expansion context does
// not have real source location information.
for i in 0 ..< exitTestID.count {
exitTestID[i] = .random(in: 0 ... .max)
}
}
// Return a tuple of integer literals (which is what the runtime __store()
// function is expecting.)
let tupleExpr = TupleExprSyntax {
for uint64 in exitTestID {
LabeledExprSyntax(expression: IntegerLiteralExprSyntax(uint64, radix: .hex))
}
}
return ExprSyntax(tupleExpr)
}
}
/// Diagnose issues with an exit test macro call.
///
/// - Parameters:
/// - macro: The exit test macro call.
/// - bodyArgumentExpr: The exit test's body.
/// - context: The macro context in which the expression is being parsed.
///
/// - Returns: Whether or not macro expansion should continue (i.e. stopping
/// if a fatal error was diagnosed.)
private static func _diagnoseIssues(
with macro: some FreestandingMacroExpansionSyntax,
body bodyArgumentExpr: ExprSyntax,
in context: some MacroExpansionContext
) -> Bool {
var diagnostics = [DiagnosticMessage]()
if let closureExpr = bodyArgumentExpr.as(ClosureExprSyntax.self),
let captureClause = closureExpr.signature?.capture,
!captureClause.items.isEmpty {
// Disallow capture lists if the experimental feature is not enabled.
if !ExitTestExpectMacro.isValueCapturingEnabled {
diagnostics.append(.captureClauseUnsupported(captureClause, in: closureExpr, inExitTest: macro))
}
}
// Disallow exit tests in generic types and functions as they cannot be
// correctly expanded due to the use of a nested type with static members.
for lexicalContext in context.lexicalContext {
if let lexicalContext = lexicalContext.asProtocol((any WithGenericParametersSyntax).self) {
if let genericClause = lexicalContext.genericParameterClause {
diagnostics.append(.expressionMacroUnsupported(macro, inGenericContextBecauseOf: genericClause, on: lexicalContext))
} else if let whereClause = lexicalContext.genericWhereClause {
diagnostics.append(.expressionMacroUnsupported(macro, inGenericContextBecauseOf: whereClause, on: lexicalContext))
} else if let functionDecl = lexicalContext.as(FunctionDeclSyntax.self) {
for parameter in functionDecl.signature.parameterClause.parameters {
if parameter.type.isSome {
diagnostics.append(.expressionMacroUnsupported(macro, inGenericContextBecauseOf: parameter, on: functionDecl))
}
}
}
}
}
for diagnostic in diagnostics {
context.diagnose(diagnostic)
}
return diagnostics.isEmpty
}
}
extension ExitTestExpectMacro {
/// Whether or not experimental value capturing via explicit capture lists is
/// enabled.
///
/// This member is declared on ``ExitTestExpectMacro`` but also applies to
/// ``ExitTestRequireMacro``.
@TaskLocal
static var isValueCapturingEnabled: Bool = {
#if ExperimentalExitTestValueCapture
return true
#else
return false
#endif
}()
}
/// A type describing the expansion of the `#expect(processExitsWith:)` macro.
///
/// This type checks for nested invocations of `#expect()` and `#require()` and
/// diagnoses them as unsupported. It is otherwise exactly equivalent to
/// ``ExpectMacro``.
public struct ExitTestExpectMacro: ExitTestConditionMacro {
public typealias Base = ExpectMacro
}
/// A type describing the expansion of the `#require(processExitsWith:)` macro.
///
/// This type checks for nested invocations of `#expect()` and `#require()` and
/// diagnoses them as unsupported. It is otherwise exactly equivalent to
/// ``RequireMacro``.
public struct ExitTestRequireMacro: ExitTestConditionMacro {
public typealias Base = RequireMacro
}
|