SE-0382: Expression Macros

having worked with SwiftSyntax extensively over the summer, i’ve learned that the version of SwiftSyntax you are using is quite important, and getting it right is more, not less difficult than getting normal build dependency resolution right. especially if you are distributing libraries and therefore (likely) targeting multiple toolchain versions per release.

i won’t comment on the pros or cons of macros themselves, but i really do not think it is a good idea to start exposing this feature until the SwiftSyntax versioning story is more fleshed out.

5 Likes

I strongly dislike Codable and Result Builders, and part of that dislike is that they remind me of early-2000s boost libraries that found "clever" ways to do things which seemed like they should be impossible which resulted in utterly alien code. They're even worse than those libraries, though, because they didn't actually find a clever way to make things like CodingKeys work, and instead had compiler magic added specifically to enable their weird misuse of the language. My assumption is that the unstated context behind the macro vision document is that other SDK teams at Apple want to do similar things for their APIs, and macros are a way to address that demand without a steady stream of niche special compiler magic. If those are indeed the two options, I would certainly pick a macro system every day.

I share many of the concerns around tooling. This proposal is implicitly promoting SwiftSyntex to being part of the language, and that's a very big deal for a library which is currently unstable, undocumented, and not really realistically usable by third-parties. I don't think it makes sense to send SwiftSyntax through the evolution process (I really can't imagine it getting much in the way of productive feedback), but there does need to be some sort of story for how SwiftSyntax is going to get to a state where it's reasonable for a language feature to depend on it.

Similarly, the question of if macros will be debuggable is very relevant to the question of if macros are a good idea or not. Macros which can be stepped through in a debugger and macros which can't be are categorically different in what sorts of problems they can cause, and it's very important to design a macro system which falls into the first category. "That's an implementation question" suggests that this macro system hasn't been designed to ensure that it's debuggable, and that's a pretty big problem.

10 Likes

To clarify, whether the design of a feature is conducive to or inhibits debugging is in scope for review, and any suggestions as to changes to the design which would improve the user debugging experience would be welcome.

“I’m only in favor of this feature if it can be implemented well”—in scope for Swift Evolution.

“I’m only in favor of this feature if it will be implemented well”—not up to us.

3 Likes

Does the dependence on SwiftSyntax mean that SwiftSyntax is now a part of official language specification?

6 Likes

MacroExpansionContext:

  • What are the moduleName and fileName properties for?

  • Does the public init(moduleName:fileName:) exist only for testing?

I finally came around to working some more on the result builder macro. Here are the results:

As I see it, there are two types of result builders:

  • Type-erasing ones that always return the same type
  • Ones that derive the result type from the code structure such as SwiftUI's ViewBuilder

The feasibility of creating a macro that takes a type-erasing result builder type and a closure and produces a transformed closure has already been proven in this and this post. It is pretty simple and works as expected.

Doing the same for ViewBuilder-style result builders is more challenging. However, I have managed to implement a very rough proof-of-concept that does exactly this and works pretty well:

let very = true
let long = true

let viewClosure = #apply(resultBuilder: ViewBuilder.self) {
    Text("This")
    Text("is")
    Text("a")
    if very {
        Text("very")
    }
    if long {
        Label("long", systemImage: "arrow.left.and.right")
        Text("(really long)")
    } else {
        let shortWord = "short"
        Text(shortWord)
    }
    let lastWord = "sentence"
    Text(lastWord)
}

print(type(of: viewClosure()))
// prints TupleView<(Text, Text, Text, Optional<Text>, _ConditionalContent<TupleView<(Label<Text, Image>, Text)>, Text>, Text)>

The macro does not depend on nor is it specialized for SwiftUI or ViewBuilder (with one little caveat I will come to later). This means it should work for other result builder types. As far as I know, the macro produces the same function calls as the built-in feature.

Full implementation

In MacroExamplesLib:

@expression public macro apply<R>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> any View) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro")

In MacroExamplesPlugin:

