Skip to content

Merge 'main' branch to 'release/6.2' #1095

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

Merged
merged 6 commits into from
Apr 24, 2025
Merged
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
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
#

* @stmontgomery @grynspan @briancroom @SeanROlszewski @suzannaratcliff
* @stmontgomery @grynspan @briancroom @suzannaratcliff
5 changes: 3 additions & 2 deletions Documentation/ABI/TestContent.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ record's kind is a 32-bit unsigned value. The following kinds are defined:
| `0x00000000` | – | Reserved (**do not use**) |
| `0x74657374` | `'test'` | Test or suite declaration |
| `0x65786974` | `'exit'` | Exit test |
| `0x706c6179` | `'play'` | [Playground](https://github.com/apple/swift-play-experimental) |

<!-- When adding cases to this enumeration, be sure to also update the
corresponding enumeration in TestContentGeneration.swift. -->
<!-- The kind values listed in this table should be a superset of the cases in
the `TestContentKind` enumeration. -->

If a test content record's `kind` field equals `0x00000000`, the values of all
other fields in that record are undefined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ extension Event.ConsoleOutputRecorder {
// text instead of just the symbol. Details may be multi-line messages,
// so split the message on newlines and indent all lines to align them
// to the indentation provided by the symbol.
var lines = message.stringValue.split(whereSeparator: \.isNewline)
var lines = message.stringValue.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
lines = CollectionOfOne(lines[0]) + lines.dropFirst().map { line in
"\(padding) \(line)"
}
Expand Down
11 changes: 10 additions & 1 deletion Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,17 @@ extension ConditionMacro {
expandedFunctionName = conditionArgument.expandedFunctionName
}

// Capture any comments as well (either in source or as a macro argument.)
// 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

// MARK: - Finding effect keywords
// MARK: - Finding effect keywords and expressions

/// A syntax visitor class that looks for effectful keywords in a given
/// expression.
Expand Down Expand Up @@ -69,6 +69,21 @@ func findEffectKeywords(in node: some SyntaxProtocol, context: some MacroExpansi
return effectFinder.effectKeywords
}

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

/// Make a function call expression to an effectful thunk function provided by
Expand Down Expand Up @@ -111,7 +126,13 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp

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

// The 'unsafe' keyword was introduced in 6.2 as part of SE-0458. Older
// toolchains are not aware of it, so avoid emitting expressions involving
// that keyword when the macro has been built using an older toolchain.
#if compiler(>=6.2)
let needUnsafe = effectfulKeywords.contains(.unsafe) && !expr.is(UnsafeExprSyntax.self)
#endif

// First, add thunk function calls.
if needAwait {
Expand All @@ -120,9 +141,11 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
if needTry {
expr = _makeCallToEffectfulThunk(.identifier("__requiringTry"), passing: expr)
}
#if compiler(>=6.2)
if needUnsafe {
expr = _makeCallToEffectfulThunk(.identifier("__requiringUnsafe"), passing: expr)
}
#endif

// 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
Expand All @@ -143,6 +166,7 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
)
)
}
#if compiler(>=6.2)
if needUnsafe {
expr = ExprSyntax(
UnsafeExprSyntax(
Expand All @@ -151,6 +175,7 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
)
)
}
#endif

expr.leadingTrivia = originalExpr.leadingTrivia
expr.trailingTrivia = originalExpr.trailingTrivia
Expand Down
53 changes: 53 additions & 0 deletions Tests/TestingMacrosTests/ConditionMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,59 @@ struct ConditionMacroTests {
// Capture me
Testing.__checkValue(try x(), expression: .__fromSyntaxNode("try x()"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
""",

"""
// Capture me
try #expect(x)
""":
"""
// Capture me
try Testing.__checkValue(x, expression: .__fromSyntaxNode("x"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
""",

"""
// Capture me
await #expect(x)
""":
"""
// Capture me
await Testing.__checkValue(x, expression: .__fromSyntaxNode("x"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
""",

"""
// Ignore me

// Comment for try
try
// Comment for await
await
// Comment for expect
#expect(x)
""":
"""
// Comment for try
try
// Comment for await
await
// Comment for expect
Testing.__checkValue(x, expression: .__fromSyntaxNode("x"), comments: [.__line("// Comment for try"), .__line("// Comment for await"), .__line("// Comment for expect")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
""",

"""
// Ignore me
func example() {
// Capture me
#expect(x())
}
""":
"""
func example() {
// Capture me
Testing.__checkFunctionCall((), calling: { _ in
x()
}, expression: .__fromFunctionCall(nil, "x"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
}
""",
]
)
func commentCapture(input: String, expectedOutput: String) throws {
Expand Down
24 changes: 24 additions & 0 deletions Tests/TestingTests/EventRecorderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ struct EventRecorderTests {
}
#endif

@Test(
"Uncommonly-formatted comments",
.bug("rdar://149482060"),
arguments: [
"", // Empty string
"\n\n\n", // Only newlines
"\nFoo\n\nBar\n\n\nBaz\n", // Newlines interspersed with non-empty strings
]
)
func uncommonComments(text: String) async throws {
let stream = Stream()

var configuration = Configuration()
configuration.eventHandlingOptions.isWarningIssueRecordedEventEnabled = true
let eventRecorder = Event.ConsoleOutputRecorder(writingUsing: stream.write)
configuration.eventHandler = { event, context in
eventRecorder.record(event, in: context)
}

await Test {
Issue.record(Comment(rawValue: text) /* empty */)
}.run(configuration: configuration)
}

@available(_regexAPI, *)
@Test("Issue counts are omitted on a successful test")
func issueCountOmittedForPassingTest() async throws {
Expand Down