mirror of
https://github.com/apple/swift.git
synced 2026-10-08 12:50:50 +02:00
80 lines
2.4 KiB
Swift
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}}
|
|
}
|
|
}
|