Hey everyone,
I’m working on a draft for #warning in Swift. I’ve implemented the draft as it stands, and it’s pretty nice to work with.
I’ve pasted it below, and I’d love some feedback! Thanks!
— Harlan Haskins
Proposal: SE-NNNN <https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md>
Author: Harlan Haskins <https://github.com/harlanhaskins>
Status: Awaiting review <se-0000-pound-warning.md · GitHub;
Review manager: TBD
<se-0000-pound-warning.md · GitHub
It's really common for developers to add TODO/FIXME comments in their source code, but there currently isn't a supported facility to make these visible. People have implemented special workarounds <https://bendodson.com/weblog/2014/10/02/showing-todo-as-warning-in-swift-xcode-project/> to coax Xcode into emitting TODOs and FIXMEs as warnings, but there isn't an accessible way to provide arbitrary warnings, and does not work in a non-Xcode environment.
<se-0000-pound-warning.md · GitHub
A #warning is for something you intend to fix before submitting your code or for writing future tasks that you or your teammates intend to complete later. Because this is such a common programming pattern, Swift should have a similar facility.
<se-0000-pound-warning.md · GitHub solution
Add #warning(_:) as a new compiler directive that emits a warning diagnostic with the contents, pointing to the start of the message.
func configPath() -> String {
#warning("TODO: load this more safely") // expected-warning {{TODO: load this more safely}}
return Bundle.main().path(forResource: "Config", ofType: "plist")!
}
<se-0000-pound-warning.md · GitHub design
This will add two new productions to the Swift grammar:
compiler-control-statement → warning-directive
warning-directive → #warning( static-string-literal )
Upon parsing this statement, the Swift compiler will immediately emit a warning and discard the statement.
If a #warning exists inside a branch of a #if statement that is not taken, then no warning is emitted.
#if false
#warning(“This won’t exist”)
#endif
<se-0000-pound-warning.md · GitHub on existing code
This change is purely additive; no migration will be required.
<se-0000-pound-warning.md · GitHub considered
We could do some kind of comment-parsing based approach to surface TODOs and FIXMEs, but #warning serves as a general-purpose facility for reporting at compile time. Plus, not all TODO or FIXME comments should surface as warnings in the source.