Commit Graph

2516 Commits

Author SHA1 Message Date
Doug Gregor
2e3863b211 Introduce _ConditionallyBridgedToObjectiveC.bridgeFromObjectiveCConditional.
This entry point is used in conditional downcasts (as?) to attempt to
bridge from an Objective-C class down to a specific native type (e.g.,
array, dictionary), bridging all elements eagerly so that it can
produce nil if the bridging would fail.

This is the scaffolding for <rdar://problem/17319154>, and makes the
example there work, but there is much more cleanup and optimization to
do.



Swift SVN r18999
2014-06-19 14:48:00 +00:00
Doug Gregor
4eeee3580f Remove *CollectionDowncastExpr; wen're not using them any more.
This simplifies constraint application quite a bit, and eliminates
some code duplication.


Swift SVN r18980
2014-06-18 13:10:22 +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
4019d28ba8 Start using the forced collection downcast entry points.
This means that we'll get deferred checking of array and dictionary
downcasts when writing "arr as Derived[]", 
"(dict as? Dictionary<DerivedKey, DerivedValue>)!", etc, when the
collection can do so.

This is both a general optimization and also staging for
<rdar://problem/17319154>.


Swift SVN r18975
2014-06-18 05:22:41 +00:00
Doug Gregor
f0186ea490 Rename CollectionDowncastExpr to ConditionalCollectionDowncastExpr.
Swift SVN r18974
2014-06-18 04:25:07 +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
Chris Lattner
60fc0e6cd2 Implement <rdar://problem/16951729> nil should be a literal type
This is all goodness, and eliminates a major source of implicit conversions.
One thing this regresses on though, is that we now reject "x == nil" where
x is an option type and the element of the optional is not Equtatable.  If
this is important, there are ways to enable this, but directly testing it as
a logic value is more straight-forward.

This does not include support for pattern matching against nil, that will be
a follow on patch.



Swift SVN r18918
2014-06-15 22:59:03 +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
c5e3dc4e20 Rename ArrayDowncastExpr to CollectionDowncastExpr. NFC
Swift SVN r18898
2014-06-14 17:08:33 +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
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 Groff
18b4650f50 Sema/SILGen: Make default argument generators work for generic functions.
Tweak the AST representation and type-checking of default arguments to preserve a full ConcreteDeclRef with substitutions to the owner of the default arguments. In SILGen, emit default argument generators with the same genericity as the original function.

Swift SVN r18760
2014-06-09 22:16:57 +00:00
Doug Gregor
8fa087311c Allow parentheses to suppress the "forced downcast will never produce nil" warning.
Addresses <rdar://problem/17017851>.


Swift SVN r18731
2014-06-06 06:13:47 +00:00
Doug Gregor
0476b98d98 Ban the use of "as?" to coerce one value to another; it only makes sense for checked casts.
Addresses <rdar://problem/17029506>.


Swift SVN r18730
2014-06-06 05:39:31 +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
08be461122 When performing an "is" check that involves bridging, do a full conditional check.
Previously, we were only changing whether the object was of the right
type, and not performing a deep check that (for example) the
underlying array contained NSStrings for an "is String[]". Fixes the
rest of <rdar://problem/16972956>.