public struct ResultBuilderMacro: ExpressionMacro {
    public static func expansion(
        of node: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
    ) throws -> ExprSyntax {
        guard
            let resultBuilderSelfExpr = node.argumentList.first?.expression.as(MemberAccessExprSyntax.self),
            let resultBuilderName = resultBuilderSelfExpr.base?.withoutTrivia().description,
            let originalClosure = node.argumentList.dropFirst().first?.expression.as(ClosureExprSyntax.self) ?? node.trailingClosure
        else {
            throw SomeError()
        }
        
        let originalStatements: [CodeBlockItemSyntax] = originalClosure.statements.map { $0.withoutTrivia() }
        return "{ () -> any View in\n\(raw: rewrittenStatements(forOriginalStatments: originalStatements, finalCallPrefix: "return ", resultBuilderName: resultBuilderName, context: &context))\n}"
    }
    
    private static func rewrittenStatements(forOriginalStatments originalStatements: [CodeBlockItemSyntax], finalCallPrefix: String, finalCallSuffix: String = "", resultBuilderName: String, context: inout MacroExpansionContext) -> String {
        var localNames: [String] = []
        var newStatements: [String] = []
        
        for statement in originalStatements {
            switch statement.item {
            case .expr(let expr):
                let localName = context.createUniqueLocalName().description
                newStatements.append("let \(localName) = \(expr);")
                localNames.append(localName)
            case .stmt(let stmt):
                let localName = context.createUniqueLocalName().description
                if let ifStmt = stmt.as(IfStmtSyntax.self) {
                    if case .codeBlock(let elseBody) = ifStmt.elseBody {
                        let tempIfName = context.createUniqueLocalName()
                        let tempElseName = context.createUniqueLocalName()
                        newStatements.append("""
                            let \(localName) = {
                                if false {
                                    \(rewrittenStatements(forOriginalStatments: ifStmt.body.statements.map { $0.withoutTrivia() }, finalCallPrefix: "let \(tempIfName) = ", resultBuilderName: resultBuilderName, context: &context))
                                    \(rewrittenStatements(forOriginalStatments: elseBody.statements.map { $0.withoutTrivia() }, finalCallPrefix: "let \(tempElseName) = ", resultBuilderName: resultBuilderName, context: &context))
                                    return true ? \(resultBuilderName).buildEither(first: \(tempIfName)) : \(resultBuilderName).buildEither(second: \(tempElseName))
                                }
                                if \(ifStmt.conditions) {
                                    \(rewrittenStatements(forOriginalStatments: ifStmt.body.statements.map { $0.withoutTrivia() }, finalCallPrefix: "return \(resultBuilderName).buildEither(first: ", finalCallSuffix: ")", resultBuilderName: resultBuilderName, context: &context))
                                } else {
                                    \(rewrittenStatements(forOriginalStatments: elseBody.statements.map { $0.withoutTrivia() }, finalCallPrefix: "return \(resultBuilderName).buildEither(second: ", finalCallSuffix: ")", resultBuilderName: resultBuilderName, context: &context))
                                }
                            }()
                        """)
                        localNames.append(localName)
                        
                    } else {
                        newStatements.append("""
                            let \(localName) = {
                                if \(ifStmt.conditions) {
                                    \(rewrittenStatements(forOriginalStatments: ifStmt.body.statements.map { $0.withoutTrivia() }, finalCallPrefix: "return \(resultBuilderName).buildIf(", finalCallSuffix: ")", resultBuilderName: resultBuilderName, context: &context))
                                }
                                return \(resultBuilderName).buildIf(nil)
                            }()
                        """)
                        localNames.append(localName)
                    }
                } else {
                    newStatements.append(stmt.description)
                }
            default:
                newStatements.append(statement.description)
            }
        }
        
        newStatements.append("\(finalCallPrefix)\(resultBuilderName).buildBlock(\(localNames.joined(separator: ", ")))\(finalCallSuffix)")
        
        let joinedStatements = newStatements.joined(separator: "\n")
        return joinedStatements
    }
}

Here are the things I have learned along the way:

  • With the built-in result builders feature, closures are able to return an opaque result type, i.e. some View. With the current implementation, macros are not allowed to return opaque result types so the macro is forced to return a closure producing any View. I suppose this is due to the fact that the macro is type-checked before being expanded. @Douglas_Gregor: Is there any chance that macros could return an opaque result type? I think this feature would be very valuable.

  • The biggest problem in the macro implementation was to satisfy the type checker when transforming if statements. This is because a macro has no way of getting the type of subexpressions of the expression passed to the macro. I work around this by putting the generated if statement into a closure to use the automatic return type inference. I am not sure if putting the code into a closure can be a problem in some situations.

    For simple if statements, this is pretty straight-forward. For ones that also have an else clause, things get pretty messy. This is because ViewBuilder-style result builders have two methods:

    public static func buildEither<TrueContent, FalseContent>(first: TrueContent) -> _ConditionalContent<TrueContent, FalseContent>
    public static func buildEither<TrueContent, FalseContent>(second: FalseContent) -> _ConditionalContent<TrueContent, FalseContent>
    

    This means the return type depends on both branches. Having no access to the type of subexpressions, the macro has to use a hack to use the automatic type inference. It looks like this in pseudo code:

    let result = {
        if false {
           ... // Code that builds result of if body
           ... // Code that builds result of else body
           return true ? ViewBuilder.buildEither(first: /*Result of if body*/)
                       : ViewBuilder.buildEither(second: /*Result of else body*/)
        }
        if long  {
            ... // Code that builds result of if body
            return ViewBuilder.buildEither(first: /*Result of if body*/)
        } else {
            ... // Code that builds result of else body
            return ViewBuilder.buildEither(second: /*Result of else body*/)
        }
    }()
    

    The if false { ... } is obviously never executed and only there to satisfy the type checker. It means that the whole code is duplicated. Of course, this is very inefficient. It could lead to problems if both if and else branch declare variables of the same name. This could probably be mitigated by renaming the variables but it feels like a brittle solution.

    Possible solutions could be:

    1. Giving macros access to type information. I think for this case, it would not be enough to provide the type of subexpressions, but to give access to the function signatures of the result builder types (via static reflection) or alternatively allow macros to query the type of arbitrary expressions.
    2. Introducing multi-statement if and case expressions that are smart enough to piece together the return type from both branches.
    3. Making type inference smart enough to infer the return type of the closure if the if false { ... } is dropped.

    The current macro implementation neither supports if statements with else ifs nor switch statements. I think that it could be extended to support them. However, the type inference hacks would probably need to get even worse.

  • If a user of the macro makes a mistake, the error produced is not so great. One example would be the error Static method 'buildBlock' requires that 'Int' conform to 'View'. The message can be helpful if the user knows a little about result builders. However, with the current implementation the error does not point to any code location. So the user must look through every line of code to find the error. I think the compiler could be improved to point to the location of the macro. However, it could not know, which subexpression is at fault.

    To provide a good error message, the macro itself would need to diagnose the error. However, to do that, again, the macro would need access to (a) the type of subexpressions and (b) static reflection of the result builder type (that includes static functions).

  • Currently, writing code using the result builder macro generates warnings such as Result of 'Text' initializer is unused, because in the original closure, the expression results are unused. I think the compiler should generate such warnings only after macro expansion.

  • This is the caveat I was alluding to earlier: The current implementation produces an error if a macro wants to return a closure whose return type depends on a generic argument:

    @expression public macro apply<R: ResultBuilder>(simpleResultBuilder: R.Type, to closure: () -> Void) -> (() -> R.FinalResult) = #externalMacro(module: "MacroExamplesPlugin", type: "SimpleResultBuilderMacro")
    
    public protocol ResultBuilder {
        associatedtype FinalResult
    }
    
    Full error
    checked decl cannot have error type
    (macro_decl range=[/Users/kocki/workspace/swift-macro-examples/MacroExamplesLib/Macros.swift:33:20 - line:33:208] "apply(simpleResultBuilder:to:)" <R : ResultBuilder> interface type='(R.Type, () -> Void) -> <<error type>>' access=public)
    Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
    Stack dump:
    0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-01-07-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/kocki/workspace/swift-macro-examples/MacroExamplesLib/Macros.swift -target arm64-apple-macos12.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -I /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Products/Debug -F /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Products/Debug -no-color-diagnostics -enable-testing -g -module-cache-path /Users/kocki/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -enable-experimental-feature Macros -load-plugin-library /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Products/Debug/libMacroExamplesPlugin.dylib -serialize-debugging-options -enable-bare-slash-regex -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-01-07-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/MacroExamplesLib-generated-files.hmap -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/MacroExamplesLib-own-target-headers.hmap -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/MacroExamplesLib-all-target-headers.hmap -Xcc -iquote -Xcc /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/MacroExamplesLib-project-headers.hmap -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Products/Debug/include -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/DerivedSources-normal/arm64 -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/DerivedSources/arm64 -Xcc -I/Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -working-directory/Users/kocki/workspace/swift-macro-examples -module-name MacroExamplesLib -disable-clang-spi -target-sdk-version 13.1 -emit-module-doc-path /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib.swiftdoc -emit-module-source-info-path /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib.swiftsourceinfo -emit-objc-header-path /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib-Swift.h -serialize-diagnostics-path /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib-master-emit-module.dia -emit-dependencies-path /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib-master-emit-module.d -parse-as-library -o /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib.swiftmodule -emit-abi-descriptor-path /Users/kocki/Library/Developer/Xcode/DerivedData/MacroExamples-esfyllwzbxrrazbckyixrjqgygek/Build/Intermediates.noindex/MacroExamples.build/Debug/MacroExamplesLib.build/Objects-normal/arm64/MacroExamplesLib.abi.json
    1.	Apple Swift version 5.8-dev (LLVM d0e30b7f831ad7b, Swift 1b92f52a9a0714a)
    2.	Compiling with the current language version
    Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
    0  swift-frontend           0x00000001074072b4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
    1  swift-frontend           0x0000000107406538 llvm::sys::RunSignalHandlers() + 128
    2  swift-frontend           0x00000001074078f4 SignalHandler(int) + 304
    3  libsystem_platform.dylib 0x000000018d1042a4 _sigtramp + 56
    4  libsystem_pthread.dylib  0x000000018d0d5cec pthread_kill + 288
    5  libsystem_c.dylib        0x000000018d00f2c8 abort + 180
    6  swift-frontend           0x0000000107860a80 (anonymous namespace)::Verifier::verifyChecked(swift::VarDecl*) (.cold.1) + 0
    7  swift-frontend           0x0000000103d32234 (anonymous namespace)::Verifier::verifyChecked(swift::ValueDecl*) + 284
    8  swift-frontend           0x0000000103d2c31c (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) + 4344
    9  swift-frontend           0x0000000103d33af4 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 288
    10 swift-frontend           0x0000000103d339c8 swift::Decl::walk(swift::ASTWalker&) + 32
    11 swift-frontend           0x0000000103eeb0fc swift::SourceFile::walk(swift::ASTWalker&) + 236
    12 swift-frontend           0x0000000103d21ff4 swift::verify(swift::SourceFile&) + 96
    13 swift-frontend           0x0000000103ff0ca4 swift::TypeCheckSourceFileRequest::cacheResult(std::__1::tuple<>) const + 76
    14 swift-frontend           0x0000000103b41bd4 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 160
    15 swift-frontend           0x0000000103b3f974 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
    16 swift-frontend           0x0000000102caf204 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 16
    17 swift-frontend           0x0000000102cab0d0 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 160
    18 swift-frontend           0x0000000102cab010 swift::CompilerInstance::performSema() + 76
    19 swift-frontend           0x0000000102b52c50 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
    20 swift-frontend           0x0000000102b45cb4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3204
    21 swift-frontend           0x00000001029b1f14 swift::mainEntry(int, char const**) + 3304
    22 dyld                     0x000000018cdabe50 start + 2544
    

    Therefore, I have to currently hard-code the return type any View into the macro. I suppose this is only a bug in the implementation. If not, this would be an unfortunate limitation.

  • Overloading macros does not seem to work in this case:

    @expression public macro apply<R: ResultBuilder>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> String) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro")
    @expression public macro apply<R>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> String) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro2")
    

    It produces the error Invalid redeclaration of 'apply(resultBuilder:to:). @Douglas_Gregor: Is this the expected behavior or just a limitation of the current implementation? I would expect overloading macros to work just like overloading functions.

All in all, I think the general directions laid out in this proposal is great and I am surprised that I came this far with the proposed feature. What I missed where (a) type information about subexpressions and (b) static reflection. I fear that without these features users of macros (macro users, not macro implementors) could face a very rough user experience because the macro often cannot anticipate errors. It must leave it up to the compiler to generate an error message that might or might not be helpful to the user.


I think it's a good first step. It doesn't bring us the whole way, but it bridges the gap until macros gain an introspection API, wich is inevitable in my opinion.

9 Likes

Yeah, that's fair. It would be silly to insist that the proposal only be accepted if the implementation will be bug-free after all.

The scenario I'm thinking of that I think it currently not well-handled is when you're using a library which provides a macro, your use of the macro is not expanding to what you expect it to be, and you want to step through the expansion in a debugger to figure out what you're doing wrong. The proposed workflow of invoking the macro executable on just the macro to expand and debugging that is a very natural way to go about developing the macro, but seems quite clunky to set up for a user of the macro.

The ideal experience here is what DrRacket does, where debugging macro expansion is identical to debugging ordinary code. That is largely made possible by the fact that in Racket macro expansion is just one of the phases of execution rather than a "compile time" thing, but it's theoretically possible that Xcode could do something sufficiently clever to fake this. If Xcode isn't planning to do something clever, then what's the best we can do? One idea would be to attach by name to the macro executable and then hit build in Xcode. This probably works well if you have exactly one use of the macro, but doesn't if you want to debug the second one. Maybe there could be a way to tell Swift to perform expansion on a specific use of a macro first?

