Files
swift-mirror/test/Macros/macro_expand_conformances.swift
Hamish Knight 59a5c48161 [AST] Handle extension macros in getTopLevelDeclsWithAuxiliaryDecls
Rather than placing extension macros in a synthesized file unit,
track them separately `getTopLevelDeclsWithAuxiliaryDecls`. This
ensures that they are placed in the same `IRGenModule` as the nominal
being extended. This requires fixing up some places that weren't
previously using `getTopLevelDeclsWithAuxiliaryDecls`. This also
required introducing `shouldWalkTopLevelAuxiliaryDecls` to ASTWalker,
which is an unfortunate hack due to the fact that many ASTWalkers
currently have misconfigured macro-walking preferences.

rdar://185645710
2026-09-12 14:50:41 +01:00

149 lines
5.3 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-swift-frontend -swift-version 5 -emit-module -o %t/ModuleWithEquatable.swiftmodule %s -DMODULE_EXPORTING_TYPE -module-name ModuleWithEquatable -load-plugin-library %t/%target-library-name(MacroDefinition) -emit-module-interface-path %t/ModuleWithEquatable.swiftinterface
// Check the generated .swiftinterface
// RUN: %FileCheck -check-prefix INTERFACE %s < %t/ModuleWithEquatable.swiftinterface
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) %s -I %t -disable-availability-checking -dump-macro-expansions > %t/expansions-dump.txt 2>&1
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 -I %t
// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -swift-version 5 -emit-tbd -emit-tbd-path %t/MacroUser.tbd -I %t
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
// Regression test for sourcekit-lsp #2696: lazy typechecking + skip-all-function-bodies
//
// The SwiftPM `--experimental-prepare-for-indexing` combo) must not drop
// macro-synthesized extension conformances from the produced .swiftmodule.
// RUN: %empty-directory(%t/lazy)
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/lazy/ModuleWithEquatable.swiftmodule %s -DMODULE_EXPORTING_TYPE -module-name ModuleWithEquatable -load-plugin-library %t/%target-library-name(MacroDefinition) -experimental-lazy-typecheck -experimental-skip-all-function-bodies
// RUN: %target-swift-ide-test -print-module -module-to-print=ModuleWithEquatable -source-filename %s -I %t/lazy -load-plugin-library %t/%target-library-name(MacroDefinition) | %FileCheck -check-prefix CHECK-LAZY %s
// CHECK-LAZY: struct Outer
// CHECK-LAZY: struct Generated
// CHECK-LAZY: extension Outer.Generated : MyProtocol
// CHECK-LAZY: struct PublicEquatable
// CHECK-LAZY: extension PublicEquatable : Equatable
// CHECK-LAZY: extension PublicEquatable.Inner : Equatable
#if TEST_DIAGNOSTICS
@attached(conformance) // expected-error{{conformance macros are replaced by extension macros}}
macro InvalidEquatable() = #externalMacro(module: "MacroDefinition", type: "EquatableMacro")
#endif
@attached(extension, conformances: Equatable)
macro Equatable() = #externalMacro(module: "MacroDefinition", type: "EquatableMacro")
@attached(extension, conformances: Hashable)
macro Hashable() = #externalMacro(module: "MacroDefinition", type: "HashableMacro")
#if MODULE_EXPORTING_TYPE
@Equatable
public struct PublicEquatable {
public init() { }
}
extension PublicEquatable {
@Equatable
public struct Inner {
public init() { }
}
}
public protocol MyProtocol {}
@attached(extension, conformances: MyProtocol)
macro AddMyProtocol() = #externalMacro(module: "MacroDefinition", type: "ConformanceViaExtensionMacro")
@attached(member, names: named(Generated))
macro AddGeneratedMember() = #externalMacro(module: "MacroDefinition", type: "ConformingMemberStructMacro")
@AddGeneratedMember
public struct Outer {
public init() {}
}
// INTERFACE-NOT: @Equatable
// INTERFACE: public struct PublicEquatable
// INTERFACE: extension ModuleWithEquatable::PublicEquatable : Swift::Equatable
// INTERFACE: extension ModuleWithEquatable::PublicEquatable.ModuleWithEquatable::Inner : Swift::Equatable
#else
import ModuleWithEquatable
func requireEquatable(_ value: some Equatable) {
print(value == value)
}
// expected-note@+1{{where 'some Hashable' = 'PublicEquatable'}}
func requireHashable(_ value: some Hashable) {
print(value.hashValue)
}
@Equatable
struct S {}
@Hashable
struct S2 {}
enum E {
@Equatable struct Nested {}
}
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1S9EquatablefMe_.swift
// CHECK-DUMP: extension S: Equatable {
// CHECK-DUMP: }
// CHECK: true
requireEquatable(S())
requireEquatable(S2())
requireHashable(S2())
requireEquatable(E.Nested())
extension E {
@Equatable struct NestedInExtension {}
}
requireEquatable(E.NestedInExtension())
#if TEST_DIAGNOSTICS
requireEquatable(PublicEquatable())
requireHashable(PublicEquatable())
//expected-error@-1{{global function 'requireHashable' requires that 'PublicEquatable' conform to 'Hashable'}}
#endif
@attached(extension, conformances: P)
@attached(member, names: named(requirement))
macro DelegatedConformance() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceMacro")
protocol P {
static func requirement()
}
struct Wrapped: P {
static func requirement() {
print("Wrapped.requirement")
}
}
@DelegatedConformance
struct Generic<Element> {}
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances7Generic20DelegatedConformancefMe_.swift
// CHECK-DUMP: extension Generic: P where Element: P {
// CHECK-DUMP: }
func requiresP(_ value: (some P).Type) {
value.requirement()
}
// CHECK: Wrapped.requirement
requiresP(Generic<Wrapped>.self)
#endif