Skip to content

Commit 5fd38df

Browse files
authored
Disallow the @Test attribute on operator declarations. (#1205)
This PR prevents `@Test` from being applied to an operator declaration such as: ```swift @test(arguments: ...) static func +(lhs: A, rhs: B) { ... } ``` Now, the following error will be emitted by the compiler: > 🛑 Attribute 'Test' cannot be applied to an operator Previously, applying `@Test` to an operator produced undefined/unstable effects. Resolves #1204. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 3c64d2b commit 5fd38df

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

Sources/TestingMacros/Support/Additions/FunctionDeclSyntaxAdditions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ extension FunctionDeclSyntax {
3434
.contains(.keyword(.nonisolated))
3535
}
3636

37+
/// Whether or not this function declares an operator.
38+
var isOperator: Bool {
39+
switch name.tokenKind {
40+
case .binaryOperator, .prefixOperator, .postfixOperator:
41+
true
42+
default:
43+
false
44+
}
45+
}
46+
3747
/// The name of this function including parentheses, parameter labels, and
3848
/// colons.
3949
var completeName: DeclReferenceExprSyntax {

Sources/TestingMacros/Support/DiagnosticMessage.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ struct DiagnosticMessage: SwiftDiagnostics.DiagnosticMessage {
9393
let result: (value: String, article: String)
9494
switch node.kind {
9595
case .functionDecl:
96-
result = ("function", "a")
96+
if node.cast(FunctionDeclSyntax.self).isOperator {
97+
result = ("operator", "an")
98+
} else {
99+
result = ("function", "a")
100+
}
97101
case .classDecl:
98102
result = ("class", "a")
99103
case .structDecl:

Sources/TestingMacros/TestDeclarationMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
6161
}
6262

6363
// The @Test attribute is only supported on function declarations.
64-
guard let function = declaration.as(FunctionDeclSyntax.self) else {
64+
guard let function = declaration.as(FunctionDeclSyntax.self), !function.isOperator else {
6565
diagnostics.append(.attributeNotSupported(testAttribute, on: declaration))
6666
return false
6767
}

Tests/TestingMacrosTests/TestDeclarationMacroTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ struct TestDeclarationMacroTests {
6767
"Attribute 'Test' cannot be applied to a structure",
6868
"@Test enum E {}":
6969
"Attribute 'Test' cannot be applied to an enumeration",
70+
"@Test func +() {}":
71+
"Attribute 'Test' cannot be applied to an operator",
7072
7173
// Availability
7274
"@available(*, unavailable) @Suite struct S {}":

0 commit comments

Comments
 (0)