Commit Graph

3245 Commits

Author SHA1 Message Date
Doug Gregor
00d2acd809 Merge pull request #21037 from DougGregor/flatten-abs
[ABI] [stdlib] Remove magnitude-based overload of abs(_:).
2018-12-06 08:36:24 -08:00
Mike Ash
6f7143bfaf [Runtime] Allow casts from AnyHashable to a Hashable enum to succeed.
rdar://problem/46472361
2018-12-06 10:33:35 -05:00
Doug Gregor
ab75577079 [stdlib] Teach abs(_:) to use magnitude when it can.
Within the (single) implementation of abs(_:), dynamically check whether
the numeric type and its `Magnitude` are of the same type and, if so,
return the result of `magnitude`. This ensures that we do the right thing
with respect to (e.g.) floating point values like -0.0, without resorting
to overloading of abs(_:).
2018-12-05 21:05:52 -08:00
Jon Shier
2bc4cbbfdd Update for revised proposal. 2018-12-05 23:17:35 -05:00
Doug Gregor
85d488d461 [stdlib] Remove magnitude-based overload of abs(_:).
The standard library has two versions of the `abs(_:)` function:

```
func abs<T : SignedNumeric>(_ x: T) -> T where T.Magnitude == T
func abs<T : SignedNumeric & Comparable>(_ x: T) -> T
```

The first is more specialized than the second because `T.Magnitude` is
known to conform to `Comparable`. Indeed, it’s a more specialized
implementation that returns `magnitude`.

However, this overload behaves oddly: in the expression `abs(-8)`, the type
checker will pick the first overload because it is more specialized. That’s
a general guiding principle for overloading: pick the most specialized
overload that works.

However, to select that overload, it needs to pick a type for the literal
“8” for which that overload works, and it chooses `Double`. The “obvious”
answer, `Int`, doesn’t work because `Int.Magnitude == UInt`.

There is a conflict between the two rules, here: we prefer more-specialized
overloads (but we’ll fall back to less-specialized if those don’t work) and we prefer to use `Int` for integer literals (but we’ll fall back to `Double` if it doesn’t work). We have a few options from a type-checker
perspective:

1. Consider the more-specialized-function rule to be more important
2. Consider the integer-literals-prefer-`Int` rule to be more important
3. Call the result ambiguous and make the user annotate it

The type checker currently does #1, although at some point in the past it
did #2. Moving forward, #1 is a better choice because it prunes the number
of overloads that need to be considered: if the more-specialized overload
succeeds its type-check, the others need not be considered. It’s also
easier to reason about than the literal-scoring approach, because there can
be a direct definition for “more specialized than” that can be reasoned
about.

I think we should dodge the issue by removing the more-specialized version
of `abs(_:)`. Its use of `magnitude` seems unlikely to provide a
significant performance benefit, and the presence of overloading either
forces us to consider both overloads always (which is bad for type checker
performance) or accept the regression that `abs(-8)` is `Double`. Better
to eliminate the overloading and, if needed in the future, find a better
way to introduce the more-specialized implementation without it being a
separate signature.

