Commit Graph

5799 Commits

Author SHA1 Message Date
Michael Gottesman
91317c723c [ownership] Make whether or not we serialize in OSSA form based off of the flag -enable-ossa-modules.
I added a change to CMake so that the stdlib still gets the option passed in so
should be NFC.
2021-02-22 19:07:45 -08:00
Doug Gregor
d9e4184742 Make Error and CodingKey conform to ConcurrentValue.
Make both Error and CodingKey conform to ConcurrentValue, so that
thrown errors always conform to ConcurrentValue. Downgrade (to
warnings) and ConcurrentValue-related diagnostics that are triggered
by this change in existing Error and CodingKey-conforming types to
reduce the impact on source compatibility.
2021-02-20 22:01:28 -08:00
Karoy Lorentey
9acf214b98 Merge pull request #36002 from lorentey/inline-partitioning-impl
[stdlib] Make MutableCollection.partition(by:) implementation inlinable
2021-02-17 18:56:55 -08:00
Karoy Lorentey
4f016c9caf [stdlib] Make MutableCollection.partition(by:) implementation inlinable
The default `partition(by:)` implementation cannot currently be specialized, which makes it ~64-100x slower than it could be for common cases. (E.g., `Array<Int>.partition(by: { $0 >= foo})` is slower than `.sort()` by a factor of 2-32.)

Make the implementation inlinable.
2021-02-16 13:56:17 -08:00
Slava Pestov
7a500a1307 stdlib: Float16 needs an @available annotation for macOS
Even though Float16 is unavailable on macOS, the type metadata can
still be referenced, for example from another unavailable declaration.

Make sure it has the correct OS version annotation so that it can be
weak linked.

Part of <rdar://problem/72151067>.
2021-02-16 13:54:27 -05:00
Mike Ash
a84a3a8f23 [Stdlib] Fix _swift_modifyAtWritableKeyPath_impl to check for ReferenceWritableKeyPaths.
Call through to _swift_modifyAtReferenceWritableKeyPath_impl in that case. This fixes an assertion failure (or worse) when upcasting a ReferenceWritableKeyPath and then using subscript(keyPath:) to modify a value with it.

rdar://74191390
2021-02-11 13:58:34 -05:00
Doug Gregor
422fb5cd57 Move String conformance to ConcurrentValue out-of-line. 2021-02-04 13:14:24 -08:00
Doug Gregor
f81621f2e1 Fix AutoreleasingUnsafeMutablePointer conformance 2021-02-04 12:31:15 -08:00
Doug Gregor
91ea115046 Use UnsafeConcurrentValue conformance for Array 2021-02-04 11:53:54 -08:00
Doug Gregor
0e2b658876 Fix build problem on Linux. _ArrayBuffer is Darwin-only 2021-02-04 08:35:22 -08: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
Doug Gregor
866a8d8944 Introduce checking for ConcurrentValue conformance across actors.
When referring to an actor-isolated declaration from outside of the
actor, ensure that the types involved conform to the `ConcurrentValue`
protocol. Otherwise, produce a diagnostic stating that it is unsafe to
pass such types across actors.

Apply the same rule to local captures within concurrent code.
2021-02-03 17:37:43 -08:00
Doug Gregor
a632118d6a Sink ConcurrentValue into the standard library as a marker protocol
By making ConcurrentValue a marker protocol, it no longer has any ABI
impact whatsoever. Sink it down into the Standard Library so it's
easier to use, even from code that isn't building with concurrency
enabled.
2021-02-03 16:43:33 -08:00
Robert Widmann
1f116aed84 Merge pull request #35223 from masters3d/patch-2
[gardening] precondition message wording in stdlib/public/core/Misc.swift
2021-01-12 21:40:51 -08:00
Andrew Trick
c4661b9b47 Fix _StringObject.init(object:...)
Add a missing fix_lifetime. This miscompiles with OSSA because
`object` is destroyed before `bridgeObject` is retained.

Reinterpreting a reference to a trivial type always requires a
fix_lifetime.
2020-12-30 00:33:34 -08:00
Chéyo Jiménez
c87c9fe998 precondition message wording in stdlib/public/core/Misc.swift 2020-12-24 15:44:55 -05:00
Matt Zanchelli
be13b470aa Fix typos
becuase -> because
preceeds -> precedes
initalizer -> initializer
intialize -> initialize
libary -> library
notfication -> notification
reciever -> receiver
collecton -> collection
exlcusive -> exclusive
techincal -> technical
compatability -> compatibility
setps -> steps
accomodate -> accommodate
brakcet -> bracket
fraciton -> fraction
programm -> program
concequently -> consequently
ecoding -> encoding
timeIntervalforSelfEnd -> timeIntervalForSelfEnd
2020-12-21 18:44:03 -05:00
Nate Cook
c35b567001 Temporary removal of RangeSet/DiscontiguousSlice (#35076) 2020-12-14 08:42:01 -06:00
Karoy Lorentey
8082b58365 Merge pull request #34961 from lorentey/buffers-need-to-be-fast-but-not-too-fast
[stdlib] Review and fix some problems with unsafe buffer and Range initialization
2020-12-12 19:13:20 -08:00
Karoy Lorentey
94a7eeebe5 [stdlib] Document sources of code bloat 2020-12-11 18:31:00 -08:00
Karoy Lorentey
1f92df093c [stdlib] Add an unsafe U[M]BP initializer to work around some inliner test failures 2020-12-09 19:31:28 -08:00
Stephen Canon
95309e1dce Fixup Float16 availability for macCatalyst/x86_64 2020-12-09 13:51:51 -05:00
Karoy Lorentey
282a1408de [stdlib] U[R]BP: Restore compile-time condition lost with removed precondition 2020-12-08 22:25:16 -08:00
Karoy Lorentey
93368213cf [stdlib] Don’t use unbound ranges (foo[…]) in String → Substring conversions
The UnboundRange → Range conversion path is complicated and it involves at least one unnecessary _precondition check for startIndex ..< endIndex.
2020-12-04 19:55:39 -08:00
Karoy Lorentey
d1ef4c4386 [stdlib] Use [Closed]Range.init(_uncheckedBounds:) in more places
When the stdlib says not to check, it’s a good idea to actually not have any checking, so that we leave existing code paths unchanged. (And because we do trust that the stdlib is responsible about such things.)
2020-12-04 19:27:19 -08:00
Karoy Lorentey
513a7d9e19 [stdlib] Substring: Restore original Range paths
This eliminates a couple of _debugPreconditions which seem to change inlining enough to interfere with some benchmarks.
2020-12-04 18:35:08 -08:00
Karoy Lorentey
2607428347 [stdlib] U[M]RBP.count: Let the compiler assume that the result is nonnegative 2020-12-04 02:06:22 -08:00
Karoy Lorentey
27978d16b9 [stdlib] Eliminate an overflow check in U[M]BP.distance 2020-12-04 02:06:22 -08:00
Karoy Lorentey
d52cd8243a [stdlib] Add a missing debug mode check to U*BP.init(rebasing:)
`Slice` does not technically guarantee that its indices are valid in its base, so these initializers accidentally allowed the creation of obviously out-of-bounds buffers.
2020-12-04 02:06:22 -08:00
Karoy Lorentey
c5c27c7c69 [stdlib] Add a missing debug precondition to [Closed]Range.init(uncheckedBounds:)
Unchecked APIs must still perform checks in debug builds to ensure that invariants aren’t violated.
(I.e., these aren’t a license to perform invalid operations — they just let us get rid of the checks when we know for sure they are unnecessary.)
2020-12-04 02:06:22 -08:00
Karoy Lorentey
90d58fef8c [stdlib] Encourage more inlining on String’s UTF-8 decoding fast path 2020-12-04 02:06:21 -08:00
Karl
83c14bca9c Make raw buffer nil/count checks debug-mode only 2020-12-04 02:06:21 -08:00
Karl
74a8254d40 Make UBP nil/count checks debug-mode only
Every other Unsafe(Mutable)BufferPointer precondition in this file is debug-mode only.

This information is already part of the documentation for this function, and it's really hard to get rid of these branches and traps otherwise.
2020-12-04 02:06:21 -08:00
Andrew Trick
820b2c6182 Merge pull request #34879 from Lukasa/cb-simpler-buffer-initializer
[stdlib] Simplify buffer pointer initialization.
2020-12-01 14:18:58 -08:00
Robert Widmann
48614412c4 Merge pull request #34885 from LucianoPAlmeida/warn-class-protocol-inheritance
[Sema] Adding deprecation warning for protocol inheritance 'class' syntax
2020-12-01 10:36:41 -08:00
Karoy Lorentey
c51b10abd0 Merge pull request #34814 from lorentey/partitioning-cleanup
[stdlib][NFC] Simplify partition(by:)’s implementation
2020-11-30 11:33:32 -08:00
Stephen Canon
74e2cb6c6d Partially revert Float16 availability changes (#34847)
* Partially revert Float16 availability changes

Specifically, when building for macOS/x86_64, use the old availability annotation instead of marking Float16 unconditionally unavailable to give downstream clients a window to adjust their own annotations.
2020-11-30 12:59:15 -05:00
Parker Schuh
4e39e59a8a Add a field reflection function that constructs keypaths. (#34815)
While the existing _forEachField in ReflectionMirror.swift
already gives the offsets and types for each field, this isn't
enough information to construct a keypath for that field in
order to modify it.

For reference, this should be sufficent to implement the features
described here: (https://forums.swift.org/t/storedpropertyiterable/19218/62)
purely at runtime without any derived conformances for many types.

Note: Since there isn't enough reflection information for
`.mutatingGetSet` fields, this means that we're not able to support
reflecting certain types of fields (functions, nonfinal class fields,
etc). Whether this is an error or not is controlled by the `.ignoreUnknown`
option.
2020-11-30 09:17:23 -08:00
Luciano Almeida
338faab324 [test] Adjusting stdlib ocurrences where class syntax is used for protocol inheritance 2020-11-29 21:44:42 -03:00
Cory Benfield
300f4da1bb [stdlib] Simplify buffer pointer initialization.
UnsafeRawBufferPointer was not made self-slicing, but is instead sliced
by Slice<UnsafeRawBufferPointer>. When working with
UnsafeRawBufferPointer objects it is quite common to want to transform
back into the underlying collection, which is why the
UnsafeRawBufferPointer provides a constructor from its subsequence:
UnsafeRawBufferPointer.init(rebasing:).

Unfortunately for an initializer on this extremely low-level type, this
initializer is surprisingly expensive. When disassembled on Linux it
emits 7 separate traps and 11 branches. This relative heft means this
method often gets outlined, which is a shame, as several of the branches
could often be eliminated due to checks elsewhere in functions where
this initializer is used.

Almost all of these branches and almost all of the traps are
unnecessary. We can remove them by noting that it is impossible to
construct a Slice whose endIndex is earlier than its startIndex. All
Slice inits are constructed with bounds expressed as Range, and Range
enforces ordering on its endpoints.

For this reason, we can do unchecked arithmetic on the start and end
index of the slice, and remove 5 traps in one fell swoop. This greatly
cheapens the cost of the initializer, improving its odds of being
inlined and having even more of its branches optimised away.

For what it's worth, I also considered trying to remove the last two
preconditions. Unfortunately I concluded I couldn't confidently do that.
I wanted to remove them based on the premise that a valid Slice must
have indices that were in-bounds for its parent Collection, and so we
could safely assume that if the parent base address was nil the count
would have to be zero, and that adding start index to the base address
would definitely not overflow. However, the existence of the
Slice.init(base:bounds:) constructor led me to be a bit suspicions of
removing those checks. Given that
UnsafeRawBufferPointer.init(start:count:) enforces those invariants, I
decided it was safest for us to continue to do that.
2020-11-27 16:57:50 +00:00
Stephen Canon
abea46ea59 Make Float16 available for macOS on Apple Silicon (#34821)
Due to an unstable (and undesirable) calling convention in the LLVM layer for x86, I had previously marked Float16 unconditionally unavailable on macOS. My hope was that Intel would stabilize the calling convention and we could make it available on both macOS platforms at the same time. Unfortunately, that hasn't happened, and we want to make the type available for macOS/arm users.

So, I am making the availability mirror Float80--the type will be unavailable for macOS on x86_64, and available on all other platforms (the other x86 platforms don't have a binary-stability guarantee to worry about). This isn't ideal. In particular, if/when the calling conventions for Float16 stabilize in LLVM, we would want to make the type available, but it would then have _different_ availability for different architectures of macOS, which the current availability system is not well-equipped to handle (it's possible, but not very ergonomic). Nonetheless, this seems like the best option.

The good news is that because the full API is already built in Swift (and simply marked unavailable), we can simply add macOS 11.0 availability for these API and it will work.
2020-11-19 09:36:03 -05:00
Karoy Lorentey
cb8ceed2af [stdlib][NFC] Make partition(by:)’s implementation easier to follow
Also, fix a case in the description of the loop invariant
2020-11-18 14:37:39 -08:00
Nate Cook
cae81eece0 Fix issues with discontiguous-slice assignment (#34708)
The mutating collection subscript for discontiguous slices assigns to the wrong group
of indices. This fixes the behavior and adds test coverage.

rdar://problem/70690643
2020-11-18 11:16:26 -06:00
Butta
8574fe7204 [build] Remove remaining dependencies on libatomic, for Android, BSD and Haiku
Follow suit from linux, #29581, tested on Android.
2020-11-16 10:16:12 +05:30
Mike Ash
2442ba29fc Merge pull request #34729 from mikeash/referencewritablekeypath-dynamic-dispatch-writes
[Stdlib] Fix swift_setAtWritableKeyPath to check for ReferenceWritableKeyPaths.
2020-11-13 16:42:19 -05:00
Mike Ash
ee8480fe71 [Stdlib] Fix swift_setAtWritableKeyPath to check for ReferenceWritableKeyPaths.
Call through to swift_setAtReferenceWritableKeyPath in that case. This fixes an assertion failure (or worse) when upcasting a ReferenceWritableKeyPath and then using subscript(keyPath:) to write a value with it.

rdar://problem/70609888
2020-11-13 13:00:16 -05:00
Erik Eckstein
7f6d86655e stdlib: add availability attributes to the COW debug checking functions.
This is only relevant for assert builds of the library.
Fixes an undefined symbol error if an executable, compiled with an assert build of the stdlib, is back deployed on a platform which does not support lazy symbol binding.

rdar://problem/71201102
2020-11-13 10:41:25 +01:00
Robert Widmann
e201c73c5c Merge pull request #34631 from kateinoigakukun/use-_resolveRelativeAddress
[stdlib][NFC] Resolve relative address in consistent way
2020-11-10 14:21:10 -08:00
Erik Eckstein
6d0bbba156 stdlib: update comments for the array buffer endCowMutation functions. 2020-11-09 15:30:23 +01:00
Erik Eckstein
51c2bd0a1c stdlib: remove a wrong internal check for COW array mutation
Don't complain if the empty array buffer singleton is set to mutable.
This can happen if reserveCapacity is called with a 0-capacity for an empty array.
In this case the resulting array is still the empty array singleton.
For the compiler, it's safe to treat the empty array singleton as "mutable", because there is never anything written to an array with zero capacity:
all mutating array operations do any form of capacity/size > 0 check in addition to the uniqueness check.

Unfortunately the empty array singleton sometimes needs such special handling with is not obvious (not to say hacky).

This wrong check only fired in very specific optimization scenarios, where the inliner and the COW optimizations must play together in a certain way.
I could not find an isolated test case for this problem.

rdar://problem/71107908
2020-11-09 15:29:41 +01:00