Commit Graph

28 Commits

Author SHA1 Message Date
Mark Lacey
51f8c93f58 Merge pull request #17918 from rudkx/remove-inc-dec-operators
Remove the '++' and '--' operators.
2018-07-14 16:27:57 -07:00
Doug Gregor
945c09b1cc [Type checker] Improve diagnostics when an optional value is not unwrapped.
When we determine that an optional value needs to be unwrapped to make
an expression type check, use notes to provide several different
Fix-It options (with descriptions) rather than always pushing users
toward '!'. Specifically, the errors + Fix-Its now looks like this:

    error: value of optional type 'X?' must be unwrapped to a value of
        type 'X'
      f(x)
        ^
    note: coalesce using '??' to provide a default when the optional
        value contains 'nil'
      f(x)
        ^
          ?? <#default value#>
    note: force-unwrap using '!' to abort execution if the optional
        value contains 'nil'
      f(x)
         ^
         !

Fixes rdar://problem/42081852.
2018-07-13 11:02:04 -07:00
Mark Lacey
8ee52763f0 Remove the '++' and '--' operators.
We still had unavailable versions of these for floating-point types
only. We shouldn't need to keep these around, and can instead just
emit a helpful diagnostic for anyone that attempts to use them.

Unfortunately I don't see any way for the diagnostic to produce an
actual fix-it, so it just suggests '+= 1' or '-= 1' without actually
producing a fix.
2018-07-12 15:46:57 -07:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Chris Lattner
0224f2d858 Convert more tests off of ++ and -- 2015-12-21 18:07:37 -08:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Chris Lattner
ede0c50856 Revamp how value & type member constraint failures are diagnosed, eliminating the
"unavoidable failure" path, along with Failure::DoesNotHaveNonMutatingMember and
just doing some basic disambiguation in CSDiags.

This provides some benefits:
 - Allows us to plug in much more specific diagnostics for the existing "only has 
   mutating members" diagnostic, including producing notes for why the base expr
   isn't mutable (see e.g. test/Sema/immutability.swift diffs).
 - Corrects issues where we'd drop full decl name info for selector references.
 - Wordsmiths diagnostics to not complain about "values of type Foo.Type" instead 
   complaining about "type Foo"
 - Where before we would diagnose all failures with "has no member named", we now
   distinguish between when there is no member, and when you can't use it.  When you
   can't use it, you get a vauge "cannot use it" diagnostic, but...
 - This provides an infrastructure for diagnosing other kinds of problems (e.g. 
   trying to use a private member or a static member from an instance).
 - Improves a number of cases where failed type member constraints would produce uglier
   diagnostics than a different constraint failure would.
 - Resolves a number of rdars, e.g. (and probably others):
   <rdar://problem/20294245> QoI: Error message mentions value rather than key for subscript



Swift SVN r30715
2015-07-28 07:04:22 +00:00
Chris Lattner
1a3d3b0bae teach resolveImmutableBase about BindOptionalExpr, making some mutability diagnostics more precise.
Swift SVN r29799
2015-06-30 03:46:29 +00:00
Chris Lattner
3c7cb837e8 Now that diagnoseGeneralOverloadFailure() has a correct notion of the
ApplyExpr involved in the overload failure, wire it up to some of the
mechanics we have for more specific situations, including diagnosing
the last (known to me) mutation issues that weren't being specifically
diagnosed, as well as printing the candidate set in these cases.



Swift SVN r29797
2015-06-29 23:52:57 +00:00
Chris Lattner
d5aaf13e04 fix <rdar://problem/21459429> QoI: Poor compilation error calling assert
This teaches overload constraint diagnosis to look at the resolved anchor 
expression that fails (instead of assuming that it is the expr itself) and
walks up the AST to find the applyexpr in question.  This allows us to give
much more specific diagnostics for overload resolution failures, and to give
much more specific location information.

Where before my recent patches we used to produce:

t.swift:2:3: error: cannot invoke 'assert' with an argument list of type '(Bool, String)'
  assert(a != nil, "ASSERT COMPILATION ERROR")
  ^
t.swift:2:9: note: expected an argument list of type '(@autoclosure () -> Bool, @autoclosure () -> String, file: StaticString, line: UWord)'
  assert(a != nil, "ASSERT COMPILATION ERROR")
        ^

with this and the other recent patches, we now produce:

t.swift:2:12: error: cannot invoke '!=' with an argument list of type '(Int, nil)'
  assert(a != nil, "ASSERT COMPILATION ERROR")
         ~~^~~~~~




Swift SVN r29792
2015-06-29 22:00:09 +00:00
Chris Lattner
be906493ac Teach CSDiag how to recursively walk into an arbitrary expression, to produce
its diagnostics in post-order.  Notably, this picks up support for if-expr, which
gives up much better diagnostics in ternary operators.  For example, rdar://17224804
used to produce:

error: could not find an overload for '<=' that accepts the supplied arguments
var monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
                  ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

now we produce:

 error: binary operator '+' cannot be applied to operands of type 'String' and 'Int'
var monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
                                        ~~~ ^ ~~~~~~~~~~~
 note: overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (UnsafeMutablePointer<Memory>, Int), (UnsafePointer<Memory>, Int)
var monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
                                        ^

which is the correct diagnostic.  While I'm at it, improve the location info for this
binary operator diagnostic to point to the right spot (the operator) and highlight
the LHS/RHS of the operator.



Swift SVN r29774
2015-06-28 17:53:36 +00:00
Chris Lattner
cf1cb14205 recommit 28958/28959, there was a testcase that didn't get updated correctly
but it was between the two revisions.


Swift SVN r28964
2015-05-23 15:50:16 +00:00
Ted Kremenek
b4b81ff657 Revert "merge assignments to UnresolvedDotExpr onto the common path,"
Backing out to fix the build.

Swift SVN r28959
2015-05-23 07:55:07 +00:00
Ted Kremenek
381eb52207 Revert "Further consolidate mutation code, this time for DeclRefExprs. With that"
Backing out to fix th build.

Swift SVN r28958
2015-05-23 07:55:04 +00:00
Chris Lattner
291d7eaad7 Further consolidate mutation code, this time for DeclRefExprs. With that
done, the rest of the infrastructure is all common and can be simplified.  This
leaves us with a quite small and maintainable subsystem for diagnosing these
kinds of problems.

 include/swift/AST/DiagnosticsSema.def |   28 ++-----
 lib/Sema/CSDiag.cpp                   |  132 ++++++++++------------------------
 2 files changed, 48 insertions(+), 112 deletions(-)




Swift SVN r28957
2015-05-23 05:49:19 +00:00
Chris Lattner
3b64718b89 merge assignments to UnresolvedDotExpr onto the common path,
this is neutral w.r.t. diagnostics quality, but deletes a ton
of code:

 include/swift/AST/DiagnosticsSema.def |   21 ++---------
 lib/Sema/CSDiag.cpp                   |   64 ++--------------------------------
 2 files changed, 9 insertions(+), 76 deletions(-)



Swift SVN r28956
2015-05-23 05:18:59 +00:00
Chris Lattner
d5f68b478f Teach the recursive part of the diagnostics to handle the various things
that make vardecls and subscripts immutable.  This makes the indirect cases
a lot more specific ("this is a get-only property" instead of "this is 
immutable") and allows us to consolidate a bunch of code:

 2 files changed, 45 insertions(+), 119 deletions(-)




Swift SVN r28954
2015-05-23 04:53:08 +00:00
Chris Lattner
3f0ca3e2c8 - Change the general assignment failure diagnostic to include the type of the
result and make it more specific.
- Move the 'assignment failure' diagnostic logic to CSDiags.cpp where it belongs.



Swift SVN r28937
2015-05-22 22:07:26 +00:00
Chris Lattner
29128d0d46 two testcase updates that went with r28854
Swift SVN r28855
2015-05-20 22:59:23 +00:00
Chris Lattner
bebbdfe001 fix <rdar://problem/17691565> attempt to modify a 'let' variable with ++ results in typecheck error not being able to apply ++ to Float
We now produce diagnostics like:
 - cannot pass 'let' value 'a' to mutating unary operator '++'
 - cannot pass get-only property 'b' to mutating unary operator '++'
 - cannot pass immutable value of type 'Int64' to mutating unary operator '++'



Swift SVN r27772
2015-04-26 20:42:19 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +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
Doug Gregor
1a8f5e1eec Remove -enable-optional-lvalues and its language option; they're always on anyway.
Swift SVN r21356
2014-08-21 15:53:31 +00:00
Joe Pamer
de9e8193c9 Fix minor regression from r21154
We only want to apply the "favored constraint" optimization to symmetric binary operators. This helps prevent the pool of favored constraints from growing so large that it becomes ineffective. (rdar://problem/18008088)

Swift SVN r21184
2014-08-13 21:25:31 +00:00
Doug Gregor
916e9a7eb5 Fix computation of “favored” constraints for binary expressions <rdar://problem/17943223>.
The determination of “favored” constraints for binary expressions was comparing the second argument to the first parameter to decide if the constraint is favored. Coupled with implicit bridging conversions through NSNumber, this meant that “1.0/10” would become Int when Foundation was imported, and hilarity ensued.

Fix the heuristic for favored constraints, tidy up this code a bit, and add ==(NSString, NSString) to cope with the ambiguity this creates.

Swift SVN r21154
2014-08-12 21:16:59 +00:00
Joe Pamer
0896a12197 Further improve type checker diagnostics through a variety of means.
Start capitalizing on some of the new diagnostic machinery in a few different ways:
- When mining constraints for type information, utilize constraints "favored" by the overload resolution process.
- When printing type variables, if the variable was created by opening a literal expression, utilize the literal
  default type or conformance if possible.
- Utilize syntactic information when crafting diagnostics:
	- If the constraint miner can produce a better diagnostic than the recorded failure, diagnose via constraints.
	- Factor in the expression kind when choosing which types to include in a diagnostic message.
- Start customizing diagnostics based on the amount of type data available.

What does all this mean?
- Fewer type variables leaking into diagnostic messages.
- Far better diagnostics for overload resolution failures. Specifically, we now print proper argument type data
  for failed function calls.
- No more "'Foo' is not convertible to 'Foo'" error messages
- A greater emphasis on type data means less dependence on the ordering of failed constraints. This means fewer
  inscrutable diagnostics complaining about 'UInt8' when all the constituent expressions are of type Float.

So we still have a ways to go, but these changes should greatly improve the number of head-scratchers served up
by the type checker.

These changes address the following radars:
rdar://problem/17618403
rdar://problem/17559042
rdar://problem/17007456
rdar://problem/17559042
rdar://problem/17590992
rdar://problem/17646988
rdar://problem/16979859
rdar://problem/16922560
rdar://problem/17144902
rdar://problem/16616948
rdar://problem/16756363
rdar://problem/16338509

Swift SVN r20927
2014-08-01 23:32:19 +00:00
Joe Groff
1893e316bd Sema: Pull infix assignment operators into optional chains.
During operator sequence folding, if we see (opt eval) <op>= expr, fold the operator into the optional evaluation. This allows for conditional reassignments like 'x?.y = 0' or 'a?.b += 2'.

Swift SVN r20611
2014-07-28 02:32:25 +00:00
Joe Groff
e527571264 Sema: Propagate lvalue-ness through optional chains.
When -enable-optional-lvalues is active, type-check '?' operations like '!' operations, using an OptionalObject constraint to match the optional subexpression type to the non-optional result type of equivalent lvalue-ness.

Swift SVN r20610
2014-07-28 01:56:00 +00:00