Commit Graph

32 Commits

Author SHA1 Message Date
Kuba (Brecka) Mracek
8b189d99e4 Turn off SWIFT_ENABLE_REFLECTION on the stdlib_minimal preset (#39030) 2021-09-12 18:54:53 -07:00
Karoy Lorentey
666a22feff [test] Modernize hashing throughout the test suite 2018-11-29 17:38:29 +00: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
Joe Groff
5461b1b079 Don't hardcode metadata kind constants in tests. 2018-08-20 10:49:40 -07:00
Mark Lacey
7fd052efd8 Revert the effect of 6fa403dc7d.
For the moment, return to the old ordering for disjunctions as the new
ordering regressed some type checker performance tests.
2018-07-17 17:27:19 -07:00
Mark Lacey
6fa403dc7d [ConstraintSystem] Change the order in which we attempt disjunctions
to be stable.

We currently will stop visiting the elements of a disjunction under
certain circumstances once we have found a solution. The result we get
is inherently dependent on the order in which we determine to visit
the disjunctions themselves (in addition to the elements of the
disjunction).

This change makes the order in which we visit disjunctions
stable. Future commits will create a stable ordering for the elements
of disjunctions. Once we also have that stable ordering in place we
can in theory short circuit more often as part of changing the way in
which we decide what the "best" solution is to a system.

This results in an expression in
validation-test/stdlib/AnyHashable.swift.gyb no longer being able to
typecheck in a reasonable amount of time, so I had to tweak that
expression.
2018-07-16 09:32:14 -07:00
Karoy Lorentey
f8e6c304e0 [test] AnyHashable: Shorten class hierarchy tests
AnyHashable.swift.gyb is by far the longest test in debug builds of the test suite. Cut it dramatically shorter by reducing the number of equivalence classes checked in “containing classes from the <foo> hierarchy” tests from 16 down to 3.

(It seems to me the extra test cases didn’t actually test any additional functionality.)
2018-07-02 21:27:37 +01: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
21932f4227 [test] Override hash, not hashValue in NSObject subclasses 2018-04-20 19:22:19 +01:00
Karoy Lorentey
5ab67115dc [test] MinimalHashable types: Replace hashValue hook with hash(into:) 2018-04-20 19:22:19 +01:00
Karoy Lorentey
8d18d1a55d [test] checkHashable: Check that unequal values produce different hashes
This is safe to do with hash(into:), because random hash collisions can be eliminated with awesome certainty by trying a number of different hash seeds. (Unless there is a weakness in SipHash.)

In some cases, we intentionally want hashing to produce looser equivalency classes than equality — to let those cases keep working, add an optional hashEqualityOracle parameter.

Review usages of checkHashable and add hash oracles as needed.
2018-04-20 19:22:19 +01:00
Karoy Lorentey
18e7470ae7 [Foundation] NSArray, NSDictionary: Add custom AnyHashable representations
Now that Array and Dictionary conform to Hashable, we need to make sure that their bridged counterparts provide the same hash values when converted to AnyHashable.
2018-03-23 19:08:39 +00:00
Arnold Schwaighofer
6a1dbef72b Revert "Disable one more test that is failing on bots on armv7"
This reverts commit 6c62307281.

The cause of the failure in LLVM should be fixed.

rdar://33761334
2017-08-16 08:36:22 -07:00
Arnold Schwaighofer
6c62307281 Disable one more test that is failing on bots on armv7
rdar://33761334
2017-08-11 08:52:07 -07:00
Jordan Rose
01cb554387 Re-apply "Make all CF types Equatable and Hashable." (#4568)
Like NSObject, CFType has primitive operations CFEqual and CFHash,
so Swift should allow those types to show up in Hashable positions
(like dictionaries). The most general way to do this was to
introduce a new protocol, _CFObject, and then have the importer
automatically make all CF types conform to it.

This did require one additional change: the == implementation that
calls through to CFEqual is in a new CoreFoundation overlay, but the
conformance is in the underlying Clang module. Therefore, operator
lookup for conformances has been changed to look in the overlay for
an imported declaration (if there is one).

This re-applies 361ab62454, reverted in
f50b1e73dc, after a /very/ long interval
where we decided if it was worth breaking people who've added these
conformances on their own. Since the workaround isn't too difficult---
use `#if swift(>=3.2)` to guard the extension introducing the
conformance---it was deemed acceptable.

https://bugs.swift.org/browse/SR-2388
2017-05-08 14:05:11 -07:00
Doug Gregor
a60f7d7702 [AnyHashable tests] Minor NFC cleanup 2016-09-22 16:31:33 -07: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
Greg Parker
f50b1e73dc Revert "Make all CF types Equatable and Hashable." 2016-08-20 04:33:55 -07:00
Jordan Rose
361ab62454 Make all CF types Equatable and Hashable. (#4394)
Like NSObject, CFType has primitive operations CFEqual and CFHash,
so Swift should allow those types to show up in Hashable positions
(like dictionaries). The most general way to do this was to
introduce a new protocol, _CFObject, and then have the importer
automatically make all CF types conform to it.

This did require one additional change: the == implementation that
calls through to CFEqual is in a new CoreFoundation overlay, but the
conformance is in the underlying Clang module. Therefore, operator
lookup for conformances has been changed to look in the overlay for
an imported declaration (if there is one).

https://bugs.swift.org/browse/SR-2388
2016-08-19 13:21:24 -07:00
Dmitri Gribenko
f1ec334fe5 stdlib: make AnyHashable(SwiftValue) unbox the value
Previously AnyHashable would consider SwiftValue to be a subclass of
NSObject (which it is in practice) and return false when trying to
compare an AnyHashable of a SwiftValue box to an AnyHashable of the
unboxed value.
2016-08-09 01:38:53 -07:00
Dmitri Gribenko
b162f60070 runtime: make _SwiftNativeNSError use the Hashable conformance, if available
If the Swift error wrapped in a _SwiftNativeNSError box conforms to
Hashable, the box now uses the Swift's conformance to Hashable.

Part of rdar://problem/27574348.
2016-08-09 00:49:20 -07:00
Dmitri Gribenko
bfadfe37e4 stdlib: add (XFAIL'ed) tests for errors and NSErrors in AnyHashable 2016-08-09 00:49:20 -07:00
Dmitri Gribenko
3db584dc45 stdlib: AnyHashable: add a fixme 2016-08-09 00:49:20 -07:00
Dmitri Gribenko
091bd19610 stdlib: add tests for AnyHashable.base, and make test names uniform 2016-08-09 00:49:20 -07:00
Robert Widmann
e1ae0391e8 Fixup validation tests 2016-07-30 03:50:16 -07:00
Robert Widmann
7ad2620f5b Keep cleaning up 2016-07-28 18:50:22 -07:00
Dmitri Gribenko
f4944f4a30 stdlib: AnyHashable tests: add a FIXME 2016-07-28 01:49:50 -07:00
Dmitri Gribenko
f4127dc85e stdlib: AnyHashable tests: more descriptive test names 2016-07-28 01:49:49 -07:00
Dmitri Gribenko
5921846ae7 stdlib: AnyHashable: tests for Swift errors and enums 2016-07-28 01:49:49 -07:00
Dmitri Gribenko
2d2c76f6ec stdlib: AnyHashable tests: finish tests for CF types 2016-07-28 01:35:58 -07:00
Dmitri Gribenko
438712ca03 stdlib: AnyHashable tests: remove variables used once 2016-07-28 01:35:58 -07:00
Dmitri Gribenko
53c424409d stdlib: add AnyHashable
Implements SE-0131 "Add AnyHashable to the standard library".
2016-07-26 03:10:52 -07:00