Skip to content

Commit 0531f20

Browse files
committed
StringGuts: Annotate decls with effects(readonly).
Annotating the unmanaged*Views with @effects(readonly) allows non-inlining callers such as NSContiguousString to avoid having to conservatively keep self alive for fear of side effects.
1 parent e65698c commit 0531f20

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -338,30 +338,35 @@ extension _StringGuts {
338338
@_inlineable
339339
internal
340340
var _unmanagedASCIIView: _UnmanagedString<UInt8> {
341-
_sanityCheck(_object.isContiguousASCII)
342-
if _object.isUnmanaged {
343-
return _asUnmanaged()
344-
} else if _object.isNative {
345-
return _object.nativeStorage(of: UInt8.self).unmanagedView
346-
} else {
347-
_sanityCheck(_object.isContiguousCocoa)
348-
return _asContiguousCocoa(of: UInt8.self)
341+
@effects(readonly)
342+
get {
343+
_sanityCheck(_object.isContiguousASCII)
344+
if _object.isUnmanaged {
345+
return _asUnmanaged()
346+
} else if _object.isNative {
347+
return _object.nativeStorage(of: UInt8.self).unmanagedView
348+
} else {
349+
_sanityCheck(_object.isContiguousCocoa)
350+
return _asContiguousCocoa(of: UInt8.self)
351+
}
349352
}
350353
}
351354

352355
@_versioned
353356
@_inlineable
354357
internal
355358
var _unmanagedUTF16View: _UnmanagedString<UTF16.CodeUnit> {
356-
_sanityCheck(_object.isContiguousUTF16)
357-
if _object.isUnmanaged {
358-
return _asUnmanaged()
359-
} else if _object.isNative {
360-
return _object.nativeStorage(of: UTF16.CodeUnit.self).unmanagedView
361-
} else if _object.isCocoa {
362-
return _asContiguousCocoa(of: UTF16.CodeUnit.self)
363-
} else {
364-
fatalError("Small strings aren't contiguous")
359+
@effects(readonly)
360+
get {
361+
_sanityCheck(_object.isContiguousUTF16)
362+
if _object.isUnmanaged {
363+
return _asUnmanaged()
364+
} else if _object.isNative {
365+
return _object.nativeStorage(of: UTF16.CodeUnit.self).unmanagedView
366+
} else {
367+
_sanityCheck(_object.isContiguousCocoa)
368+
return _asContiguousCocoa(of: UTF16.CodeUnit.self)
369+
}
365370
}
366371
}
367372
}

0 commit comments

Comments
 (0)