Commit Graph

149 Commits

Author SHA1 Message Date
Joe Groff
f9440d0cb7 Sema: Correctly resolve the metatype of '.member's found by optional unwrapping.
If the lookup was resolved by optional unwrapping, unwrap the metatype when we apply the solution so we don't try to create an invalid metatype conversion from T?.Type to T.Type. Fixes <rdar://problem/17542185>.

Swift SVN r19500
2014-07-03 03:00:46 +00:00
Joe Groff
3c539b7f24 Sema: Look through optional types for .member lookup.
When we see a '.member' expression in optional context, look for the member in the optional's object type if it isn't found in Optional itself. <rdar://problem/16125392>

Swift SVN r19469
2014-07-02 16:33:45 +00:00
Doug Gregor
eb8bd789d5 Only reflect NSString categories (outside of Foundation) on String.
This removes my bridging over-generalization <rdar://problem/17526544>.


Swift SVN r19430
2014-07-01 21:43:23 +00:00
Joe Groff
a796c76ae4 Sema: Allow string-to-pointer argument conversions.
Allow a String value to be implicitly converted to ConstUnsafePointer<{UInt8,Int8,Void}> by string-to-pointer conversion, when enabled by a staging flag.

Swift SVN r19366
2014-06-30 20:46:04 +00:00
Dave Abrahams
e35c581677 Revert "Sema: Allow string-to-pointer argument conversions."
This reverts commit r19349, which was printing garbage to the console
and breaking the Sema/availability.swift test

Swift SVN r19360
2014-06-30 19:57:20 +00:00
Joe Groff
a548fa3523 Sema: Allow string-to-pointer argument conversions.
Allow a String value to be implicitly converted to ConstUnsafePointer<{UInt8,Int8,Void}> by string-to-pointer conversion.

Swift SVN r19349
2014-06-30 18:44:11 +00:00
Joe Groff
fc392cc942 Sema: Strip out inout conversion constraint systems.
JoeP helped tweak things to ensure that pointer conversions are still
considered, but we no longer need the disjunction on InOutExprs to accommodate
user-defined inout conversions.

This causes some regressions in error reporting:

<rdar://problem/17489983> inout type mismatches complain about '@lvalue inout T'
<rdar://problem/17489894> inout not rejected as operand to assignment operator

Swift SVN r19306
2014-06-27 21:50:33 +00:00
Joe Groff
22f0e39ef2 Remove the -enable-pointer-conversions flag and dead code.
Swift SVN r19278
2014-06-26 23:56:59 +00:00
Joe Groff
ab1e99772a Sema: Disallow pointer conversions in operators.
We still don't want 'array1 == array2' to accidentally work via pointer comparison. However, we still admit pointer conversions when an inout is involved. 'pointer == &x' is desirable, and more importantly, explicit about what the user wants.

Swift SVN r19271
2014-06-26 23:06:32 +00:00
Joe Groff
fb7ef8c2a1 Sema: Allow inout-to-pointer conversions for pointers to arrays.
We need to admit a potential inout-to-pointer conversion even if the inout references an array, because we can have pointers to arrays. Add a short-circuit so that array-to-pointer conversions always beat inout-to-pointer conversions; both solutions could otherwise be considered valid for an UnsafePointer<Void>, and passing a pointer to the array reference rather than to the array data would be very bad.

Swift SVN r19270
2014-06-26 22:37:27 +00:00
Joe Groff
22288baf09 Sema: Tweak pointer conversion scoring to better favor no-conversion cases.
Patch by JoeP. This fixes cases where intermediate type variables introduced by generics or closures caused the type checker to favor UnsafePointer-to-ConstUnsafePointer conversions over no-conversion solutions.

Swift SVN r19198
2014-06-26 00:21:20 +00:00
Joe Groff
ab6b9cc4e3 Sema: Favor pointer parameter solutions that don't convert types.
Fixes ambiguity errors that arise from using pointer operators or constructors which could be satisfied multiple ways due to pointer conversions.

Swift SVN r19172
2014-06-25 18:23:30 +00:00
John McCall
385879beea Remove the CheckedCastKind from SIL dynamic casts.
It is straightforward and less redundant to recover this
information from the operand types.

Swift SVN r19056
2014-06-20 22:43:53 +00:00
Joe Groff
dba8a23d64 AST: Refactor get*UnsafePointerDecl() lookups onto the AST context.
These types are needed by enough of the stack now that it makes sense to centralize their lookup and caching onto the AST context like other core types.

