-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Open
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareBug: A crash, i.e., an abnormal termination of softwareexpressionsFeature: expressionsFeature: expressionskey pathsFeature: key paths (both native and Objective-C)Feature: key paths (both native and Objective-C)type checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysis
Description
Description
When trying to access the key path of a static nested container type, the compiler crashes.
Important: Xcode IDE does not show any error for this code.
let resource = Utils.resource(\.Category, resource: \.nestedResource)
Reproduction
Try to compile or run this, the compiler crashes
1. Full version, required to work on the project
struct Resource {
let name: String
}
enum Resources {
enum Category {
static let nestedResource = Resource(name: "test")
}
}
struct Utils {
static func resource<ResourceCategory>(
_ type: KeyPath<Resources.Type, ResourceCategory.Type>,
resource: KeyPath<ResourceCategory.Type, Resource>
) -> Resource {
ResourceCategory.self[keyPath: resource]
}
}
func testResourceGetter() {
// 👇 This line crashes the compiler. Btw, Xcode editor does not show errors.
let resource = Utils.resource(\.Category, resource: \.nestedResource)
}
2. Simplified version
enum Resources {
enum Category { }
}
struct Utils {
static func resource<ResourceCategory>(_ type: KeyPath<Resources.Type, ResourceCategory.Type>) -> ResourceCategory.Type {
Resources.self[keyPath: type]
}
}
func testResourceGetter() {
let resource = Utils.resource(\.Category)
}
Stack dump
error: compile command failed due to signal 6 (use -v to see invocation)
Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 578.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0. Program arguments: /Users/{my_username}/Library/Developer/Toolchains/swift-6.1.2-RELEASE.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file generic_keypath_test.swift -target arm64-apple-macosx16.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=5 -swift-version 6 -empty-abi-descriptor -resource-dir /Users/{my_username}/Library/Developer/Toolchains/swift-6.1.2-RELEASE.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir "/Users/{path_to_project}/swift-playground" -module-name my_binary -in-process-plugin-server-path /Users/{my_username}/Library/Developer/Toolchains/swift-6.1.2-RELEASE.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Users/{my_username}/Library/Developer/Toolchains/swift-6.1.2-RELEASE.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Users/{my_username}/Library/Developer/Toolchains/swift-6.1.2-RELEASE.xctoolchain/usr/local/lib/swift/host/plugins -target-sdk-version 15.5 -target-sdk-name macosx15.5 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -o /var/folders/pq/sqq5mbnx4810_9_5y0vw1scw0000gn/T/TemporaryDirectory.dURTVW/generic_keypath_test-1.o
1. Apple Swift version 6.1.2 (swift-6.1.2-RELEASE)
2. Compiling with the current language version
3. While evaluating request TypeCheckSourceFileRequest(source_file "generic_keypath_test.swift")
4. While evaluating request TypeCheckFunctionBodyRequest(my_binary.(file).testResourceGetter()@generic_keypath_test.swift:20:6)
5. While type-checking statement at [generic_keypath_test.swift:20:27 - line:22:1] RangeText="{
let resource = Utils.resource(\.Category, resource: \.nestedResource)
"
6. While type-checking declaration 0xaf4fdba88 (at generic_keypath_test.swift:21:5)
7. While evaluating request PatternBindingEntryRequest((unknown decl)@generic_keypath_test.swift:21:5, 0)
8. While type-checking expression at [generic_keypath_test.swift:21:20 - line:21:73] RangeText="Utils.resource(\.Category, resource: \.nestedResource"
9. While type-checking-target starting at generic_keypath_test.swift:21:26
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 swift-frontend 0x0000000109ed3854 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1 swift-frontend 0x0000000109ed1f80 llvm::sys::RunSignalHandlers() + 112
2 swift-frontend 0x0000000109ed3e98 SignalHandler(int) + 292
3 libsystem_platform.dylib 0x000000018c7aa704 _sigtramp + 56
4 libsystem_pthread.dylib 0x000000018c7a088c pthread_kill + 296
5 libsystem_c.dylib 0x000000018c6a5898 abort + 124
6 libsystem_c.dylib 0x000000018c6a4acc err + 0
7 swift-frontend 0x0000000109f5be8c (anonymous namespace)::TypeMemberDiffFinder::visit(swift::ide::api::SDKNode*) (.cold.2) + 0
8 swift-frontend 0x0000000105af2908 (anonymous namespace)::ExprRewriter::buildKeyPathPropertyComponent(swift::constraints::SelectedOverload const&, swift::SourceLoc, swift::constraints::ConstraintLocator*, llvm::SmallVectorImpl<swift::KeyPathExpr::Component>&) + 488
9 swift-frontend 0x0000000105ae14f4 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 17984
10 swift-frontend 0x000000010605d39c (anonymous namespace)::Traversal::doIt(swift::Expr*) + 988
11 swift-frontend 0x0000000106061694 swift::ArgumentList* llvm::function_ref<swift::ArgumentList* (swift::ArgumentList*)>::callback_fn<(anonymous namespace)::Traversal::doIt(swift::ArgumentList*)::'lambda'(swift::ArgumentList*)>(long, swift::ArgumentList*) + 264
12 swift-frontend 0x000000010605eecc swift::Pattern* (anonymous namespace)::Traversal::traverse<swift::Pattern>(swift::ASTWalker::PreWalkResult<swift::Pattern*>, llvm::function_ref<swift::Pattern* (swift::Pattern*)>, llvm::function_ref<swift::ASTWalker::PostWalkResult<swift::Pattern*> (swift::Pattern*)>) + 84
13 swift-frontend 0x000000010605ed04 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 108
14 swift-frontend 0x000000010605d09c (anonymous namespace)::Traversal::doIt(swift::Expr*) + 220
15 swift-frontend 0x000000010605cfb4 swift::Expr::walk(swift::ASTWalker&) + 32
16 swift-frontend 0x0000000105ad8814 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 636
17 swift-frontend 0x0000000105ad84c8 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 632
18 swift-frontend 0x0000000105d0061c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 384
19 swift-frontend 0x0000000105d00440 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 196
20 swift-frontend 0x0000000105d01658 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 124
21 swift-frontend 0x0000000105d018a0 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 264
22 swift-frontend 0x0000000105dd1e3c swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int) const + 1932
23 swift-frontend 0x00000001060ef478 swift::PatternBindingEntryRequest::OutputType swift::Evaluator::getResultUncached<swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()>(swift::PatternBindingEntryRequest const&, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()) + 228
24 swift-frontend 0x00000001060c050c swift::PatternBindingDecl::getCheckedPatternBindingEntry(unsigned int) const + 124
25 swift-frontend 0x0000000105d49424 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 2100
26 swift-frontend 0x0000000105d48bd8 swift::TypeChecker::typeCheckDecl(swift::Decl*) + 116
27 swift-frontend 0x0000000105dca540 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 152
28 swift-frontend 0x0000000105dc8d94 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
29 swift-frontend 0x0000000105dc8788 (anonymous namespace)::StmtChecker::typeCheckBody(swift::BraceStmt*&) + 32
30 swift-frontend 0x0000000105dc84e0 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1056
31 swift-frontend 0x000000010615b4a0 swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 240
32 swift-frontend 0x00000001060d6d40 swift::AbstractFunctionDecl::getTypecheckedBody() const + 112
33 swift-frontend 0x00000001061fec00 swift::SourceFile::typeCheckDelayedFunctions() + 96
34 swift-frontend 0x0000000105e0fb34 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 244
35 swift-frontend 0x0000000105e11394 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 232
36 swift-frontend 0x0000000105e0fa18 swift::performTypeChecking(swift::SourceFile&) + 84
37 swift-frontend 0x0000000104e1e994 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_0>(long, swift::SourceFile&) + 16
38 swift-frontend 0x0000000104e18438 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 68
39 swift-frontend 0x0000000104e183d4 swift::CompilerInstance::performSema() + 76
40 swift-frontend 0x0000000104bf08a8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
41 swift-frontend 0x0000000104be650c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 716
42 swift-frontend 0x0000000104be5d28 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2228
43 swift-frontend 0x00000001049c96b0 swift::mainEntry(int, char const**) + 3064
44 dyld 0x000000018c3e9924 start + 6400
Expected behavior
Attached code has to compile, both versions 1 and 2.
Because what works to access the Type, does not work for KeyPath.
Container.staticProperty
- ОК, \.staticProperty
- ОК
Container.Category
- ОК, \.Category
- WRONG
Environment
- Swift 6.1.2
- 6.2-snapshot-2025-07-17
MacOS 26.0 Beta (25A5306g)
Additional information
Tested with Swift 6.1.2 and Swift 6.2-snapshot-2025-07-17. Swift 6.2 does not give any helpful stack trace but also crashed, attaching swift 6.1.2 stack trace below.
JessyCatterwaul
Metadata
Metadata
Assignees
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareBug: A crash, i.e., an abnormal termination of softwareexpressionsFeature: expressionsFeature: expressionskey pathsFeature: key paths (both native and Objective-C)Feature: key paths (both native and Objective-C)type checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysis