Commit Graph

180 Commits

Author SHA1 Message Date
Meghana Gupta
ceb3264e40 Disable runtime cow verification for mutableSpan property 2025-04-30 13:38:51 -07:00
Doug Gregor
fe6856726e [Strict memory safety] Remove now-extraneous "unsafe" from the standard libraries
Now that we aren't propagating "unsafe" to nested types, remove
unnecessary "unsafe" keywords from the standard library.
2025-04-19 08:13:05 -07:00
Allan Shortlidge
322443d91d stdlib: Address StrictMemorySafety warnings in Array related code. 2025-03-31 16:43:01 -07:00
Guillaume Lessard
35b851453d [gardening] update some copyright years 2025-03-12 09:56:41 -07:00
Guillaume Lessard
940628a23a [stdlib] add newly-required unsafe annotations 2025-03-10 15:20:51 -07:00
Guillaume Lessard
acdfb3711a [stdlib] refactor lazy-eager-bridging for reusability 2025-03-10 08:51:56 -07:00
Doug Gregor
22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Guillaume Lessard
c9c5253fb3 [stdlib] fix getting eager-lazy-bridged buffer 2025-01-14 16:18:24 -08:00
Guillaume Lessard
9743ec9fd6 [stdlib] loading a reference can’t use loadUnaligned 2025-01-10 09:10:32 -08:00
David Smith
26b2f7bb47 Re-land using associated objects to attach bridged Array buffers (#78452) 2025-01-07 19:38:21 -08:00
Doug Gregor
8760b7caad Consistently use @usableFromInline and remove redundant @inlinables 2024-08-16 13:03:07 -07:00
Doug Gregor
45a44a7d01 Use @_alwaysEmitIntoClient for new entrypoints 2024-08-16 11:13:19 -07:00
Doug Gregor
4d037ce6ef Adopt typed throws for withUnsafeMutableBufferPointer 2024-08-16 11:13:19 -07:00
Doug Gregor
5c07fd9502 Adopt typed throws in withUnsafeBufferPointer.
Part of rdar://131405937.
2024-08-16 11:13:18 -07:00
Mishal Shah
48d5aca28d Revert "Use associated objects to attach contiguous array buffers to lazy one…"
This reverts commit 7c011813f2.
2024-07-21 23:34:57 -07:00
David Smith
7c011813f2 Use associated objects to attach contiguous array buffers to lazy ones (#75148)
rdar://132124808
2024-07-19 23:21:41 -07:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Erik Eckstein
1fa0120926 stdlib: remove the inout-violation check from Array bounds checking
As we have exclusivity checking since a long time, this "explicit" check for inout violations is not needed anymore.
The `_checkInoutAndNativeTypeCheckedBounds` function must remain in the library for backward compatibility.
The remaining relevant subscript index checking from that function is now simply inlined in into the caller.
2022-07-07 16:53:55 +02:00
Erik Eckstein
a2c29858c3 stdlib: annotate the Array semantics functions with escape effects 2022-05-02 12:00:06 +02:00
Doug Gregor
353daabf8d Replace UnsafeSendable with @unchecked Sendable in the standard library. 2021-11-12 07:56:10 -08:00
Guillaume Lessard
e87a3e30ca [stdlib] replace precondition with guard-else-_assertionFailure 2021-11-06 20:12:04 +02:00
Alex Martini
1186fc7f13 Remove "iff" from doc comments.
This abbreviation for "if and only if" is confusing to those not coming
from a background in formal mathematics, and is frequently reported as a
type by developers reading the documentation.

This commit also changes doc comments in internal and private members,
which don't become part of the public documentation, because omitting
"iff" everywhere makes it much easier to check for any later changes
that reintroduce it.
2021-10-29 10:16:27 -07: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
Erik Eckstein
eb9621fe86 stdlib: add a build-script option --enable-array-cow-checks to enable compilation of COW checks.
And set this option in various presets for buildbots.

Don't enable the checks by default because when linking against the OS library (which does not support COW checking) it will result in unresolved symbol errors.
So far it was handled by an availability checks against 9999 (which was a hack), but this does not work anymore.

Note, all this is only relevant for assert builds of the stdlib.

rdar://83673798
2021-09-30 18:06:52 +02:00
Alastair Houghton
abec55f432 [Runtime] Add ObjC support to isKnownUniquelyReferenced.
Add code to support detecting uniquely referenced Objective-C and Core
Foundation objects.

rdar://47902425
rdar://66805490
2021-07-29 16:29:48 +01:00
Karoy Lorentey
466e26a872 [stdlib] Implement _copyContents on internal Array types
`_copyContents(initializing:)` is a core method of Sequence, and it is used surprisingly often to copy stuff out of sequences. Array’s internal types currently have explicit implementations of it that trap (to prevent a performance bug due to the default iterator-based implementation. This has proved a bad idea, as not all code paths that end up calling `_copyContents` have actually been expunged — so we replaced a performance bug with a catastrophic correctness bug. 😥

Rather than trying to play whack-a-mole with code paths that end up in `_copyContents`, replace the traps with (relatively) efficient implementations, based on the ancient `_copyContents(subRange:initializing)` methods that have already been there all this time.

This resolves https://bugs.swift.org/browse/SR-14663.

I expect specialization will make this fix deploy back to earlier OSes in most (but unfortunately not all) cases.
2021-06-16 13:47:00 -07:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07: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
Erik Eckstein
6d0bbba156 stdlib: update comments for the array buffer endCowMutation functions. 2020-11-09 15:30:23 +01:00
Erik Eckstein
71a642e51b stdlib, SIL optimizer: use the SIL copy-on-write representation in the Array types.
Use the new builtins for COW representation in Array, ContiguousArray and ArraySlice.
The basic idea is to strictly separate code which mutates an array buffer from code which reads from an array.
The concept is explained in more detail in docs/SIL.rst, section "Copy-on-Write Representation".

The main change is to use beginCOWMutation() instead of isUniquelyReferenced() and insert endCOWMutation() at the end of all mutating functions. Also, reading from the array buffer must be done differently, depending on if the buffer is in a mutable or immutable state.

All the required invariants are enforced by runtime checks - but only in an assert-build of the library: a bit in the buffer object side-table indicates if the buffer is mutable or not.

Along with the library changes, also two optimizations needed to be updated: COWArrayOpt and ObjectOutliner.
2020-06-08 15:02:22 +02:00
Erik Eckstein
68728dcb7d stdlib: move the new-buffer creation function from Array to ArrayBuffer
This has two advantages:
1. It does not force the Array in memory (to pass it as inout self to the non-inlinable _createNewBuffer).
2. The new _consumeAndCreateNew is annotated to consume self. This helps to reduce unnecessary retains/releases.

The change applies for Array and ContiguousArray.
2020-05-29 08:46:54 +02:00
Meghana Gupta
0a77ce33b2 Remove @inline(always) from Array's nonNative getter
This forces it to be inlined even in cold paths
2020-05-18 14:36:20 -07:00
Meghana Gupta
a461a48862 stdlib: Make non-native ArrayBuffer.capacity smaller
Similar to #28486 in motivation.
Before this change 'count' got lowered to multiple 'endIndex' calls

Fixes rdar://46702232
2020-05-18 13:17:57 -07:00
Erik Eckstein
87298c07c8 stdlib: make the non-native Array.count smaller more efficient.
Use _CocoaArrayWrapper.endIndex which returns the NSArray.count.
In the old version, "count" translated to RandomAccessCollection.count, which ended up in multiple calls to endIndex.
2019-11-26 18:20:39 +01: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
Andrew Trick
6d3397337d Fix performance of array initialization from a generic sequence.
This fixes a major perform bug involving array initialization from any
contiguously stored collection.	 This is not a recent regression. This fix
results in a 10,000X speedup (that's 4 zeros) for this code path:

func initializeFromSlice(_ a: [Int]) -> [Int] {
  return Array<Int>(a[...])
}

A benchmark is included.
2019-02-11 19:41:45 -08:00
Ben Cohen
1673c12d78 [stdlib] Replace "sanityCheck" with "internalInvariant" (#20616)
* Replace "sanityCheck" with "internalInvariant"
2018-11-15 20:50:22 -08:00
Karoy Lorentey
c4c4c17871 [stdlib] _BridgeStorage: Remove second type parameter (ObjCClass)
It used to be a shadow protocol existential, but now it’s invariably AnyObject.
I see no reason to keep supporting this unused abstraction.
2018-11-15 09:54:25 +00:00
Karoy Lorentey
ee17e975ca [stdlib] Make protocol _NSArrayCore internal
These shadow protocols don’t add any real type safety, and exposing them in the ABI seems unnecessary.
2018-11-15 09:54:25 +00:00
Jordan Rose
65fe556c1a [stdlib] Mark associated type witnesses as @usableFromInline
Inlinable code is permitted to rely on these associated types, so we
need to make sure their declarations are printed in the parseable
interface.

Part of rdar://problem/43824052
2018-11-06 17:00:46 -08:00
Karoy Lorentey
0304c3a567 [stdlib] ABI audit for _BridgeStorage
- Make internal (but @inlinable)
- Limit API to what is actually supported: a single boolean value.
- Rename members:

    init(native:,bits:) ⟹ init(native:,isFlagged:)
    func isNativeWithClearedSpareBits() ⟹ isUnflaggedNative
    var nativeInstance_noSpareBits ⟹ unflaggedNativeInstance
    func isUniquelyReferenced_native_noSpareBits() ⟹ isUniquelyReferencedUnflaggedNative()
2018-10-31 11:59:41 +00:00
Mike Ash
49c3547449 Merge remote-tracking branch 'origin/master' into rename-conflicting-classes-and-methods 2018-09-21 15:52:38 -04: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
Mike Ash
798edb9d0e [Runtime][Stdlib][Overlays] Rename various Objective-C classes and methods that would conflict when loading old Swift libraries into a process alongside ABI-stable libraries.
rdar://problem/35768222
2018-09-13 16:55:10 -04:00
Erik Eckstein
6ba45473df Remove the pinning addressors
It was used for Array + related types.
With exclusivity checking the pinned addressors are not useful anymore.

rdar://problem/35401528
2018-08-23 12:47:56 -07:00
Lance Parker
6375508c92 print the types involved when the array's element type doesn't meet expectations 2018-07-09 13:07:15 -07:00
Karoy Lorentey
23c630ac92 [stdlib] Add @usableFromInline to internal typealiases that need it
This fixes 3659 warnings in the standard library.
2018-06-18 16:34:19 +01:00
Michael Ilseman
4267d4d526 [stdlib] Restore @inlinable for _getElementSlowPath
Done to address benchmark regressions.
2018-04-26 07:24:28 -07:00