SE-0382: Expression Macros

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