Skip to content

Wrong UTF8Span span bytes from string #81931

Closed
2 of 2 issues completed
Closed
2 of 2 issues completed
@RandomHashTags

Description

@RandomHashTags

Description

Title describes it; observing different behavior for debug and release runs.

Reproduction

@Test
func stringUTF8SpanSpan() {
    let span = String(200).utf8Span.span
    for i in span.indices {
        if span[i] != 50 && span[i] != 48 {
            #expect(Bool(false)) // always fails in debug mode (never fails in release mode)
        }
    }
    #expect(span.indices.count == 3) // always passed
    #expect(span[0] == 50) // passes sometimes, but usually always fails
    #expect(span[1] == 48) // always fails
    #expect(span[2] == 48) // always fails
}

Expected behavior

UTF8Span's span bytes being equal to the contents of the buffer in

var string = String(200)
string.withUTF8 { buffer in }

Environment

Swift

Swift version 6.2-dev (LLVM 5e7837820b1c886, Swift d5ef256)
Target: x86_64-unknown-linux-gnu

Swift Toolchain

main-snapshot-2025-06-01

Swiftly

1.0.0

OS

Arch Linux (6.14.9-arch1-1)

IDE

VS Code (1.100.2)

Swift (VS Code Extension)

2.2.0

Additional information

No response

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.ownershipFeature: Ownership modifiers and semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions