Skip to content

Runtime crash in property wrapper init #85442

@AdamCmiel

Description

@AdamCmiel

Description

Also found when investigating #85441:

When initializing a struct with a property wrapper PW of type C where C is a swift class with generic parameter T where T is constrained by objc protocol P but not meeting the PW's generic constraint, we get a crash in the getTypeContextDescriptor rather than a type-checking error.

Reproduction

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

class CFromSwift<Param: Constraint>: @unchecked Sendable {}

struct WithObjCGenerics {
    @Wrapper private var wrappedProperty: CFromSwift<SomeModel>? = nil
}

let wog = WithObjCGenerics()

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

// interface.h
#import <Foundation/Foundation.h>
#import <ModelAndConstraint/ModelAndConstraint.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
@implementation Model
@end

executable depends on libWrapper, libModelAndConstraint

Stack dump

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xfffffffffffffff8)
    frame #0: 0x0000000100001590 StructGenericsCrash`Wrapper.init(valueProvider=0x0000000000000000) at <stdin>:0
    frame #1: 0x0000000100000e14 StructGenericsCrash`property wrapper backing initializer of WithObjCGenerics.wrappedProperty() at main.swift:0
    frame #2: 0x0000000100000c80 StructGenericsCrash`StructGenericsCrash.WithObjCGenerics.init() -> StructGenericsCrash.WithObjCGenerics at main.swift:0
  * frame #3: 0x0000000100000c44 StructGenericsCrash`main at main.swift:19:11
    frame #4: 0x000000018319eb98 dyld`start + 6076

Expected behavior

Either a type-checking error for the property wrapper or no crash at runtime

Environment

Xcode Version 26.0 (17A324)

Additional information

swapping SomeModel in main.swift for any SomeInterface will no longer crash

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