The #warning compiler directive, from SE–196, is useful for marking lines of code that need to be changed. However, it is currently parsed as a statement, which means it can only appear in statement position.
Sometimes the line of code that needs to be marked is located where a statement cannot appear, such as within the body of an expression. It could be an element of an array, an argument to a function call, or a parameter in a declaration.
In all those situations, the #warning directive today results in a compiler error related to finding a statement where an expression was expected. This occurs regardless of whether the directive appears on a line by itself or trailing some other code. For example these are both errors:
foo(
a: 123,
#warning("change this") // currently an error
b: 456,
c: 789
)
let x = [
123,
456, #warning("change this") // currently an error
789
]
I propose that #warning should instead be treated like whitespace, similar to the way that comments are, and allowed to appear in these positions.
• • •
Interestingly, within an if expression, #warning can already appear on a line by itself even though other statements (such as print) cannot:
Example
let x = if true {
#warning("change this") // okay, not currently an error
123
} else {
456
}
let y = if true {
print("change this") // currently an error
123
} else {
456
}
let z = if true {
123 #warning("change this") // currently an error
} else {
456
}
I don’t know exactly what’s going on there, but it would get subsumed by this proposal.
• • •
The precise details of how this should work have a few possibilities:
-
Allow
#warningon any line by itself, with no other code (except comments). -
Allow both (1) and the use of
#warningin trailing position, after all other code on a line, similar to//...comments (though those could still come after). -
Allow
#warningin any position, including the middle of a line of code, similar to/*...*/comments.
I personally lean toward option 2, though I’m not tied to that choice. The important thing is to enable the use of #warning within multi-line expressions and declarations, where it is currently an error.
(For consistency, I suppose I ought to propose the same thing for #error, even though it would just trade one error for another. So let’s tack that on as well.)