Commit Graph

172 Commits

Author SHA1 Message Date
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 Willmore
a17ebe2f16 Add check to simplifyLocator for TupleElement.
When simplifying tuple element locator, be careful about possibly
accessing non-existent elements of TupleExpr anchor.

<rdar://problem/22426860> CrashTracer: [USER] swift at …mous_namespace::ConstraintGenerator::getTypeForPattern + 698

Swift SVN r31629
2015-09-02 08:50:59 +00:00
Chris Lattner
712d3447ce improve some diagnostics around inout expr.
Swift SVN r31415
2015-08-22 22:29:14 +00:00
Chris Lattner
3a03435a1e Now that we have various infrastructure improvements in place, we can
remove some unprincipled code from typeCheckChildIndependently that was
special casing some expr nodes.


Swift SVN r31407
2015-08-22 06:06:14 +00:00
Chris Lattner
6358c3049d Now that we consistently propagate contextual type information when it
is available, use it to start producing useful diagnostics for contextually
typed member references.


Swift SVN r31362
2015-08-20 17:07:15 +00:00
Chris Willmore
032f7532f7 Don't bind unlabeled type variable to single labeled argument.
<rdar://problem/22348394> Compiler crash on invoking function with labeled defaulted param with non-labeled argument

Swift SVN r31355
2015-08-19 22:57:29 +00: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
8e758ba19f Make the "type of expression is ambiguous without more context" diagnostic more
specific when it fails, by printing a potentially partially resolved type for the
ambiguous expression in question, which it carries information.  This can at least
tell what the ambiguous parts of the resultant type *are* in some cases (e.g. in
the Constraints/array_literal.swift case).  That said, this diagnostic is still
admittedly not great.

This also exposes a couple of cases where we produce bogus diagnostics in general 
(expr/cast/as_coerce.swift).  The issue here is that these shouldn't be ambiguous 
at all, they are being misreported due to 22320758), which I'll fix separately.



Swift SVN r31292
2015-08-18 06:05:46 +00:00
Chris Willmore
265f91fead Allow string literals in string literal interpolation.
Teach skipToEndOfInterpolatedExpression() to match quote marks as well
as parentheses in the interpolated expression. This makes expressions
like "hello \(names["bob"])" possible.

Swift SVN r31283
2015-08-18 00:41:01 +00:00
Chris Willmore
86f64d3e26 Don't simplify argument tuple conversion constraint $T1 < (T2, ...) to
$T1 < T2 if there are other required params in the tuple.

<rdar://problem/22211854> wrong arg list crashing sourcekit

Swift SVN r31218
2015-08-13 18:28:25 +00:00
Chris Willmore
215eef9565 CSDiag: Set CTPurpose in right place when typechecking function argument independently.
<rdar://problem/22211854> wrong arg list crashing sourcekit

Swift SVN r31216
2015-08-13 17:46:34 +00:00
Chris Lattner
42d53dc414 Fix:
<rdar://problem/18397777> QoI: special case comparisons with nil
<rdar://problem/18042123> QoI: Fixit for "if !optional" should suggest "if optional == nil"



Swift SVN r31204
2015-08-13 04:36:43 +00:00
Jordan Rose
a26716290c When adding " != nil" to an optional in an 'if', we may need parens.
...such as with 'try?'.

rdar://problem/22259712

Swift SVN r31199
2015-08-13 03:08:15 +00:00
Dmitri Hrybenko
ee20926b20 stdlib: rename join() to joinWithSeparator()
rdar://22022419, rdar://21474222

Swift SVN r31188
2015-08-12 21:16:38 +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
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
8c88ebc535 now that we have a simpler structure for the overal CSDiag algorithm, change
diagnoseGeneralFailure to be named diagnoseConstraintFailure and change how
it works:
Now it ranks unresolved constraints  in the system based on kind (e.g. whether
they are favored, member constraints ahead of conversion constraints, etc) and
then tries to emit a diagnostic for each failure kind one after another.

This means that if there are multiple failed conversion constraints, but one
is obviously satisfiable, that we continue on to diagnose the next one.  This 
clears up a swath of embarassing diagnostics and refixes:
<rdar://problem/19658691> QoI: Incorrect diagnostic for calling nonexistent members on literals



Swift SVN r31046
2015-08-06 05:55:28 +00:00
Chris Lattner
072868d3f6 Simplify the implementation of visitCoerceExpr/visitForcedCheckedCastExpr,
and diagnoseGeneralConversionFailure().  The previous approach of trying
to dig into anchors would often lead to complaining about types at 
different levels in the same diagnostic, and the complexity of the former
code isn't needed now that other changes have landed.



Swift SVN r31036
2015-08-06 00:58:55 +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
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
Jordan Rose
953424072e Guard "object literals" feature with SWIFT_ENABLE_OBJECT_LITERALS.
This is not a feature we're releasing at the moment, so provide a way
to turn it off.

rdar://problem/21935551

Swift SVN r30966
2015-08-04 00:16:52 +00:00
Chris Lattner
6d3a1cc6fc improve a diagnostic, resolving a fixme.
Swift SVN r30951
2015-08-03 18:37:03 +00:00
Chris Lattner
dcfc6180cf improve QoI of inout argument failures, and strip some single element tuple labels.
Swift SVN r30908
2015-08-01 05:20:23 +00:00
Chris Lattner
97e6a50148 Start using contextual information from function calls to diagnose issues in their
argument.  For now we start with some of the most simple cases: single argument 
calls.  This dramatically improves the QoI for error messages in argument lists,
typically turning a error+note combo into a single specific error message.

Some minor improvements coming (and also generalizing this to n-ary calls), but it 
is nice that all the infrastructure is starting to come together...



Swift SVN r30905
2015-08-01 04:37:52 +00:00
Chris Lattner
3a5e3b23fe testcase for radar fixed earlier today
Swift SVN r30757
2015-07-29 05:18:46 +00:00
Chris Lattner
d3e1f2b2f7 testcase for a bug fixed by r30746
Swift SVN r30754
2015-07-29 04:42:38 +00:00
Chris Lattner
f2746eaf7b Add inout expr validation to the SyntacticUseRestrictions checker
in MiscDiagnostics, allowing us to correctly and consistently reject 
InOutExprs that do not appear in argument contexts.


Swift SVN r30746
2015-07-29 01:02:46 +00:00
Chris Lattner
8a9c93da66 With the previous fix, all of the tests from:
https://twitter.com/practicalswift/status/625429628107255808?refsrc=email&s=11

all compile without crashing, other than "()=()", which is tracked by:
<rdar://problem/21886435> Swift compiler crashes on "()=()"

This one crashes because "()" isn't being type checked as an lvalue.  If someone
feels inspired to fix this, it is our shortest known compiler crash.



Swift SVN r30735
2015-07-28 23:56:29 +00:00
Chris Lattner
794f9d9b74 fix <rdar://problem/18496742> Passing ternary operator expression as inout crashes Swift compiler
and some related crashers.



Swift SVN r30734
2015-07-28 23:52:12 +00:00
Chris Lattner
1a0a0315fe wordsmith a diagnostic, NFC otherwise.
Swift SVN r30731
2015-07-28 23:35:25 +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
922a7f53b3 consolidate the diagnostics produced by the "Failure" case and the expr walker in CSDiags to
get the same wording, fixing <rdar://problem/21964599> Different diagnostics for the same issue

While I'm in the area, remove some dead code.



Swift SVN r30713
2015-07-28 04:43:37 +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
467cd07968 Two changes:
- Improve handling of if_expr in a couple of ways: teach constraint simplification
   about IfThen/IfElse and teach CSDiags about the case when the cond expr doesn't match
   BooleanType.  This is rarely necessary, but CSDiags is all about cornercases, and this
   does fix a problem in a testcase.
 - Be a bit more specific about the constraint failure kind (e.g. say subtype) and when
   we have a protocol conformance failure, emit a specific diagnostic about it, instead of
   just saying that the types aren't convertible.




Swift SVN r30650
2015-07-26 05:31:12 +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
Dmitri Hrybenko
d97ac3e64c stdlib: rename RangeReplaceableCollectionType.extend() to appendContentsOf()
rdar://21972324

Swift SVN r30607
2015-07-25 00:36:37 +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
0001dc27bb remove support for the experiemental "character literals" feature.
Swift SVN r30509
2015-07-22 22:35:19 +00:00
Chris Lattner
5d3dc0d77b Now that sufficient complexity is pushed into the right corners of the compiler,
we can start taking advantage of ambiguously typed subexpressions in CSDiags.  We
start by validating the callee function of ApplyExprs, which substantially improves
our abilities to generate precise diagnostics about malformed calls.

This is the minimal introduction of this concept to CSDiags, a lot of refactoring
is yet to come, however, this is enough to resolve:

<rdar://problem/21080030> Bad diagnostic for invalid method call in boolean expression
<rdar://problem/21784170> Incongruous `unexpected trailing closure` error in `init` function which is cast and called without trailing closure.

one of the testcases from:
<rdar://problem/20789423> Unclear diagnostic for multi-statement closure with no return type

and a bunch of other places where we got weird "unexpected trailing closure" 
diagnostics that made no sense.  As usual, it is two steps forward and one step back,
as this exposed some other weird latent issues like:
<rdar://problem/21900971> QoI: Bogus conversion error in generics case




Swift SVN r30429
2015-07-21 01:06:55 +00:00
John McCall
bc3b47b98a Infer the return type of a closure to be () if it contains no
return statements, or a return statement with no operand.

Also, fix a special-case diagnostic about converting a return
expression to (1) only apply to converting the actual return
expression, not an arbitrary sub-expression, and (2) use the
actual operand and return types, not the drilled-down types
that caused the failure.

Swift SVN r30420
2015-07-20 21:52:18 +00:00
Joe Pamer
3edab3db8d When walking overload groups during constraint analysis, defend against function/decl type mismatches. (rdar://problem/20162476)
Swift SVN r30202
2015-07-14 22:58:54 +00:00
Chris Lattner
63f99a486c Move CallExpr diagnostics over to the same overload candidate diagnosis
facilities used by operators etc.  This required a bunch of changes to make
the diagnostics changes strictly an improvement:

  - Teach the new path about calls to TypeExprs.
  - Teach evaluateCloseness some simple things about varargs.
  - Make the generic diagnosis logic produce a better error when there is 
    exactly one match.

Overall, the resultant diagnostics are a step forward: we now produce candidate
set notes more uniformly, and the messages about some existing ones are 
more specific.  This is just another stepping stone towards progress though.



Swift SVN r30057
2015-07-10 04:26:42 +00:00
Chris Lattner
2afc0dbaa9 Minor improvements:
- Remove a weird special case for literals from TypeChecker::typeCheckCondition.
 - Enhance FailureDiagnosis::getTypeOfIndependentSubExpression to know about situations
   where recursive type checks fail (in some nested situation) but still produce a type
   for the top level of the expr tree.
 - Remove dead code from CSApply now that you can't branch on Builtin.Int1.

The first & second combine to slightly improve one case I've been looking at in 
test/expr/expressions.swift.




Swift SVN r29860
2015-07-01 22:53:37 +00:00
Chris Lattner
45dc17d20f move some expected-error lines onto the line of their expr to make it easier to read dumps, nfc
Swift SVN r29849
2015-07-01 19:19:24 +00:00
Dmitri Hrybenko
7ee84ed6b3 stdlib: rename Reflectable.{getMirror() -> _getMirror()}
Start removing the old mirrors.

rdar://21428474

Swift SVN r29823
2015-07-01 00:31:32 +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