Commit Graph

49 Commits

Author SHA1 Message Date
Andrew Trick
0b75ee975e Remove "illegal" UnsafePointer casts from the stdlib.
Update for SE-0107: UnsafeRawPointer

This adds a "mutating" initialize to UnsafePointer to make
Immutable -> Mutable conversions explicit.

These are quick fixes to stdlib, overlays, and test cases that are necessary
in order to remove arbitrary UnsafePointer conversions.

Many cases can be expressed better up by reworking the surrounding
code, but we first need a working starting point.
2016-07-28 20:42:23 -07:00
Andrew Trick
9886e4ef54 Use UnsafeRawPointer in StringCore.
This is another necessary step in introducing changes
for SE-0107: UnsafeRawPointer.

UnsafeRawPointer is great for bytewise pointer operations.

OpaquePointer goes away.

The _RawByte type goes away.

StringBuffer always binds memory to the correct CodeUnit
when allocating memory.

Before accessing the string, a dynamic element width check
allows us to assume the bound memory type.

Generic entry points like atomicCompareExchange no longer handle
both kinds of pointers. Normally that's good because you
should not be using generics in that case, just upcast
to raw pointer. However, with pointers-to-pointers
you can't do that.
2016-07-27 09:07:35 -07:00
Joe Groff
9effc047e6 Update standard library for id-as-Any. 2016-07-25 06:01:21 -07:00
Chris Lattner
45f2cfaaa0 Implement SE-0099, but where the migration diagnostics are left as warnings
for now.  I'll upgrade them to errors in a week or two to give downstream
projects a chance to update.
2016-07-02 15:44:57 -07:00
Nate Cook
44b2d56a7f [stdlib] Revise documentation for string-related types
This documentation revision covers a large number of types & protocols:
String, its views and their indices, the Unicode codec types and protocol,
as well as Character, UnicodeScalar, and StaticString, among others.

This also includes a few small changes across the standard library for
consistency.
2016-05-22 03:04:22 -05:00
Dmitri Gribenko
0984f81a50 Merge remote-tracking branch 'origin/master' into swift-3-indexing-model 2016-04-22 18:56:35 -07:00
Arnold Schwaighofer
7447b68627 ARC black magic for _NSContiguousString.getCharacters
What happened here is that we owned to guaranteed specialize getCharacters but
then did not inline the specialized function leaving us with the stub:

  getCharacters() {
    retain self
    getCharacters_o2g(self, ...)
    release self
  }

If we mark the function with always inline getCharacters_o2g will get inlined
and the retain/release is removed.

This helps hashing and string comparison and should consequently speedup
Dictionary and Set of String.

rdar://25797071
2016-04-19 13:05:48 -07:00
Dmitri Gribenko
2b35fea059 Merge remote-tracking branch 'origin/master' into swift-3-indexing-model 2016-04-16 00:30:12 -07:00
Chris Lattner
4fd8418ba7 move the stdlib to put noescape and autoclosure on the type, instead of
the parameter.  Progress towards SE-0049.
2016-04-14 23:13:43 -07:00
Dmitri Gribenko
c52787e900 Merge commit '9cdbec13eee72feccfc5f8b987882a8c52e8107b' into swift-3-indexing-model 2016-04-14 16:23:53 -07:00
Dmitri Gribenko
10697f939f Merge commit '510f29abf77e202780c11d5f6c7449313c819030' into swift-3-indexing-model 2016-04-14 13:45:27 -07:00
Jordan Rose
bc83940301 Make pointer nullability explicit using Optional.
Implements SE-0055: https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md

- Add NULL as an extra inhabitant of Builtin.RawPointer (currently
  hardcoded to 0 rather than being target-dependent).
- Import non-object pointers as Optional/IUO when nullable/null_unspecified
  (like everything else).
- Change the type checker's *-to-pointer conversions to handle a layer of
  optional.
- Use 'AutoreleasingUnsafeMutablePointer<NSError?>?' as the type of error
  parameters exported to Objective-C.
- Drop NilLiteralConvertible conformance for all pointer types.
- Update the standard library and then all the tests.

I've decided to leave this commit only updating existing tests; any new
tests will come in the following commits. (That may mean some additional
implementation work to follow.)

The other major piece that's missing here is migration. I'm hoping we get
a lot of that with Swift 1.1's work for optional object references, but
I still need to investigate.
2016-04-11 20:06:38 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Dmitri Gribenko
6985b958fd Merge remote-tracking branch 'origin/master' into swift-3-indexing-model 2016-04-04 11:42:17 -07:00
Slava Pestov
7b91cbd551 stdlib: Add @_versioned attributes needed for resilient build 2016-04-01 13:07:18 -07:00
Dave Abrahams
8e4f85277b Merge remote-tracking branch 'refs/remotes/origin/master' into merge 2016-03-29 09:19:34 -07:00
Arnold Schwaighofer
7fb2cceec0 Add a method to _NSContiguousString to facilitate stack promotion
Use it for hashing and comparison.

During String's hashValue and comparison function we create a
_NSContiguousString instance to call Foundation's hash/compare function. This is
expensive because we have allocate and deallocate a short lived object on the
heap (and deallocation for Swift objects is expensive).  Instead help the
optimizer to allocate this object on the stack.

Introduces two functions on the internal _NSContiguousString:
_unsafeWithNotEscapedSelfPointer and _unsafeWithNotEscapedSelfPointerPair that
pass the _NSContiguousString instance as an opaque pointer to their closure
argument. Usage of these functions asserts that the closure will not escape
objects transitively reachable from the opaque pointer.

We then use those functions to call into the runtime to call foundation
functions on the passed strings. The optimizer can promote the strings to the
stack because of the assertion this API makes.

  let lhsStr = _NSContiguousString(self._core) // will be promoted to the stack.
  let rhsStr = _NSContiguousString(rhs._core) // will be promoted to the stack.
  let res = lhsStr._unsafeWithNotEscapedSelfPointerPair(rhsStr) {
    return _stdlib_compareNSStringDeterministicUnicodeCollationPointer($0, $1)
  }

Tested by existing String tests.

We should see some nice performance improvements for string comparison and
dictionary benchmarks.

Here is what I measured at -O on my machine

Name                          Speedup
Dictionary                      2.00x
Dictionary2                     1.45x
Dictionary2OfObjects            1.20x
Dictionary3                     1.50x
Dictionary3OfObjects            1.45x
DictionaryOfObjects             1.40x
SuperChars                      1.60x

rdar://22173647
2016-03-24 05:43:29 -07:00
Dave Abrahams
f493b54e44 [stdlib] indexing model: Interval/Range merge
This is step 1; we still need to introduce ClosedRange.
2016-03-16 15:59:10 -07:00
Dmitri Gribenko
63ec99b186 stdlib: add argument labels to UTF16._copy() 2016-02-24 19:55:57 -08:00
Dmitri Gribenko
18965b370b stdlib: add argument labels to _cocoaStringToContiguous() 2016-02-24 14:09:04 -08:00
Max Moiseev
40b1a0b7e0 [stdlib] all sorts of require renamed back to precondition 2016-02-19 18:21:29 -08:00
Dmitri Gribenko
efaa39ea79 stdlib: add first argument labels and some other changes to conform to API guidelines 2016-02-15 23:47:54 -08:00
Dmitri Gribenko
9bcd5a1056 Collection.length => .count 2016-01-22 18:41:19 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Dmitri Gribenko
2245f7505f func _unsafeUnwrap() => var _unsafelyUnwrapped 2015-12-18 16:20:01 -08:00
Dmitri Gribenko
73ce9ae7e9 Collection.count => .length
And other API changes that naturally fall out from this, like
Array(repeating:count:) => Array(repeating:length:).
2015-12-17 15:55:29 -08:00
Maxim Moiseev
ee7d55f74b moving unsafeUnwrap into Optional 2015-12-16 17:24:12 -08:00
Maxim Moiseev
e6468a0eca _precondition => _require 2015-12-16 17:19:01 -08:00
Maxim Moiseev
0e54467bfa Final bulk removal of Type suffix 2015-12-16 17:06:19 -08:00
Dmitri Gribenko
280feafd10 public struct RawByte => internal struct _RawByte 2015-12-09 17:19:27 -08:00
Dmitri Gribenko
ab0a2a6044 Rename 'subrange' to 'bounds' in non-API arguments 2015-12-09 17:17:20 -08:00
Maxim Moiseev
7372e9e045 COpaquePointer => OpaquePointer 2015-12-07 16:52:45 -08:00
Andrew Trick
b3604aaa83 Remove the fake _CocoaStringType protocol, NFC.
Adding a conformance to Foundation doesn't work because NSString can
be used without loading Foundation. debugDescription is one example of
this.

The only value we derive from the _CocoaStringType is its name, which
makes some APIs more readable. Adding a type safe wrapper around it
serves no purpose since we're almost always immediately casting back
and forth from an 'id'. This was previously done with unsafeBitCast,
which should be avoided unless we really need to reinterpret a bit
pattern.
2015-11-18 18:40:45 -08:00
Arnold Schwaighofer
7fa673c6bc Revert "stdlib: Replace unsafeBitCast calls with unsafeCastReference."
Revert "For unsafeReferenceCast rely on static verifier checks."

This reverts commit r32796.
This reverts commit r32795.

They very likely broke a buildbot.

Swift SVN r32813
2015-10-21 22:06:01 +00:00
Andrew Trick
b1b07ef0fa stdlib: Replace unsafeBitCast calls with unsafeCastReference.
unsafeBitCast should only be used when we actually need to lie to the type system (as opposed to just having an unchecked downcast).

Theses are the places where unsafeReferenceCast makes sense:
(In general it makes sense whenever the source & dest are class or class existential types)

- ArrayBuffer.getElement.

  The deferred downcast case cannot be benchmarked. It is never on the critical path.

  The ObjC array case cannot conceivably matter either, however, it is touched by
  DollarChain, JSONHelperDeserialize, and StrSplitter.
  These benchmarks do not regress at -O.

- arrayForceCast

  No regressions at -O based on microbenchmarks.

None of these remaining cases affect PerfTestSuite at -O:

- General ObjC bridging

- Set/Dictionary bridging

- String bridging

- AutoreleasingUnsafeMutablePointer

These are confirmed speedups but I did not investigate the cause:

|.Chars...................|.32.1%.|
|.Sim2DArray..............|.15.4%.|
|.Calculator..............|.13.0%.|
|.RecursiveOwnedParameter.|..7.9%.|

Swift SVN r32796
2015-10-21 19:41:57 +00:00
Joe Groff
a7b65aeea1 stdlib: Use shim functions to avoid name+type collisions with CoreFoundation functions.
Fixes rdar://problem/20981649.

Swift SVN r30426
2015-07-21 01:02:04 +00:00
Dmitri Hrybenko
53f3ccf850 stdlib: change CollectionType.count() into a property
Swift SVN r28829
2015-05-20 09:14:43 +00:00
Dmitri Hrybenko
313701286b stdlib: Various punctuation and markup improvements to the comments.
Patch by Brian Lanier.

Swift SVN r28659
2015-05-16 03:04:51 +00:00
Chris Lattner
c1df892d47 improve stdlib hygiene a bit.
Swift SVN r28392
2015-05-10 02:55:18 +00:00
Dmitri Hrybenko
c109ec9125 stdlib: protocol extensions: de-underscore count()
Swift SVN r28246
2015-05-07 00:30:38 +00:00
Joe Groff
ab09922966 Runtime/IRGen: Replace the _SwiftNativeNS*Base +load hack with a compiler hack.
Rather than swizzle the superclass of these bridging classes at +load time, have the compiler set their ObjC runtime base classes, using a "@_swift_native_objc_runtime_base" attribute that tells the compiler to use a different implicit base class from SwiftObject. This lets the runtime shed its last lingering +loads, and should overall be more robust, since it doesn't rely on static initialization order or deprecated ObjC runtime calls.

Swift SVN r28219
2015-05-06 22:00:59 +00:00
Chris Lattner
31c01eab73 Change the meaning of "if let x = foo()" back to Xcode 6.4 semantics. The compiler
includes a number of QoI things to help people write the correct code.  I will commit
the testcase for it as the next patch.

The bulk of this patch is moving the stdlib, testsuite and validation testsuite to
the new syntax.  I moved a few uses of "as" patterns back to as? expressions in the 
stdlib as well.



Swift SVN r27959
2015-04-30 04:38:13 +00:00
Doug Gregor
793b3326af Implement the new rules for argument label defaults.
The rule changes are as follows:
  * All functions (introduced with the 'func' keyword) have argument
  labels for arguments beyond the first, by default. Methods are no
  longer special in this regard.
  * The presence of a default argument no longer implies an argument
  label.

The actual changes to the parser and printer are fairly simple; the
rest of the noise is updating the standard library, overlays, tests,
etc.

With the standard library, this change is intended to be API neutral:
I've added/removed #'s and _'s as appropriate to keep the user
interface the same. If we want to separately consider using argument
labels for more free functions now that the defaults in the language
have shifted, we can tackle that separately.

Fixes rdar://problem/17218256.

Swift SVN r27704
2015-04-24 19:03:30 +00:00
Chris Lattner
3ec09eec8e force adopt let/else and the Swift 1.2 if/let extensions in the stdlib a bit, for dogfooding
and cleanup.

I changes cases that had a non-trivial "then" body but a trivial else.  Most of the cases in
the stdlib have a trivial "then" clause, so I didn't change them.




Swift SVN r27567
2015-04-22 06:24:06 +00:00
Nadav Rotem
32211041d2 Rename @semantics -> @_semantics.
Swift SVN r27533
2015-04-21 17:10:06 +00:00
Dave Abrahams
a1d3895bd2 [stdlib] Plug a leak in String bridging
The way we bridge CFStringCreateCopy remains a nasty hack. The patch
attached to <rdar://20185167> is better, but that radar blocks the
better solution.

Fixes <rdar://20031203>.

Swift SVN r26223
2015-03-17 12:11:15 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Dmitri Hrybenko
350248dae5 Reorganize the directory structure under 'stdlib'
The standard library has grown significantly, and we need a new
directory structure that clearly reflects the role of the APIs, and
allows future growth.

See stdlib/{public,internal,private}/README.txt for more information.

Swift SVN r25876
2015-03-09 05:26:05 +00:00