1 Like

Hmm, this seems to add a lot of new syntax. Perhaps we could reuse some idioms we already have, like importing macros from modules instead of having to do separate declarations for them. And I think they would be better suited as functions or callable structs. Just my two cents.

2 Likes

One concern I have is as macro authors how can we write test cases for macro?
I would like this to be addressed by this proposal as well.

2 Likes

Generally, you write test cases for the syntactic transforms. The sample repository has just the one simple one, and there's a whole lot more interesting ones here in the swift-syntax repository.

Doug

5 Likes

@Douglas_Gregor I would appreciate it if you could respond to the questions in my previous post before the review deadline. I would especially want to know if the problems I encountered where bugs or features. Then we will be able to discuss the implications of that. Thank you.


And there is one more concern I want to mention: Up until recently, if you wanted to use SwiftSyntax in a library (e.g. for code generation in a swift package plugin), you had to fulfill one of these requirements:

  1. Depend on the exact SwiftSyntax version matching the toolchain of the library user. This is not feasible because different users use different versions of Xcode, etc.
  2. Supply a dylib for every toolchain-platform combination with the library.

Is this still a requirement? The SwiftSyntax changelog seems to suggest, it isn't and I didn't have time to check. If this is still a requirement, it will be very problematic for libraries vending macros.

1 Like

Very cool!

I can certainly see how it would be valuable. Opaque result types as they exist today for functions/properties/subscripts have a notion of identity that is tied to the function/property/subscript returning them, where the identity of the opaque type is determined by that function/property/subscript and any generic arguments to it. That notion of identity doesn't work for macros, because macros get the source code of the arguments and everything that you can access via the macro expansion context, so there's no way to establish identity. That's the logic I followed in banning opaque result types for macros.

However, we could say that every use of a macro that has an opaque result type produces a completely unique opaque type, similar to what happens when we open an existential. From a compiler perspective, we know the opaque result type after macro expansion.

That's all a very long-winded way to say that I think we can lift the restriction, and allow opaque result types for macros. @Joe_Groff does the above seem plausible to you?

I think this is my preferred solution, which we've talked about at various times as being a "join" of the types of each of the returns.

The compiler model here does have some of this information, but it's not being surfaced well to users. When a macro is expanded, the compiler creates an internal buffer containing the source code produced by the macro implementation. If something goes wrong when type-checking that code, the compiler will print an error message pointing into that buffer, with a follow-up note showing the location where the macro expansion was triggered. The result looks something like this (extracted from a compiler test case):

macro:addBlocker:/swift/test/Macros/macro_expand.swift:81:7-81:27:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands
oa - oa
~~ ^ ~~
/swift/test/Macros/macro_expand.swift:81:7: note: in expansion of macro 'addBlocker' here
  _ = #addBlocker(oa + oa)
      ^~~~~~~~~~~~~~~~~~~~

