Skip to content

Runtime crash when using result builder via synthesized initializer #86205

@calda

Description

@calda

Description

The following code compiles successfully but crashes at runtime. The value.array is unexpectedly <uninitialized>.

Reproduction

@resultBuilder
struct StringArrayBuilder {
  static func buildBlock(_ elements: String...) -> [String] {
    elements
  }
}

struct MyValue {
  @StringArrayBuilder let array: [String]
}

let value = MyValue(array: {
  "foo"
  "bar"
})

print("Value?")
print(value.array.count) // Crashes because value.array is uninitialized

Stack dump

#0	0x00000001959fd178 in swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::incrementSlow ()
#1	(access of value.array)

Expected behavior

It should not crash, and the array should be initialized to ["foo", "bar"]. This works correctly if you change @StringArrayBuilder let array: [String] to @StringArrayBuilder let array: () -> [String], but that changes the semantics of the type.

Environment

Swift version 6.2.3 (swift-6.2.3-RELEASE)

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