Commit Graph

7762 Commits

Author SHA1 Message Date
swift_jenkins
3e64a66b04 Merge remote-tracking branch 'origin/main' into next 2022-03-05 12:21:09 -08:00
Guillaume Lessard
f4d56d3734 Merge pull request #41679 from amartini51/trailing_double_tab
Remove trailing double tabs
2022-03-05 13:16:24 -07:00
Alsey Coleman Miller
0ab3eec987 Added PowerPC 32-bit support 2022-03-03 22:21:33 -05:00
swift_jenkins
3042d132a0 Merge remote-tracking branch 'origin/main' into next 2022-03-03 12:21:14 -08:00
Guillaume Lessard
bc3a9d287f Merge pull request #41392 from amartini51/UnsafeMutableRawBufferPointer_storeBytes_88982081
Add missing parameter in doc comment.
2022-03-03 13:14:20 -07:00
swift_jenkins
40852e11cc Merge remote-tracking branch 'origin/main' into next 2022-03-01 16:41:46 -08:00
Karoy Lorentey
1da581642c Merge pull request #41599 from lorentey/string-unicodescalarview-index-validation
[stdlib] String.UnicodeScalarView: Fix some index validation issues
2022-03-01 16:27:34 -08:00
Karoy Lorentey
20c293ee18 Merge pull request #41598 from lorentey/string-index-validation
[stdlib] String.index(before:): Fix index validation issues
2022-03-01 16:27:03 -08:00
Alex Martini
17d3b01b48 Remove trailing double tabs. 2022-03-01 14:46:40 -08:00
swift_jenkins
1abe4b3310 Merge remote-tracking branch 'origin/main' into next 2022-03-01 12:41:06 -08:00
Guillaume Lessard
577015f5be Merge pull request #39639 from glessard/se-pointer-convenience
SE-0334: Pointer API Usability Improvements
2022-03-01 13:24:25 -07:00
Karoy Lorentey
6f9edc8be8 [stdlib] String.UnicodeScalarView: Fix some index validation issues
`String.UnicodeScalarView` currently lacks proper index validation in its `index(after:)` and `index(before:)` methods, leading to out-of-bounds memory accesses when index navigation methods in this view are given invalid indices.

(Also see https://github.com/apple/swift/pull/41598 and https://github.com/apple/swift/pull/41417)

rdar://89498541
2022-02-28 19:49:22 -08:00
Karoy Lorentey
5b364c4e2b [stdlib] String.index(before:): Fix index validation issues
`String.index(before:)` (and methods that rely on it, such as `Substring.index(before:)`, `.distance(from:to:)` etc.) does not currently verify that the given index falls before the `endIndex` before aligning it to a scalar boundary. This allows an out-of-bounds memory access when the provided index points to a position beyond the end of `self`’s storage.

Additionally, the `i > startIndex` check needs to be done after scalar alignment, not before, as alignment can round the index down to `startIndex`.

rdar://89497074&89495373
2022-02-28 17:26:52 -08:00
swift_jenkins
c90e2eae0d Merge remote-tracking branch 'origin/main' into next 2022-02-28 15:41:21 -08:00
Guillaume Lessard
ee08dc9951 Merge pull request #41586 from stephentyrone/order-of-operations-is-hard
|| has higher precedence than the ternary operator
2022-02-28 16:33:46 -07:00
swift_jenkins
1cfdef4807 Merge remote-tracking branch 'origin/main' into next 2022-02-28 15:01:20 -08:00
Karoy Lorentey
57af818823 Merge pull request #41548 from lorentey/simplify-failEarlyRangeCheck
[stdlib] Collection: simplify default _failEarlyRangeCheck implementations
2022-02-28 14:52:31 -08:00
Karoy Lorentey
d2673ed342 Merge pull request #41572 from lorentey/stop-mentioning-IndexDistance
[stdlib] Mark String.IndexDistance as deprecated and stop mentioning it in method signatures
2022-02-28 14:50:58 -08:00
Stephen Canon
b520063e53 || has higher precedence than the ternary operator, so this check didn't work. 2022-02-28 15:33:27 -05:00
Karoy Lorentey
626582ce5f [stdlib] Mark String.IndexDistance as deprecated and stop mentioning it in method signatures
`IndexDistance` is an artifact of ancient history — it used to be an associated type of Collection in the Swift 3 era. The Collection requirement got replaced with a deprecated typealias of Int in Swift 4, the same release that re-introduced the Collection conformance for String.

However, the typealias declaration for String.IndexDistance has fallen through the cracks. It was accidentally left un-deprecated and it’s still actively mentioned in String API declarations to this day. (These usages leak into the API docs, which can be a source of unnecessary confusion.)

Let’s put an end to this and add the missing deprecation attribute, replacing `IndexDistance` usages with `Int`.

While we’re here, also bring `String.index(_:offsetBy:)` and `String.index(_:offsetBy:limitedBy:)` in sync with their declarations in Collection by renaming the second argument from `n` to `distance`.

(These method declaration changes do get emitted into .swiftinterface files, but they aren’t breaking any client code — the argument names are only relevant to the docs (and the method implementation). They do not affect callers of these functions.)
2022-02-25 17:32:04 -08: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
Guillaume Lessard
758cdaebb0 Merge branch 'main' into se-pointer-convenience 2022-02-25 13:52:45 -07:00
Guillaume Lessard
bb05e851af [stdlib] restrict pointer arithmetic to UInt 2022-02-25 13:35:20 -07:00
Guillaume Lessard
d80a50bda0 [stdlib] comparisons for heterogeneous pointer types 2022-02-25 13:34:23 -07:00
Guillaume Lessard
5c10c67a01 [stdlib] pointer(to property:) for UP and UMP 2022-02-25 13:34:23 -07:00
Guillaume Lessard
b3f96e8cea [stdlib] alignedUp(for:) and siblings for URP and UMRP 2022-02-25 13:34:23 -07:00
swift_jenkins
9013f5dd6e Merge remote-tracking branch 'origin/main' into next 2022-02-25 07:00:37 -08:00
Guillaume Lessard
9ead7d039d [stdlib] relax stride check in UnsafePointer.withMemoryRebound (#41553)
* [stdlib] relax stride check

- The stride check in `UnsafePointer.withMemoryRebound` makes less sense when rebinding memory for a single element.
- This skips the stride-matching portion of the `_debugPrecondition` in `withMemoryRebound` when `count == 1`.

* [test] UnsafePointer.withMemoryRebound with capacity 1
2022-02-25 09:54:32 -05:00
Guillaume Lessard
82fa4b0ea8 [stdlib] relax stride check
- The stride check in `UnsafePointer.withMemoryRebound` makes less sense when rebinding memory for a single element.
- This skips the stride-matching portion of the `_debugPrecondition` in `withMemoryRebound` when `count == 1`.
2022-02-24 18:57:51 -07:00
swift_jenkins
945cea69fc Merge remote-tracking branch 'origin/main' into next 2022-02-23 05:20:33 -08:00
Luciano Almeida
1218d7fee8 Merge pull request #41489 from LucianoPAlmeida/minor-nfc-warnings
[NFC] Fixing some stdlib unused warnings
2022-02-23 10:17:43 -03:00
swift_jenkins
c3aa3da6b1 Merge remote-tracking branch 'origin/main' into next 2022-02-22 18:20:41 -08:00
Karoy Lorentey
d384a796eb Merge pull request #41512 from lorentey/clock-fixes
[stdlib] Some more clock adjustments
2022-02-22 18:13:35 -08:00
Karoy Lorentey
ea10e15829 [stdlib] Duration: use optimized methods for multiplication/division by constants 2022-02-22 02:32:02 -08:00
Karoy Lorentey
524f499a0e [stdlib] Duration: match parameter order in direct initializer with _Int128 2022-02-22 02:32:01 -08:00
Karoy Lorentey
c00322d513 [stdlib] Int128: Add direct multiplication by a single UInt64 value 2022-02-22 02:26:57 -08:00
Karoy Lorentey
2cc1e2aecc [stdlib] Int128: Add direct division by some constant powers of ten 2022-02-22 02:25:31 -08:00
Karoy Lorentey
b963f7ce1d [stdlib] Int128: Fix thinko in _wideDivide22 2022-02-22 02:22:43 -08:00
swift_jenkins
dedc417e68 Merge remote-tracking branch 'origin/main' into next 2022-02-21 21:00:42 -08:00
Karoy Lorentey
0704746bfa Merge pull request #41445 from lorentey/unconditional-debugPrecondition
[stdlib] Add build option to enable _debugPrecondition in Release mode
2022-02-21 20:59:22 -08:00
Karoy Lorentey
b3016c9615 Merge pull request #41485 from lorentey/clock-fixes
[stdlib] Fix some Clock issues
2022-02-21 20:59:03 -08:00
swift_jenkins
3a2b931e5f Merge remote-tracking branch 'origin/main' into next 2022-02-21 17:00:42 -08:00
Karoy Lorentey
b7cae85482 [stdlib] Leave [U]Int128.~ disabled for now 2022-02-21 12:26:21 -08:00
LucianoAlmeida
09bf4cb6d1 [NFC] Fixing some stdlib unused warnings 2022-02-20 18:18:35 -03:00
Karoy Lorentey
1da0bb8f2e [stdlib] Int128: Fix Words; simplify/tweak some operations
`Words.Iterator.next()` used to call `Int128(truncatingIfNeeded:)`, which in turn iterates over words, leading to an infinite recursion.

Implement half-width multiplication from scratch instead of masking off the full width results.
2022-02-19 18:43:32 -08:00
Karoy Lorentey
890a7f25e8 [stdlib] Duration: add an initializer that takes low/high components
Having a direct initializer in the ABI is critically important — otherwise we wouldn’t be able to add back deployable conversion initializers in the future (without going through unnecessary overhead).
2022-02-19 18:43:32 -08:00
Saagar Jha
bb499b2dcf [stdlib] Fix comment on _forEachFieldWithKeyPath
The description for the body block appears to have been copied from
_forEachField without modification, even though the type signature
differs.
2022-02-19 00:59:47 -08:00
Karoy Lorentey
2643e8dcf3 [stdlib] Add build option to enable _debugPrecondition in Release mode
It is sometimes desirable to always perform (relatively cheap) runtime checks in the stdlib, even in configurations where we’d otherwise elide them.

Add a build-time option to the standard library (`SWIFT_STDLIB_ENABLE_DEBUG_PRECONDITIONS_IN_RELEASE`) to make `_debugPrecondition` work like `_precondition`, i.e., to enable it even in ReleaseAssert configurations.

This option will keep additional checks in the following places, even in release mode:

- Range checking in the subscript operations of all unsafe buffer pointer types.
- Nil checking in `Optional.unsafelyUnwrapped`
- Additional argument validation in `Unsafe[Mutable][Raw]Pointer`’s initialization/assignment/move/deinitialization/binding methods
- Protection against initializing `Unsafe[Mutable][Raw]Pointer` with invalid data. (Negative count, nil pointer for non-empty buffer, etc)
- Checks against index overflow in `Unsafe[Mutable]BufferPointer`’s index manipulation methods
- Checks against backward ranges in `Range(uncheckedBounds:)`, `ClosedRange(uncheckedBounds:)`
- Dynamic isa check in `unsafeDowncast(_:to:)`
- Additional [cheap] checks to catch invalid Sequence/Collection implementations in `Array.init<S:Sequence>(_:)` and elsewhere
- Checks against `Character` containing multiple grapheme clusters
- More index validation in `EmptyCollection`

(Additional cases will get added as the stdlib evolves.)

The option is disabled by default — so `_debugPrecondition`s continue to be disabled in optimized builds, even after this change lands, unless someone specifically builds a stdlib that enables them.

rdar://89118585
2022-02-18 21:53:23 -08:00
swift_jenkins
4759690b43 Merge remote-tracking branch 'origin/main' into next 2022-02-17 21:40:49 -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