Commit Graph

50 Commits

Author SHA1 Message Date
Max Moiseev
859db53d87 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-01 12:56:26 -08:00
gregomni
545a3f913d [SR-832][Sema] Fix for function type args passed to @autoclosure params
Passing a function type to an @autoclosure param would always fail to
type check because of the attempt to decompose the parallel structure
of the two (both being functions). In this case, though, we don’t want
to do any such thing, we want to allow the ExprRewriter to explicitly
insert an AutoClosureExpr in coerceToType, as it would do with any
other non-function arg type.
2016-02-29 23:43:37 -08:00
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
Doug Gregor
7d70b704e4 Merge commit '5e11e3f7287427d386636a169c4065c0373931a8' into swift-3-api-guidelines 2016-01-19 23:18:20 -08:00
Chris Lattner
a5a988e726 Fix rdar://22509125 QoI: Error when unable to infer generic archetype lacks greatness
Rearrange diagnoseGeneralConversionFailure to diagnose structural problems
even if we have some UnresolvedTypes floating around, then reject constraint
failures with UnresolvedTypes in them even harder.  This keeps us giving
good errors about failures where we have a structural problem (with buried
irrelevant details) while not complaining about cases that are actually
ambiguous.

The end result of this is that we produce a lot better error messages in the
case of failed archetype inference.  This also highlights the poor job we do
handling multi-stmt closureexprs...
2016-01-11 20:45:11 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
ken0nek
c4e7d0b83a Make it pass test 2015-12-23 17:02:13 +09:00
ken0nek
3ac60b13f5 Add spaces before and after closure arrow in test 2015-12-23 04:38:46 +09:00
Max Moiseev
2f7b64e475 Merge remote-tracking branch 'origin' into swift-3-api-guidelines 2015-12-21 12:02:13 -08:00
Daniel Duan
239c6629e9 Remove trailing semi-colons in .swift files 2015-12-20 21:12:11 -08:00
Dmitri Gribenko
63cbe16b68 Rename Memory to Pointee in generic parameter names
UnsafePointer<Memory> => UnsafePointer<Pointee>
UnsafeMutablePointer<Memory> => UnsafeMutablePointer<Pointee>
AutoreleasingUnsafeMutablePointer<Memory> =>
  AutoreleasingUnsafeMutablePointer<Pointee>
2015-12-16 14:54:36 -08:00
Slava Pestov
f6e4355b69 Sema: Fix monthly TypeNullifier bug
This time, the issue is that TypeNullifier skips bodies of
multi-statement closures. However, ExprRewriter will type
happily pass them on to typeCheckClosureBody(). This could
trigger assertions. Fix this by skipping type checking of
multi-statement closures when diagnosing.

There seems to be a minor QoI regression in some test cases
that already looked pretty dodgy and/or had FIXMEs. However
I think its worth fixing a crash.
2015-12-11 08:58:52 -08:00
Chris Lattner
aad3cf9e10 Factor out structural error diagnostics out to a helper method (allowing
the code to be actually readable since it unnests it greatly), and call it
both before and after argument type validation.  This allows us to capture
many more structural errors than before, leading to much better diagnostics
in a lot of cases.  This also fixes the specific regressions introduced by
96a1e96.
2015-12-07 23:48:29 -08:00
Chris Lattner
df9d57219e Fix <rdar://problem/22602657> better diagnostics for closures w/o "in" clause
improving the message when too-many arguments are used in a closureexpr with
a known contextual type.
2015-11-17 21:31:53 -08:00
David Farler
93b6962478 Warn when using 'var' bindings in function parameters
These will no longer be allowed in a future Swift release.

rdar://problem/23172698
2015-11-03 17:24:20 -08:00
Chris Willmore
49e5130103 Allow inout closure param type to be inferred from context or usage.
Introduce a new constraint kind, BindParam, which relates the type of a
function parameter to the type of a reference to it from within the
function body. If the param type is an inout type, the ref type is an
lvalue type with the same underlying object type; otherwise the two
types must be the same. This prevents DeclRefExprs from being inferred
to have inout type in some cases.

<rdar://problem/15998821> Fail to infer types for closure that takes an inout argument

Swift SVN r32183
2015-09-23 18:46:12 +00:00
Chris Lattner
f571e64161 Enhance visitCallExpr in the face of a contextual type. Only perform the
forced conversion to "_ -> T" if it will refine the type otherwise found by
doing a non-contextual type check.  This allows us to diagnose calls to 
non-function values with more specificity, e.g. adding another case were we
recommend "do" when using bare braces.


Swift SVN r31726
2015-09-06 19:01:47 +00:00
Chris Lattner
e7a86e00b9 Finish up the last bit of work I plan on
<rdar://problem/22333281> QoI: improve diagnostic when contextual type of closure disagrees with arguments

In the common case where someone doesn't care about the argument 
list to a closure, we now generate a tailored error message with a 
fixit to introduce the necessary "_,_ in " nonsense at the start 
of the closure.  IMO ideally we wouldn't require this, but until we
fix that type checker issue, we should at least give people the
obvious fix.


Swift SVN r31720
2015-09-05 22:37:55 +00:00
Chris Lattner
86b47b8186 remove the TupleSizeMismatch failure mode and diagnose the problem in the mainline
expr diagnosis stuff, giving us much better diagnostics on the cases in
expr/closure/closures.swift.  This is part #2 of resolving
<rdar://problem/22333281> QoI: improve diagnostic when contextual type of closure disagrees with arguments



Swift SVN r31717
2015-09-05 21:38:06 +00:00
Chris Lattner
6619cf76dd Improve the diagnostics generated when the contextual type for a closure
specifies some # of arguments but the closureexpr itself disagrees.  This is 
step #1 to resolving
<rdar://problem/22333281> QoI: improve diagnostic when contextual type of closure disagrees with arguments



Swift SVN r31715
2015-09-05 21:02:18 +00:00
Chris Lattner
778d8d67e6 Use isUnresolvedOrTypeVarType more generally, and fix a crash on the new testcase, handling
unresolved subscript bases.


Swift SVN r31698
2015-09-04 23:03:11 +00:00
Chris Lattner
a3048cf950 fix <rdar://problem/22414757> "UnresolvedDot" "in wrong phase" assertion from verifier
Swift SVN r31463
2015-08-26 00:01:47 +00:00
Chris Lattner
a899872d91 Reapply r31105, with some fixes to invalid unconstrained generics. These fixes correct
the regressions that r31105 introduced in the validation tests, as well as fixing a number
of other validation tests as well.

Introduce a new UnresolvedType to the type system, and have CSDiags start to use it
as a way to get more type information out of incorrect subexpressions.  UnresolvedType
generally just propagates around the type system like a type variable:
 - it magically conforms to all protocols
 - it CSGens as an unconstrained type variable.
 - it ASTPrints as _, just like a type variable.

The major difference is that UnresolvedType can be used outside the context of a
ConstraintSystem, which is useful for CSGen since it sets up several of them to 
diagnose subexpressions w.r.t. their types.

For now, our use of this is extremely limited: when a closureexpr has no contextual
type available and its parameters are invalid, we wipe them out with UnresolvedType
(instead of the previous nulltype dance) to get ambiguities later on.

We also introduce a new FreeTypeVariableBinding::UnresolvedType approach for
constraint solving (and use this only in one place in CSDiags so far, to resolve
the callee of a CallExpr) which solves a system and rewrites any leftover type 
variables as UnresolvedTypes.  This allows us to get more precise information out,
for example, diagnosing:

 func r22162441(lines: [String]) {
   lines.map { line in line.fooBar() }
 }

with: value of type 'String' has no member 'fooBar'
instead of: type of expression is ambiguous without more context

This improves a number of other diagnostics as well, but is just the infrastructural
stepping stone for greater things.





Swift SVN r31130
2015-08-11 06:06:05 +00:00
Chris Lattner
2204dbcbfd revert r31105, it causes some regressions on validation tests.
Swift SVN r31107
2015-08-10 15:01:22 +00:00
Chris Lattner
bc0ef07727 a diagnostic case that got better with my recent patch.
Swift SVN r31106
2015-08-10 06:20:38 +00:00
Chris Lattner
de79b60c89 Introduce a new UnresolvedType to the type system, and have CSDiags start to use it
as a way to get more type information out of incorrect subexpressions.  UnresolvedType
generally just propagates around the type system like a type variable:
 - it magically conforms to all protocols
 - it CSGens as an unconstrained type variable.
 - it ASTPrints as _, just like a type variable.

The major difference is that UnresolvedType can be used outside the context of a
ConstraintSystem, which is useful for CSGen since it sets up several of them to 
diagnose subexpressions w.r.t. their types.

For now, our use of this is extremely limited: when a closureexpr has no contextual
type available and its parameters are invalid, we wipe them out with UnresolvedType
(instead of the previous nulltype dance) to get ambiguities later on.

We also introduce a new FreeTypeVariableBinding::UnresolvedType approach for
constraint solving (and use this only in one place in CSDiags so far, to resolve
the callee of a CallExpr) which solves a system and rewrites any leftover type 
variables as UnresolvedTypes.  This allows us to get more precise information out,
for example, diagnosing:

 func r22162441(lines: [String]) {
   lines.map { line in line.fooBar() }
 }

with: value of type 'String' has no member 'fooBar'
instead of: type of expression is ambiguous without more context

This improves a number of other diagnostics as well, but is just the infrastructural
stepping stone for greater things.



Swift SVN r31105
2015-08-10 06:18:27 +00:00
Chris Lattner
442de4d4b0 fix <rdar://problem/22162441> Crash from failing to diagnose nonexistent method access inside closure
When CSGen was analyzing a DeclRefExpr reference, it was accidentally 
applying some magic only to anonymous closure parameters, not named ones,
leading to a silent CSGen failure.  Fix this by handling all closure parameters
the same way.



Swift SVN r31098
2015-08-08 19:09:15 +00:00
Chris Lattner
b6fbccc1c1 Fix <rdar://problem/21544303> QoI: "Unexpected trailing closure" should have a fixit to insert a 'do' statement
which is gratuitous QoI, but also something that people reasonably hit when used to C programming.



Swift SVN r30986
2015-08-04 06:01:21 +00:00
Chris Lattner
7a73392087 improve diagnostics relating to closure result types, both when they are
explicitly written and disagree with context, and when context provides a
non-explicitly written type that disagrees with the body of the closure.



Swift SVN r30984
2015-08-04 05:41:04 +00:00
Chris Lattner
748845aa4d Now that we have contextual type information more generally available, start
using it to improve closure diagnostics by inferring the types of otherwise
untyped closure paramdecls from this context information.  This 
resolves:

<rdar://problem/20371273> Type errors inside anonymous functions don't provide enough information
producing 
  error: binary operator '==' cannot be applied to operands of type 'Int' and 'UInt'
  note: overloads for '==' exist with these partially matching parameter lists: (UInt, UInt), (Int, Int)

and:
<rdar://problem/20978044> QoI: Poor diagnostic when using an incorrect tuple element in a closure
producing:
error: value of tuple type '(Int, Int)' has no member '2'

and probably a lot more.  We're still limited from getting things like "foo.map {...}" because 
we're not doing type subsitutions from the base into the protocol extension member.



Swift SVN r30971
2015-08-04 00:55:06 +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
220855433a rework some of CSDiags to be more type based and simplify some things. No major
win from this other than simplification.  Some minor wins are that we handle varargs
better and don't get extraneous ()'s in types in some cases.


Swift SVN r29729
2015-06-26 03:56:40 +00:00
Chris Lattner
e4b6afb9ae Start moving the testsuite to the "_ = foo()" idiom for evaluating an
expression but ignoring its value.  This is the right canonical way to do
this.  NFC, just testsuite changes.



Swift SVN r28638
2015-05-15 20:15:54 +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
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
Chris Lattner
8c6b2b6b5f adjust testsuite to put @autoclosure on decls instead of types, and remove a
couple of soon-to-be-invalid cases.



Swift SVN r24074
2014-12-22 20:14:11 +00:00
Joe Pamer
dc338c2a71 Update wording of some new diagnostics.
Swift SVN r23783
2014-12-08 21:56:52 +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
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
Chris Lattner
bc481f0fe1 implement <rdar://problem/16859927> remove the underscore in "auto_closure"
autoclosure is one work, not two.



Swift SVN r20253
2014-07-21 15:23:50 +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
Doug Gregor
9210cd5ff4 Replace T[] array syntax with [T] in the test suite
Swift SVN r19192
2014-06-25 23:39:24 +00:00
Ted Kremenek
0ce574eb9e Correct mistake where some 'pred' arguments became 'predecessor' instead of 'predicate'.
Thanks to Dmitri and Joe for pointing this out.

Swift SVN r19151
2014-06-24 23:47:00 +00:00
Ted Kremenek
58558fcca3 Rename 'succ' and 'pred' to 'successor' and 'predecessor' respectively.
This is motivated by <rdar://problem/17051606>.

This ends up renaming variables as well, which seems right for
consistency since we use "predicate" as variable name.

Swift SVN r19135
2014-06-24 19:27:19 +00:00
Doug Gregor
1efd9fba5b Start accepting '#' in addition to '`' to mark a keyword argument <rdar://problem/16891828>.
Update the standard library, tests, diagnostics, and Fix-Its.

Swift SVN r17981
2014-05-13 00:03:04 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Doug Gregor
6d514c17cf When type-checking explicit closure expressions within the
constraint-based type checker, include the body of the explicit
closure within the set of constraints. This allows type information to
flow from the body expression, so that we can type-check, e.g.,

  map(new Int[N], { $0.toString() })

to a String[] without context information. Fixes <rdar://problem/11229738>.


Swift SVN r2794
2012-08-27 19:01:29 +00:00