Skip to content

Crash when using Self in KeyPath expression #80669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
elijah-semyonov opened this issue Apr 9, 2025 · 1 comment · May be fixed by #80913
Open

Crash when using Self in KeyPath expression #80669

elijah-semyonov opened this issue Apr 9, 2025 · 1 comment · May be fixed by #80913
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@elijah-semyonov
Copy link

Description

The program crashes with Fatal error: could not demangle keypath type from '��.

Reproduction

public func type<Root, Value>(at keyPath: KeyPath<Root, Value>) -> Value.Type {
    return Value.self
}

extension Int {
    static func bar() {
        print("Hello")
    }
}

class Foo {
    var int = 10
    
    static func staticFunc() {
        type(at: \Self.int).bar()
    }
}

Foo.staticFunc() // crashes
public func type<Root, Value>(at keyPath: KeyPath<Root, Value>) -> Value.Type {
    return Value.self
}

extension Int {
    static func bar() {
        print("Hello")
    }
}

class Foo {
    var int = 10
    
    static func staticFunc() {
        type(at: \Foo.int).bar()
    }
}

Foo.staticFunc() // Prints `Hello` correctly

Stack dump

#0	0x00000001adb1c95c in _swift_runtime_on_report ()
#1	0x00000001adbf49c8 in _swift_stdlib_reportFatalErrorInFile ()
#2	0x00000001ad7bc080 in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never ()
#3	0x00000001ad7bb1f8 in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never ()
#4	0x00000001ad8ed61c in Swift._resolveKeyPathGenericArgReference(_: Swift.UnsafeRawPointer, genericEnvironment: Swift.Optional<Swift.UnsafeRawPointer>, arguments: Swift.Optional<Swift.UnsafeRawPointer>) -> Swift.UnsafeRawPointer ()
#5	0x00000001ad8ed9cc in generic specialization <Swift.GetKeyPathClassAndInstanceSizeFromPattern> of Swift._walkKeyPathPattern<τ_0_0 where τ_0_0: Swift.KeyPathPatternVisitor>(_: Swift.UnsafeRawPointer, walker: inout τ_0_0) -> () ()
#6	0x00000001ad8ece30 in Swift._getKeyPathClassAndInstanceSizeFromPattern(Swift.UnsafeRawPointer, Swift.UnsafeRawPointer) -> (keyPathClass: Swift.AnyKeyPath.Type, rootType: Any.Type, size: Swift.Int, alignmentMask: Swift.Int) ()
#7	0x00000001ad8ecc04 in Swift._swift_getKeyPath(pattern: Swift.UnsafeMutableRawPointer, arguments: Swift.UnsafeRawPointer) -> Swift.UnsafeRawPointer ()

Expected behavior

No crash should happen

Environment

Swift compiler in toolchain shipped with Xcode 16.3 (16E140)

Additional information

No response

@elijah-semyonov elijah-semyonov added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Apr 9, 2025
@stzn
Copy link
Contributor

stzn commented Apr 13, 2025

While debugging the compiler, I encountered the following crash with the same code:

SIL verification failed: can't upcast to same type: UI->getType() != UI->getOperand()->getType()
Verifying instruction:
     %4 = load [take] %2 : $*Foo                  // user: %5
->   %5 = upcast %4 : $Foo to $Foo                // users: %11, %6
     %6 = begin_borrow %5 : $Foo                  // users: %9, %8, %7
     destroy_value %5 : $Foo                      // id: %11
In function:
// key path getter for Foo.other : Self
sil shared [thunk] [ossa] @$s4main3FooC5otherACvpACXDTK : $@convention(keypath_accessor_getter) (@in_guaranteed Foo) -> @out Foo {
// %0                                             // user: %10
// %1                                             // user: %3
bb0(%0 : $*Foo, %1 : $*Foo):
  %2 = alloc_stack $Foo                           // users: %12, %4, %3
  copy_addr %1 to [init] %2                       // id: %3
  %4 = load [take] %2                             // user: %5
  %5 = upcast %4 to $Foo                          // users: %11, %6
  %6 = begin_borrow %5                            // users: %9, %8, %7
  %7 = class_method %6, #Foo.other!getter : (Foo) -> () -> Foo, $@convention(method) (@guaranteed Foo) -> @owned Foo // user: %8
  %8 = apply %7(%6) : $@convention(method) (@guaranteed Foo) -> @owned Foo // user: %10
  end_borrow %6                                   // id: %9
  store %8 to [init] %0                           // id: %10
  destroy_value %5                                // id: %11
  dealloc_stack %2                                // id: %12
  %13 = tuple ()                                  // user: %14
  return %13                                      // id: %14
} // end sil function '$s4main3FooC5otherACvpACXDTK'
Stack dump:
0.      Program arguments: /usr/bin/swift-frontend -frontend -interpret - -disable-objc-interop -color-diagnostics -enable-bare-slash-regex -empty-abi-descriptor -resource-dir /usr/lib/swift -no-auto-bridging-header-chaining -module-name main -in-process-plugin-server-path /usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /usr/lib/swift/host/plugins -plugin-path /usr/local/lib/swift/host/plugins
1.      Swift version 6.2-dev (LLVM a3e32cdaae72f3d, Swift fa8609703528715)
2.      Compiling with effective version 5.10
3.      While evaluating request ASTLoweringRequest(Lowering AST to SIL for module main)
4.      While verifying SIL function "@$s4main3FooC5otherACvpACXDTK".
 for <<debugloc at "<compiler-generated>":0:0>>
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 0x00006316761cd618
1  swift-frontend 0x00006316761cb13e
2  swift-frontend 0x00006316761cdcb1
3  libc.so.6      0x00007db1ff5ca520
4  libc.so.6      0x00007db1ff61e9fc pthread_kill + 300
5  libc.so.6      0x00007db1ff5ca476 raise + 22
6  libc.so.6      0x00007db1ff5b07f3 abort + 211
7  swift-frontend 0x000063166f9fab99
8  swift-frontend 0x000063166fa17837
9  swift-frontend 0x000063166fa0478f
10 swift-frontend 0x000063166fa02b9b
11 swift-frontend 0x000063166f9fad9b
12 swift-frontend 0x000063166f9fe6a8
13 swift-frontend 0x000063166f9fe55f
14 swift-frontend 0x000063166ee157a0
15 swift-frontend 0x000063166ee20b6d
16 swift-frontend 0x000063166ef2d026
17 swift-frontend 0x000063166ee258e3
18 swift-frontend 0x000063166ee20d33
19 swift-frontend 0x000063166e615240
20 swift-frontend 0x000063166e6264ea
21 swift-frontend 0x000063166e617f5e
22 swift-frontend 0x000063166e617054
23 swift-frontend 0x000063166e3b45ab
24 libc.so.6      0x00007db1ff5b1d90
25 libc.so.6      0x00007db1ff5b1e40 __libc_start_main + 128
26 swift-frontend 0x000063166e3b35a5

Environment: Swift version 6.2-dev (LLVM a3e32cdaae72f3d, Swift fa86097)

It seems that in the case of \Self, the baseType is DynamicSelfType, which prevents retrieving the baseClass using getClassOrBoundGenericClass.
https://github.com/swiftlang/swift/blob/main/lib/SILGen/SILGenExpr.cpp#L3402

If this is the cause, is it okay for me to deal with this issue?

@stzn stzn linked a pull request Apr 18, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants