Skip to content

Commit

Permalink
Make sure this works with the February 2, 2023 snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
DougGregor committed Feb 3, 2023
1 parent 94de859 commit 9ffd286
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
33 changes: 13 additions & 20 deletions MacroExamples/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,29 @@ let y = 2
let z = 3

// "Stringify" macro turns the expression into a string.
func testStringify() {
print(#stringify(x + y))
}
print(#stringify(x + y))

// "AddBlocker" complains about addition operations. We emit a warning
// so it doesn't block compilation.
func blockAdd() {
print(#addBlocker(x * y + z))
}
print(#addBlocker(x * y + z))

func warningAndError() {
#myWarning("remember to pass a string literal here")
#myWarning("remember to pass a string literal here")

// Uncomment to get an error out of the macro.
// let text = "oops"
// #myWarning(text)
}
// Uncomment to get an error out of the macro.
// let text = "oops"
// #myWarning(text)

struct Font: ExpressibleByFontLiteral {
init(fontLiteralName: String, size: Int, weight: MacroExamplesLib.FontWeight) {
}
}

func testFontLiteral() {
let _: Font = #fontLiteral(name: "Comic Sans", size: 14, weight: .thin)
}

testStringify()
blockAdd()
warningAndError()
testFontLiteral()
let _: Font = #fontLiteral(name: "Comic Sans", size: 14, weight: .thin)

// TODO: February 2, 2023 snapshot toolchain cannot handle this, so disable it
// for now. The bug has been fixed on main and should be available in the next
// snapshot.
#if false
// Use the "wrapStoredProperties" macro to deprecate all of the stored
// properties.
@wrapStoredProperties(#"available(*, deprecated, message: "hands off my data")"#)
Expand All @@ -47,6 +39,7 @@ struct OldStorage {
// The deprecation warning below comes from the deprecation attribute
// introduced by @wrapStoredProperties on OldStorage.
_ = OldStorage(x: 5).x
#endif

// Move the storage from each of the stored properties into a dictionary
// called `_storage`, turning the stored properties into computed properties.
Expand Down
1 change: 0 additions & 1 deletion MacroExamplesLib/Macros.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/// "Stringify" the provided value and produce a tuple that includes both the
/// original value as well as the source code that generated it.
@freestanding(expression) public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesPlugin", type: "StringifyMacro")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ There is an active effort to introduce [macros](https://forums.swift.org/t/a-pos

Macros are an experimental feature, so you will need a custom Swift toolchain and some extra compiler flags. The Xcode project in this repository is a good starting point. To use it:

1. Download a [development snapshot](https://www.swift.org/download/#snapshots) of the compiler from Swift.org from January 1, 2022 or later. At present, we only have these working on macOS, but are working to get other platforms working with other build systems.
1. Download a [development snapshot](https://www.swift.org/download/#snapshots) of the compiler from Swift.org from February 2, 2023 or later. At present, we only have these working on macOS, but are working to get other platforms working with other build systems.
2. Open the project `MacroExamples.xcodeproj` in Xcode.
3. Go to the Xcode -> Toolchains menu and select the development toolchain you downloaded.
4. Make sure the `MacroExamples` scheme is selected, then build and run! If the first build fails, build again--there's something funky going on with the dependencies.
Expand Down

0 comments on commit 9ffd286

Please sign in to comment.