Skip to content

[stdlib] adjust addressable attribute on String type family #82013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/core/StringGuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import SwiftShims
// functionality and guidance for efficiently working with Strings.
//
@frozen
@_addressableForDependencies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, d'oh. Shouldn't this also be applied to _StringObject? Substring? The Slice within that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Slice should not need to be @_addressableForDependencies. Only a value that has an in-line array-like representation that isn't using InlineArray already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that it would be transitively applied from whatever holds a _StringGuts (in a non-opaque way.) _StringObject is lower, but we don't need it directly to make a span, the information from _StringGuts is enough.

public // SPI(corelibs-foundation)
struct _StringGuts: @unchecked Sendable {
@usableFromInline
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/StringUTF8View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ extension String {
/// print(String(s1.utf8.prefix(15))!)
/// // Prints "They call me 'B"
@frozen
@_addressableForDependencies
public struct UTF8View: Sendable {
@usableFromInline
internal var _guts: _StringGuts
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/Substring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ extension Substring: LosslessStringConvertible {

extension Substring {
@frozen
@_addressableForDependencies
public struct UTF8View: Sendable {
@usableFromInline
internal var _slice: Slice<String.UTF8View>
Expand Down
8 changes: 4 additions & 4 deletions test/SILOptimizer/lifetime_dependence/semantics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public func testTrivialInoutBorrow(p: inout UnsafePointer<Int>) -> Span<Int> {

private let immortalInt = 0

private let immortalString = ""
private let immortalStrings: [String] = []
Copy link
Contributor Author

@glessard glessard Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If String is marked addressable, escape diagnostics behave slightly differently when it is a global. This seems incidental and could be addressed separately if it is an issue. I changed the type of this variable to get the expected diagnostics out of this test.


@lifetime(immortal)
func testImmortalInt() -> Span<Int> {
Expand All @@ -427,10 +427,10 @@ func testImmortalInt() -> Span<Int> {
}

@lifetime(immortal)
func testImmortalString() -> Span<String> {
let nilBasedBuffer = UnsafeBufferPointer<String>(start: nil, count: 0)
func testImmortalStrings() -> Span<[String]> {
let nilBasedBuffer = UnsafeBufferPointer<[String]>(start: nil, count: 0)
let span = Span(base: nilBasedBuffer.baseAddress, count: nilBasedBuffer.count)
return _overrideLifetime(span, borrowing: immortalString)
return _overrideLifetime(span, borrowing: immortalStrings)
}

let ptr = UnsafePointer<Int>(bitPattern: 1)!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ Func ContiguousArray.withUnsafeMutableBufferPointer(_:) is now without rethrows

// Adoption of @_addressableForDependencies
Struct CollectionOfOne is now with @_addressableForDependencies
Struct String.UTF8View is now with @_addressableForDependencies
Struct Substring.UTF8View is now with @_addressableForDependencies

Protocol CodingKey has added inherited protocol SendableMetatype
Protocol Error has added inherited protocol SendableMetatype
Expand Down
3 changes: 1 addition & 2 deletions test/api-digester/stability-stdlib-abi-without-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,7 @@ Func _SliceBuffer.withUnsafeMutableBufferPointer(_:) has mangled name changing f
Struct String.Index has added a conformance to an existing protocol CustomDebugStringConvertible

Struct CollectionOfOne is now with @_addressableForDependencies
Struct String.UTF8View is now with @_addressableForDependencies
Struct Substring.UTF8View is now with @_addressableForDependencies
Struct _StringGuts is now with @_addressableForDependencies

Enum _SwiftifyInfo is a new API without '@available'
Enum _SwiftifyExpr is a new API without '@available'
Expand Down