Commit Graph

56 Commits

Author SHA1 Message Date
Slava Pestov
5c1b20d2bc stdlib: Build libswiftCompatibilitySpan.dylib compatibility shim
This dynamic library contains a copy of the standard library's
exported entry points for the Span and RawSpan types. This will
allow backward deployment of code that uses those new types.
2025-03-24 17:56:44 -04:00
Guillaume Lessard
c21de062bb Merge pull request #78561 from glessard/rdar137710901-span-properties-prototype
[SE-0456] Span properties (part 1)
2025-03-24 08:47:47 -07:00
Guillaume Lessard
d5cb7ddf64 [stdlib] changes to lifetime annotations
Changes to lifetime annotations implemented in https://github.com/swiftlang/swift/pull/80064
2025-03-21 17:24:45 -07:00
Andrew Trick
c6116be7b5 Update stdlib source with explicit @lifetime(copy self) 2025-03-19 11:59:04 -07:00
Andrew Trick
411a65be2e Add @lifetime annotation to RawSpan.init() and Span.init()
These empty initializers have @lifetime(immortal).
2025-03-19 11:59:04 -07:00
Guillaume Lessard
39ca3dee4d Merge pull request #80003 from glessard/span-gardening
[stdlib] fix `Span`'s `Sendable` conformance
2025-03-13 18:54:42 -07:00
Guillaume Lessard
e81a2ee3a1 [stdlib] correctly express Span sendability 2025-03-13 13:54:18 -07:00
Doug Gregor
f668feaf64 [Standard library] Additional strict-memory-safety annotations 2025-03-10 15:42:05 -07:00
Guillaume Lessard
940628a23a [stdlib] add newly-required unsafe annotations 2025-03-10 15:20:51 -07:00
Guillaume Lessard
85b53bb023 [stdlib] add bytes property to Span 2025-03-10 08:51:56 -07:00
Guillaume Lessard
7214717fba Merge pull request #79708 from glessard/span-gardening
[SE-0447] Span and RawSpan tests
2025-03-07 22:00:58 -08:00
Guillaume Lessard
4ec8c6e345 [stdlib] document unsafe behaviour for empty spans 2025-03-07 13:23:25 -08:00
Guillaume Lessard
3bd04acadb Apply suggestions from code review 2025-03-07 13:23:25 -08:00
Guillaume Lessard
5f24c66ea7 [stdlib] define properties also for ~Escapable elements
These should be available for any type of element `Span` can have in the future, including non-escapable elements. Making this change now avoids future churn.

Addresses rdar://146130842
2025-03-07 13:23:24 -08:00
Guillaume Lessard
dfb2e2f12e [stdlib] annotate uses of Range.init(_uncheckedBounds:) 2025-03-05 18:52:11 -08:00
Guillaume Lessard
66adb21502 [stdlib] annotate Span and RawSpan unsafe initializers 2025-03-05 17:15:03 -08:00
Guillaume Lessard
bd32aa5f6e [stdlib] remove an extracting overload
- `RangeExpression` implicitly involves bounds checking, so this was misleading.
2025-03-05 17:15:03 -08:00
Guillaume Lessard
712c79c711 [gardening] 80-column vigilance 2025-03-05 16:28:11 -08:00
Guillaume Lessard
1c1a845d67 [stdlib] fix withUnsafe[BufferPointer,Bytes] impls 2025-03-05 16:28:11 -08:00
Guillaume Lessard
10a0ee212e [stdlib] add an _extracting() overload for ClosedRange 2025-03-05 16:28:11 -08:00
Guillaume Lessard
89956e71ec [stdlib] use the Index typealias in Span
- these were missed in a previous code change
2025-03-05 16:28:11 -08:00
Guillaume Lessard
6802d22dc3 [stdlib] Update availability annotations for Span 2025-03-05 16:28:11 -08:00
Henrik G. Olsson
27a29c0baf [Swiftify][StrictMemorySafety] Test unsafe warnings in wrappers (NFC) (#79719)
[Swiftify][StrictMemorySafety] Test `unsafe` warnings in wrappers (NFC)

As _SwiftifyImport now emits the `unsafe` keyword to prevent warnings
about unsafe code in the macro expansions, we should make sure that our
tests do not emit any warnings. It turns out that calls to
RawSpan::withUnsafeRawPointer are not recognised as unsafe, so we
sometimes get warnings about using `unsafe` on a safe expression. This
issue is tracked under rdar://145899513.
2025-03-03 17:56:12 -08:00
Meghana Gupta
cf6c4c9422 Merge pull request #79699 from meg-gupta/prboundscheck
Add support for bounds check optimization of Span and InlineArray
2025-03-03 14:32:27 -08:00
Guillaume Lessard
fab65890fb [stdlib] remove now-unnecessary unsafe annotations 2025-03-01 20:29:11 -08:00
Meghana Gupta
1fd2689985 Remove Span._checkIndex overload 2025-02-28 19:12:03 -08:00
Meghana Gupta
6908e9b776 Annotate some Span and InlineArray's methods with semantics 2025-02-28 09:52:00 -08:00
Doug Gregor
22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
honghoker
7e30a91e47 fix: add missing whitespace in MARK comment 2025-02-20 21:57:08 +09:00
Andrew Trick
b08caffc37 Rename _unsafeLifetime to @unsafe _overrideLifetime and reformat.
Also, fix the _overrideLifetime doc comments.
2024-12-11 22:02:57 -08:00
Andrew Trick
496296e503 [stdlib] Fix RawSpan initializer lifetime dependencies.
Two are fixes needed in most of the `RawSpan` and `Span` initializers. For example:

```
    let baseAddress = buffer.baseAddress
    let span = RawSpan(_unchecked: baseAddress, byteCount: buffer.count)
    // As a trivial value, 'baseAddress' does not formally depend on the
    // lifetime of 'buffer'. Make the dependence explicit.
    self = _overrideLifetime(span, borrowing: buffer)
```

Fix #1. baseAddress needs to be a variable

`span` has a lifetime dependence on `baseAddress` via its
initializer. Therefore, the lifetime of `baseAddress` needs to include the call
to `_overrideLifetime`. The override sets the lifetime dependency of its result,
not its argument. It's argument still needs to be non-escaping when it is passed
in.

Alternatives:

- Make the RawSpan initializer `@_unsafeNonescapableResult`.

  Any occurrence of `@_unsafeNonescapableResult` actually signals a bug. We never
  want to expose this annotation.

  In addition to being gross, it would totally disable enforcement of the
  initialized span. But we really don't want to side-step `_overrideLifetime`
  where it makes sense. We want the library author to explicitly indicate that
  they understand exactly which dependence is unsafe. And we do want to
  eventually expose the `_overrideLifetime` API, which needs to be well
  understood, supported, and tested.

- Add lifetime annotations to a bunch of `UnsafePointer`-family APIs so the
  compiler can see that the resulting pointer is derived from self, where self is
  an incoming `Unsafe[Buffer]Pointer`. This would create a massive lifetime
  annotation burden on the `UnsafePointer`-family APIs, which don't really have
  anything to do with lifetime dependence. It makes more sense for the author of
  `Span`-like APIs to reason about pointer lifetimes.

Fix #2. `_overrideLifetime` changes the lifetime dependency of span to be on an
incoming argument rather than a local variable.

This makes it legal to escape the function (by assigning it to self). Remember
that self is implicitly returned, so the `@lifetime(borrow buffer)` tells the
compiler that `self` is valid within `buffer`'s borrow scope.
2024-12-11 22:02:57 -08:00
Andrew Trick
d7806ebb76 Span: Remove ownership modifiers and explicit copies from pointers.
Without this fix, the standard library source will break with shortly upcoming
compiler toolchain.

Never explicitly copy a pointer before passing it to an argument that is the
source of a lifetime dependency on the function's return value. That will always
raise a diagnostic error: depending on a temporary value is not the same as
depending on a variable. A temporary value's scope is only the current expression.

Also avoid using ownership modifiers for UnsafePointer. We don't want to treat
them like noncopyable types. They are simply values. Treating them like
noncopyable types creates a lot of overhead in the representation, which is
likely to interfere with diagnostics and optimization.
2024-11-20 11:44:18 -08:00
Meghana Gupta
92782a0e08 Add an initializer for Span without lifetime dependence
Without this, supported old compilers which don't recognize LifetimeDependence feature but
recognize the NonEscapableTypes feature will run into lifetime dependence inference errors
on the implicit initializer generated for the stdlib's swiftinterface
2024-11-18 18:09:19 -08:00
Meghana Gupta
5b5acc64e0 Promote Nonescapable types to a language feature 2024-11-18 18:09:17 -08:00
Guillaume Lessard
9084af187f [span] remove the _elementsEqual() functions
We should only add them once they reach their final form.
2024-11-18 16:18:17 -05:00
Guillaume Lessard
9936393183 [span] temporarily add a feature guard 2024-11-18 15:45:07 -05:00
Guillaume Lessard
9073576e84 [span] formulate Span.withUnsafeBytes() better 2024-11-18 15:14:16 -05:00
Guillaume Lessard
2b916a10b3 [gardening] improves some comments in Span and RawSpan 2024-11-18 14:24:56 -05:00
Guillaume Lessard
76d22cc675 [span] correctly express lifetimes for initializers 2024-11-11 23:31:16 -05:00
Guillaume Lessard
4fe9328b65 [span] follow the leading underscore rule
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2024-11-07 12:40:16 -08:00
Guillaume Lessard
386f041aa8 [span] fix an argument label
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2024-11-07 12:39:12 -08:00
Guillaume Lessard
0922b6b622 [span] improve argument labels for safe initializers 2024-11-07 11:19:28 -08:00
Guillaume Lessard
6272ad66a0 [span] address review feedback 2024-11-06 22:48:28 -08:00
Guillaume Lessard
c2abbbf4f6 [span] annotate lifetimes that can’t be correct 2024-10-25 18:45:10 -07:00
Guillaume Lessard
c4c9383658 [span] borrow pointer or buffer initializer parameters 2024-10-25 18:45:10 -07:00
Guillaume Lessard
fafd9d3690 [span] readability improvements 2024-10-25 18:45:10 -07:00
Guillaume Lessard
e37475bace [span] small ABI tweaks 2024-10-25 18:45:10 -07:00
Guillaume Lessard
57345f7f6f [span] remove _shrink functions added for review purposes 2024-10-23 18:19:21 -07:00
Guillaume Lessard
b508206844 [span] improve indices(of:) and byteOffsets(of:) 2024-10-23 18:19:21 -07:00
Guillaume Lessard
5d49bd60b7 [span] allow start pointers of empty spans to be exposed 2024-10-23 14:59:07 -07:00