Swift SVN r19029
2014-06-20 03:02:29 +00:00
Joe Groff
08a48565fb Sema: Introduce intrinsic pointer argument conversions.
Add primitive type-checker rules for pointer arguments. An UnsafePointer argument accepts:

- an UnsafePointer value of matching element type, or of any type if the argument is UnsafePointer<Void>,
- an inout parameter of matching element type, or of any type if the argument is UnsafePointer<Void>, or
- an inout Array parameter of matching element type, or of any type if the argument is UnsafePointer<Void>.

A ConstUnsafePointer argument accepts:

- an UnsafePointer, ConstUnsafePointer, or AutoreleasingUnsafePointer value of matching element type, or of any type if the argument is ConstUnsafePointer<Void>,
- an inout parameter of matching element type, or of any type if the argument is ConstUnsafePointer<Void>, or
- an inout or non-inout Array parameter of matching element type, or of any type if the argument is ConstUnsafePointer<Void>.

An AutoreleasingUnsafePointer argument accepts:

- an AutoreleasingUnsafePointer value of matching element type, or
- an inout parameter of matching element type.

This disrupts some error messages in unrelated tests, which is tracked by <rdar://problem/17380520>.

Swift SVN r19008
2014-06-19 18:03:10 +00:00
Doug Gregor
39e1b0886d Start using ConditionalCheckedCastExpr and ForcedCheckedCastExpr for collection downcasts.
NFC; just a cleanup.


Swift SVN r18979
2014-06-18 13:00:57 +00:00
Doug Gregor
1c06c309bc Automatically reflect non-Foundation categories of bridged Objective-C classes onto their bridged value types.
This makes categories of NSString, NSArray, and NSDictionary available
on String, Array, and Dictionary. Note that we only consider
categories not present in the Objective-C Foundation module, because
we want to manually map those APIs ourselves. Hence, no changes to the
NSStringAPI. Implements <rdar://problem/13653329>.


Swift SVN r18920
2014-06-16 09:07:00 +00:00
Doug Gregor
dd8c668aa8 Import NSDictionary as Dictionary<NSObject, AnyObject>
Fixes <rdar://problem/16870626>.


Swift SVN r18907
2014-06-15 10:44:23 +00:00
Doug Gregor
5bb6969af3 Type checker support for dictionary downcasting.
This is the semantic-analysis portion of <rdar://problem/16847470>.


Swift SVN r18900
2014-06-14 18:27:38 +00:00
Doug Gregor
47fbf0d166 Extend dictionary upcast support for handle mixed bridging/upcasting.
This allows dictionary upcasting where the key is bridged and the
value is not, or vice-versa. Finishes <rdar://problem/17289296>.


Swift SVN r18895
2014-06-14 15:48:12 +00:00
Doug Gregor
33ecf5888d For now, only allow bridged upcasting of dictionaries when the destination’s key and value have object representation.
Tights up the semantics of <rdar://problem/17289296> slightly.

Swift SVN r18888
2014-06-14 04:34:47 +00:00
Doug Gregor
0f1f518a17 Type checker: allow dictionary upcasts, including bridging upcasts.
This is the type checker portion of <rdar://problem/17114737>.

Swift SVN r18887
2014-06-14 04:34:44 +00:00
Nadav Rotem
2a7b156427 Handle CheckedCastKind::Identical in a few places that I missed.
Swift SVN r18868
2014-06-13 16:35:21 +00:00
Doug Gregor
1789c4ccbc Collapse CollectionBridgedConversionExpr into CollectionUpcastConversionExpr.
Semantically, these expressions handle the same thing: an upcast of a
collection when the underlying element types of the source are
subtypes of or can be bridged to subtypes of the destination. This
reduces some branching in the type checker and eliminates duplication
in SILGen.

Swift SVN r18865
2014-06-13 16:32:47 +00:00
Joe Groff
ca42a7b377 Turn on metatype-to-object conversions.
Swift SVN r18850
2014-06-13 01:01:38 +00:00
Doug Gregor
5ca3882b06 Rename array upcast/bridge expressions to "collection".
This is staging for dictionary bridging upcasts.


Swift SVN r18840
2014-06-12 21:41:55 +00:00
Joe Groff
cf9e0d2624 Sema: Introduce metatype-to-object conversions.
Allow class metatypes (including class-constrained existential metatypes) to be treated as subtypes of AnyObject, and single-@objc protocol metatypes to be treated as subtypes of the Protocol class from objc. No codegen support yet, so this is hidden behind a frontend flag for now.

Swift SVN r18810
2014-06-11 23:06:23 +00:00
Joe Pamer
1914df72f3 Begin making locators non-optional for constraints.
One difficulty in generating reasonable diagnostic data for type check failures has been the fact that many constraints had been synthesized without regard for where they were rooted in the program source. The result of this was that even though we would store failure information for specific constraints, we wouldn't emit it for lack of a source location. By making location data a non-optional component of constraints, we can begin diagnosing type check errors closer to their point of failure.

Swift SVN r18751
2014-06-09 17:49:46 +00:00
Doug Gregor
fb2f72ab48 Eliminate type checker support for forced contextual downcasting via "expr!".
We removed this feature when we changed casting syntax, but left it in
the type checker to help migrate code via a Fix-It. We no longer need
it.



Swift SVN r18729
2014-06-06 05:10:15 +00:00
Doug Gregor
fb93d14aec Generalize downcasting through an Objective-C class to a bridged value type.
Rather than only allowing downcasting from AnyObject, allow it for any
class or Objective-C existential type, e.g., "NSArray() as
Int[]". While here, reduce our reliance on implicit conversions when
checking bridging. This is most of <rdar://problem/16972956>, but 'is'
still doesn't work properly in these cases.


Swift SVN r18693
2014-06-02 15:44:48 +00:00
Doug Gregor
c1de0bf27f Suggest "as T" rather than "!" for implicit downcast fixes <rdar://problem/17029454>.
Swift SVN r18626
2014-05-25 19:48:44 +00:00
Joe Pamer
1c53181667 Again, fix two problems with implicit conversions:
- rdar://problem/16776273, wherein conversions between nil and .None were permitted
due to an implicit conversion between nil and COpaquePointer.
- rdar://problem/16877526, where we needed to add new equality overloads to handle
conversions between nil and .None given the supression of user conversions.

(Some minor tweaks this time around for better interoperability with AnyObject.)

Swift SVN r18498
2014-05-21 18:56:35 +00:00
Joe Pamer
1d34a88477 Revert "Fix two problems with implicit conversions: - rdar://problem/16776273, wherein conversions between nil and .None were permitted due to an implicit conversion between nil and COpaquePointer. - rdar://problem/16877526, where we needed to add new equality overloads to handle conversions between nil and .None given the supression of user conversions. (Thanks to Ted for the overloads and test.)"
This reverts commit r18473.

Swift SVN r18477
2014-05-21 00:28:46 +00:00
Joe Pamer
f469d8f1f6 Fix two problems with implicit conversions:
- rdar://problem/16776273, wherein conversions between nil and .None were permitted
due to an implicit conversion between nil and COpaquePointer.
- rdar://problem/16877526, where we needed to add new equality overloads to handle
conversions between nil and .None given the supression of user conversions.
(Thanks to Ted for the overloads and test.)

Swift SVN r18473
2014-05-20 23:53:34 +00:00
Doug Gregor
1cb05e5b0b When force-casting AnyObject to a contextual type, bridge through Objective-C.
Previously, we were relying on user-defined conversions to perform the
final bridging from the Objective-C class type (e.g., NSString) to its
Swift value type (String). That works for NSString <-> String, but not
for arbitrary arrays. Use the bridgeFromObjectiveC() witness instead,
so we can handle:

  let obj: AnyObject = ...
  let strArr: String[] = obj!

Fixes <rdar://problem/16952771>.

Swift SVN r18422
2014-05-19 15:32:06 +00:00
Doug Gregor
2cea48a91d Unify ArrayDowncastExpr handling with ConditionalCheckedCastExpr handling.
The latter has some brilliant code to deal with differing levels of
optionality, binding inner optionals etc. Unifying these code paths
makes array downcasts work when the source has (possibly
implicitly-wrapped) optional type, e.g.,

var arrImplicitOpt: AnyObject[]! = nsarr
if let strArr = arrImplicitOpt as String[] {
  println("String array contains \(strArr)")
} else {
  println("Not a string array")
}

Another part of <rdar://problem/16952771> and the array-bridging story.


Swift SVN r18392
2014-05-19 05:14:39 +00:00
Doug Gregor
e7ad745be0 Support downcasting from AnyObject to a bridged value type.
This allows us to cast "through" a bridged class type in an "as" case,
e.g.,

  if let str = obj as String { ... }

where obj is an AnyObject (or optional/implicitly unwrapped optional
thereof). In such cases, we perform a checked cast to the
corresponding class type (NSString in this case) and then convert the
(optional!) result down to the value type.

Addresses the main part of <rdar://problem/15288553>, but we still
have trouble with "is" with optionals, and the #if false'd out
testcase incorrectly fails due to <rdar://problem/16953860>.

