Files
swift-mirror/test/Macros/macro_expand_synthesized_members.swift
Ben Barham 18cb5f4f3d [Diagnostics] Update in_macro_expansion note for attached macros
Use the attached atttribute's location as the location of the macro,
rather than the location of the declaration it's attached to. Also add
the kind and name of that declaration to the note itself.
2023-07-25 16:03:52 -07:00

115 lines
3.0 KiB
Swift

// REQUIRES: swift_swift_parser, executable_test
//
// RUN: %empty-directory(%t)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 -verify-ignore-unknown
// RUN: %target-build-swift -g -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -swift-version 5
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
@attached(
member,
names: named(init), named(Storage), named(storage), named(getStorage()), named(method), named(init(other:))
)
macro addMembers() = #externalMacro(module: "MacroDefinition", type: "AddMembers")
@attached(
member,
names: named(`init`), named(Storage), named(storage), named(getStorage()), named(method)
)
macro addMembersQuotedInit() = #externalMacro(module: "MacroDefinition", type: "AddMembers")
#if TEST_DIAGNOSTICS
@addMembers
import Swift
// expected-error@-2 {{'member' macro cannot be attached to import}}
#endif
@addMembers
struct S {
func useSynthesized() {
S.method()
print(type(of: getStorage()))
}
}
@attached(
member,
names: named(extInstanceMethod), named(extStaticMethod)
)
macro addExtMembers() = #externalMacro(module: "MacroDefinition", type: "AddExtMembers")
@addExtMembers
extension S { }
let s = S()
// CHECK: synthesized method
// CHECK: Storage
s.useSynthesized()
// Members added via extension.
s.extInstanceMethod()
S.extStaticMethod()
@attached(member, names: arbitrary)
macro addArbitraryMembers() = #externalMacro(module: "MacroDefinition", type: "AddArbitraryMembers")
@addArbitraryMembers
struct MyType {}
// CHECK: MyType1
// CHECK: MyType2
// CHECK: MyType3
print(MyType.MyType1.self)
print(MyType.MyType2.self)
print(MyType.MyType3.self)
@attached(
member,
names: named(RawValue), named(rawValue), named(init)
)
public macro NewType<T>() = #externalMacro(module: "MacroDefinition", type: "NewTypeMacro")
@NewType<String>
public struct MyString {}
// CHECK: String
// CHECK: hello
let myString = MyString("hello")
print(MyString.RawValue.self)
print(myString.rawValue)
struct Base {
static func member() -> Base { .init() }
}
@attached(member) macro empty(
_ : Base
) = #externalMacro(module: "MacroDefinition", type: "EmptyMacro")
@empty(.member())
struct TestMacroTypechecking {}
// Macros adding to an enum
@attached(member, names: named(unknown), arbitrary)
public macro ExtendableEnum() = #externalMacro(module: "MacroDefinition", type: "ExtendableEnum")
@ExtendableEnum
enum ElementType {
case paper
}
print(ElementType.paper.unknown())
#if TEST_DIAGNOSTICS
@addMembersQuotedInit
struct S2 {
// expected-note@-2 {{in expansion of macro 'addMembersQuotedInit' on struct 'S2' here}}
func useSynthesized() {
S.method()
print(type(of: getStorage()))
}
}
#endif