Macro diagnostic position ignored?

I'm trying to add some static analysis to a string passed to a macro, and am explicitly giving the associated diagnostic a position in the string, but Xcode appears to ignore it:

In the above example, the diagnostic is positioned at the ( character in the string, but the highlighting is done at the node level, which is the beginning of the string literal.

Am I mishandling SwiftSyntax in some way? Is this an Xcode bug?

Do you receive the expected diagnostic highlights when you compile on the command line? If so, this is likely an Xcode issue and an issue at http://feedbackassistant.apple.com would be appreciated for that.

Nope, it appears to affect the compiler output as well:

….swift:153:8: warning: Cannot find ')' to match opening '(' in SQL string, producing incomplete fragment; did you mean to make this explicit? (from macro 'sql')
151 | 
152 | func f() {
153 |   #sql("SELECT (1", as: Int.self)
    |        |- warning: Cannot find ')' to match opening '(' in SQL string, producing incomplete fragment; did you mean to make this explicit? (from macro 'sql')
    |        `- note: Use 'SQLQueryExpression.init(_:)' to silence this warning
154 | }
155 | 

I will note that we have a custom macro snapshot tool that uses SwiftSyntax's diagnostics formatter and things are anchored correctly:

It looks like we loose the character-based source location somewhere in the compiler and switch to the token-anchored locations somewhere. Could you file an issue with a reproducer project to GitHub - swiftlang/swift: The Swift Programming Language?

Done: Compiler loses position offset of macro diagnostic · Issue #80110 · swiftlang/swift · GitHub

1 Like