The addBlocker macro is my silly example which replaces + with -, so the oa - oa code is the result of macro expansion of #addBlocker(oa + oa). That weird macro:addBlocker: file name is the name of the internal buffer, showing that the macro-expanded source code is there in a compiler, but it's only showing one line of it. We can do better here, for example by leveraging the nifty formatter in swift-syntax to show more of the macro expansion buffer in the compiler's output. (This idea just came to me; I haven't tried it yet)

If you're using Xcode, you'll have to look into the build log to see the compiler output. Clang and Swift use a different serialized format for diagnostics when talking to IDEs, so all of this information is lost right now. I have an RFC on the LLVM Discourse to extend that serialized format to include macro-expansion buffers. Swift's compiler already implements it, but IDEs would need to pick up the new APIs to provide a good experience here.

That looks like a bug in the compiler implementation, rather than a limitation of the design.

It's a limitation of the current implementation, thanks!

Thank you for diving deep into result builders-via-macros! It's great to see what can be achieved with macros, what is still out of reach, and where the pain points are.

Sorry 'bout the delay. Answers above.

Nope! With the advent of the new Swift parser, the swift-syntax package is all-Swift and fully standalone.

Doug

9 Likes

Sorry I missed your questions before, and you bring up a good point: given that we don't expose any other source location information, why these? Perhaps it would be better to remove them entirely and extend MacroExpansionContext later, with an eye toward a more complete approach to source-location information for source code that macros work with.

