I have a long list of “
String interpolation produces a debug description for an optional value” build warnings caused by use of the DebugDescription macro like this
@DebugDescription
extension DateElement: CustomDebugStringConvertible {
public var debugDescription: String {
"[form: \(form), variable: \(variable), part: \(showParts)]"
}
}
where form
is optional.
Trying to fix with \(String(describing: form))
or \(form ?? “”)
causes the DebugDescription error, “
Only references to stored properties are allowed.”
Is there a trick to reconcile these?
1 Like
Swift Macros currently only know the syntactic representation of the arguments supplied, not the referenced variables (or their content). Unfortunately this restricts the functionality of a macro. I've seen some posts mentioning that it would cripple build performance and make them less deterministic, which I find dubious when done correctly, but I digress.
They need more information about the supplied arguments to become something truly exceptional, like handling your optional variable. Some exciting development in this area is the module SwiftLexicalLookup
in swift-syntax
, but it is currently experimental and I don't see how it would fix this specific warning. Then again I have no experience with lldb or the inner workings of Swift.
Your best option right now is to change your data structure, ignore the warning or write some documentation about it where applicable.
1 Like
I appreciate the feedback. That makes sense. I didn’t expect there was a way to silence the warnings but it didn’t hurt very much to ask. I’ll continue weeding through the warnings for actionable items, ignoring these.
Tiny follow-up on how to filter these warnings from the Xcode issue navigator since it differs from the instructions that already took a while to find.
Type the word to exclude and press Enter. Ignore the little menu that pops up and instead click the tiny menu arrow that has been added ahead of the word.
It’s simple once you’ve seen it.