Skip to content

[6.2] Disallow the @Test attribute on operator declarations. #1207

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ extension FunctionDeclSyntax {
.contains(.keyword(.nonisolated))
}

/// Whether or not this function declares an operator.
var isOperator: Bool {
switch name.tokenKind {
case .binaryOperator, .prefixOperator, .postfixOperator:
true
default:
false
}
}

/// The name of this function including parentheses, parameter labels, and
/// colons.
var completeName: DeclReferenceExprSyntax {
Expand Down
6 changes: 5 additions & 1 deletion Sources/TestingMacros/Support/DiagnosticMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ struct DiagnosticMessage: SwiftDiagnostics.DiagnosticMessage {
let result: (value: String, article: String)
switch node.kind {
case .functionDecl:
result = ("function", "a")
if node.cast(FunctionDeclSyntax.self).isOperator {
result = ("operator", "an")
} else {
result = ("function", "a")
}
case .classDecl:
result = ("class", "a")
case .structDecl:
Expand Down
2 changes: 1 addition & 1 deletion Sources/TestingMacros/TestDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
}

// The @Test attribute is only supported on function declarations.
guard let function = declaration.as(FunctionDeclSyntax.self) else {
guard let function = declaration.as(FunctionDeclSyntax.self), !function.isOperator else {
diagnostics.append(.attributeNotSupported(testAttribute, on: declaration))
return false
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/TestingMacrosTests/TestDeclarationMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ struct TestDeclarationMacroTests {
"Attribute 'Test' cannot be applied to a structure",
"@Test enum E {}":
"Attribute 'Test' cannot be applied to an enumeration",
"@Test func +() {}":
"Attribute 'Test' cannot be applied to an operator",

// Availability
"@available(*, unavailable) @Suite struct S {}":
Expand Down