[EDIT: Yes, I'd like to remove moduleName and fileName. Your comment about the initializer made me realize that we should also switch MacroExpansionContext to a class-bound protocol so there can be separate macro expansion context implementations for e.g., testing vs. compilation. I put up a pull request with these changes.

Doug

5 Likes

I don't think "not realistically usably by third-parties" is a fair characterization. SwiftLint is actively migrating over to SwiftSyntax, and a number of folks here in this thread have managed to build nontrivial macros with it despite the limitations of the prototype. Yes, we can improve tutorials (some of which ha happened during this review), and as more examples and docs are written over time it'll get easier to get started.

There's several kinds of "stepped through in a debugger" that apply for macros. The first is stepping through the syntactic transformation as it occurs, to debug the workings of the macro itself. The macro being a normal Swift program means this is the just the normal debugger. Since we're doing syntactic transforms, stepping through the transform involves putting the Swift code you want to start with in a string literal that initializes a SourceFileSyntax:

let sf: SourceFileSyntax = 
  """
  my code that uses macros
  "

and then calling sf.expand(macros: [MyMacroType.self], in: testContext) and checking whether you get the source code out. The simple test in the example repository is all it takes, and you can single-step through the transformation.

If you want to do it "online", as part of the compiler running, you can break at the start of the macro program. Debuggers can do that (by following the process launch), and one could make it a little easier with extra tooling.

This is actually a huge benefit to this "macros are normal Swift programs" approach, because all the existing tools apply. In contrast, if we had some kind of macro interpreter in the compiler---say, a more declaration macro scheme, or interpreted sublanguage---then you would need special debugging tools.

A second thing "debugging macros" can mean is understanding what's failing to type check in the code that resulted from the expansion. Perhaps the expansion is correct, but your inputs were wrong in some way, and you need to make sure you see the result of the expansion to understand the diagnostics. This is less "step through in a debugger", yet still important. I gave a long-winded answer about how the system is designed to track all of the information needed to see through the layers of macro expansion.

Finally, "debugging macros" could mean debugging the code produced by macro expansion. This means ensuring that the results of macro expansion persist in some form that can be used for debugging, and that one can (say) step into a macro expansion to walk into the expanded code. This is absolutely achievable in the implementation.

I vehemently disagree with this statement. The model has been designed to be debuggable. But Xiaodi is correct in drawing the line between "can be implemented well" and "is guaranteed to be implemented well". The former is relevant to the design, the latter is not.

Doug

5 Likes

I have a question about the intended scope of the use-case for macros. Is it just supposed to replace ad-hoc code generation, or is the vision for broader use in “everyday” Swift code? Code generation has always felt like a hack, so macros make sense there to migrate such generation to code an “official” mechanism in the language. However, the rampant abuse of macros in C and other C-family languages (including Objective-C) makes me worry about the readability and maintainability of post-macro-introduction Swift code since these macros won’t be hygienic, the key issue being the possibility of unexpected side effects and identifier conflicts that aren’t visible in the raw source code. Great tooling can mitigate these issues by offering inline expansions and whatnot, but as other people have mentioned, these Swift Evolution proposals can only consider whether good tooling could be built, not whether it actually will be built. Plus, Swift should be a great language even without specific tooling, such as on new platforms and and in new environments, lest it repeat the mistakes in the Java ecosystem.

2 Likes

Just to make sure I'm not crazy.

With expression macros it's not possible to recreate the Rust try macro for Swift's Result type? https://doc.rust-lang.org/std/macro.try.html

It would require an expansion similar to the last two lines of:

func composeAB() -> Result<Int, MyError> {
    let resultA : Result<String, MyError> = fallibleA()
    guard let a : String = resultA.success else { return .failure(resultA.failure!) }

From playing around it seems incompatible (early return doesn't fit an expression), but thought I'd ask given the payoff.

No Problem. My main worry was that my issues with the current implementation were by design. Based on your answers, this seems not to be the case.

That sounds great!

This would be good. However, the macro implementation would still need to put the code inside a closure, which may have other repercussions.

That's a slight improvement. However, it may not always be obvious which generated code was caused by which piece of original code. And the compiler does not have this knowledge.

I had the idea that the macro could add #sourceLocation() annotations to the expanded code. This way, errors would point to the correct code location. However, the error message itself may still be confusing. For that to work, the macro would need access to the path of the source file. I am not sure if the fileName property of MacroExpansionContext or #file could be used to make that work.

Whatever is decided, it should definitely be possible for the macro to use #sourceLocation().

Great!

With my questions answered, I still stand by this sentiment.


I also think so. While in its current (and past) state, it is not easy to work with, you can definitely use it to build cool and reliable products. I have built a library with it that automatically generates API code.

Details for anyone inerested

The library is called SwiftyBridges. It can be used by Vapor servers to automatically generate API code for server and client. The library uses SwiftSyntax to scan struct declarations and generates an API endpoint for every public method.

I am currently in the process of completely overhauling the library. The new version uses SwiftSyntax to also scan for imports, protocol conformances and custom annotations in comments.


I believe that for the macro you describe, you would need a statement macro and not an expression macro.

It's not possible now, because as you noted, we cannot put a return into an expression like that. This intersects a bit with the discussion of the if/switch expression proposal, and the ideas around early returns there. For example, if do became an expression and allowed returns, then that could be used in expression macros. Personally, I'm not sure this is something we'd ever want to allow, because I don't like the idea of hiding control flow within expressions.

Right. I mentioned this a bit in my review of if/switch expressions, where I note that adding multi-statement do expressions would be really nice for macros.

We could add something like this, although I was thinking it would be an operation on the MacroExpansionContext that gives a source location for the syntax node it's passed. It's also conceivable that the compiler could infer the relationship in many cases. When your macro is splicing in syntax nodes from its arguments, swift-syntax could maintain identity for those syntax nodes (or a mapping). Perhaps we could use that to establish the link between the code you wrote and the code that ended up in the instantiation. I still don't know to present that in a general way to the user.

Doug

1 Like

this is good to hear, although i'd feel less apprehensive about it if LLDB already had a GUI (a portable GUI, maybe similar to the kind that you can make with Dear ImGui).

Like, if LLDB were already in the kind of business where it depends on HarfBuzz and renders glyphs[*], then i would expect that it would have no problem toggling an exploded view of an invocation of a macro (or toggling the watch-window display of variables whose declarations were generated by that invocation).

in other words, the lowest quality-of-implementation would be reasonably good.

but at this time we don't have that gui, so i have doubts about debugging QoI on linux & windows (when stepping through code that contains a macro invocation).

[*] and i don't know why it's not in that business, because there's only so much you can do by sending a stream of UTF-8 units to a terminal emulator

I'd say that LLDB already has several "GUI" frontends---VS Code, Xcode, Emacs, and so on. That's where you want integration for debugger features like this. The macro expansion buffer can already be encoded in a DWARF 5 extension so this is something that can be generally supported.

Doug

3 Likes