In Swift, how do -enable-upcoming-feature and -enable-experimental-feature differ, and which upcoming features does Swift 6 mode already include?
answer
- accepted versus unfinished
- one name, one module
- Swift 6 set versus future set
- some refused in production
- :migrate and hasFeature
basics
~20 s-enable-upcoming-feature turns on one accepted, source-breaking change before its language mode makes it default; -enable-experimental-feature is for unfinished work with no stability promise, some of it refused by release compilers. Swift 6 mode includes StrictConcurrency and the rest of the Swift 6 set, not ExistentialAny.
solid answer
~40 s**Upcoming features** (Swift 5.8, SE-0362) are accepted changes held back only because they break source. `-enable-upcoming-feature X` turns one on for the module being compiled, and the mode that schedules X later turns it on by default. Swift 6 mode includes `StrictConcurrency`, `RegionBasedIsolation`, `InferSendableFromCaptures`, `GlobalConcurrency`, `ConciseMagicFile`, `ImplicitOpenExistentials` and the rest of the Swift 6 set. `ExistentialAny`, `MemberImportVisibility`, `InternalImportsByDefault`, `NonisolatedNonsendingByDefault`, `InferIsolatedConformances` and `ImmutableWeakCaptures` belong to a *future* mode, so even Swift 6 mode needs the flag. **Experimental features** are work in progress with no stability promise; each declares whether a release compiler may enable it, and Swift 6.4 refuses the rest, such as `KeyPathWithMethodMembers`, with "cannot be enabled in production compiler". Since 6.2, `X:migrate` emits fix-it warnings without changing behaviour, and `#if hasFeature(X)` tests whether X is on.
code
swift · 13 linesprotocol Logger {
func log(_ message: String)
}
struct ConsoleLogger: Logger {
func log(_ message: String) { print(message) }
}
func report(to logger: Logger) {
logger.log("build finished")
}
report(to: ConsoleLogger())go deeper
Recall that -enable-upcoming-feature turns on one accepted future rule now, and that experimental features are unfinished work with no stability promise.
Explain which features Swift 6 mode includes, which belong to a future mode, and how hasFeature, :migrate and the per-module scope of the flag work.
Catch the silent failures: misspelt names ignored, redundant flags after a mode switch, and experimental flags that will not build on a release toolchain.
Decide which future-mode features, such as ExistentialAny, a team adopts by choice and record why, separately from the Swift 6 migration path.
## Why upcoming features exist Some accepted Swift Evolution proposals reject code that used to compile, or change what it means. Swift keeps source compatibility within a language mode, so those changes wait for the next mode. SE-0362 (Swift 5.8) made each of them adoptable early. Every such change gets an **upcoming feature** identifier, and `-enable-upcoming-feature X` turns it on for the module being compiled. When the module later moves to the mode that includes X, the flag has nothing left to do. SE-0362 deliberately rejected a single "enable all upcoming features" flag, because the set grows each release and such a flag would become a shifting dialect. ## Upcoming versus experimental | | Upcoming feature | Experimental feature | |---|---|---| | Status | Accepted and shipped, held back for source compatibility | Under development; may change or vanish | | Flag | `-enable-upcoming-feature X` | `-enable-experimental-feature X` | | Release compiler | Accepted | Only if the feature allows it; otherwise "experimental feature 'X' cannot be enabled in production compiler" | | Becomes the default | In the language mode that schedules it | Only after it is accepted and shipped | Each experimental feature declares whether a release compiler may enable it. Embedded Swift is one that may; `KeyPathWithMethodMembers`, key paths to methods, is one that may not. Being allowed in a release compiler is not a stability promise: the feature can still change shape before it ships. Handing an upcoming feature's name to the experimental flag does not fail the build; with strict feature checking on, the compiler warns "'ExistentialAny' is not an experimental feature, use -enable-upcoming-feature instead". ## Which features Swift 6 mode includes The compiler's feature table tags every upcoming feature with a mode. **Swift 6 mode** turns on: - the concurrency set: `StrictConcurrency`, `RegionBasedIsolation`, `InferSendableFromCaptures`, `GlobalConcurrency`, `IsolatedDefaultValues`, `DisableOutwardActorInference`, `DynamicActorIsolation`, `GlobalActorIsolatedTypesUsability`; - the rest: `ConciseMagicFile`, `ForwardTrailingClosures`, `BareSlashRegexLiterals`, `ImplicitOpenExistentials`, `NonfrozenEnumExhaustivity`, `DeprecateApplicationMain`, `ImportObjcForwardDeclarations`. Features tagged for a **future** mode stay off in Swift 6 mode until you pass the flag: - `ExistentialAny`, which wants `any` before every existential type; since Swift 6.1 its diagnostics are warnings, and `-Werror ExistentialAny` escalates them; - `MemberImportVisibility` and `InternalImportsByDefault`, which change import rules; - `NonisolatedNonsendingByDefault` and `InferIsolatedConformances`, which change isolation defaults; - `ImmutableWeakCaptures`. ## A worked example Take a module that declares `func report(to logger: Logger)`, where `Logger` is a protocol, and builds with the Swift 6.4 compiler: - In Swift 5 mode and in Swift 6 mode, it typechecks without a word, because neither mode includes `ExistentialAny`. - With `-swift-version 6 -enable-upcoming-feature ExistentialAny`, the compiler warns "use of protocol 'Logger' as a type must be written 'any Logger'; this will be an error in a future Swift language mode [#ExistentialAny]". - With `-enable-upcoming-feature ExistentialAny:migrate`, the warning drops the "future mode" suffix and carries a fix-it that rewrites the parameter to `any Logger`; the code compiles exactly as if the feature were off. - With `-enable-experimental-feature KeyPathWithMethodMembers`, a feature whose proposal was returned for revision, the release compiler stops: "experimental feature 'KeyPathWithMethodMembers' cannot be enabled in production compiler". The same source, four command lines, four different outcomes: the flags, not the code, decide which rules apply. ## How the flag behaves 1. **Per module.** SE-0362 says the effect of an upcoming feature does not cross module boundaries. A client that wants the rule passes the flag itself. 2. **Unknown names are ignored.** A misspelt name does nothing by default, so one command line can serve older and newer toolchains. `-Wwarning StrictLanguageFeatures` turns on the warning "'X' is not a recognized upcoming feature", and `-Werror StrictLanguageFeatures` makes it fail the build. 3. **Redundant names warn.** In Swift 6 mode, passing a feature the mode already includes, such as `RegionBasedIsolation`, warns "upcoming feature 'RegionBasedIsolation' already enabled as of the Swift 6 language mode". 4. **Migration mode.** Since Swift 6.2 (SE-0486), `-enable-upcoming-feature ExistentialAny:migrate` compiles as if the feature were off but emits warnings with fix-its that prepare the code. It works for features that declare migration support, such as `ExistentialAny`, `MemberImportVisibility`, `NonisolatedNonsendingByDefault` and `InferIsolatedConformances`; for others the compiler warns "feature 'X' does not support migration mode". 5. **Detection in source.** `#if hasFeature(X)` is true when X is on, whether a flag or the mode turned it on. ## What interviewers listen for - Whether you assume Swift 6 mode means "every upcoming feature on". It does not. - Whether you treat experimental flags as a way to get ahead in shipping code. They are not. - Whether you know that a typo in a feature name passes silently unless strict feature checking is on. - Whether you can say which features a module still carries as flags, and which ones will disappear at the next mode switch.
- If a library module enables an upcoming feature, do the modules that import it get the feature too?No. SE-0362 states that the effect of upcoming features does not cross module boundaries: each module is compiled with its own flags. A client that wants the stricter rule passes `-enable-upcoming-feature` for its own module. What crosses the boundary is the library's compiled interface, not its dialect.
- What happens when a build passes a misspelt name such as -enable-upcoming-feature ExistentialAnyy?By default, nothing: the compiler ignores unrecognized names so one command line can serve several toolchains. That also hides typos, even under `-warnings-as-errors`, because the warning is off by default. `-Wwarning StrictLanguageFeatures` prints "'ExistentialAnyy' is not a recognized upcoming feature", and `-Werror StrictLanguageFeatures` fails the build.
- What does the :migrate suffix change compared with enabling the feature outright?Migration mode, added in Swift 6.2, keeps the code compiling as if the feature were off and emits warnings in the feature's diagnostic group with fix-its that make the code ready for it. Applying the fix-its then enabling the feature changes nothing at run time. Features without migration support produce "feature 'X' does not support migration mode".
- Swift 6 language mode enables every upcoming feature, ExistentialAny included.
- An upcoming feature enabled in a library also applies to the apps importing it.
- A misspelt upcoming feature name always fails the build.
- Experimental feature flags are the safe way to adopt Swift 6 rules early.
- One compiler flag enables all upcoming features at once.
Assumes Swift 6.4: upcoming feature flags date from Swift 5.8 (SE-0362) and the :migrate suffix from Swift 6.2 (SE-0486); features scheduled for a future mode, such as ExistentialAny, stay off in Swift 6 mode.