Commit Graph

2448 Commits

Author SHA1 Message Date
Chris Willmore
af12966eb8 When fixing implicit conversion of bridged types, only insert 'as T'
instead of 'as! T' if explicit conversion is sufficient.

Refinement of fix for <rdar://problem/19551164>.

Swift SVN r25104
2015-02-10 00:29:13 +00:00
Chris Willmore
aee4c26eb0 Fix indentation of fix cases in ConstraintSystem::applySolution().
Swift SVN r25062
2015-02-07 00:37:10 +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
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
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
666646fee9 Fix "the world's shortest Swift crasher" (case 1083), as described this afternoon on Twitter by @PracticalSwift. Doing so has the nice side effect of addressing several other crashers.
Swift SVN r24857
2015-01-30 21:50:22 +00:00
Devin Coughlin
00ccdbc6c1 Sema: Move potential unavailability diagnostics into MiscDiagnostics.
This commit moves potential unavailability diagnostics (when not treating unavailable
symbols as optional) out of CSApply.cpp and into MiscDiagnostics.cpp, alongside the
deprecation-based and explicitly unavailable checks. This move is a precursor to adding
support for separate availability information on getters and setters. No intended
functional change.

Swift SVN r24836
2015-01-30 00:25:27 +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
d2c2add5de Introduce a hack for ensuring that, in the face of unresolved member exprs, call argument types line up nicely with the associated argument tuple type. (rdar://problem/18841652, rdar://problem/18913150, etc.)
This was one of our most visible user-facing crashers, manifesting itself any time a user performed an equality comparison on an unresolved enum case.

Swift SVN r24753
2015-01-27 20:56:03 +00:00
Chris Willmore
32438add4a <rdar://problem/19495253> Incorrect diagnostic for explicitly casting to the same type
Change "downcast" to "cast" in warnings where downcast isn't actually
downcast.

Swift SVN r24704
2015-01-24 01:28:55 +00:00
Graham Batty
057c27f009 Disable existential metatype casting on non-objc.
Also fixes getting the size of an instance of a class to
work without it when objective-c interop is turned off.

Swift SVN r24477
2015-01-16 20:27:54 +00:00
John McCall
6a46350546 Bugfixes for the new addressor kinds.
Permit non-Ordinary accesses on references to functions,
with the semantics of devirtualizing the call if the
function is a class member.  This is important for
constructing direct call to addressors from synthesized
materializeForSet accessors: for one, it's more
performant, and for another, addressors do not currently
appear in v-tables.

Synthesize trivial accessors for addressed class members.
We weren't doing this at all before, and I'm still not
sure we're doing it right in all cases.  This is a mess.

Assorted other fixes.  The new addressor kinds seem
to work now.

Swift SVN r24393
2015-01-13 10:37:22 +00:00
Chris Willmore
03a6190a1f <rdar://problem/19031957> Change failable casts from "as" to "as!"
Previously the "as" keyword could either represent coercion or or forced
downcasting. This change separates the two notions. "as" now only means
type conversion, while the new "as!" operator is used to perform forced
downcasting. If a program uses "as" where "as!" is called for, we emit a
diagnostic and fixit.

Internally, this change removes the UnresolvedCheckedCastExpr class, in
favor of directly instantiating CoerceExpr when parsing the "as"
operator, and ForcedCheckedCastExpr when parsing the "as!" operator.

Swift SVN r24253
2015-01-08 00:33:59 +00:00
Chris Lattner
1486238412 Reapply:
propagate the no-escape bit from a parameter into a closure-expr argument.  When a 
ClosureExpr is thus inferred to be non-escape, disable the "self." requirement.

.. with fixes.  Thanks again to Dmitri for reverting + adding testcase.



Swift SVN r24115
2014-12-23 18:21:14 +00:00
Dmitri Hrybenko
7ef8a95f8c Revert "propagate the no-capture bit from a parameter into a
closure-expr argument.  When a "

This reverts commit r24085.  It causes crashes in the validation
testsuite even with r24094.  Testcase:

    var x = { f: Int {} }

Swift SVN r24102
2014-12-23 04:06:15 +00:00
Chris Lattner
410c3ef431 propagate the no-capture bit from a parameter into a closure-expr argument. When a
ClosureExpr is thus inferred to be non-captured, disable the "self." requirement.


Swift SVN r24085
2014-12-22 23:58:39 +00:00
Chris Lattner
4345fce329 random tidying, NFC.
Swift SVN r24082
2014-12-22 22:51:09 +00:00
Joe Pamer
2912159776 Improve diagnostics for expression typecheck errors
These changes make the following improvements to how we generate diagnostics for expression typecheck failure:
- Customizing a diagnostic for a specific expression kind is as easy as adding a new method to the FailureDiagnosis class,
  and does not require intimate knowledge of the constraint solver’s inner workings.
    - As part of this patch, I’ve introduced specialized diagnostics for call, binop, unop, subscript, assignment and inout
      expressions, but we can go pretty far with this.
    - This also opens up the possibility to customize diagnostics not just for the expression kind, but for the specific types
      involved as well.
- For the purpose of presenting accurate type info, partially-specialized subexpressions are individually re-typechecked
  free of any contextual types. This allows us to:
    - Properly surface subexpression errors.
    - Almost completely avoid any type variables in our diagnostics. In cases where they could not be eliminated, we now
      substitute in "_".
    - More accurately indicate the sources of errors.
- We do a much better job of diagnosing disjunction failures. (So no more nonsensical ‘UInt8’ error messages.)
- We now present reasonable error messages for overload resolution failures, informing the user of partially-matching
  parameter lists when possible.

At the very least, these changes address the following bugs:

<rdar://problem/15863738> More information needed in type-checking error messages
<rdar://problem/16306600> QoI: passing a 'let' value as an inout results in an unfriendly diagnostic
<rdar://problem/16449805> Wrong error for struct-to-protocol downcast
<rdar://problem/16699932> improve type checker diagnostic when passing Double to function taking a Float
<rdar://problem/16707914> fatal error: Can't unwrap Optional.None…Optional.swift, line 75 running Master-Detail Swift app built from template
<rdar://problem/16785829> Inout parameter fixit
<rdar://problem/16900438> We shouldn't leak the internal type placeholder
<rdar://problem/16909379> confusing type check diagnostics
<rdar://problem/16951521> Extra arguments to functions result in an unhelpful error
<rdar://problem/16971025> Two Terrible Diagnostics
<rdar://problem/17007804> $T2 in compiler error string
<rdar://problem/17027483> Terrible diagnostic
<rdar://problem/17083239> Mysterious error using find() with Foundation types
<rdar://problem/17149771> Diagnostic for closure with no inferred return value leaks type variables
<rdar://problem/17212371> Swift poorly-worded error message when overload resolution fails on return type
<rdar://problem/17236976> QoI: Swift error for incorrectly typed parameter is confusing/misleading
<rdar://problem/17304200> Wrong error for non-self-conforming protocols
<rdar://problem/17321369> better error message for inout protocols
<rdar://problem/17539380> Swift error seems wrong
<rdar://problem/17559593> Bogus locationless "treating a forced downcast to 'NSData' as optional will never produce 'nil'" warning
<rdar://problem/17567973> 32-bit error message is really far from the mark: error: missing argument for parameter 'withFont' in call
<rdar://problem/17671058> Wrong error message: "Missing argument for parameter 'completion' in call"
<rdar://problem/17704609> Float is not convertible to UInt8
<rdar://problem/17705424> Poor error reporting for passing Doubles to NSColor: extra argument 'red' in call
<rdar://problem/17743603> Swift compiler gives misleading error message in "NSLayoutConstraint.constraintsWithVisualFormat("x", options: 123, metrics: nil, views: views)"
<rdar://problem/17784167> application of operator to generic type results in odd diagnostic
<rdar://problem/17801696> Awful diagnostic trying to construct an Int when .Int is around
<rdar://problem/17863882> cannot convert the expression's type '()' to type 'Seq'
<rdar://problem/17865869> "has different argument names" diagnostic when parameter defaulted-ness differs
<rdar://problem/17937593> Unclear error message for empty array literal without type context
<rdar://problem/17943023> QoI: compiler displays wrong error when a float is provided to a Int16 parameter in init method
<rdar://problem/17951148> Improve error messages for expressions inside if statements by pre-evaluating outside the 'if'
<rdar://problem/18057815> Unhelpful Swift error message
<rdar://problem/18077468> Incorrect argument label for insertSubview(...)
<rdar://problem/18079213> 'T1' is not identical to 'T2' lacks directionality
<rdar://problem/18086470> Confusing Swift error message: error: 'T' is not convertible to 'MirrorDisposition'
<rdar://problem/18098995> QoI: Unhelpful compiler error when leaving off an & on an inout parameter
<rdar://problem/18104379> Terrible error message
<rdar://problem/18121897> unexpected low-level error on assignment to immutable value through array writeback
<rdar://problem/18123596> unexpected error on self. capture inside class method
<rdar://problem/18152074> QoI: Improve diagnostic for type mismatch in dictionary subscripting
<rdar://problem/18242160> There could be a better error message when using [] instead of [:]
<rdar://problem/18242812> 6A1021a : Type variable leaked
<rdar://problem/18331819> Unclear error message when trying to set an element of an array constant (Swift)
<rdar://problem/18414834> Bad diagnostics example
<rdar://problem/18422468> Calculation of constant value yields unexplainable error
<rdar://problem/18427217> Misleading error message makes debugging difficult
<rdar://problem/18439742> Misleading error: "cannot invoke" mentions completely unrelated types as arguments
<rdar://problem/18535804> Wrong compiler error from swift compiler
<rdar://problem/18567914> Xcode 6.1. GM, Swift, assignment from Int64 to NSNumber. Warning shown as problem with UInt8
<rdar://problem/18784027> Negating Int? Yields Float
<rdar://problem/17691565> attempt to modify a 'let' variable with ++ results in typecheck error about @lvalue Float
<rdar://problem/17164001> "++" on let value could give a better error message

Swift SVN r23782
2014-12-08 21:56:47 +00:00
Chris Lattner
f3ed7e93e1 Completely redesign our AST representation of capturelists. Formerly,
a capture list hung off the CaptureExpr it was associated with.  This made
sense lexically (since a capture list is nested inside of the closure) but
not semantically.  Semantically, the capture list initializers are evaluated
outside the closure, the variables are bound to those values, then the closure
captures the newly bound values.

To directly represent this, represent captures with a new CaptureListExpr node,
which contains the ClosureExpr inside of it.  This correctly models the semantic
relationship, and makes sure that AST walkers all process the initializers of the
capture list as being *outside* of the closure.

This fixes rdar://19146761 and probably others.


Swift SVN r23756
2014-12-06 04:36:11 +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
Chris Lattner
180e957063 reject all optional chains that don't chain anything. Thanks to Jordan and Joe for the feedback.
--This line, and those bel that ow, will be ignored--

M    test/Parse/optional.swift
M    test/ClangModules/Security_test.swift
M    test/expr/expressions.swift
M    include/swift/AST/DiagnosticsSema.def
M    lib/Sema/CSApply.cpp


Swift SVN r23685
2014-12-04 18:46:46 +00:00
Chris Lattner
88c1b0a58b Implement <rdar://problem/19032294> Disallow postfix ? when not chaining
Swift SVN r23684
2014-12-04 18:26:43 +00:00
Jordan Rose
02a286821f Dependencies: member lookups in constraint systems are usually private.
This adds a new parameter to TypeChecker::lookupMember to specify when
something is known-private, which can then be passed along to
DeclContext::lookupQualified. This makes many of the existing member
lookup dependencies in the reference-dependencies.swift test correctly
count as private.

Swift SVN r23631
2014-12-03 02:55:58 +00:00
Graham Batty
e537c75d0c Revert "Disable existential metatype casting on non-objc."
This reverts commit 3e918838b3708bf2287e7caab437ccf72fe9a3f6.

Swift SVN r23579
2014-11-24 17:42:19 +00:00
Graham Batty
277891d8b5 Disable existential metatype casting on non-objc.
Swift SVN r23575
2014-11-24 17:40:44 +00:00
Jordan Rose
92a31beb26 Dependencies: many more pattern-related updates.
No test cases because all of them are about coercions that still do lookups
for other reasons.

Swift SVN r23526
2014-11-21 19:32:20 +00:00
Joe Groff
01da305569 Factor out a TypeBase::hasDependentProtocolConformances() method.
Make it easier to get the "do I expect null ProtocolConformance* pointers" logic right. Audit existing uses of is<ArchetypeType>() for this purpose.

Swift SVN r23479
2014-11-20 18:43:17 +00:00
Doug Gregor
c1c51b4e8b Move checking of non-failable-to-failable initializer calls into constraint application.
This makes sure we get the same checking for initializer delegation in
structs/enums as we do for classes, fixing rdar://problem/18458622.

Swift SVN r23128
2014-11-06 06:29:35 +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
Jordan Rose
cc23b8a936 Replace some unqualified lookups with direct lookups.
These are very-slightly-but-not-significantly faster; there's no need to
go through the intermediate layer of unqualified lookup.

No functionality change.

Swift SVN r22872
2014-10-22 01:00:22 +00:00
Devin Coughlin
4fcbf5a8bb [Sema] Add checking for declarations with potentially unavailable types.
This commit adds checking of declarations with potentially unavailable types to
the existing checking for explicitly unavailable types. At the moment, these
two checking mechanisms are separate -- I will unify them in a future commit.

This commit removes the TypeExpr-based checking added in r22651; such checks
are now superfluous.


Swift SVN r22679
2014-10-10 22:40:27 +00:00
Devin Coughlin
f8fe884e98 Add availability checking for references to classes to Sema.
This commit adds availability checking for references to classes in Sema. We
now emit a diagnostic when a potentially unavailable class is referenced
in an expression, either directly (via a TypeExpr) or as the result of being
substituted as a replacement type in a call to a generic function. This
latter check is needed at the callsite because the callee may use its
formal type parameter to get at the metadata for the type.

This commit does not check declarations of variables, etc., with unavailable
types -- this will come in a later patch.


Swift SVN r22651
2014-10-10 01:11:40 +00:00
Devin Coughlin
2e3acba163 Add availability checking for properties to Sema
This commit adds availability checking for properties to Sema. When
EnableExperimentalAvailabilityChecking is enabled, we now emit a diagnostic
when a potentially unavailable property is accessed. To support this, we
now build type refinement contexts for most ValueDecls.


Swift SVN r22622
2014-10-09 17:21:52 +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
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
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
Joe Groff
8faf876eb0 AST: Drop array injection functions from vararg representations.
We only need the array type (and a known intrinsic stdlib function) now in order to produce a literal array.

Swift SVN r22295
2014-09-26 04:22:00 +00:00
John McCall
6923101341 Rename "AccessKind" to "AccessSemantics". NFC.
There are a lot of different ways to interpret the
"kind" of an access.  This enum specifically dictates
the semantic rules for an access:  direct-to-storage
and direct-to-accessor accesses may be semantically
different from ordinary accesses, e.g. if there are
observers or overrides.

Swift SVN r22290
2014-09-25 23:12:39 +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
df33350219 Switch string interpolation protocol over to initializer requirements.
With this, we're now using initializer requirements rather than
"convertFromXXX" requirements everywhere, addressing the rest of
rdar://problem/18154091. r22176 eliminated the performance penalty
that prevented this change from sticking earlier.



Swift SVN r22177
2014-09-22 20:17:55 +00:00