Skip to content
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
30 changes: 0 additions & 30 deletions stdlib/public/core/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,6 @@ public macro column<T: ExpressibleByIntegerLiteral>() -> T =
@freestanding(expression)
public macro dsohandle() -> UnsafeRawPointer = Builtin.DSOHandleMacro

/// Create an option set from a struct that contains a nested `Options` enum.
///
/// Attach this macro to a struct that contains a nested `Options` enum
/// with an integer raw value. The struct will be transformed to conform to
/// `OptionSet` by
/// 1. Introducing a `rawValue` stored property to track which options are set,
/// along with the necessary `RawType` typealias and initializers to satisfy
/// the `OptionSet` protocol. The raw type is specified after `@OptionSet`,
/// e.g., `@OptionSet<UInt8>`.
/// 2. Introducing static properties for each of the cases within the `Options`
/// enum, of the type of the struct.
///
/// The `Options` enum must have a raw value, where its case elements
/// each indicate a different option in the resulting option set. For example,
/// the struct and its nested `Options` enum could look like this:
///
/// @OptionSet<UInt8>
/// struct ShippingOptions {
/// private enum Options: Int {
/// case nextDay
/// case secondDay
/// case priority
/// case standard
/// }
/// }
@attached(member, names: named(RawValue), named(rawValue), named(`init`), arbitrary)
@attached(conformance)
public macro OptionSet<RawType>() =
#externalMacro(module: "SwiftMacros", type: "OptionSetMacro")

/// Produce the given warning message during compilation.
@freestanding(declaration)
public macro warning(_ message: String) = Builtin.WarningMacro
Expand Down
5 changes: 5 additions & 0 deletions test/IDE/complete_optionset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func foo() {
ShippingOptions.#^MEMBER_STATIC^#
}

@attached(member, names: named(RawValue), named(rawValue), named(`init`), arbitrary)
@attached(conformance)
public macro OptionSet<RawType>() =
#externalMacro(module: "SwiftMacros", type: "OptionSetMacro")

// MEMBER_STATIC: Keyword[self]/CurrNominal: self[#ShippingOptions.Type#]; name=self
// MEMBER_STATIC: Decl[TypeAlias]/CurrNominal: RawValue[#UInt8#]; name=RawValue
// MEMBER_STATIC: Decl[Constructor]/CurrNominal: init({#rawValue: ShippingOptions.RawValue#})[#ShippingOptions#]; name=init(rawValue:)
Expand Down
5 changes: 5 additions & 0 deletions test/Macros/option_set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import Swift

@attached(member, names: named(RawValue), named(rawValue), named(`init`), arbitrary)
@attached(conformance)
public macro OptionSet<RawType>() =
#externalMacro(module: "SwiftMacros", type: "OptionSetMacro")

@OptionSet<UInt8>
struct ShippingOptions {
private enum Options: Int {
Expand Down
5 changes: 5 additions & 0 deletions test/SourceKit/Macros/macro_option_set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func test(opts: ShippingOptions) {
let _ = ShippingOptions.nextDay
}

@attached(member, names: named(RawValue), named(rawValue), named(`init`), arbitrary)
@attached(conformance)
public macro OptionSet<RawType>() =
#externalMacro(module: "SwiftMacros", type: "OptionSetMacro")

// REQUIRES: swift_swift_parser

// RUN: %empty-directory(%t)
Expand Down