Commit Graph

7182 Commits

Author SHA1 Message Date
Doug Gregor
4b9615e244 Don't make older compilers parse @expression at all 2023-01-10 11:32:08 -08:00
Doug Gregor
cfa7d379e1 Merge pull request #62932 from DougGregor/stdlib-expression-attr-check 2023-01-09 18:05:20 -08:00
Doug Gregor
728598907d Allow the standard library to build with a slightly older compiler
Check for the `@expression` attribute before using it. Fixes
rdar://104036723.
2023-01-09 11:46:57 -08:00
Karoy Lorentey
2f1ed631e2 [stdlib] _CharacterRecognizer: Add Sendable, Equatable, CustomStringConvertible conformances
Equatability allows faster implementations for updating cached grapheme boundary state after a text mutation, because it enables quick detection of before/after state equality, without having to feed the recognizers until they produce a synchronized grapheme break.

The CustomStringConvertible conformance makes it orders of magnitude more pleasant to debug code that uses this.

Sendable is a baseline requirement for value types these days.
2023-01-06 14:51:37 -08:00
Karoy Lorentey
0bdc84b8de Merge pull request #62859 from lorentey/mark-utf8-index-encoding
[stdlib] String.UTF8View.index(_:offsetBy:limitedBy:): mark encoding of result
2023-01-05 13:57:25 -08:00
Karoy Lorentey
c94556165e Merge pull request #62794 from lorentey/character-recognizer
[stdlib] Export grapheme breaking facility
2023-01-04 23:54:48 -08:00
Karoy Lorentey
d358ece41d Merge pull request #62798 from lorentey/string-index-rounding
[stdlib] Expose index rounding entry points
2023-01-04 21:24:32 -08:00
Karoy Lorentey
4ffc5fe737 Merge pull request #62717 from lorentey/string-utf16-speedup
[stdlib] Speed up short UTF-16 distance calculations
2023-01-04 21:20:41 -08:00
Karoy Lorentey
892d0a278d [stdlib] String.UTF8View.index(_:offsetBy:limitedBy:): mark encoding of result 2023-01-04 20:22:39 -08:00
Evan Wilde
e88e947272 Merge pull request #62712 from etcwilde/ewilde/zipping-zippers-zip
Add zippering support
2023-01-03 21:45:33 -08:00
Karoy Lorentey
fa2f63cae0 [stdlib] _CharacterRecognizer._firstBreak(inUncheckedUnsafeUTF8Buffer:startingAt:) 2023-01-03 21:00:01 -08:00
Karoy Lorentey
87422e5dc4 [stdlib] _CharacterRecognizer: Remove initializer argument 2023-01-03 20:59:24 -08:00
Stephen Canon
701a03b41e Remove prefix+ from StaticBigInt (#62733)
* Remove prefix+ from StaticBigInt

This operator causes source breakage in cases like:
```
let a:Int = 7
let b = +1
let c = a + b  // Error: Cannot convert `b` from `StaticBigInt` to `Int`
```
2023-01-03 19:30:01 -05:00
Karoy Lorentey
e46f8f8244 [stdlib] String.UTF16View: Align indices before calling default algorithms
[Bidirectional]Collection’s default index manipulation methods (as
well as _utf16Distance) do not expect to be given unreachable
indices, and they tend to fail when operating on them. Round indices
down to the nearest scalar boundary before calling these.
2023-01-03 16:12:04 -08:00
Doug Gregor
0868889ba9 Add new source file Macros.swift 2023-01-02 21:22:05 -08:00
Doug Gregor
7000969f14 Introduce and use #externalMacro for externally-defined macros.
Align the grammar of macro declarations with SE-0382, so that macro
definitions are parsed as an expression. External macro definitions
are referenced via a referenced to the macro `#externalMacro`. Define
that macro in the standard library, and recognize uses of it as the
definition of other macros to use externally-defined macros. For
example, this means that the "stringify" macro used in a lot of
examples is now defined as something like this:

    @expression macro stringify<T>(_ value: T) -> (T, String) =
        #externalMacro(module: "MyMacros", type: "StringifyMacro")

We still parse the old "A.B" syntax for two reasons. First, it's
helpful to anyone who has existing code using the prior syntax, so they
get a warning + Fix-It to rewrite to the new syntax. Second, we use it
to define builtin macros like `externalMacro` itself, which looks like this:

    @expression
    public macro externalMacro<T>(module: String, type: String) -> T =
        Builtin.ExternalMacro

This uses the same virtual `Builtin` module as other library builtins,
and we can expand it to handle other builtin macro implementations
(such as #line) over time.
2023-01-02 21:22:05 -08:00
Karoy Lorentey
5d354ceb96 [stdlib] Fix String.UTF16View.distance(from:to:)
- Align input indices to scalar boundaries
- Don’t pass decreasing indices to _utf16Distance
2023-01-01 20:58:25 -08:00
Karoy Lorentey
e885037068 [stdlib] String: Expose _index(roundingDown:) functions in all String views
These simply expose the preexisting internal
`_StringGuts.validate*Index` functions that indexing operations
use to implicitly round indices down to the nearest valid index. (Or, in the case of the encoding views, the nearest scalar boundary.)

Being able to do this as a standalone, explicit, efficient operation
is crucial when implementing some `String` algorithms that need to
work with arbitrary indices.
2022-12-31 17:42:32 -08:00
Karoy Lorentey
55583ac13c [stdlib] Add new SPI for grapheme breaking (outside String)
`Unicode._CharacterRecognizer` is a newly exported opaque type that
exposes the stdlib’s extended grapheme cluster breaking facility,
independent of `String`.

This essentially makes the underlying simple state machine public,
without exposing any of the (unstable) Unicode details.

The ability to perform grapheme breaking over, say, the scalars stored
in multiple `String` values can be extremely useful while building
custom text processing algorithms and data structures.

Ideally this would eventually become API, but before proposing this
to Swift Evolution, I’d like to prove the shape of the type in actual
use (and we’ll also need to find better names for its operations).
2022-12-30 16:32:01 -08:00
Karoy Lorentey
ef0e79b70f [stdlib] String: Move shouldBreak into _GraphemeBreakingState
This turns _GraphemeBreakingState into a more proper state
machine, although it is only able to recognize breaks in the
forward direction.

The backward direction requires arbitrarily long lookback,
and it currently remains in _StringGuts.
2022-12-29 18:04:02 -08:00
Karoy Lorentey
fce428e715 [stdlib] String.UTF16View: Tweak ASCII paths 2022-12-29 16:32:40 -08:00
Karoy Lorentey
7d89d62dce [stdlib] Remove @_specialize attributes obsoleted by explicit type checks 2022-12-29 13:18:10 -08:00
Karoy Lorentey
d00f8ed44b [stdlib] Optimize StringProtocol._toUTF16Indices/_toUTF16Offsets
Speed up conversion between UTF-16 offset ranges
and string index ranges, by carefully switching
between absolute and relative index calculations,
depending on the distance we need to go.

It is a surprisingly tricky puzzle to do this
correctly while avoiding redundant calculations.
Offset ranges within substrings add the additional
complication of having to bias offset values with
the absolute offset of the substring’s start index.
2022-12-28 20:08:05 -08:00
Karoy Lorentey
ec35728b8d [stdlib] String.UTF16View: Rework thresholds for relative indexing
We commonly start from the `startIndex`, in which case
`_nativeGetOffset` is essentially free. Consider this
case when calculating the threshold for using breadcrumbs.
2022-12-28 20:07:53 -08:00
Karoy Lorentey
6fee1b372b [stdlib] Breadcrumbs are spaced in UTF-16 code units, not UTF-8 2022-12-27 20:22:38 -08:00
Karoy Lorentey
2423b8ba3c [stdlib] StringProtocol._toUTF16Indices: Speed up conversion of short ranges
Instead of calling `_toUTF16Index` twice, call it once and then use
`index(_:offsetBy:)` to potentially avoid another breadcrumbs lookup.
2022-12-27 20:22:38 -08:00
Karoy Lorentey
f3a930592f [stdlib] Simplify breadcrumbs avoidance paths in String.UTF16View 2022-12-27 20:22:37 -08:00
Karoy Lorentey
483087a47d [stdlib] Speed up short UTF-16 distance calculations
Previously we insisted on using breadcrumbs even if we only needed to
travel a very short way. This could be as much as ten times slower
than the naive algorithm of simply visiting all the Unicode scalars
in between the start and the end.

(Using breadcrumbs generally means that we need to walk to both
endpoints from their nearest breadcrumb, which on average requires
walking half the distance between breadcrumbs — and this can mean
visiting vastly more Unicode scalars than the ones that are simply
lying in between the endpoints themselves.)
2022-12-27 20:22:34 -08:00
Evan Wilde
f8a54181a9 Zipper all the things!
This patch goes through and adds zippering and the swift module
dependencies to a bunch of pieces of the swift runtimes. Here's to
hoping I hit everything that needed to be hit. :D

With this patch, I'm seeing the appropriate modules under
lib/swift/maccatalyst, so things seem to be working right.
2022-12-22 13:41:59 -08:00
Mike Ash
47d02db6ff Merge pull request #62528 from mikeash/self-keypath-debug-description
[Runtime] Fix debugDescription of .self keypaths.
2022-12-13 16:24:57 -05:00
Jonathan Grynspan
26fc627ad0 Fix a use-after-free bug on Win32 when calling lookupSymbol() (#62484) 2022-12-13 09:04:34 -05:00
Ben Rimmington
620ba334fb [SE-0368] StaticBigInt: support older compilers (#62541) 2022-12-13 11:15:48 +00:00
Mike Ash
f440432891 [Runtime] Fix debugDescription of .self keypaths.
AnyKeyPath's debugDescription assumes there's always at least one component, but `\Type.self` produces an empty keypath. Special-case the empty case to display a `.self` component.

rdar://103237845
2022-12-12 14:34:22 -05:00
Ben Rimmington
0b29450a3d [SE-0368] StaticBigInt (#40722) 2022-12-06 21:59:42 +00:00
Alex Martini
7e58b81092 Call out time complexity of String.count.
This discussion borrows wording from the docs for the default
implementation of Collection.count[1], which are inherited by many other
symbols, but with a rewrite here because String doesn't guarantee
random-access performance (doesn't conform to RandomAccessCollection),
so accessing the count is never an O(1) operation.

1: https://developer.apple.com/documentation/swift/collection/count-4l4qk
2022-12-01 16:06:55 -08:00
Erik Eckstein
126f3e14c1 stdlib: create optimal code for initializing a Set with an empty array literal
Always inline the empty-array-check of `Set.init(arrayLiteral:)`, because for array literal this check can always be constant folded.

rdar://53509655
2022-12-01 07:02:22 +01:00
Karoy Lorentey
7b45c8bb60 Merge pull request #62158 from karwa/patch-16
[stdlib] Do not allocate when creating a Set from a generic Sequence which happens to be another Set
2022-11-30 17:55:48 -08:00
Bradley Mackey
a16329f848 Improve Never documentation 2022-11-29 20:11:11 +00:00
Karl
d16f4679e5 [stdlib] Do not allocate when creating a Set from a generic Sequence which happens to be another Set 2022-11-17 13:47:20 +01:00
Valeriy Van
95d25d3ba5 Minor comment typo fix 2022-11-15 10:40:43 +02:00
Saleem Abdulrasool
04dd83983b Merge pull request #61756 from mhjacobson/freebsd-define-CLongDouble-typealias
stdlib: define typealias CLongDouble for FreeBSD
2022-11-02 10:02:26 -07:00
Saleem Abdulrasool
0a2b10b777 Merge pull request #61815 from MillerTechnologyPeru/trunk/riscv64
[stdlib] Add RISCV64 support
2022-11-02 09:53:34 -07:00
Alsey Coleman Miller
62b7be4e9c [stdlib] Add RISCV64 support 2022-11-01 23:59:42 -07:00
Joe Groff
121adf6eb2 Merge pull request #60758 from fibrechannelscsi/faster-keypaths
KeyPath performance improvement: Omit projection across trivially-typed memory.
2022-11-01 11:59:44 -07:00
Martin Cwikla
bc4b38d747 Removed an unnecessary disabling of isPureStruct.
This would have prevented explicitly specified KeyPaths through pure structs, e.g., \A.b.c, from taking the optimized path.
2022-10-31 14:43:44 -06:00
Matt Jacobson
802d877460 stdlib: define typealias CLongDouble for FreeBSD on i386, x86_64
This fixes a failure compiling the stdlib where the importer cannot find (i.e.,
refuses to import) functions _stdlib_remainderl and _stdlib_squareRootl.
2022-10-31 15:52:26 -04:00
Martin Cwikla
92d7cf5725 Revised _tryToAppendKeyPaths(). Removed two instances of isPureStruct.append() that weren't needed. 2022-10-26 11:05:02 -06:00
Martin Cwikla
21f175ae3c Added missing instances of pureStruct information propagation.
This means we no longer need to check for empty KeyPath Walker results.
2022-10-24 17:43:00 -06:00
유재호
c0c84f4a95 [stdlib] Fix typo in Hashable.swift 2022-10-22 10:31:19 +09:00
Martin Cwikla
a6a2f509b3 Use Int(bitPattern) rather than distance(to) to compute the offset. 2022-10-20 14:26:35 -06:00