skip to content

In Swift 6.1 and later, how do -warnings-as-errors, -Werror and -Wwarning combine on diagnostic groups, and what does Swift 6.4's @diagnose add?

level: seniorimportance: nice to knowfreq 20%

answer

  1. stable names for warnings
  2. [#GroupName] after the message
  3. the last flag wins
  4. errors stay errors
  5. a scoped exception with a reason

basics

~20 s

Each grouped warning has a stable diagnostic group name, printed as [#Group]. Swift 6.1's -Werror Group and -Wwarning Group are applied left to right with -warnings-as-errors, last one winning; Swift 6.4's @diagnose overrides a group inside one declaration.

solid answer

~40 s

Since Swift 6.1 (SE-0443) grouped warnings carry a stable **diagnostic group** name, printed after the message as `[#DeprecatedDeclaration]`. `-Werror Group` escalates that group to errors; `-Wwarning Group` keeps it a warning and also turns on a group that is off by default. The compiler applies these and `-warnings-as-errors` / `-no-warnings-as-errors` left to right, and the last one wins: `-warnings-as-errors -Wwarning DeprecatedDeclaration` fails on everything except deprecations, while the reverse order fails on deprecations too. `-suppress-warnings` cannot be combined with them. Swift 6.4 (SE-0522) adds `@diagnose(Group, as: error|warning|ignored, reason: "…")` on a declaration, overriding the module-wide behaviour inside that declaration's signature and body. Neither mechanism touches real errors: Swift 6 mode's data-race errors stay errors.

code

swift · 17 lines
swift
@available(*, deprecated, message: "Use fetchRatesV2()")
func fetchRates() -> [Double] { [1.0, 0.92] }

func refreshDashboard() -> Int {
    var retries = 0                       // never used
    return fetchRates().count
}

@diagnose(DeprecatedDeclaration, as: warning, reason: "Old rates feed until the v2 rollout")
func refreshLegacyWidget() -> Int {
    fetchRates().count
}

@diagnose(DeprecatedDeclaration, as: ignored)
func refreshArchive() -> Int {
    fetchRates().count
}

go deeper

for a junior

Recall that Swift 6.1 and later print a group name like [#DeprecatedDeclaration] after a warning, and that -Werror makes a group an error.

for a middle

Explain the left-to-right, last-flag-wins rule across -warnings-as-errors, -Werror and -Wwarning, and which groups are off until named.

for a senior

Keep a warnings-as-errors build green through an SDK bump with group exemptions, and place @diagnose exceptions with a reason instead of weakening module flags.

for a principal

Decide which groups the team escalates, which it exempts and who may add an @diagnose exception, so the policy does not erode one exemption at a time.

## Diagnostic groups Swift 6.1 (SE-0443) gave warnings stable public names. A **diagnostic group** collects related diagnostics under an identifier such as `DeprecatedDeclaration`, `NoUsage`, `ExistentialAny` or `MutableGlobalVariable`. The Swift 6.4 compiler prints the group after each message, as in `'fetchRates()' is deprecated: Use fetchRatesV2() [#DeprecatedDeclaration]`, and links its documentation page. Groups are not the compiler's internal diagnostic identifiers (those are what `-debug-diagnostic-names` prints). A group can contain other groups, and SE-0443's rule is that a group's contents should not shrink, so a flag naming it keeps meaning the same thing across releases. ## The flags | Flag | Effect | |---|---| | `-warnings-as-errors` | every warning becomes an error | | `-no-warnings-as-errors` | every warning goes back to being a warning | | `-Werror Group` | warnings in `Group` become errors | | `-Wwarning Group` | warnings in `Group` stay or become warnings; also enables a group that is off by default | | `-suppress-warnings` | hides all warnings; refused together with `-Werror`, `-Wwarning` or `-warnings-as-errors` | Some groups are off by default. `-warnings-as-errors` does not switch those on; `-Wwarning` or `-Werror` naming the group does. `UseAnyAppleOSAvailability` is one, and so is the part of `StrictLanguageFeatures` that reports misspelt feature names; `-Wwarning StrictLanguageFeatures` turns that warning on. ## Order: the last flag wins The compiler applies `-Werror`, `-Wwarning`, `-warnings-as-errors` and `-no-warnings-as-errors` left to right. When two of them touch the same warning, the later one decides: 1. `-warnings-as-errors -Wwarning DeprecatedDeclaration`: every warning fails the build except deprecations. 2. `-Wwarning DeprecatedDeclaration -warnings-as-errors`: deprecations fail too, because the blanket flag came last. 3. `-Werror DeprecatedDeclaration` alone: deprecations fail, every other warning stays a warning. Overlapping groups follow the same rule: the last flag that names any group containing the warning wins. ## `@diagnose` in Swift 6.4 SE-0522 moves the same control into source, scoped to a declaration: - **Syntax.** `@diagnose(Group, as: error | warning | ignored, reason: "…")`. The `reason:` argument takes a plain string literal; an interpolated one is rejected. - **Scope.** The declaration's signature and body: functions, initializers, types, extensions, accessors, `import` declarations and more. - **Precedence.** It overrides the module-wide behaviour set by the flags, a nested `@diagnose` overrides its enclosing one, and among several on one declaration the last one wins. - **Silencing.** `ignored` suppresses that group in that scope. There is no module-wide flag for silencing a single group. - **Mistakes.** An unknown group name produces the warning "the diagnostic group identifier 'X' is unknown". Under `-suppress-warnings`, `@diagnose` has no effect, even `as: error`. ## Reading the example The file with this question has one deprecated function, one unused variable and two `@diagnose` declarations. Under Swift 6.4: - `-Werror DeprecatedDeclaration` alone makes the deprecated call in `refreshDashboard()` an error. The call in `refreshLegacyWidget()` stays a warning, because its `@diagnose(…, as: warning)` overrides the module-wide escalation, and `refreshArchive()` reports nothing, because its group is `ignored` there. The unused variable stays a warning. - `-warnings-as-errors -Wwarning DeprecatedDeclaration` flips the picture: the unused variable (`[#NoUsage]`) is now the only error, and both remaining deprecations are warnings. - `-warnings-as-errors -Wwarning NoUsage` reverses it again: the deprecated call in `refreshDashboard()` is fatal and the unused variable a warning, while `refreshLegacyWidget()` keeps its scoped warning. Same source, three policies, each one a single line of flags. ## What neither tool changes - **Errors stay errors.** Both mechanisms act only on warnings. In Swift 6 mode, "var 'requestCount' is not concurrency-safe because it is nonisolated global shared mutable state" is an error, and `-Wwarning MutableGlobalVariable` leaves it an error. In Swift 5 mode with complete checking the same diagnostic is a warning, and these flags can escalate it. - **Ungrouped warnings.** A warning printed without a `[#Group]` tag can only be changed by the blanket flags. ## Where it pays off - Keeping `-warnings-as-errors` when a new SDK deprecates dozens of APIs at once. - Escalating one group a module has finished cleaning, such as `-Werror ExistentialAny`, before the whole module is clean. - Recording a single justified exception with `reason:` right beside the code that needs it, where review and search can find it. - Turning on an opt-in group, such as `-Wwarning UseAnyAppleOSAvailability`, to get suggestions a team wants without changing the default for everyone else. Because `@diagnose` is Swift 6.4 syntax, code that must still build with older toolchains keeps it behind `#if compiler(>=6.4)`; the command-line flags need no such care beyond requiring Swift 6.1.

  • How do you find the group name to pass to -Werror or @diagnose?
    Swift 6.4 prints it in brackets after each grouped warning, for example `[#NoUsage]`, and lists a documentation link for it. SE-0443 introduced `-print-diagnostic-groups` for this. Do not rely on the names `-debug-diagnostic-names` prints: those are internal diagnostic identifiers, which sometimes match a group name but are not what the group flags take.
  • Can @diagnose or -Wwarning turn a Swift 6 mode data-race error back into a warning?
    No. Both act only on warnings; error diagnostics cannot be controlled by either, even when they belong to a group. In Swift 6 mode `-Wwarning MutableGlobalVariable` leaves the nonisolated-global error an error. The only route back to a warning is building the module in Swift 5 mode, where those diagnostics are warnings.
  • What does @diagnose(Group, as: error) do in a module built with -suppress-warnings?
    Nothing observable. SE-0522 places `-suppress-warnings` outside the scoped model: it silences every warning module-wide and the attribute is ignored, even when it asks for an error. That matches how a package build of a remote dependency swaps the warning flags for `-suppress-warnings`, so source-level escalation cannot break someone else's build.

  • Flag order does not matter; -warnings-as-errors always beats -Wwarning.
  • -Wwarning or @diagnose can lower a Swift 6 mode concurrency error to a warning.
  • Group names are the identifiers -debug-diagnostic-names prints.
  • @diagnose(Group, as: ignored) silences that group across the whole module.
  • Tolerating one noisy SDK deprecation means dropping -warnings-as-errors.

Assumes Swift 6.4: -Werror and -Wwarning for diagnostic groups arrived in Swift 6.1 (SE-0443) and the @diagnose attribute in Swift 6.4 (SE-0522).