Commit Graph

439 Commits

Author SHA1 Message Date
Doug Gregor
7088022eef [Standard library] Eliminate ABI-only rethrows versions from Embedded
Where we have rethrowing versions of functions that have typed-throws
counterparts that are only retained for ABI compatibility, wrap them
in `#if !$Embedded` so they aren't compiled into the Embedded version
of the standard library. This eliminates warnings about this code,
which cannot actually be used with arbitrary errors anyway.
2025-09-17 13:09:49 -07:00
Changhyun-Kyle
1b97c574a4 [stdlib] Fix typo in Collection.swift 2024-08-26 18:53:05 +09:00
Allan Shortlidge
20d97556ae stdlib: Remove #if $TypedThrows guards.
https://github.com/swiftlang/swift/pull/72612 can be reverted because it is no
longer necessary for the interface of the stdlib to be compatible with
compilers without `$TypedThrows` support.
2024-07-08 16:52:52 -07:00
Kuba Mracek
bd5913a176 [embedded] Make RNG APIs available on embedded Swift 2024-01-17 14:53:24 -08:00
Doug Gregor
af79dd815f Improve ABI-only rethrowing map shims based on code review
Replace the hackish use of `@_disfavoredOverload` with the more principled
use of `@_silgen_name` for the entrypoint we are maintaining, then rename
these functions in source to `__rethrows_map` to indicate what they're for.

While here, make them `throws` instead of `rethrows`. The ABI is the
same, and `throws` allows us do avoid to do/catch tricks with rethrows
functions.
2024-01-12 10:17:59 -08:00
Doug Gregor
9e4e9ea400 Replace rethrowing map with generic typed throws
Adopt typed throws for the `map` operation to propagate thrown error
types through the `map` API. This is done in a manner that is backward
compatible for almost all cases:

* The new typed-throws entrypoint is `@_alwaysEmitIntoClient` so it
can back-deploy all the way back.
* The old `rethrows` entrypoint is left in place, using
`@usableFromInline` with `internal` so it is part of the ABI but not
the public interface, and `@_disfavoredOverload` so the type checker
avoids it while building the standard library itself. The old
entrypoint is implemented in terms of the new one.

Note that the implementation details for the existential collection
"box" classes rely on method overriding of `_map` operations, and the
types are frozen, so  we don't get to change their signatures. However,
these are only implementations, not API: the actual API `map`
functions can be upgraded to typed throws.

Note that this code makes use of a known hole in `rethrows` checking
to allow calling between `rethrows` and typed throws. We'll need to do
something about this for source-compatibility reasons, but I'll follow
up with that separately.
2024-01-12 10:17:58 -08:00
Doug Gregor
3baf6ac31a Revert "[Typed throws] Support overrides that are contravariant in the thrown error" 2023-11-16 10:40:23 -08:00
Doug Gregor
1892396e14 Improve ABI-only rethrowing map shims based on code review
Replace the hackish use of `@_disfavoredOverload` with the more principled
use of `@_silgen_name` for the entrypoint we are maintaining, then rename
these functions in source to `__rethrows_map` to indicate what they're for.

While here, make them `throws` instead of `rethrows`. The ABI is the
same, and `throws` allows us do avoid to do/catch tricks with rethrows
functions.
2023-11-13 11:47:00 -08:00
Doug Gregor
0d15d4edbe Replace rethrowing map with generic typed throws
Adopt typed throws for the `map` operation to propagate thrown error
types through the `map` API. This is done in a manner that is backward
compatible for almost all cases:

* The new typed-throws entrypoint is `@_alwaysEmitIntoClient` so it
can back-deploy all the way back.
* The old `rethrows` entrypoint is left in place, using
`@usableFromInline` with `internal` so it is part of the ABI but not
the public interface, and `@_disfavoredOverload` so the type checker
avoids it while building the standard library itself. The old
entrypoint is implemented in terms of the new one.

Note that the implementation details for the existential collection
"box" classes rely on method overriding of `_map` operations, and the
types are frozen, so  we don't get to change their signatures. However,
these are only implementations, not API: the actual API `map`
functions can be upgraded to typed throws.

Note that this code makes use of a known hole in `rethrows` checking
to allow calling between `rethrows` and typed throws. We'll need to do
something about this for source-compatibility reasons, but I'll follow
up with that separately.
2023-11-13 11:47:00 -08:00
Philippe Hausler
7a2c21d312 [Embedded] enable random APIs if a RandomNumberGenerator is provided, but leave system random number generators unavailable (#69645) 2023-11-03 21:12:22 -07:00
Kuba Mracek
ae2e903574 [embedded] Build an initial embedded Swift standard library
This isn't a "complete" port of the standard library for embedded Swift, but
something that should serve as a starting point for further iterations on the
stdlib.

- General CMake logic for building a library as ".swiftmodule only" (ONLY_SWIFTMODULE).
- CMake logic in stdlib/public/core/CMakeLists.txt to start building the embedded stdlib for a handful of hardcoded target triples.
- Lots of annotations throughout the standard library to make types, functions, protocols unavailable in embedded Swift (@_unavailableInEmbedded).
- Mainly this is about stdlib functionality that relies on existentials, type erasure, metatypes, reflection, string interpolations.
- We rely on function body removal of unavailable functions to eliminate the actual problematic SIL code (existentials).
- Many .swift files are not included in the compilation of embedded stdlib at all, to simplify the scope of the annotations.
- EmbeddedStubs.swift is used to stub out (as unavailable and fatalError'd) the missing functionality.
2023-09-16 12:38:46 -07:00
Karoy Lorentey
7a7ebd8970 [stdlib] Adopt primary associated types in the stdlib 2022-05-09 18:06:17 -07:00
Karoy Lorentey
5293bde558 [stdlib] Collection: simplify default _failEarlyRangeCheck implementations
Currently, the default implementations of the various `_failEarlyRangeCheck` forms contain several _precondition invocations, like this:

```
@inlinable
  public func _failEarlyRangeCheck(_ index: Index, bounds: Range<Index>) {
    _precondition(
      bounds.lowerBound <= index,
      "Out of bounds: index < startIndex")
    _precondition(
      index < bounds.upperBound,
      "Out of bounds: index >= endIndex")
  }
```

Each such precondition call generates a separate trap instruction, which seems like a waste — theoretically it would be helpful to know which condition was violated, but in practice, that information tends not to be surfaced anyway. Combining these will lead to a minuscule code size improvement.

(The separate messages do surface these in debug builds, but only if these generic defaults actually execute, which isn’t often. These are not public entry points, so concrete collection types tend ignore these and roll their own custom index validation instead. From what I’ve seen, custom code tends to combine the upper/lower checks into a single precondition check. These underscored requirements tend to be only called by the standard `Slice`; and it seems reasonable for that to follow suit.)

More interestingly, the range-in-range version of `_failEarlyRangeCheck` performs two times as many checks than are necessary:

```
  @inlinable
  public func _failEarlyRangeCheck(_ range: Range<Index>, bounds: Range<Index>) {
    _precondition(
      bounds.lowerBound <= range.lowerBound,
      "Out of bounds: range begins before startIndex")
    _precondition(
      range.lowerBound <= bounds.upperBound,
      "Out of bounds: range ends after endIndex")
    _precondition(
      bounds.lowerBound <= range.upperBound,
      "Out of bounds: range ends before bounds.lowerBound")
    _precondition(
      range.upperBound <= bounds.upperBound,
      "Out of bounds: range begins after bounds.upperBound")
  }
```

It is safe to assume that `range.lowerBound <= range.upperBound`, so it’s enough to test that `bounds.lowerBound <= range.lowerBound && range.upperBound <= bounds.upperBound` — we don’t need to check each combination separately.
2022-02-25 17:06:08 -08:00
Slava Pestov
c0adf40758 stdlib: Move Generator and _Element typealiases to Sequence and Collection protocols
The requirement machine does not allow 'where' clauses to reference
typealiases defined in protocol extensions.

These should probably be removed entirely since they're not useful
anymore, but I'd rather that decision was made by the stdlib folks.
2022-02-17 19:40:58 -05:00
Alex Martini
f1dfaaa663 Fix mentions of nonexistent 'end' parameter.
Follow-on to commit 4c82b7705c.
2021-12-13 11:00:44 -08:00
Alex Martini
e6cbf54832 Merge pull request #40454 from amartini51/doc_fixes_main
Cherry pick documentation fixes to 'main'
2021-12-13 10:31:41 -08:00
Karoy Lorentey
dd40ff2929 [stdlib][NFC] Put stored properties & primary initializers before other members in struct declarations 2021-10-05 22:01:35 -07:00
Alex Martini
d8aa4deacb Also fix parameter name in parameter docs.
Follow-on to commit 36832a8d47.

(cherry picked from commit 4c82b7705c)
2021-08-26 17:03:47 -07:00
Alex Martini
16a7cc390a Correct parameter names in doc comments.
Fixes rdar://76254761.

(cherry picked from commit 36832a8d47)
2021-08-26 17:03:47 -07:00
Karoy Lorentey
849f6b9818 [stdlib] Update SubSequence docs 2021-07-27 18:40:23 -07:00
Guillaume Lessard
7d6260560e [stdlib] fix an accidental recursion bug involving Collection
- adds a default implementation of Collection’s subscript(bounds: Range<_>)
  with the most general signature possible
- it is marked unavailable in order to prevent the
  infinite recursion bug reported in SR-14848
- Collections whose SubSequence is Slice<Self> still get the proper default, as intended.
2021-06-30 21:58:48 -06:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07:00
Doug Gregor
1a1f79c0de Introduce safety checkin for ConcurrentValue conformance.
Introduce checking of ConcurrentValue conformances:
- For structs, check that each stored property conforms to ConcurrentValue
- For enums, check that each associated value conforms to ConcurrentValue
- For classes, check that each stored property is immutable and conforms
  to ConcurrentValue

Because all of the stored properties / associated values need to be
visible for this check to work, limit ConcurrentValue conformances to
be in the same source file as the type definition.

This checking can be disabled by conforming to a new marker protocol,
UnsafeConcurrentValue, that refines ConcurrentValue.
UnsafeConcurrentValue otherwise his no specific meaning. This allows
both "I know what I'm doing" for types that manage concurrent access
themselves as well as enabling retroactive conformance, both of which
are fundamentally unsafe but also quite necessary.

The bulk of this change ended up being to the standard library, because
all conformances of standard library types to the ConcurrentValue
protocol needed to be sunk down into the standard library so they
would benefit from the checking above. There were numerous little
mistakes in the initial pass through the stsandard library types that
have now been corrected.
2021-02-04 03:45:09 -08:00
swift-ci
e25934fa4b Merge pull request #32451 from stephencelis/patch-1 2020-06-28 10:45:46 -07:00
Alex Martini
a3a20d923b Use a shorter example string. (#32557)
Fixes <rdar://problem/64180813>.
2020-06-26 14:18:27 -05:00
Stephen Celis
3b8d8b776e Update Collection.swift 2020-06-18 15:44:33 -04:00
Stephen Celis
0ce01d5dfe Update Collection.swift 2020-06-18 15:42:22 -04:00
Stephen Celis
ce78396eef Update stdlib/public/core/Collection.swift
Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
2020-06-18 15:41:35 -04:00
Stephen Celis
099ecbe0b0 Improve performance of Collection.removeFirst(_:) where Self == SubSequence 2020-06-18 15:29:45 -04:00
Paul Hudson
06f82a53b5 Replaced the majority of ' : ' with ': '. 2019-07-18 20:46:07 +01:00
Ben Cohen
e9d4687e31 De-underscore @frozen, apply it to structs (#24185)
* De-underscore @frozen for enums

* Add @frozen for structs, deprecate @_fixed_layout for them

* Switch usage from _fixed_layout to frozen
2019-05-30 17:55:37 -07:00
John McCall
8be4ec32e6 Protocol requirement overrides must match in mutating-ness.
Without this change, SILGen will crash when compiling a use of the
derived protocol's requirement: it will instead attempt to use
the base protocol's requirement, but the code will have been
type-checked incorrectly for that.

This has a potential for source-compatibility impact if anyone's
using explicit override checking for their protocol requirements:
reasonable idioms like overriding a mutating requirement with a
non-mutating one will no longer count as an override.  However,
this is arguably a bug-fix, because the current designed intent
of protocol override checking is to not allow any differences in
type, even "covariant" changes like making a mutating requirement
non-mutating.  Moreover, we believe explicit override checking in
protocols is quite uncommon, so the overall compatibility impact
will be low.

This also has a potential for ABI impact whenever something that
was once an override becomes a non-override and thus requires a
new entry.  It might require a contrived test case to demonstrate
that while using the derived entry, but it's quite possible to
imagine a situation where the derived entry is not used directly
but nonetheless has ABI impact.

Furthermore, as part of developing this patch (earlier versions of
which used stricter rules in places), I discovered a number of
places where the standard library was unintentionally introducing
a new requirement in a derived protocol when it intended only to
guide associated type deduction.  Fixing that (as I have in this
patch) *definitely* has ABI impact.
2019-01-30 01:33:09 -05:00
John McCall
91a7c7635e Merge pull request #20415 from rjmccall/stdlib-borrowed-storage
Use @_borrowed on a few declarations in the stdlib and overlays
2018-11-28 21:29:27 -05:00
Ben Cohen
df2307e035 [stdlib][DNM] Collapse sequence and collection wrappers (#20221)
* Concretize dropFirst/Last/sufix/prefix from Sequence

Remove split customization point

Eliminate SubSequence from Sequence protocol

Collapse LazyCollection

Collapse LazyMapCollection

Eliminate _SequenceWrapper

Collapse LazyFilterCollection

Collapse LazyDrop/PrefixWhileCollection

Fix tests, ABI stability update

Collapse FlattenSequence

* Add entries to source/ABI compatible expected results.

* Update tests to avoid pre-10.14 objc runtime bug

* Expunge _preprocessingPass
2018-11-14 10:05:58 -08:00
Azoy
309f46ebba condense index to one line 2018-11-08 17:50:16 -06:00
John McCall
87e9fb5560 Use @_borrowed on a few declarations in the stdlib and overlays.
Most of the stdlib's properties don't need @_borrowed because they're
@inlinable, but I did find one place in an overlay where it's probably
sensible to make the operation use generalized accessors even if
they're resilient.
2018-11-08 12:19:09 -05:00
Azoy
68a2a2ca3d remember to use the generator 2018-11-04 23:24:54 -06:00
Azoy
7af435c22a clean up some random generator calls 2018-11-04 22:38:34 -06:00
Ben Cohen
dbc2e21522 Remove customization points from Sequence and Collection (#19995) 2018-10-31 18:58:00 -07:00
Nate Cook
dfb01b6a6a [stdlib] Minor documentation revisions (#20045)
* [stdlib] Minor documentation revisions
* [docs] Convert 'nonoptional' to 'non-optional'
We're switching to 'non-optional' across the board, as the unhyphenated
form is too easy to read as 'no-noptional'.
2018-10-26 00:05:45 -05:00
Ben Cohen
2b9544966f Switch .first to use an index not an iterator (#18075) 2018-10-08 20:44:38 -07:00
Karoy Lorentey
4615e18366 [stdlib] Force-inline trivial default implementations for hidden Sequence/Collection customization points
This is usually a code size pessimization, but in this case the bodies are trivial, so inlining them eliminates a call + a conditional branch.
2018-10-03 13:18:16 +01:00
Ben Cohen
ae6f5dd604 [stdlib] Add consuming/owned annotations to Collection implementations (#19360)
* Add consuming/owned annotations to Collection implementations

* Update SILOptimizer tests

* Fix access_marker_verify test

* XFAIL reconstruct_type_from_mangled_name
2018-09-21 12:06:56 -07:00
Doug Gregor
7bc7b1e22e [Standard library] Downgrade non-ABI ABI FIXMEs to FIXMEs.
Now that we have removed overriding protocol requirements from witness
tables, they no longer have any effect on the ABI. Replace the FIXME
(ABI) comments with normal FIXMEs: there is no more ABI work to do
here.
2018-09-05 22:01:06 -07:00
Doug Gregor
f8e53d9129 [Standard library] Audit protocol member overrides in protocols.
Add the `-warn-implicit-overrides` flag when building the standard library
and overlays, so that each protocol member that overrides a member of an
inherited protocol will produce a warning unless annotated with either
‘override’ or ‘@_nonoverride’.

An annotation of `override` will mean that the overriding requirement will be treated identically to the overridden declaration. If for some reason a concrete type’s conformance to the inheriting protocol provides a different witness for the overriding requirement than the conformance to the inherited protocol’s witness for the overridden requirement, the witness for the inheriting (more-specialized) protocol will be ignored. A protocol requirement marked ‘override’ only makes sense when the declaration is needed to help associated type inference, which is why the ‘override’ annotations correlate so closely with ABI FIXMEs.

An annotation of `@_nonoverride` means that the two protocol requirements will be treated independently, and may be bound to different witnesses. Use `@_nonoverride` when we might need different witnesses, e.g., because the semantics of the potentially-overriding declaration differ from that of the potentially-overridden declaration. `BidirectionalCollection.index(_:offsetBy:)` is the most obvious example, because the `BidirectionalCollection` ’s version of `index(_:offsetBy:)` allows negative indices. `RandomAccessCollection` ’s version is also marked `@_nonoverride` because it is required to be asymptotically faster than the `Collection` or `BidirectionalCollection` versions.
2018-09-05 13:51:26 -07:00
Nate Cook
908ac9f43b Revise note about future random algorithm changes 2018-09-03 22:16:23 -05:00
Nate Cook
8174511a3f Add notes about future changes to high-level random algorithms 2018-09-03 22:16:23 -05:00
Nate Cook
efb0415a61 [stdlib] More documentation revisions (#18263)
- Fix error in `last(where:)` example
- Improve MemoryLayout, UnsafePointer, and integer operator discussions
- Clean up ranges and random APIs
- Revisions to overflow operators and the SignedNumeric requirements
- Standardize on 'nonoptional' in remaining uses
2018-08-08 00:25:09 -05:00
Nate Cook
3d7dfc232b [stdlib] Update complexity docs for seq/collection algorithms (#17254)
* [stdlib] Update complexity docs for seq/collection algorithms

This corrects and standardizes the complexity documentation for Sequence
and Collection methods. The use of constants is more consistent, with `n`
equal to the length of the target collection, `m` equal to the length of
a collection passed in as a parameter, and `k` equal to any other passed
or calculated constant.

* Apply notes from @brentdax about complexity nomenclature

* Change `n` to `distance` in `index(_:offsetBy:)`

* Use equivalency language more places; sync across array types

* Use k instead of n for parameter names

* Slight changes to index(_:offsetBy:) discussion.

* Update tests with new parameter names
2018-07-24 01:01:34 -05:00
Ben Cohen
4694310e51 [stdlib] Some minor cleanup (#18130)
* Remove case destructuring to _

* Remove some Iterator.Element

* Which idiot wrote this? Oh.

* Switch NibbleSort to just use default impls... shouldn't change perf
2018-07-21 17:29:57 -07:00