Swift SVN r18347
2014-05-18 20:30:39 +00:00
Doug Gregor
97977a2bb4 Zap some dead code.
Swift SVN r18346
2014-05-18 20:30:34 +00:00
Doug Gregor
0aba627a86 Reinstate "Drive a wedge between array upcasts and array bridged
upcasts." 
Reinstate "Restrict the array-bridged conversion to non-verbatim
bridging." 
Reinstate "[stdlib] Fix T[].bridgeFromObjectiveC" 
Reinstate "[stdlib] Fix T[].bridgeFromObjectiveC" 
Reinstate "[stdlib] Move _arrayBridgedDownCast to Foundation"
 Reinstate "Replace "can" with "cannot" in a message." 
Reinstate "Implement support for non-verbatim T[] -> AnyObject[]
upcasts."  

This reinstates commit r18291. 
This reinstates commit r18290. 
This reinstates commit r18288. 
This reinstates commit r18287. 
This reinstates commit r18286. 
This reinstates commit r18293. 
This reinstates commit r18283.

John fixed the issue in r18294.


Swift SVN r18299
2014-05-18 03:36:09 +00:00
Michael Gottesman
263dca4b47 Revert "Drive a wedge between array upcasts and array bridged upcasts."
Revert "Restrict the array-bridged conversion to non-verbatim bridging."
Revert "[stdlib] Fix T[].bridgeFromObjectiveC"
Revert "[stdlib] Fix T[].bridgeFromObjectiveC"
Revert "[stdlib] Move _arrayBridgedDownCast to Foundation"
Revert "Replace "can" with "cannot" in a message."
Revert "Implement support for non-verbatim T[] -> AnyObject[] upcasts."

This reverts commit r18291.
This reverts commit r18290.
This reverts commit r18288.
This reverts commit r18287.
This reverts commit r18286.
This reverts commit r18293.
This reverts commit r18283.

Sorry for the number of reverts, but I needed to do this many to get a clean
revert to r18283.

Swift SVN r18296
2014-05-18 02:42:56 +00:00
Doug Gregor
3a8a652370 Drive a wedge between array upcasts and array bridged upcasts.
Previously, we were relying on overly-general subtyping to determine
when we could perform an array upcast, which pushed some non-verbatim
bridging through that path. Instead, restrict array upcasts to classes
and ObjC existentials, and use bridging casts for the other cases.

Swift SVN r18291
2014-05-18 00:42:01 +00:00
Doug Gregor
937504bc5e Restrict the array-bridged conversion to non-verbatim bridging.
This allows, e.g., String[] -> NSString[] and String[] -> NSObject[]. Part of <rdar://problem/16952238>.

Swift SVN r18290
2014-05-18 00:42:00 +00:00
Doug Gregor
8215dff6c3 A trailing closure means that a keyword argument can be omitted, not wrong.
Fixes the type checker issue reported in <rdar://problem/16909180>.

Swift SVN r18260
2014-05-17 17:59:17 +00:00
Joe Pamer
b10a2bf48a Fix regression in implicit conversions of implicitly unwrapped optional AnyObject types to their contextual types. (rdar://problem/16922332)
Swift SVN r18203
2014-05-16 18:13:35 +00:00
Joe Pamer
50ef69c745 Allow for implicit forcing of enumerated unchecked optional values. (rdar://problem/16265970, rdar://problem/16569776, rdar://problem/16931457)
Swift SVN r18145
2014-05-16 00:06:39 +00:00
Joe Pamer
a74c44a6e4 Fix two crashing bugs related to checked downcasts - rdar://problem/16093456 and rdar://problem/16892211.
Swift SVN r18118
2014-05-15 18:36:44 +00:00
John McCall
fc57ea2cf8 Add __conversion as a known identifier.
Swift SVN r18068
2014-05-14 08:19:20 +00:00
Doug Gregor
80a85d3670 Improve handling of repeated keyword arguments missing at the call site.
Fixes <rdar://problem/16900465>.

Swift SVN r18043
2014-05-14 00:20:04 +00:00
Joe Pamer
5d95457efc Disallow operator overloads that require implicit conversions for all arguments (rdar://problem/16785445)
Swift SVN r18019
2014-05-13 20:09:50 +00:00
Joe Pamer
1e5b9116d4 More array casting work:
- Continue adding support for checked downcasts of array types (rdar://problem/16535104)
- Fix non-bridged array conversions post-r17868
- Fix rdar://problem/16773693
- Add tests for NSArray coercions to and from Array<T>

Swift SVN r17957
2014-05-12 20:49:42 +00:00