You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UTF8Span instances obtained from a String in the small representation are not valid. They point to the wrong memory. In contrast, gettin a UTF8Span from a Span<UInt8> given out by UTF8View is fine. This is very surprising given that String.utf8Span is a thin wrapper over UTF8View.span.
Reproduction
letmaster=String(200) //"This string is not a small string."
func smallStringTest1(){lets= master
letutf8= s.utf8Span
letspan= utf8.span
foriin span.indices {print(span[i], terminator:"")}print()}smallStringTest1()func smallStringTest2(){lets= master.utf8
letview= s.span
letutf8=try!UTF8Span(validating: view)letspan= utf8.span
foriin span.indices {print(span[i], terminator:"")}print()}smallStringTest2()func smallStringTest3(){lets= master
letutf8= s.utf8Span
varit= utf8.makeUnicodeScalarIterator()whilelet c = it.next(){print(c, terminator:"")}print()}smallStringTest3()func smallStringTest4(){lets= master.utf8
letview= s.span
letutf8=try!UTF8Span(validating: view)varit= utf8.makeUnicodeScalarIterator()whilelet c = it.next(){print(c, terminator:"")}print()}smallStringTest4()
The text was updated successfully, but these errors were encountered:
glessard
added
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
ownership
Feature: Ownership modifiers and semantics
labels
Jun 5, 2025
From a quick glance, it looks like String isn't @_addressableForDependencies, which it would need to be in order for a Span to be able to depend on its internal representation.
Description
UTF8Span
instances obtained from aString
in the small representation are not valid. They point to the wrong memory. In contrast, gettin aUTF8Span
from aSpan<UInt8>
given out byUTF8View
is fine. This is very surprising given thatString.utf8Span
is a thin wrapper overUTF8View.span
.Reproduction
(also here: https://swift.godbolt.org/z/zv15z94MG)
The 1st and 3rd functions print surprising output. The 3rd sometimes crashes. Sample output:
Expected behavior
Given this code, we'd expect the output of the 1st and 3rd to be identical to the 2nd and 4th, respectively:
Note that when
master
's value is changed to a "large"String
, all the output is as expected.Environment
swift-DEVELOPMENT-SNAPSHOT-2025-06-03-a
Observed on macOS and Linux.
Code example: https://swift.godbolt.org/z/zv15z94MG
Additional information
Related to: #81931
Also tracked as rdar://152615664
The text was updated successfully, but these errors were encountered: