Commit Graph

67 Commits

Author SHA1 Message Date
Stephen Canon
ee9b38ca22 Use an explicit unsigned comparison for span index checks (#83150)
LLVM ought to be able to do this transformation for us, but it currently fails to do so. We can code around it easily enough. https://github.com/swiftlang/swift/pull/83172 has a better long-term fix.
2025-07-21 16:06:17 -04:00
Guillaume Lessard
2689ef31e1 Merge pull request #82959 from glessard/rdar155474776-extracting
[stdlib] Implementation for SE-0488
2025-07-15 13:59:00 -07:00
Guillaume Lessard
dd833569e0 [stdlib] rename Span.extracting functions 2025-07-09 17:25:27 -07:00
Guillaume Lessard
619ec95693 [stdlib] add no-parameter initializers for Span types
This was an addition in SE-0485.
2025-07-07 17:57:12 -07:00
Valeriy Van
43ead66f42 Fix doc comment 2025-06-09 11:56:45 +03:00
Valeriy Van
c14699310f Fix typo (#82052) 2025-06-07 23:16:43 -04:00
Andrew Trick
e9d11283b9 Add _overrideLifetime to Span APIs
The implementation previously took advantage of a missing diagnostic. They
returned a borrowed Span with a dependency on a copied argument. This illegally
promotes the dependency. This is what the author intended to do (the
implementation was correct) but the compiler can't know that so the code needs
to be explicit about overriding the lifetime.
2025-04-25 17:58:24 -07:00
Slava Pestov
17868a7b50 CompatibilitySpan: Fix stdlib build on visionOS
Swift CI tests visionOS, but PR testing does not. Oops.
2025-04-09 12:37:45 -04:00
Slava Pestov
43da773685 stdlib: Update {Mutable,}{Raw,}Span availability for backward deployment 2025-04-07 21:32:48 -04:00
Allan Shortlidge
d2e8edfc93 stdlib: Address StrictMemorySafety warnings in Span related code. 2025-03-31 16:45:08 -07:00
Ben Rimmington
2f9f5ffd85 [stdlib] Update element constraints of Span 2025-03-26 22:32:08 +00:00
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