Commit Graph

1334 Commits

Author SHA1 Message Date
Chris Willmore
7cbadb1d2c When UnsafeMutablePointer<$T1> has a Conversion constraint to
UnsafeMutablePointer<Void>, don't bind $T1 to Void. This fixes an
unintentional dependency on the order in which constraints are visited
by the solver.

Fix some resulting underconstrained expressions in the stdlib.

<rdar://problem/19835413> Reference to value from array changed

Swift SVN r25921
2015-03-10 10:08:38 +00:00
Chris Willmore
4b939eef89 Allow implicit conversion constraint to bridge String to NSString even
when it is the favored constraint of a disjunction.

This allows 'ns ?? "str" as String as String' to typecheck where ns is
an NSString. This does not regress the relevant test for 17962491.

<rdar://problem/20029786> Swift compiler sometimes suggests changing "as!" to "as?!"

Swift SVN r25910
2015-03-10 03:45:53 +00:00
Chris Willmore
9910c75c67 Don't offer incorrect fixit for NSString? -> String? conversion.
Update the locator when matching optional types in parallel fashion.
This causes the simplifyLocator() call to fail to simplify the locator
completely, and avoids the bogus diagnostic.

<rdar://problem/19836341> Incorrect fixit for NSString? to String? conversions

Swift SVN r25596
2015-02-27 05:30:11 +00:00
Chris Willmore
4cf775f37a For class C and protocol P, C.Type is not convertible to P.Protocol.
Swift SVN r25376
2015-02-18 19:58:28 +00:00
Joe Pamer
81df1eeee0 Revert "Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)"
This reverts commit r25319.

Swift SVN r25333
2015-02-16 23:46:59 +00:00
Joe Pamer
6e9ddbafd0 Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)
Swift SVN r25319
2015-02-16 20:00:04 +00:00
Joe Pamer
989aa3d58a When converting a non-void-returning closure to a contextual function type that returns void, increase the function conversion score count. This ensures that, in the case of overload resolution, we don't accidentally favor a void-returning function over a better (or exact) fit. (rdar://problem/19776288)
Swift SVN r25140
2015-02-10 22:02:20 +00:00
Jordan Rose
07041fc7d3 Revert all the function type ABI restriction changes.
John pointed out that messing with the type checker's notion of "subtype"
is a bad idea. Instead, we should just have a separate check for ABI
compatibility...and eventually (rdar://problem/19517003) just insert the
appropriate thunks rather than forcing the user to perform the conversion.

I'm leaving all the tests as they are because I'm adding a post-type-checking
diagnostic in the next commit, and that should pass all the same tests.

Part of rdar://problem/19600325

Swift SVN r25116
2015-02-10 03:46:46 +00:00
Chris Willmore
e2ac9f65ac Add FixKind for 'as' -> 'as!' conversion
Penalize solutions that involve 'as' -> 'as!' changes by recording a Fix
when simplifying the corresponding checked-cast constraint.

<rdar://problem/19724719> Type checker thinks "(optionalNSString ?? nonoptionalNSString) as String" is a forced cast

Swift SVN r25061
2015-02-07 00:33:37 +00:00
Jordan Rose
19af8a124c Re-apply "If a function conversion fails, suggest wrapping in a closure."
This re-applies r24987, reverted in r24990, with a fix for a spuriously-
introduced error: don't use a favored constraint in a disjunction to avoid
applying a fix. (Why not? Because favoring bubbles up, i.e. the
/disjunction/ becomes favored even if the particular branch is eventually
rejected.) This doesn't seem to affect the outcome, though: the other
branch of the disjunction doesn't seem to be tried anyway.

Finishes rdar://problem/19600325

Swift SVN r25054
2015-02-06 23:12:54 +00:00
Jordan Rose
003821a956 Value-to-optional conversions are only subtypes for class references.
let x: Int? = 4 as Int // okay
  let f: (Int) -> Void = { (x: Int?) -> Void in println(x) } // error!

As part of this, remove an invalid "protection" of value-to-optional
conversions that involve T? -> T??. Unfortunately, this results in an
ambiguity in the following code:

  var z: Int?
  z = z ?? 42

Both of our overloads for ?? here require one value-to-optional conversion,
and the overload preference isn't conclusive. (Turns out (T?, T) and
(U?, U?) can be unified as T=U or as T=U?.) The best thing to do would be
to take our solved T=Int binding into account, but the type checker isn't
set up to do that at the moment.