Swift SVN r18728
2014-06-06 04:22:45 +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
278bebc6a5 Use "is" rather than "isa" for testing against AnyFunctionType in the nullary call fixit.
Swift SVN r18605
2014-05-23 21:49:31 +00:00
Joe Pamer
bd8bc4df7d Fix crash during fixit generation by avoiding a bogus cast. (rdar://problem/16996525)
Swift SVN r18604
2014-05-23 21:34:15 +00:00
Doug Gregor
f305cd7946 Look through bridgeFromObjectiveC calls to find forced casts within optional injections.
Fixes <rdar://problem/17003302>.


Swift SVN r18563
2014-05-22 15:47:13 +00:00
Doug Gregor
67ca1c9ea1 Implement the new casting syntaxes "as" and "as?".
There's a bit of a reshuffle of the ExplicitCastExpr subclasses:
  - The existing ConditionalCheckedCastExpr expression node now represents
"as?". 
  - A new ForcedCheckedCastExpr node represents "as" when it is a
  downcast.
  - CoerceExpr represents "as" when it is a coercion.
  - A new UnresolvedCheckedCastExpr node describes "as" before it has
  been type-checked down to ForcedCheckedCastExpr or CoerceExpr. This
  wasn't a strictly necessary change, but it helps us detangle what's
  going on.

There are a few new diagnostics to help users avoid getting bitten by
as/as? mistakes:
  - Custom errors when a forced downcast (as) is used as the operand
  of postfix '!' or '?', with Fix-Its to remove the '!' or make the
  downcast conditional (with as?), respectively.
  - A warning when a forced downcast is injected into an optional,
  with a suggestion to use a conditional downcast.
  - A new error when the postfix '!' is used for a contextual
  downcast, with a Fix-It to replace it with "as T" with the
  contextual type T.

Lots of test updates, none of which felt like regressions. The new
tests are in test/expr/cast/optionals.swift. 

Addresses <rdar://problem/17000058>


Swift SVN r18556
2014-05-22 06:15:29 +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
10380943d4 Support checked downcasting through a class type to a non-verbatim bridged value type.
This allows us to cast an AnyObject (or optional thereof) down to a
specific array, e.g.,

  if let strArr = obj as String[] { ... }

Addresses most of <rdar://problem/16952771>.



Swift SVN r18397
2014-05-19 07:15:30 +00:00
Doug Gregor
d4b7bb8355 Factor out the optional-binding code for checked casts. NFC
Swift SVN r18396
2014-05-19 06:19:48 +00:00
Doug Gregor
9ccd1ffcc9 Make "x is T[]" for array downcasting go through checked casting.
We can't actually make the "is" check any more efficient for arrays
anyway, and re-using the checked casting code makes "is" work properly
for arrays. More of <rdar://problem/16952771>.


Swift SVN r18395
2014-05-19 05:44:53 +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
f477f061fe Replace ArrayDowncastConversionExpr with ArrayDowncastExpr.
Array downcast is an explicit cast written "x as U[]", not an implicit
conversion, so make it a subclass of ExplicitCastExpr. The only
effective change here is that we retain the location of the "as" and
the type as written in the AST. No semantic change.



Swift SVN r18391
2014-05-19 04:41:42 +00:00
John McCall
a2a4eea598 Use a unique constraint locator path for the operand of a
checked cast expression.

We don't actually *use* that path for anything right now,
because we basically re-check the cast from scratch after
constraint application.  This is nonetheless necessary to
avoid collisions with constraints which might be located
on the result, such as would arise in an initialization
context.

In particular, this patch fixes a crash arising when both
the operand and the result of a coercion require a
user-defined conversion.  Test to follow.

Swift SVN r18386
2014-05-19 04:00:45 +00:00
Dave Abrahams
e5fad5ed06 Remove unused function
I were gettin' warned.

Swift SVN r18379
2014-05-19 01:12:08 +00:00
Doug Gregor
2da304d05e Start using the _arrayBridgeFromObjectiveC entry point for checked downcasts.
This allows us to perform a checked downcast from an Object[] to a String[].

Swift SVN r18371
2014-05-19 00:04:41 +00:00
Doug Gregor
9c8f571d96 Teach "is" expressions to cope with optionals.
The "is" expression itself isn't really able to cope with optionals
well itself, so in the hard cases we'll defer to a conditional checked
cast and then check whether the resulting optional is non-empty. Fixes
the rest of <rdar://problem/16953860>.

Swift SVN r18361
2014-05-18 22:05:20 +00:00
Joe Pamer
a22b391e2a Greatly improve performance for type checking large, type-annotated, array literal expressions by directly applying contextual type information to its constituent element expressions. (rdar://problem/16736884)
There's a lot more that can be done with this, but for now this change will make the OpenGLES template usable.

Swift SVN r18353
2014-05-18 21:27:41 +00:00
Doug Gregor
2df46e4733 Don't unwrap implicitly unwrapped optionals before casting.
Checked casting handles multiple levels of optionality properly
already, and does so by binding rather than forcing. Fixes the
immediate issue in <rdar://problem/16953860>.

Swift SVN r18351
2014-05-18 21:03:56 +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
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
df657180f4 Implement support for non-verbatim T[] -> AnyObject[] upcasts.
One of my recent type-checker changes starting passing all array
upcasts, including "bridged" ones like String[] -> AnyObject[],
through the library entry point _arrayUpCast, which is only meant for
class/ObjC existential types that are bridged verbatim.

Fixes part of <rdar://problem/16952238>; more cleanup to follow.

Swift SVN r18283
2014-05-17 23:27:44 +00:00
Doug Gregor
ad18ed81fb Start importing NSArray* as (AnyObject[])! by default <rdar://problem/16535097>.
Swift SVN r18157
2014-05-16 01:10:13 +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
Doug Gregor
87904b8228 Make ImplicitlyUnwrappedOptional conform to _ConditionallyBridgedToObjectiveC.
This is a better solution to <rdar://problem/16899681> because the
runtime magic is limited to implementing the witnesses of this
conformance.

The type checker fixes are because we can end up using unchecked
optionals in more places, via bridging, than we could before.


Swift SVN r18120
2014-05-15 19:00:52 +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
Dmitri Hrybenko
011584a0b6 stdlib/pritning: untie string interpolation from string initializers
String interpolation invokes convertFromStringInterpolationSegment() function
now.  There is no need to add extensions to String to allow custom types to
participate in string interpolation.  Just implementing Printable will do the
right thing.


Swift SVN r18104
2014-05-15 09:56:02 +00:00
Doug Gregor
73528dc0e0 Enable importing factory methods as initializers by default.
Finishes <rdar://problem/16509024>.

Swift SVN r17972
2014-05-12 23:01:20 +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