Commit Graph

54 Commits

Author SHA1 Message Date
Doug Gregor
22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Alejandro Alonso
f9f640b141 Sendablize the standard library
oops dont add this flag

no more nonisolated
2024-03-05 15:02:09 -08: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
Erik Eckstein
5eff9066cc stdlib: use _withUnprotected... functions instead of Builtin.unprotectedAddressOf 2022-09-08 08:42:25 +02:00
Erik Eckstein
0f8dd3a551 stdlib: opt out of stack protection in places where there can't be buffer overflows.
We trust the internal implementation of the stdlib to not cause any unintentional buffer overflows.
In such cases we can use the "unprotected" address-to-pointer conversions.
This avoids inserting stack protections where it's not needed.
2022-09-08 08:42:25 +02:00
David Smith
12166a9c2d Optimize NSString->AnyHashable and String->AnyHashable 2022-01-08 02:33:51 -08:00
Karoy Lorentey
8ed81ae063 [stdlib] Adopt availability macros 2021-10-31 15:00:58 -07:00
Kuba (Brecka) Mracek
404badb49a Introduce SWIFT_ENABLE_REFLECTION to turn on/off the support for Mirrors and reflection (#33617) 2021-09-08 13:08:13 -07:00
Mishal Shah
23c3b15f5f Support Xcode 13 beta
* Updating availability versions
* Remove all remaining overlays in stdlib/public/Darwin/*:
   - ObjectiveC
   - Dispatch
   - CoreFoundation
   - CoreGraphics
   - Foundation
2021-06-07 12:04:31 -07:00
Doug Gregor
194536472a [AnyHashable] Work around older compiler limitation.
An older Swift compiler failed to account for the witnesses in a
conformance with platform availability having their own availability,
which causes that compiler to reject the Swift module's .swiftinterface
file when it includes `AnyHashable` 's conformance to
`_HasCustomAnyHashableRepresentation`. Work around the issue by making
the one witness (`_toCustomAnyHashable`, which is trivial)
always-emit-into-client, so it does not need any availability.

Fixes rdar://76370138.
2021-04-15 21:21:41 -07:00
tbkka
26ab27648e Casting from AnyHashable to AnyHashable should never create another wrapper (#36470)
* Casting from AnyHashable to AnyHashable should never create another wrapper

This adds a conformance for _HasCustomAnyHashableRepresentation to
AnyHashable that simply returns self.  This ensures that anytime
you try to create a new AnyHashable wrapper for an existing
AnyHashable, you just get back the original.

Resolves rdar://75180619

* Move the `Struct AnyHashable` change to `without-asserts` list

As suggested by @lorentey
2021-03-22 09:03:29 -07:00
Xiaodi Wu
06f12fc268 Incorporate further review comments 2020-02-05 20:30:33 -05:00
Xiaodi Wu
4ee0cc16e1 Update stdlib/public/core/AnyHashable.swift
Co-Authored-By: Karoy Lorentey <klorentey@apple.com>
2020-01-29 21:15:04 -05:00
Xiaodi Wu
518ccce520 Optimize documentation wording for clarity 2020-01-28 21:07:58 -05:00
Xiaodi Wu
1e861acd1f Incorporate review comments 2020-01-28 21:00:52 -05:00
Xiaodi Wu
31e9807fc8 Update AnyHashable documentation 2020-01-27 22:00:33 -05:00
swift-ci
598042b521 Merge pull request #21550 from valeriyvan/patch-1 2019-11-18 15:27:40 -08: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
Daniel Rodríguez Troitiño
59c669936d [stdlib] Remove documentation piece that it is not longer true.
Since a4e9109 (#17396), both the hashes and the equality of numeric
types inside of AnyHashable do not follow the rules that this part of
the comment was talking about.

I couldn't find an easy example that shows the same behaviour, so I
decided to remove the comment completely.
2019-03-18 14:10:50 -07:00
Valeriy Van
9c733ed67d Fixes example snippet of AnyHashable
Otherwise, `print(descriptions[AnyHashable(45)])` prints `an Int8`, not `nil`.
2018-12-26 18:35:49 +02:00
Karoy Lorentey
adb5627bc2 [stdlib] AnyHashable: Audit remaining inlinability fixmes 2018-11-09 10:37:20 +00:00
Karoy Lorentey
c61d4265b3 [stdlib] Hide most of AnyHashable’s implementation details from the ABI
Leave the _AnyHashableBox protocol visible and AnyHashable itself @_fixed_layout.
2018-10-26 21:27:35 +01:00
swift-ci
aca00505d9 Merge pull request #19496 from lorentey/hashing-consuming-audit 2018-09-25 11:27:59 -07:00
Karoy Lorentey
c3bc84e680 [stdlib] Set, Dictionary: __consuming/__owned audit
Add __consuming and __owned to Set and Dictionary members where applicable.

Ignore compiler intrinsics for casting for now — their ARC behavior is covered by unit tests that need to be updated.
2018-09-24 13:46:40 +01:00
Karoy Lorentey
6d81663716 [stdlib] Make Hashable._rawHashValue(seed:) safer and more convenient
- Don’t expose the raw execution seed to _rawHashValue.
- Change the type of _rawHashValue’s seed from (UInt64,UInt64) to a single Int. Working with a pair of UInt64s is unwieldy, and overkill in practice. Int as a seed also integrates nicely with Int as a hash value.
- Remove _HasherCore._generateSeed(). Instead, simply call finalize() on a copy of the hasher to get a seed suitable for _rawHashValue.
- Update Set and Dictionary to store a single Int as the seed value.

Note that this doesn’t affect the core hasher, which still mixes in the actual 128-bit execution seed during its initialization. To reduce the potential of confusion, use the name “rawSeed” to refer to an actual 128-bit seed value.
2018-09-24 13:30:05 +01:00
Karoy Lorentey
a4e9109618 Merge pull request #17396 from lorentey/anyhashable-is-not-hashable
[stdlib] Fix AnyHashable's Equatable/Hashable conformance
2018-06-29 17:38:08 +01:00
Karoy Lorentey
2f4ad7982d [stdlib] Eliminate _AnyHashableBox._asSet() & ._asDictionary()
_canonicalBox can perform essentially the same job, so there is no reason to have these requirements.
2018-06-28 18:48:56 +01:00
Ben Cohen
92b6d8cb8f Remove inlineability from mirrors (#17476) 2018-06-25 19:54:13 -07:00
Karoy Lorentey
ff91f36a9d [stdlib] Fix AnyHashable's Equatable/Hashable conformance
AnyHashable has numerous edge cases where two AnyHashable values compare equal but produce different hashes. This breaks Set and Dictionary invariants and can cause unexpected behavior and/or traps. This change overhauls AnyHashable's implementation to fix these edge cases, hopefully without introducing new issues.

- Fix transitivity of ==. Previously, comparisons involving AnyHashable values with Objective-C provenance were handled specially, breaking Equatable:

    let a = (42 as Int as AnyHashable)
    let b = (42 as NSNumber as AnyHashable)
    let c = (42 as Double as AnyHashable)
    a == b // true
    b == c // true
    a == c // was false(!), now true

    let d = ("foo" as AnyHashable)
    let e = ("foo" as NSString as AnyHashable)
    let f = ("foo" as NSString as NSAttributedStringKey as AnyHashable)
    d == e // true
    e == f // true
    d == f // was false(!), now true

- Fix Hashable conformance for numeric types boxed into AnyHashable:

    b == c // true
    b.hashValue == c.hashValue // was false(!), now true

  Fixing this required adding a custom AnyHashable box for all standard integer and floating point types. The custom box was needed to ensure that two AnyHashables containing the same number compare equal and hash the same way, no matter what their original type was. (This behavior is required to ensure consistency with NSNumber, which has not been preserving types since SE-0170.

- Add custom AnyHashable representations for Arrays, Sets and Dictionaries, so that when they contain numeric types, they hash correctly under the new rules above.

- Remove AnyHashable._usedCustomRepresentation. The provenance of a value should not affect its behavior.

- Allow AnyHashable values to be downcasted into compatible types more often.

- Forward _rawHashValue(seed:) to AnyHashable box. This fixes AnyHashable hashing for types that customize single-shot hashing.

https://bugs.swift.org/browse/SR-7496
rdar://problem/39648819
2018-06-25 20:14:01 +01:00
Karoy Lorentey
325a63a1bd Merge pull request #16938 from lorentey/hashing-inlinable-audit
[stdlib] Audit inlinability of Hashable implementations
2018-06-04 14:36:20 -07:00
Jordan Rose
cd5acad1b5 Remove unused property from _AnyHashableBox (#16926)
No functionality change.
2018-05-31 19:28:38 -07:00
Karoy Lorentey
07c1b74cc4 [stdlib] Audit inlinability of Hashable implementations
As a general rule, it is safe to mark the implementation of hash(into:) and _rawHashValue(seed:) for @_fixed_layout structs as inlinable.

However, some structs (like String guts, Character, KeyPath-related types) have complicated enough hashing that it seems counterproductive to inline them. Mark these with @effects(releasenone) instead.
2018-05-31 18:24:59 -07:00
Nate Cook
7a4e0a32f6 [stdlib] Revise documentation
This includes various revisions to the APIs landing in Swift 4.2, including:
- Random and other randomness APIs
- Hashable changes
- MemoryLayout.offset(of:)
2018-05-18 11:31:54 -05:00
Karoy Lorentey
0fb3e88110 [SE-0206][stdlib] De-underscore Hashable.hash(into:) 2018-04-24 17:42:42 +01:00
Karoy Lorentey
45cb8b7123 [SE-0206][stdlib] De-underscore Hasher 2018-04-24 17:42:42 +01:00
Slava Pestov
2e5aef9c8d stdlib: Remove redundant @usableFromInline attributes 2018-04-06 00:02:30 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Karoy Lorentey
54b857ed0b [stdlib] Hashable: Add support for resilient hashing
Introduce _Hasher, representing an opaque hash compression function.

Add the method _hash(into:) as a Hashable requirement, decoupling the choice of hash function from Hashable's implementation. The default implementation of _hash(into:) has a default implementation that simply feeds hashValue to the hasher.

Add _hash(into:) implementations for the default integer types. Note that Int.hashValue does not return self anymore.

Add struct _LegacyHasher, emulating Swift 4.1 hashes in the new interface.
2018-03-09 14:34:57 +00:00
Greg Parker
1e894cd80b [runtime] Clean up symbols in error machinery. (#12853)
* [runtime] Clean up symbols in error machinery.

* [runtime] Clean up symbols in Foundation overlay.

* [runtime] Clean up symbols in collections and hashing.

* [runtime] Remove symbol controls from the Linux definition of swift_allocError.

* [tests] Add more stub functions for tests that link directly to the runtime.
2017-11-15 22:20:11 -08:00
Max Moiseev
a24998a5b1 [stdlib] Add missing @_fixed_layout attributes to fix resilience build 2017-10-02 15:19:06 -07:00
Max Moiseev
53b8419279 [stdlib] Make all the stdlib APIs @_inlineable
This change in theory should allow us to remove a special stdlib-only
sil-serialize-all compilation mode.

<rdar://problem/34138683>
2017-09-29 11:26:56 -07:00
Slava Pestov
66bcf5b756 stdlib: Address a couple of ABI FIXMEs by deleting code
We have an implicit conversion to AnyHashable, so there's no
need to have the special subscript on Dictionary at all.
2017-02-21 20:28:49 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Doug Gregor
6f34118cb6 [AnyHashable] Handle comparisons/casting for wrappers around bridged types.
Swift value types are their bridged Objective-C classes can have
different hash values. To address this, AnyHashable's responds to the
_HasCustomAnyHashableRepresentation protocol, which bridge objects of
those class types---NSString, NSNumber, etc---into their Swift
counterparts. That way, we get consistent (Swift) hashing behavior
across platforms.

However, there are cases where multiple Swift value types map to the
same Objective-C class type. In such cases, AnyHashable ends up
converting the object of class type back to some canonical type. For
example, an NS_STRING_ENUM (such as (NS)RunLoopMode) is a Swift
wrapper around a String. If an (NS)RunLoopMode is placed into an
AnyHashable, it maintains it's Swift type identity (which is correct
behavior). If it is bridged to Objective-C, it becomes an NSString; if
that NSString is placed into an AnyHashable, it produces a String. The
hash values still line up, but equality of the AnyHashable values
fails, which breaks when (for example) a dictionary with AnyHashable
keys is used from Objective-C. See SR-2648 / rdar://problem/27992351
for a case where this breaks interoperability.

To address this problem, make AnyHashable's casting and equality
sensitive to the origin of the hashed value: if the AnyHashable was
created through a _HasCustomAnyHashableRepresentation conformance,
treat comparisons/casting from it as "fuzzy":

* For equality, if one of the AnyHashable's comes from a custom
  representation (e.g., it originated with an Objective-C type like
  NSString) but the other did not, bridge the value of the *other*
  AnyHashable to Objective-C, re-wrap it in an AnyHashable, and
  compare that. This allows, e.g., an (NS)RunLoopMode created in Swift
  to compare to an NSString constant with the same string value.
* For casting, if the AnyHashable we're casting from came from a
  custom representation and the cast would fail, bridge to Objective-C
  and then initiate the cast again. This allows an NSString to be
  casted to (NS)RunLoopMode.

Fixes SR-2648 / rdar://problem/27992351.
2016-09-22 14:16:21 -07:00
practicalswift
3a4ee89034 [gardening] Use consistent formatting. 2016-09-17 12:12:49 +02:00
practicalswift
b19481f887 [gardening] Fix 67 recently introduced typos 2016-09-16 11:16:07 +02:00
Dmitri Gribenko
fb52e6dfd8 stdlib: coding style and doc comment fixes 2016-08-09 00:49:20 -07:00
Nate Cook
559092bbf2 [stdlib] Revise stdlib documentation comments
- Expand pre-example explanations
- Update documentation for SE-0118
- Removing remaining 'iff' usage
- Revise Array discussion
- Fix formIndex(_:offsetBy) parameter formatting
- Improve index/formIndex(_:offsetBy:(limitedBy:)?) discussion
- Update Quick Look discussions
- Fixes grammar inconsistencies
- Adds parameter / return documentation
- Adds and expands on examples
- Revises AnyObject discussion for new `id` bridging rules
- Revise readLine, print, and assertion functions
- Add missing docs to String index-moving methods
2016-08-05 16:07:46 -05:00