Skip to content

Runtime crash (in the runtime libswiftCore) in getTypeContextDescriptor for property wrapper #85441

@AdamCmiel

Description

@AdamCmiel

Description

When initializing a struct with a property wrapper of type C where C is a class with generic parameter T where T is constrained by objc protocol P, we get a crash in the lazy protocol witness table accessor.

I've reproduced the crash with at least the final frames matching up in a simplified example in Xcode but it seems to only repro with specific arrangements of the module boundaries. Putting all the objc code in one module seems to avoid the crash for instance, and we have instances in our prod code where I cannot discern any difference from the crashing examples. The xcode example also crashes whether class C is defined in objc or in Swift (if the Swift class doesn't explicitly define the sendable conformance, I'll report that one separately).

Reproduction

// -- executable
// main.swift
import Foundation
import Wrapper

struct WithObjCGenerics {
    @Wrapper private var wrappedProperty: ObjCGenericType<any Model & SomeInterface>?
}

let wog = WithObjCGenerics() // EXE_BAD_ACCESS (code=1, address=0x0)

// main-bridging-header.h
#import "interface.h"

// interface.h
#import <Foundation/Foundation.h>
#import <ModelAndConstraint/ModelAndConstraint.h>
#import <ObjCGenericType/ObjCGenericType.h>

NS_ASSUME_NONNULL_BEGIN

@protocol SomeInterface <Constraint>
@end

typedef Model<SomeInterface> *SomeModel;

NS_ASSUME_NONNULL_END

// -- lib wrapper
// wrapper.swift
@propertyWrapper
public struct Wrapper<Value: Sendable>: Sendable {
    private let value: Value

    public init(wrappedValue valueProvider: @Sendable @escaping @autoclosure () -> Value) {
        self.value = valueProvider()
    }
    
    public var wrappedValue: Value {
        get { value }
    }
}

// -- lib ModelAndConstraint
// ModelAndConstraint.h

#import <Foundation/Foundation.h>

@protocol Constraint
@end

NS_SWIFT_SENDABLE
@interface Model: NSObject
@end

// ModelAndConstraint.m
#import "ModelProtocol.h"

@implementation Model
@end


// -- lib BaseType
// BaseType.h

#import <Foundation/Foundation.h>

@interface BaseType <__covariant DataType : id <NSObject>> : NSObject
@end

// BaseType.m
#import "DataObservable.h"

@implementation BaseType
@end


// -- lib ObjCGenericType
// ObjCGenericType.h
#import <Foundation/Foundation.h>
#import <ModelAndConstraint/ModelAndConstraint.h>
#import <BaseType/BaseType.h>

@interface WRP <__covariant ResponseType : id <Constraint>> : NSObject
@end

NS_SWIFT_SENDABLE
@interface ObjCGenericType <__covariant ResponseType : id <Constraint>> : BaseType <WRP<ResponseType> *>
@end

// ObjCGenericType.m
#import "PNObservable.h"

@implementation WRP
@end

@implementation ObjCGenericType
@end

executable depends on libWrapper, libModelAndConstraint, libBaseType, libObjCGenericType
libObjCGenericType depends on libModelAndConstraint, libBaseType

Stack dump

frame #0: 0x0000000195059054 libswiftCore.dylib`swift::TargetMetadata<swift::InProcess>::getTypeContextDescriptor() const + 4
    frame #1: 0x00000001950865e0 libswiftCore.dylib`swift::TargetMetadata<swift::InProcess>::getGenericArgs() const + 24
    frame #2: 0x00000001950a067c libswiftCore.dylib`instantiateWitnessTable(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolConformanceDescriptor<swift::InProcess> const*, void const* const*, void**) + 260
    frame #3: 0x0000000195093328 libswiftCore.dylib`swift::_getWitnessTable(swift::TargetProtocolConformanceDescriptor<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, void const* const*) + 4528
    frame #6: 0x00000001000011a0 StructGenericsCrash`property wrapper backing initializer of WithObjCGenerics.wrappedProperty() at main.swift:0
    frame #7: 0x0000000100000e10 StructGenericsCrash`StructGenericsCrash.WithObjCGenerics.init() -> StructGenericsCrash.WithObjCGenerics at main.swift:0
  * frame #8: 0x0000000100000dd4 StructGenericsCrash`main at main.swift:21:11
    frame #9: 0x000000018319eb98 dyld`start + 6076

Expected behavior

Either this should diagnose with a type-checking error about a missing protocol constraint or not crash at runtime

Environment

Xcode Version 26.0 (17A324)

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