Skip to content

Instantly share code, notes, and snippets.

@ole
Last active April 29, 2024 01:14
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ole/478874632fca61869928a0cc0a956972 to your computer and use it in GitHub Desktop.
Save ole/478874632fca61869928a0cc0a956972 to your computer and use it in GitHub Desktop.
List Swift compiler upcoming and experimental feature flags. (Note: the second script below, swift-list-features.sh, is probably the more useful one of the two. Unfortunately, I can't reorder them.)
#!/bin/zsh
# Test if the Swift compiler knows about a particular language feature.
#
# Usage:
#
# swift-has-feature [--swift SWIFT_PATH] FEATURE
#
# The exit code signals success (= the compiler knows this feature) or failure.
#
# Example:
#
# swift-has-feature FullTypedThrows
# swift-has-feature --swift /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-01-04-a.xctoolchain/usr/bin/swift FullTypedThrows
#
# The feature be an upcoming or experimental language feature,
# such as `"StrictConcurrency"` or `"FullTypedThrows"`.
#
# Unfortunately, the script can’t tell you whether the feature in question
# is upcoming or experimental or neither. Use swift-list-features.sh for that.
usage="swift-has-feature [--swift <path_to_swift>] [--silent] <feature>"
zmodload zsh/zutil
zparseopts -D -F -- -swift:=arg_swift_path -silent=flag_silent || exit 1
if test -z "$1"; then
echo "Usage: $usage"
exit
fi
swift_path=${arg_swift_path[-1]}
if test -z "$swift_path"; then
swift_path="swift"
fi
# Print compiler version
if test -z "$flag_silent"; then
"$swift_path" --version
fi
"$swift_path" \
-enable-upcoming-feature "$1" \
-enable-experimental-feature "$1" \
- << END_OF_SWIFT_SCRIPT
import Foundation
#if hasFeature($1)
exit(EXIT_SUCCESS)
#else
exit(EXIT_FAILURE)
#endif
END_OF_SWIFT_SCRIPT
if test $? -eq 0; then
if test -z "$flag_silent"; then
echo "Supported: Swift compiler knows feature '$1'"
fi
else
if test -z "$flag_silent"; then
echo "Not supported: Swift compiler doesn’t know feature '$1'"
fi
exit 1
fi
#!/bin/bash
# List Swift language features the compiler knows about.
#
# Usage:
#
# swift-list-features [version] # default: main branch
#
# Examples:
#
# swift-list-features # Queries main branch
# swift-list-features 5.9 # Queries release/5.9 branch
#
# This script uses curl to download the file in which the language features
# are defined from the Swift repo and uses Clang to parse it.
#
# Original author: Gor Gyolchanyan
# <https://forums.swift.org/t/how-to-test-if-swiftc-supports-an-upcoming-experimental-feature/69095/10>
#
# Enhanced/modified by: Ole Begemann
swift_version=$1
if test -z "$swift_version" || test "$swift_version" = "main"; then
branch="main"
else
branch="release/${swift_version}"
fi
GITHUB_URL="https://raw.githubusercontent.com/apple/swift/${branch}/include/swift/Basic/Features.def"
FEATURES_DEF_FILE="$(curl --fail-with-body --silent "${GITHUB_URL}")"
curlStatus=$?
if test $curlStatus -ne 0; then
echo "$FEATURES_DEF_FILE"
echo "Error: failed to download '$GITHUB_URL'. Invalid URL?"
exit $curlStatus
fi
echo "Swift language features in $branch"
echo "======================================"
clang --preprocess --no-line-commands -nostdinc -x c - <<EOF | sort
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, ...) FeatureName
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) [Upcoming] FeatureName (Swift Version)
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) [Experimental] FeatureName
${FEATURES_DEF_FILE}
EOF
@ole
Copy link
Author

ole commented Feb 16, 2024

Example output of swift-list-features for Swift 5.10:

$ swift-list-features.sh 5.10
Swift language features in release/5.10
======================================
Actors
AsyncAwait
AttachedMacros
BuiltinAssumeAlignment
BuiltinBuildComplexEqualityExecutor
BuiltinBuildExecutor
BuiltinBuildMainExecutor
BuiltinContinuation
BuiltinCopy
BuiltinCreateAsyncTaskInGroup
BuiltinCreateTaskGroupWithFlags
BuiltinExecutor
BuiltinHopToActor
BuiltinIntLiteralAccessors
BuiltinJob
BuiltinStackAlloc
BuiltinTaskGroupWithArgument
BuiltinTaskRunInline
BuiltinUnprotectedAddressOf
BuiltinUnprotectedStackAlloc
ConcurrentFunctions
EffectfulProp
ExtensionMacroAttr
ExtensionMacros
FreestandingExpressionMacros
FreestandingMacros
GlobalActors
ImplicitSelfCapture
InheritActorContext
LexicalLifetimes
Macros
MarkerProtocol
MoveOnly
MoveOnlyResilientTypes
NewCxxMethodSafetyHeuristics
NoAsyncAvailability
ParameterPacks
PrimaryAssociatedTypes2
RethrowsProtocol
Sendable
SpecializeAttributeWithAvailability
UnavailableFromAsync
UnsafeInheritExecutor
[Experimental] ASTGenTypes
[Experimental] AccessLevelOnImport
[Experimental] AdditiveArithmeticDerivedConformances
[Experimental] BuiltinMacros
[Experimental] BuiltinModule
[Experimental] CodeItemMacros
[Experimental] DifferentiableProgramming
[Experimental] Embedded
[Experimental] FlowSensitiveConcurrencyCaptures
[Experimental] ForwardModeDifferentiation
[Experimental] GenerateBindingsForThrowingFunctionsInCXX
[Experimental] ImplicitSome
[Experimental] ImportSymbolicCXXDecls
[Experimental] LayoutPrespecialization
[Experimental] LayoutStringValueWitnesses
[Experimental] LayoutStringValueWitnessesInstantiation
[Experimental] LazyImmediate
[Experimental] ModuleInterfaceExportAs
[Experimental] MoveOnlyClasses
[Experimental] MoveOnlyEnumDeinits
[Experimental] MoveOnlyPartialConsumption
[Experimental] MoveOnlyTuples
[Experimental] NamedOpaqueTypes
[Experimental] NoImplicitCopy
[Experimental] OldOwnershipOperatorSpellings
[Experimental] OneWayClosureParameters
[Experimental] OpaqueTypeErasure
[Experimental] ParserASTGen
[Experimental] ParserDiagnostics
[Experimental] ParserRoundTrip
[Experimental] ParserValidation
[Experimental] PlaygroundExtendedCallbacks
[Experimental] RawLayout
[Experimental] ReferenceBindings
[Experimental] SendNonSendable
[Experimental] SendableCompletionHandlers
[Experimental] StaticAssert
[Experimental] StrictConcurrency
[Experimental] SymbolLinkageMarkers
[Experimental] ThenStatements
[Experimental] TupleConformances
[Experimental] TypeWitnessSystemInference
[Upcoming] BareSlashRegexLiterals (Swift 6)
[Upcoming] ConciseMagicFile (Swift 6)
[Upcoming] DeprecateApplicationMain (Swift 6)
[Upcoming] DisableOutwardActorInference (Swift 6)
[Upcoming] ExistentialAny (Swift 7)
[Upcoming] ForwardTrailingClosures (Swift 6)
[Upcoming] GlobalConcurrency (Swift 6)
[Upcoming] ImportObjcForwardDeclarations (Swift 6)
[Upcoming] IsolatedDefaultValues (Swift 6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment