14

Imagine I mark the following method deprecated in Swift:

@available(*, deprecated=1.0)
func myFunc() { 
    // ...
}

And I treat warnings as errors in Swift by setting OTHER_SWIFT_FLAGS="-warnings-as-errors".

How do I make it show these deprecation notices as warnings, while still treating the rest of the warnings as errors?


It seems like GCC had a pretty good solution to this problem:

-Werror // treat all warnings as errors
-Wno-error=<warning> // don't treat <warning> as error (e.g. -Wno-error=switch)
-Werror=<warning> // treat <warning> as error

So if this was Objective-C, I could simply use -Werror -Wno-error=deprecated-declarations and get exactly what I want.

What is the equivalent for Swift?


I tried adding -Wno-error=deprecated-declarations to the OTHER_SWIFT_FLAGS, but it seems like it's not meant for Swift, so it doesn't work.

Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465

1 Answers1

4

This isn't possible. As of Swift 4, the Swift compiler doesn't have switches to either enable/disable particular warnings or promote particular warnings to errors.

The Swift core developers expressed their reluctance to add a litany of compiler flags on several occasions on the swift-evolution mailing list. The rationale is that they want to keep a single "dialect" of Swift so that every developer works with the same language features etc.

Whether this should extend to something like particular warning flags is of course debatable, but that's the current official stance. It's definitely possible that these rules will be somewhat loosened in the future, but I wouldn't bet on it.

EDIT: As of Swift 4.2, Swift added a #warning syntax.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 2
    That there's an option to treat all warnings as errors means that that rationale is already void. It's annoying being limited by some other people I don't making decisions that make my job harder. I thought swift was meant to make app development easier... – Jonathan. Jul 24 '18 at 10:18
  • @Jonathan. The existence of warnings already made it void. Either something is a problem, then you must fix it and thus it is an error. Or something is not a problem but then it requires no attention. What is a warning anyway? A problem that is no problem? A way for compilers to express their inability to judge if something is a problem? Some people will always fix all warnings, some will always ignore all of them and the rest is in-between, so how does that keep code a single "dialect"? If I want a vague list of possible errors, I run the analyzer, not the compiler. – Mecki Jun 26 '19 at 19:10
  • @Jonathan. Oh, and I'm not sure if Swift was meant to make app development easier, I rather have the impression it was meant to make it more robust and less error prone. – Mecki Jun 26 '19 at 19:12
  • @Mecki I think Apple has indeed always touted Swift as making app development easier, as well as more robust. The reason I want some more control over this is because I want to be able to treat warnings as errors, and yet have my user-generated warnings (#warning warning goes here) to not be errors so that I can make temporary notes and reminders during development that I clean up before I make my pull request. – Paul Bruneau Jan 02 '20 at 14:49
  • @PaulBruneau Swift doesn't have `#warning` or an equivalent to begin with. And `#warning` is not even part of any C/C++ standard, only `#error` is; MSVC doesn't even support `#warning`. User generated warnings are not standard conform. The convention for what you do is to use comments, e.g. `// TODO: ...` or `// FIXME: ...`, Xcode even recognizes these (as well as `// ???: ...` and `// !!!: ...`) and shows them in the symbol overview menu. They are easy to find and collect with `grep` for people that care and easy to ignore for the rest. – Mecki Jan 02 '20 at 20:12
  • @Mecki it doesn't? https://www.hackingwithswift.com/example-code/language/how-to-add-warnings-and-errors-to-your-code-using-warning-and-error I'm not interested in C standards, I'm interested in Xcode. I'm also not interested in standard conform. I'm interested in doing things I used to do in Obj-C that were very helpful. I will try the comment style ones like you listed, thank you. Also I don't use grep, I use find. I tried those comment ones. They have to be searched for. I don't want that. I want them presented, like #warning – Paul Bruneau Jan 03 '20 at 21:26
  • @PaulBruneau This was fist added in Swift 4.2, see https://stackoverflow.com/q/24183812/15809 And Xcode is just an IDE, it's not a programming language, it's not a compiler. It currently uses `llvm` with `clang` for Obj-C but used to use `gcc` for that in the past and who knows what it will use tomorrow and if that will support `#warning`. I prefer to stick to standards as that guarantees your code will still work tomorrow without major refactoring and Obj-C has no own pre-processor, it uses the C pre-processor and `#...` are processed by the pre-processor. – Mecki Jan 03 '20 at 22:10
  • Feel free not to use #warning then. I want to use it and have it be as useful as it can be – Paul Bruneau Jan 05 '20 at 01:32