Skip to content

Commit 93fd967

Browse files
authored
[6.2] Disallow the @Test attribute on operator declarations. (#1207)
- **Explanation**: Block `@Test` on operator declarations, which doesn't work correctly and was never supported. - **Scope**: Attempting to declare `@Test func +()`. - **Issues**: #1204 - **Original PRs**: #1205 - **Risk**: Low (this was never supported) - **Testing**: New test case added. - **Reviewers**: @stmontgomery @briancroom
1 parent 754c808 commit 93fd967

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)