After talking to JoeP, I ended up just hardcoding a preference against
the (T?, T?) -> T? overload of ??. This is only acceptable because both
overloads have the same result, and I'll be filing another Radar to
remove this hack in the future.

Part of rdar://problem/19600325

Swift SVN r25045
2015-02-06 20:41:51 +00:00
Jordan Rose
18355ca44a Revert "If a function conversion fails, suggest wrapping in a closure."
This reverts commit r24987. The constraint system is choosing the fix
case over the normal case in Dollar.swift.

Swift SVN r24990
2015-02-05 03:56:20 +00:00
Jordan Rose
ad7440989b If a function conversion fails, suggest wrapping in a closure.
And even if we don't suggest wrapping in a closure (say, because there's
already a closure involved), emit a more relevant diagnostic anyway.
(Wordsmithing welcome.)

Wrapping a function value in a closure essentially explicitly inserts a
conversion thunk that we should eventually be able to implicitly insert;
that's rdar://problem/19517003.

Part of rdar://problem/19600325

Swift SVN r24987
2015-02-05 01:56:47 +00:00
Doug Gregor
9637137d45 Provide forced-downcasting Fix-Its where we used to have implicit bridging conversions.
For example, we used to have an implicit conversion from NSString to
String, but it was removed. Provide a Fix-It that suggests "as!
String". Fixes <rdar://problem/19551164>.

Swift SVN r24948
2015-02-04 07:08:27 +00:00
Doug Gregor
92ff07a556 If member lookup into an optional fails, suggest a '!'.
This Fix-It handles simple cases such as "foo.bar" where "foo" is of
optional type. Addresses rdar://problem/19707036.

Swift SVN r24944
2015-02-04 06:15:36 +00:00
Jordan Rose
3e1e2c4852 Lock down on function conversions that change the ABI of the function.
These haven't ever been safe in Swift's development because they require
generating thunks, and we currently don't do that. However, we were letting
existential conversions slip through the cracks because we consider them
subtypes, so that /metatype/ conversions work correctly. To be concrete:

  "let _: Any.Type = Int.self" is okay.
  "let _: (Int) -> Void = { (_: Any) -> Void in return }" is not.

We should implement this some day; that's rdar://problem/19517003.

This produces some lousy error messages, which I intend to fix soon.

Part of rdar://problem/19600325

Swift SVN r24915
2015-02-03 03:44:05 +00:00
Doug Gregor
6a1b7348e0 Make trailing closure syntax match the last parameter, always.
Previously, trailing closures would try to match the first parameter
of (possibly optional) function type that didn't seem to have an
argument already, but in practice this broke when there were
parameters with default arguments before the function parameter.

The new rule is far simpler: a trailing closure matches the last
parameter. Fixes rdar://problem/17965209.

Swift SVN r24898
2015-02-02 19:47:31 +00:00
Chris Willmore
ab86515fb2 <rdar://problem/19671476> Offer as -> as! changes in all nested contexts
When generating constraints for an 'as' expression, consider the
possibility that the code is supposed to be 'as!' instead of 'as'. Emit
the appropriate fixit if that branch of the disjunction is chosen by the
constraint solver.

This is a more comprehensive fix for <rdar://problem/19499340> than the
one in r24815.

Swift SVN r24872
2015-01-31 00:55:53 +00:00
Joe Pamer
6a70bd085e These changes implement some oft-requested tweaks and fixes to our closure implementation:
- Closures that are comprised of only a single return statement are now considered to be "single expression" closures. (rdar://problem/17550847)
- Unannotated single expression closures with non-void return types can now be used in void contexts. (rdar://problem/17228969)
- Situations where a multi-statement closure's type could not be inferred because of the lack of a return-type annotation are now properly diagnosed. (rdar://problem/17212107)

I also encountered a number of crashers along the way, which should now be fixed.

Swift SVN r24817
2015-01-29 18:48:39 +00:00
Joe Pamer
a18bedf079 Factor the constraint-favoring machinery out of the constraint generation process, and re-work it into a series of passes over an expression sub-tree.
Aside from tidying things up, doing this results in some significant benefits:
- Allows for global constraint ordering optimizations over a given expression, not just on a peephole basis.
- Eliminates a set of order-dependent bugs in the solver that have been dogging us for a while. (rdar://problem/19459079)
- Brings another set of tyvar-to-tyvar solving problems out of the realm of the exponential. (rdar://problem/19005271)
- Opens up the possibility of optimizing constraints during later solving phases - not just while generating them.

Swift SVN r24693
2015-01-23 23:10:50 +00:00
Joe Pamer
35184ff7b5 Utilize argument type information to favor overload binding constraints on initializers. Doing so addresses a number of cases where the type checker was going exponential on seemingly simple user code.
Also, these changes fix the performance regressions that were introduced as a result of September's convertible/init requirement modifications, and allow us to roll back the associated workarounds that were added to the Adventure sample (rdar://problem/18942100).

Swift SVN r24520
2015-01-19 20:59:14 +00:00
Joe Pamer
87cbad9ec1 Improve performance of solving over apply expressions by directly applying the return type whenever possible. This has some nice side-effects:
- Addresses many common user-reported "expression too complex" bugs, including rdar://problem/18876786.
- Shaves up to 10% off of the total time to run our unit tests. (Unscientifically measured on my iMac: 427.46s before, 385.17s after.)

Swift SVN r24514
2015-01-19 20:59:11 +00:00
Chris Willmore
68dd563fbf <rdar://problem/18311362> TLF: Eliminate implicit bridging conversions
Require 'as' when converting from Objective-C type to native type (but
continue to allow implicit conversion from native to Objective-C). This
conversion constraint is called ExplicitConversion; all implicit
conversions are covered by the existing Conversion constraint. Update
standard library and tests to match.

Swift SVN r24496
2015-01-18 00:07:45 +00:00
Doug Gregor
376c39bc74 Add test for rdar://problem/17855378
Swift SVN r24356
2015-01-10 05:48:43 +00:00
Chris Lattner
3578c7fde0 Teach the type checker about subtype relationships of noescape, and use this
to allow passing off a no-escape formal parameter to the argument of a 
noescape parameter call.



Swift SVN r24126
2014-12-23 22:18:09 +00:00
Chris Willmore
36d0f187ec Sema, SILGen, ClangImporter: Add special support for Set<T>
Add the following functionality to the Swift compiler:

* covariant subtyping of Set
* upcasting, downcasting of Set
* automatic bridging between Set and NSSet, including
    * NSSet params/return values in ObjC are imported as Set<NSObject>
    * Set params/return values in Swift are visible to ObjC as NSSet

<rdar://problem/18853078> Implement Set<T> up and downcasting

Swift SVN r23751
2014-12-06 02:52:33 +00:00
Jordan Rose
f0582e27b9 Testing whether a type is bridged to ObjC isn't always a public use.
Push the "in expression" flag up through TypeChecker::getBridgedToObjC and
TypeChecker::getDynamicBridgedThroughObjCClass.

Swift SVN r23701
2014-12-05 00:23:29 +00:00
Jordan Rose
2b0fbcbe80 Looking up conformances for a type isn't always a public use of the type.
Specifically, it's not when
- the conformance is being used within a function body (test included)
- the conformance is being used for or within a private type (test included)
- the conformance is being used to generate a diagnostic string

We're still a bit imprecise in some places (checking ObjC bridging), but
in general this means less of an issue for checking literals.

Swift SVN r23700
2014-12-05 00:23:24 +00:00
Jordan Rose
470706eb61 Dependencies: Push isKnownPrivate up to TypeChecker::lookupConstructor.
Some initializer lookups are known to be private dependencies.

Swift SVN r23632
2014-12-03 02:55:59 +00:00
Jordan Rose
e97764fbd8 Dependencies: Type lookups are private in constraint systems within functions.
This is the start of distinguishing qualified lookups within function bodies
from qualified lookups in a function signature. Both of these use the
function itself as the decl context, so we need to distinguish them manually.

This particular commit doesn't change much because expressions don't often
use TypeMember constraints. But it does help with for-loops!

Swift SVN r23484
2014-11-20 20:58:19 +00:00
Joe Groff
2405002991 Sema: Remove the subtype constraint introduced for checked casts.
This prevented metatype casts and other kinds of cast that could be allowed from being accepted. Keep the subtype constraint if we're casting between generic class types with open type variables, because it can provide context to deduce type arguments in this case. This causes a regression in test/Constraints/members.swift that uses 'as' as a coercion, but we're planning to disambiguate cast/coercion syntax soon, which should fix that issue.

Swift SVN r23303
2014-11-13 17:08:04 +00:00
David Farler
c453eb4c48 Add Set type.
<rdar://problem/14661754> TLF: [data-structure] Set<T> data type + Bridging from NSSet

Swift SVN r23262
2014-11-12 07:07:00 +00:00
Joe Groff
53deeebe39 Sema: Relax existential-to-concrete type checking.
There's no reason to constrain casting to or from a generic or existential, since their types may be dynamically equal even if we don't have static knowledge that they are. First step here is to remove the subtype constraint we used to introduce for existential-to-concrete casts.

Swift SVN r23148
2014-11-07 01:17:48 +00:00
Joe Groff
5e682924f6 AST: Drop insignificant CheckedCastKinds.
We'd like to kill this enum off eventually, since the runtime inevitably needs to be able to handle arbitrary checked casts in opaque contexts, and SILGen and IRGen can deal with picking more optimal runtime entry points for specific casts. Only the container bridging kinds are still depended on anymore, and even those ought to eventually be handlable by the runtime in 'x as T' situations. NFC yet.

Swift SVN r23127
2014-11-06 04:05:05 +00:00
Chris Willmore
22f2452909 Only allow labeled parameters with function type to claim trailing closures, as opposed to any unlabeled argument.
For real this time. Added some additional tests.

rdar://problem/18778670

Swift SVN r23094
2014-11-04 03:40:08 +00:00
Chris Willmore
da2e3c4c73 Revert "Only allow labeled parameters with function type to claim trailing closures, as opposed to any unlabeled argument."
The commit broke the PerfTestSuite build:

/Users/buildslave/jenkins/sharedspace/swift-release-asserts/swift/src/tools/swift/benchmark/PerfTestSuite/SingleSource/DollarChain.swift:30:14:
error: missing argument label 'function:' in call
       $.tap(beatle, {$0.name = "Beatle"}).color = "Blue"

This reverts commit r23090.

Swift SVN r23093
2014-11-04 02:19:10 +00:00
Chris Willmore
f64bdb3021 Only allow labeled parameters with function type to claim trailing closures, as opposed to any unlabeled argument.
rdar://problem/18778670

Swift SVN r23090
2014-11-03 22:22:48 +00:00
Devin Coughlin
0aa115c09b Add diagnostics for potentially unavailable initializers.
This patch adds diagnostics for initializers that are potentially unavailable.
It does not treat such initializers as optionals, even when
EnableExperimentalUnavailableAsOptional is true -- there is some tricky
interaction with failable initializers that still needs to be worked out.



Swift SVN r22548
2014-10-06 19:50:08 +00:00
Devin Coughlin
59814bd140 Add type checking for potentially unavailable method references.
This commit modifies Sema to add type checking for potentially unavailable
method references. We now record the reason for method unavailability when
recording a potential overload choice during member constraint simplification
and either diagnose or lift to an optional type during CSApply. This commit also
generalizes UnavailableToOptionalExpr to take an arbitrary subexpression.

This commit does not address potentially unavailable properties, initializers,
or dynamic member references.


Swift SVN r22508
2014-10-03 23:23:13 +00:00
Jordan Rose
042569a3be Optional: Replace uses of Nothing with None.
llvm::Optional (like Swift.Optional!) uses None as its placeholder value,
not Nothing.

Swift SVN r22476
2014-10-02 18:51:42 +00:00
Jordan Rose
e999f40628 Replace "optVal = Nothing" with "optVal.reset()" for Optional<ArrayRef<T>>.
In preparation for the switch to llvm::Optional, which is constructible from
the same dummy llvm::NoneType as llvm::ArrayRef, making the assignment
ambiguous.

Swift SVN r22473
2014-10-02 18:51:37 +00:00
Devin Coughlin
2e8d4bc718 Add handling of potentially unavailable overloaded global functions to Sema.
This commit also factors out some common checking and diagnostic code; it
additionally moves diagnostic emission for unavailable references from CSGen to
CSApply.


Swift SVN r22447
2014-10-01 23:52:08 +00:00
Doug Gregor
8cca0ae85d Solver: eliminate construction constraints. We never need to retain them.
Swift SVN r22432
2014-10-01 18:25:49 +00:00
John McCall
8c303ef7a6 Representational changes towards get-and-mutableAddress
properties.

The main design change here is that, rather than having
purportedly orthogonal storage kinds and has-addressor
bits, I've merged them into an exhaustive enum of the
possibilities.  I've also split the observing storage kind
into stored-observing and inherited-observing cases, which
is possible to do in the parser because the latter are
always marked 'override' and the former aren't.  This
should lead to much better consideration for inheriting
observers, which were otherwise very easy to forget about.
It also gives us much better recovery when override checking
fails before we can identify the overridden declaration;
previously, we would end up spuriously considering the
override to be a stored property despite the user's
clearly expressed intent.

Swift SVN r22381
2014-09-30 08:39:38 +00:00
Devin Coughlin
3749a98763 Record the reason a declaration is potentially unavailable.
This commit adds tracking of the reason a declaration reference is potentially
unavailable to the UnavailableToOptionalExpr AST node and to OverloadChoice. We
will use this reason during SILGen to emit the appropriate run-time check and
during typechecking to provide more helpful diagnostics.

To keep OverloadChoice as small as possible, we encode the reason as an index
into a vector of reasons stored in a given instance of ConstraintSystem (this is
the same approach that Fix takes).

This commit adds Sema/OverloadChoice.cpp (for the parts of OverloadChoice that
now rely on ConstraintSystem) and AST/Availability.h (to bring in
availability-related structures without TypeRefinementContext).


Swift SVN r22377
2014-09-30 01:53:59 +00:00
Dmitri Hrybenko
d396134832 Portability: use std::make_tuple instead of relying on a libc++
extension (an implicit constructor in std::tuple)

Swift SVN r22339
2014-09-28 18:44:34 +00:00
John McCall
1b3771c10b Fix a pair of bugs with rvalue addressed subscripting.
Swift SVN r22258
2014-09-24 09:07:30 +00:00
Devin Coughlin
b727b6d932 Treat potentially unavailable global variable references as optional.
This patch adds the ability (-enable-experimental-unavailable-as-optional) to
treat potentially unavailable declarations as if they had optional types. For
the moment, this is only implemented for global variables.

The high-level approach is to (1) record the potential unavailability of a
declaration reference in the overload choice during constraint generation; (2)
treat the declaration as if it had an optional type during overload resolution
(this is similar to how optional protocol members are treated); and (3) add an
implicit conversion (UnavailableToOptionalExpr) during constraint application
to represent the run-time availability check and optional injection.

This patch does not implement SILGen for UnavailableToOptionalExpr.


Swift SVN r22245
2014-09-24 00:07:46 +00:00
Doug Gregor
55afb33c94 Associate argument labels with the callee in the solver.
When performing name lookup for a declaration that is being called,
use the argument labels at the call site to filter out those
declarations with incompatible argument labels.


Swift SVN r22176
2014-09-22 20:08:01 +00:00
Doug Gregor
5c5cc998a1 Provide Fix-Its for the RawRepresentable interface change.
Calls to fromRaw are replaced with uses of the new failable
initializer init(rawValue:). Similarly, calls to toRaw are replaced
with uses of the rawValue property. Fixes rdar://problem/18357647.


Swift SVN r22164
2014-09-21 23:10:07 +00:00