Skip to content

Add a test for a macro introducing members to an enum #64114

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 1 commit into from
Mar 5, 2023
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
14 changes: 14 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,17 @@ public struct DelegatedConformanceMacro: ConformanceMacro, MemberMacro {
return [requirement]
}
}

public struct ExtendableEnum: MemberMacro {
public static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
let unknownDecl: DeclSyntax =
"""
func unknown() -> Int { 34 } // or something like: `case unknown`
"""
return [unknownDecl]
}
}
11 changes: 11 additions & 0 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ func testNestedDeclInExpr() {
@freestanding(declaration, names: named(A), named(B), named(foo), named(addOne))
macro defineDeclsWithKnownNames() = #externalMacro(module: "MacroDefinition", type: "DefineDeclsWithKnownNamesMacro")

// 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())

// FIXME: Declaration macro expansions in BraceStmt don't work yet.
//#bitwidthNumberedStructs("MyIntGlobal")

Expand Down