Fixes rdar://problem/42345366.
2018-12-04 23:10:04 -08:00
Ben Cohen
fc7830a4ed Remove Swift 3-only Compatibility Shims (#21019) 2018-12-04 18:22:27 -08:00
Jon Shier
7fb5905f5f Updates for review comments. 2018-12-03 21:52:21 -05:00
Jon Shier
33dcfc462f Add update Result type to standard library. 2018-12-02 22:15:02 -05:00
Azoy
a2dafc3d26 Remove _getBool 2018-12-01 18:51:25 -06:00
Mike Ash
4b3c821aff Merge pull request #20836 from mikeash/anyhashable-cast-leak-fix
[Runtime] Fix leak when casting to AnyHashable.
2018-11-30 15:36:30 -05:00
Karoy Lorentey
e94361e642 Merge pull request #20705 from lorentey/rawrepresentable-hashing
Add default Hashable implementations for RawRepresentable types
2018-11-30 14:21:37 +00:00
Karoy Lorentey
1ef6cf4813 Merge pull request #20866 from lorentey/deprecate-hashValue-1
[test] Modernize hashing throughout the test suite
2018-11-30 14:21:04 +00:00
Stephen Canon
fb8b9e143d SIMD into stdlib
Implements SE-0229.

Also updates simd module types in the Apple SDKs to use the new types, and updates a couple tests to work with the new types and protocols.
2018-11-29 17:09:01 -05:00
Karoy Lorentey
666a22feff [test] Modernize hashing throughout the test suite 2018-11-29 17:38:29 +00:00
Maxim Moiseev
bda7841cc5 Add test for Metal APIs new in iOS 12 2018-11-28 16:47:09 -08:00
Mike Ash
331f0d2394 [Runtime] Fix leak when casting to AnyHashable.
_dynamicCastToAnyHashable assumed that _swift_convertToAnyHashableIndirect takes its argument at +1, but that is no longer the case.

rdar://problem/44686587
2018-11-28 14:54:13 -05:00
David Zarzycki
2d4745bc9f [Testing] NFC: Do not conflate ObjC interop with Darwin 2018-11-27 10:22:52 -05:00
Michael Ilseman
f0e3209dbb [test] Disable test on Linux that's ICU version-specific 2018-11-26 15:15:22 -08:00
Karoy Lorentey
cc3b270691 [test] Set/Dictionary: Restore bucket-level tests for collision handling 2018-11-23 12:49:35 +00:00
Karoy Lorentey
8bd56509b2 [stdlib] Add hashing methods to RawRepresentable to match == 2018-11-22 17:14:56 +00:00
Slava Pestov
e8e1f4f80d Merge pull request #20593 from slavapestov/keypath-resilience-fixes
Fix key paths for resilience and @inlinable
2018-11-17 13:13:53 -05:00
swift-ci
8a2e327637 Merge pull request #20652 from DougGregor/anonymous-context-parent-descriptors 2018-11-16 22:45:42 -08:00
Doug Gregor
17699d4e33 [Metadata] Emit complete context descriptors for parents of anonymous contexts
When a (file)private entity occurs inside a generic context, we still need
information about the genericity of the enclosing context to demangle
to metadata. Emit complete context descriptors for parents of anonymous
contexts.

Fixes rdar://problem/46109026.
2018-11-16 21:34:32 -08:00
Slava Pestov
b570a5de87 Remove -enable-key-path-resilience staging flag 2018-11-16 23:18:37 -05:00
Max Moiseev
d04237556f Merge pull request #20641 from moiseev/varargs
[test] Fix VarArgs test for architectures without Float80
2018-11-16 14:35:25 -08:00
swift-ci
d8f1917ec1 Merge pull request #20640 from DougGregor/keypath-type-accessors 2018-11-16 11:58:06 -08:00
Max Moiseev
85a2cc4b46 Merge pull request #20019 from drodriguez/android-fix-posix-test
[android] Adapt POSIX test to Android
2018-11-16 11:54:09 -08:00
Doug Gregor
d4fe6e9ff7 [Runtime] Fix a bug in handling multi-level generic environment substitutions. 2018-11-16 10:39:19 -08:00
Maxim Moiseev
e25a63aaeb Fix VarArgs test for architectures without Float80
Since FileCheck does not know anything about the `#if` blocks in the
code, it tries to match the expected string against the output that is
simply never produced.

Solving this by giving it an expected string even on the architectures
that don't provide Float80.

Fixes <rdar://problem/45654446>
2018-11-16 10:35:29 -08:00
Maxim Moiseev
cf9208dd77 Fix warnings in the test 2018-11-16 10:34:51 -08:00
Mike Ash
68094fac75 Merge pull request #20608 from mikeash/fix-bundleForClass-nil
[Runtime] Accept Nil in the patched bundleForClass:.
2018-11-16 10:43:54 -05:00
swift-ci
f093925036 Merge pull request #20452 from Catfish-Man/stringtrampolines 2018-11-15 17:55:34 -08:00
David Smith
8bb6b78e36 Use super trampolines to let us override several more NSString methods (-UTF8String, -cStringUsingEncoding:, and -getCString:maxLength:encoding:) for performance 2018-11-15 17:10:51 -08:00
Mike Ash
bdb822a788 [Runtime] Accept Nil in the patched bundleForClass:.
The patched +[NSBundle bundleForClass:] crashes on Nil, while the original Foundation implementation returns the main bundle. Avoid the crash and pass Nil through to Foundation. This also ensures that Nil is passed through to class_getImageName rather than crashing.

SR-9188 rdar://problem/45849924
2018-11-15 17:19:12 -05:00
Michael Ilseman
f9e6df312a Implement SE-0221: Character Properties (#20520)
Provide convenience properties on Character.
2018-11-15 14:03:49 -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
Johannes Weiss
17e5fa3506 Merge pull request #19421 from weissi/jw-managed-buffer-realloc
implement ManagerBuffer.reallocated to allow realloc'ing the storage
2018-11-09 17:14:08 +00:00
Michael Ilseman
863b4979b5 [test] Disable new emoji testing on Linux 2018-11-08 16:19:27 -08:00
Slava Pestov
94b1a28011 Fix remaining tests that pass -swift-version 3
Fixes <rdar://problem/45749460>.
2018-11-08 15:48:26 -05:00
Johannes Weiss
2d9fa20c6b implement ManagerBuffer.reallocated to allow realloc'ing the storage 2018-11-08 18:18:23 +00:00
Michael Ilseman
0ca620a994 [test] Adjust tests to run on older OSes.
Version-gate newer emoji in test, and adjust a simple CHECK line.
2018-11-07 15:32:56 -08:00
Maxim Moiseev
ca51626fd3 [stdlib] Move _stdlib_AtomicInt and friends out of the stdlib 2018-11-06 09:53:58 -08:00
Daniel Rodríguez Troitiño
a17f45d220 [android] Adapt POSIX test to Android
Android differs in subtle ways from Linux. For example sem_open is
completely unimplemented, and always returns ENOSYS. None of the
test using sem_open were going to work.

Additionally the l_len field of the flock struct is typed as
__kernel_off_t, which doesn't have the same size as off_t.

Finally, the current working directory is always /, so creating
relative files will fail. We have to find the executable directory
and create the file there.
2018-11-05 15:29:13 -08:00
Michael Ilseman
53ee971dbc [test] Update test to reflect DCE 2018-11-04 10:42:42 -08:00
Karoy Lorentey
40aae6b235 [String] 32-bit platform support
Add support for 32-bit platforms for UTF-8 backed String.
2018-11-04 10:42:41 -08:00
Michael Ilseman
e6582c37ee [test] Adjust String tests for UTF-8 representation.
Adjust tests for the UTF-8 representation, in preparation for 32-bit
support. Includes UTF-8 literal update.
2018-11-04 10:42:41 -08:00
Lance Parker
7376009ccc Add benchmarks and tests for the normalized iterator (#32)
Add benchmarks and tests for the normalized iterator
2018-11-04 10:42:41 -08:00
Michael Ilseman
fe7c3ce2e4 [String] Refactorings and cleanup
* Refactor out RRC implementation into dedicated file.

* Change our `_invariantCheck` pattern to generate efficient code in
  asserts builds and make the optimizer job's easier.

* Drop a few Bidi shims we no longer need.

* Restore View decls to String, workaround no longer needed

* Cleaner unicode helper facilities
2018-11-04 10:42:40 -08:00
Brent Royal-Gordon
9bd1a26089 Implementation for SE-0228: Fix ExpressibleByStringInterpolation (#20214)
* [CodeCompletion] Restrict ancestor search to brace

This change allows ExprParentFinder to restrict certain searches for parents to just AST nodes within the nearest surrounding BraceStmt. In the string interpolation rework, BraceStmts can appear in new places in the AST; this keeps code completion from looking at irrelevant context.

NFC in this commit, but keeps code completion from crashing once TapExpr is introduced.

* Remove test relying on ExpressibleByStringInterpolation being deprecated

Since soon enough, it won’t be anymore.

* [AST] Introduce TapExpr

TapExpr allows a block of code to to be inserted between two expressions, accessing and potentially mutating the result of its subexpression before giving it to its parent expression. It’s roughly equivalent to this function:

  func _tap<T>(_ value: T, do body: (inout T) throws -> Void) rethrows -> T {
    var copy = value
    try body(&copy)
    return copy
  }

Except that it doesn’t use a closure, so no variables are captured and no call frame is (even notionally) added.

This commit does not include tests because nothing in it actually uses TapExpr yet. It will be used by string interpolation.

* SE-0228: Fix ExpressibleByStringInterpolation

This is the bulk of the implementation of the string interpolation rework. It includes a redesigned AST node, new parsing logic, new constraints and post-typechecking code generation, and new standard library types and members.

* [Sema] Rip out typeCheckExpressionShallow()

With new string interpolation in place, it is no longer used by anything in the compiler.

* [Sema] Diagnose invalid StringInterpolationProtocols

StringInterpolationProtocol informally requires conforming types to provide at least one method with the base name “appendInterpolation” with no (or a discardable) return value and visibility at least as broad as the conforming type’s. This change diagnoses an error when a conforming type does not have a method that meets those criteria.

* [Stdlib] Fix map(String.init) source break

Some users, including some in the source compatibility suite, accidentally used init(stringInterpolationSegment:) by writing code like `map(String.init)`. Now that these intializers have been removed, the remaining initializers often end up tying during overload resolution. This change adds several overloads of `String.init(describing:)` which will break these ties in cases where the compiler previously selected `String.init(stringInterpolationSegment:)`.

* [Sema] Make callWitness() take non-mutable arrays

It doesn’t actually need to mutate them.

* [Stdlib] Improve floating-point interpolation performance

This change avoids constructing a String when interpolating a Float, Double, or Float80. Instead, we write the characters to a fixed-size buffer and then append them directly to the string’s storage.

This seems to improve performance for all three types, but especially for Double and Float80, which cannot always fit into a small string when stringified.

* [NameLookup] Improve MemberLookupTable invalidation

In rare cases usually involving generated code, an overload added by an extension in the middle of a file would not be visible below it if the type had lazy members and the same base name had already been referenced above the extension. This change essentially dirties a type’s member lookup table whenever an extension is added to it, ensuring the entries in it will be updated.

This change also includes some debugging improvements for NameLookup.

* [SILOptimizer] XFAIL dead object removal failure

The DeadObjectRemoval pass in SILOptimizer does not currently remove reworked string interpolations as well as the old design because their effects cannot be described by @_effects(readonly). That causes a test failure on Linux. This change temporarily silences that test. The SILOptimizer issue has been filed as SR-9008.

* Confess string interpolation’s source stability sins

* [Parser] Parse empty interpolations

Previously, the parser had an odd asymmetry which caused the same function to accept foo(), but reject “\()”. This change fixes the issue.

Already tested by test/Parse/try.swift, which uses this construct in one of its throwing interpolation tests.

* [Sema] Fix batch-mode-only lazy var bug

The temporary variable used by string interpolation needs to be recontextualized when it’s inserted into a synthesized getter. Fixes a compilation failure in Alamofire.

I’ll probably follow up on this bug a bit more after merging.
2018-11-02 19:16:03 -07:00
levivic
44c7a827ac Fix Runtime.swift.gyb failur. 2018-11-02 10:46:31 -04:00