Files
swift-mirror/test/attr/diagnose_peer_macro.swift
Artem Chikin 740800dd21 [SourceWarningControl] Promote '@diagnose' attribute to a default language feature
No longer experimental as of approval of SE-0522.

Resolves rdar://176535491
2026-05-08 09:59:11 +01:00

80 lines
2.4 KiB
Swift

// REQUIRES: swift_swift_parser, executable_test
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// Build the macro library
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %t/macro.swift
// Type check and verify
// RUN: %target-swift-frontend -typecheck -verify -load-plugin-library %t/%target-library-name(MacroDefinition) %t/test.swift
// This test verifies that @diagnose on a declaration does NOT propagate to peer
// declarations generated by an attached peer macro, because peers are
// independent siblings, not children of the annotated declaration's scope.
//--- macro.swift
import SwiftSyntax
import SwiftSyntaxMacros
public struct AddCompanionMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return [
"""
func companion() -> String {
return bar()
}
"""
]
}
}
/// A companion function with its own @diagnose.
public struct AddSuppressedCompanionMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return [
"""
@diagnose(DeprecatedDeclaration, as: ignored)
func suppressed() -> String {
return bar()
}
"""
]
}
}
//--- test.swift
@available(*, deprecated)
func bar() -> String { return "GoodBye" }
@attached(peer, names: named(companion))
macro AddCompanion() = #externalMacro(module: "MacroDefinition", type: "AddCompanionMacro")
@attached(peer, names: named(suppressed))
macro AddSuppressedCompanion() = #externalMacro(module: "MacroDefinition", type: "AddSuppressedCompanionMacro")
struct TestPeerMacro {
@diagnose(DeprecatedDeclaration, as: ignored)
@AddCompanion // expected-note {{in expansion of macro 'AddCompanion' on instance method 'original()' here}}
func original() -> String {
return bar() // no warning — suppressed by @diagnose on this declaration
}
// expected-warning@@__swiftmacro_4test13TestPeerMacroV8original12AddCompanionfMp_.swift:2:10{{'bar()' is deprecated}}
}
struct TestPeerMacroSuppressed {
@AddSuppressedCompanion
func another() -> String {
return bar() // expected-warning {{'bar()' is deprecated}}
}
}