Skip to content

Crash when using Self in KeyPath expression #80669

Closed
webfutureiorepo/apple-swift
#779
@elijah-semyonov

Description

@elijah-semyonov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions