Commit Graph

31 Commits

Author SHA1 Message Date
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
Paul Meng
e172383e2f [Sema] Implement SE-0110
This commit built upon the work of Pull Request 3895. Apart from the
work to make the following work

```swift
let f: (Int, Int) -> Void = { x in  } // this is now an error
```

This patch also implement the part 2 mentioned in the #3895

```swift
let g: ((Int, Int)) -> Void = { y in  } // y should have type (Int, Int)
```
2016-08-14 12:02:27 +08:00
Jordan Rose
f42158b12e Revert "[Sema] ban multi-arguments to tuple coercion" (#3922)
It breaks cases where there really is a single unlabeled argument of tuple type, like this:

  let pairs = [(1, "A"), (2, "B")]
  print(pairs.map { $0.0 })
2016-08-01 19:22:19 -07:00
Daniel Duan
c9b73dacc2 [Sema] ban multi-arguments to tuple coercion
Implements part of SE-0110. Single argument in closures will not be accepted if
there exists explicit type with a number of arguments that's not 1.

```swift
let f: (Int, Int) -> Void = { x in } // this is now an error
```

Note there's a second part of SE-0110 which could be considered additive,
which says one must add an extra pair of parens to specify a single arugment
type that is a tuple:

```swift
let g ((Int, Int)) -> Void = { y in } // y should have type (Int, Int)
```

This patch does not implement that part.
2016-07-31 16:22:57 -07:00
Doug Gregor
b9363fe6bd [SE-0111] Enable SE-0111 by default. 2016-07-29 17:28:24 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Chris Lattner
f0377bee52 Fix <rdar://problem/22753605> QoI: bad diagnostic when closure has default argument
Fix <rdar://problem/16812341> QoI: Poor error message when providing a default value for a subscript parameter

by emitting a more specific diagnostic about the cases that aren't allowed.
2016-01-22 23:25:47 -08:00
Chris Lattner
7daaa22d93 Completely reimplement/redesign the AST representation of parameters.
Parameters (to methods, initializers, accessors, subscripts, etc) have always been represented
as Pattern's (of a particular sort), stemming from an early design direction that was abandoned.
Being built on top of patterns leads to patterns being overly complicated (e.g. tuple patterns
have to have varargs and default parameters) and make working on parameter lists complicated
and error prone.  This might have been ok in 2015, but there is no way we can live like this in
2016.

Instead of using Patterns, carve out a new ParameterList and Parameter type to represent all the
parameter specific stuff.  This simplifies many things and allows a lot of simplifications.
Unfortunately, I wasn't able to do this very incrementally, so this is a huge patch.  The good
news is that it erases a ton of code, and the technical debt that went with it.  Ignoring test
suite changes, we have:
   77 files changed, 2359 insertions(+), 3221 deletions(-)

This patch also makes a bunch of wierd things dead, but I'll sweep those out in follow-on
patches.

Fixes <rdar://problem/22846558> No code completions in Foo( when Foo has error type
Fixes <rdar://problem/24026538> Slight regression in generated header, which I filed to go with 3a23d75.

Fixes an overloading bug involving default arguments and curried functions (see the diff to
Constraints/diagnostics.swift, which we now correctly accept).

Fixes cases where problems with parameters would get emitted multiple times, e.g. in the
test/Parse/subscripting.swift testcase.

The source range for ParamDecl now includes its type, which permutes some of the IDE / SourceModel tests
(for the better, I think).

Eliminates the bogus "type annotation missing in pattern" error message when a type isn't
specified for a parameter (see test/decl/func/functions.swift).

This now consistently parenthesizes argument lists in function types, which leads to many diffs in the
SILGen tests among others.

This does break the "sibling indentation" test in SourceKit/CodeFormat/indent-sibling.swift, and
I haven't been able to figure it out.  Given that this is experimental functionality anyway,
I'm just XFAILing the test for now.  i'll look at it separately from this mongo diff.
2015-12-31 19:24:46 -08:00
Chris Lattner
248727780f Now that enough yaks are cleanly shaven, completely reimplement how
we process contextual constraints when producing diagnostic.  Formerly,
we would aggressively drop contextual type information on the floor under
the idea that it would reduce constraints on the system and make it more
likely to be solvable.  However, this also has the downside of introducing
ambiguity into the system, and some expr nodes (notably closures) cannot
usually be solved without that contextual information.

In the new model, expr diagnostics are expected to handle the fact that
contextual information may be present, and bail out without diagnosing an
error if that is the case.  This gets us more information into closures,
allowing more specific return type information, e.g. in the case in
test/expr/closure/closures.swift.

This approach also produces more correct diagnostics in a bunch of other
cases as well, e.g.:

-  var c = [:]  // expected-error {{type '[_ : _]' does not conform to protocol 'DictionaryLiteralConvertible'}} 
+  var c = [:]  // expected-error {{expression type '[_ : _]' is ambiguous without more context}}

and the examples in test/stmt/foreach.swift, test/expr/cast/as_coerce.swift,
test/expr/cast/array_iteration.swift, etc.

That said, this another two steps forward, one back thing.  Because we
don't handle propagating sametype constraints from results of calls to their
arguments, we regress a couple of (admittedly weird) cases.  This is now 
tracked by:
<rdar://problem/22333090> QoI: Propagate contextual information in a call to operands

There is also the one-off narrow case tracked by:
<rdar://problem/22333281> QoI: improve diagnostic when contextual type of closure disagrees with arguments



Swift SVN r31319
2015-08-18 23:55:02 +00:00
Chris Lattner
b6206ab418 add fixit checks to various type checker testcases
Swift SVN r31004
2015-08-04 20:30:54 +00:00
Chris Lattner
1a0a0315fe wordsmith a diagnostic, NFC otherwise.
Swift SVN r30731
2015-07-28 23:35:25 +00:00
Chris Lattner
06cc05daa9 reword a diagnostic, as suggested by Jordan
Swift SVN r30712
2015-07-28 04:03:25 +00:00
Chris Lattner
8c3e62d7c5 Provide contextually sensitive conversion failure messages for situations in
which we have a contextual type that was the failure reason.  These are a bit
longer but also more explicit than the previous diagnostics.



Swift SVN r30669
2015-07-26 23:06:40 +00:00
Chris Lattner
d91f5861d2 Change diagnoseFailure() for unavoidable failures to stop doing anything with
conversion failures, making a bunch of diagnostics more specific and useful.

UnavoidableFailures can be very helpful, but they can also be the first constraint
failure that the system happened to come across... which is not always the most
meaningful one.  CSDiag's expr processing machinery has a generally better way of
narrowing down which ones make the most sense.


Swift SVN r30647
2015-07-26 04:09:34 +00:00
Chris Lattner
ffc3554cf1 fix error recovery when parsing an invalid default argument to drop the default argument,
this avoids having downstream errors perturbed by their illegal presence.


Swift SVN r30645
2015-07-26 02:03:33 +00:00
Chris Lattner
04cca603c8 Start peering through the fog of ClosureExprs, using ambiguously typed subexprs to
diagnose problems inside of them instead of punting on them completely.

This leads to substantially better error messages in many cases, fixing:
 <rdar://problem/19870975> Incorrect diagnostic for failed member lookups within closures passed as arguments ("(_) -> _")
 <rdar://problem/21883806> Bogus "'_' can only appear in a pattern or on the left side of an assignment" is back
 <rdar://problem/20712541> QoI: Int/UInt mismatch produces useless error inside a block

and possibly others.  We are not yet capitalizing on available type information we do
have about closure exprs, so there are some cases where we produce
  "error: type of expression is ambiguous without more context"
when this isn't strictly true, but this is still a huge step forward.



Swift SVN r30547
2015-07-23 20:31:43 +00:00
Chris Lattner
e41c23801f A bunch of conflated changes:
- Fix TypeCheckExpr.cpp to be more careful when propagating sugar from an 
   argument to the result of the function.  We don't want to propagate parens,
   because they show up in diagnostics later.

 - Restructure FailureDiagnosis::diagnoseFailure() to strictly process the tree
   in depth first order.  Before it would only do this if contextual typing was
   unavailable, leading to unpredictable inconsistencies between diagnostics.

 - Always perform diagnoseContextualConversionError early, as part of the thing
   that calls the visitor, instead of in each visit method.  This may change in
   the future, but is a simplification for now.

 - Make the operator processing code handle the "candidate is an exact match"
   case by emitting a diagnostic indicating that the result type of the operator
   must not match expectations, instead of emitting the silly things like
   "binary operator '&' cannot be applied to two Int operands" which is obviously
   false.

These changes lead to minor improvements across the testsuite, and should make the
diagnostics more predictable for more complex real-world ones, but I haven't gone
through the radars yet.  

Major missing pieces:
 - CallExpr isn't using the same logic that the operators are.
 - When you have a near match (only one argument mismatches) we should specifically
   complain about that argument, instead of spewing an entire argument list.
 - The noescape function attr diagnostic is being emitted twice now.
 



Swift SVN r29733
2015-06-26 07:29:05 +00:00
Chris Lattner
4366da9250 more testcase updates for upcoming diagnostics change.
Swift SVN r28409
2015-05-11 06:05:00 +00:00
Doug Gregor
b2cc34c241 Remove '#' for making parameter names into argument labels.
If you want to make the parameter and argument label the same in
places where you don't get the argument label for free (i.e., the
first parameter of a function or a parameter of a subscript),
double-up the identifier:

  func translate(dx dx: Int, dy: Int) { }

Make this a warning with Fix-Its to ease migration. Part of
rdar://problem/17218256.

Swift SVN r27715
2015-04-24 23:58:57 +00:00
Doug Gregor
793b3326af Implement the new rules for argument label defaults.
The rule changes are as follows:
  * All functions (introduced with the 'func' keyword) have argument
  labels for arguments beyond the first, by default. Methods are no
  longer special in this regard.
  * The presence of a default argument no longer implies an argument
  label.

The actual changes to the parser and printer are fairly simple; the
rest of the noise is updating the standard library, overlays, tests,
etc.

With the standard library, this change is intended to be API neutral:
I've added/removed #'s and _'s as appropriate to keep the user
interface the same. If we want to separately consider using argument
labels for more free functions now that the defaults in the language
have shifted, we can tackle that separately.

Fixes rdar://problem/17218256.

Swift SVN r27704
2015-04-24 19:03:30 +00:00
Jordan Rose
ecf260e1cd [test] Fix expectations I changed without re-testing.
- Remove incorrectly copy/pasted expected-error in default_args.swift.
- Move expected-error to the correct line in protocols.swift

Swift SVN r25125
2015-02-10 17:37:47 +00:00
Jordan Rose
6fe976d3b0 Diagnose function conversions that change the function's ABI.
This post-hoc diagnostic replaces r24915, r25045, and r25054 by doing a
very basic check for representation incompatibility between two types.
More cases can be added as necessary.

rdar://problem/19600325, again.

Swift SVN r25117
2015-02-10 03:46:47 +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
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
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
Joe Pamer
a314db950e Remove another point of non-determinism while tracking failed constraints, and use this as an opportunity to improve certain diagnostics. (rdar://problem/16808495)
Swift SVN r19244
2014-06-26 19:48:48 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Doug Gregor
6e64ca66f0 Treat '|' as a delimiter while parsing the signature of a closure.
'|' is part of the character set for operators, but within the
signature of a closure we need to treat the first non-nested '|' as
the closing delimiter for the closure parameter list. For example,

  { |x = 1| 2 + x}

parses with the default value of '1' for x, with the body 2 + x. If
the '|' operator is needed in the default value, it can be wrapped in
parentheses:

  { |x = (1|2)| x }

Note that we have problems with both name binding and type checking
for default values in closures (<rdar://problem/13372694>), so they
aren't actually enabled. However, this allows us to parse them and
recover better in their presence.



Swift SVN r5202
2013-05-17 16:02:44 +00:00