Commit Graph

425 Commits

Author SHA1 Message Date
Chris Lattner
ceafc03074 make CSDiags more tolerant of a contextual unresolved type, fixing a
regression introduced by r31575


Swift SVN r31577
2015-08-30 03:05:37 +00:00
Chris Lattner
55628a97d2 Fix an overactive assertion: <rdar://problem/22470302> Crash with parenthesized call result
We type check expressions using a contextual purpose of CTP_CalleeResult
without a specific contextualType, because we install the contextual type
as a conversion constraint.  This formerly failed the assertion expecting
that you have to have a type if you have a purpose, because parenexprs 
propagated their contextual info down.

In addition to making the assertion in TypeCheckConstraints.cpp more
lenient, change visitCallExpr to just pass down the purpose directly
instead of installing it in its ExprTypeCheckListener.



Swift SVN r31575
2015-08-29 21:40:45 +00:00
Chris Lattner
1e4aa27407 fix <rdar://problem/22409190> QoI: Passing unsigned integer to ManagedBuffer elements.destroy()
... by remapping archetypes in partially resolved types down to 
unresolved type when providing contextual types.


Swift SVN r31522
2015-08-27 05:10:54 +00:00
Chris Lattner
d167dfbbfa When typechecking the callee of a CallExpr, and when we have a contextual type,
use that contextual type to guide typechecking of the callee.  This allows us to
propagate that type through generic constraints effectively, making us produce
much more useful diagnostics within closures taking methods like "map" (for 
example).

This fixes:
<rdar://problem/20491794> QoI closures: Error message does not tell me what the problem is
Specifically, running the testcase:

enum Color { case Unknown(description: String) }
let xs: (Int, Color) = [1,2].map({ ($0, .Unknown("")) })

produces: error: cannot convert call result type '[_]' to expected type '(Int, Color)'

Changing that to:
let xs: [(Int, Color)] = [1,2].map({ ($0, .Unknown("")) })

produces: error: missing argument label 'description:' in call
... with a fixit to introduce the label.

This also fixes most of 22333090, but we're only using this machinery for CallExprs
so far, not for operators yet.



Swift SVN r31484
2015-08-26 05:41:47 +00:00
Chris Lattner
1ce4511f14 add a TCC_ForceRecheck flag to typeCheckChildIndependently to factor handling
of the "isExprBeingDiagnosed" bit.  NFC.


Swift SVN r31482
2015-08-26 05:02:17 +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
dd5489a9ad When diagnosing overload set candidates, pass the decl into diagnose
instead of the location of the decl.  This matters when the candidates
are coming from the stdlib because they have no location, and we can
synthesize a prototype from the decl.  Before we'd get:

t.swift:36:28: error: ambiguous reference to member 'String.init'
    return lazy.map(String.init).joinBySeparator(", ")
                    ~~~~~~~^~~~
<unknown>:0: note: found this candidate
<unknown>:0: note: found this candidate
<unknown>:0: note: found this candidate
.... 40 more of these.

Now we get:

t.swift:36:28: error: ambiguous reference to member 'String.init'
    return lazy.map(String.init).joinBySeparator(", ")
                    ~~~~~~~^~~~
Swift.String:95:3: note: found this candidate
  init()
  ^
Swift.String:96:3: note: found this candidate
  init(_ _core: _StringCore)
  ^
Swift.String:3:3: note: found this candidate
  init(_ c: Character)
  ^
Swift.String:24:3: note: found this candidate
  init(_ characters: String.CharacterView)
  ^
...



Swift SVN r31458
2015-08-25 23:12:54 +00:00
Chris Lattner
712d3447ce improve some diagnostics around inout expr.
Swift SVN r31415
2015-08-22 22:29:14 +00:00
Chris Lattner
d044bb3527 remove some special case code that is now not necessary, NFC.
Swift SVN r31413
2015-08-22 21:40:49 +00:00
Chris Lattner
76a26ae69c make ParenExpr handling more consistent, which is basically just a cleanup. This
wraps up 22102895


Swift SVN r31411
2015-08-22 21:09:11 +00:00
Chris Lattner
2c22cdc44c Teach CSDiag how to propagate contextual type information through TupleExpr,
one more step towards 20491794.



Swift SVN r31410
2015-08-22 20:35:41 +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
186fa8e9d6 Eliminate the TCC_AllowUnresolved flag by having typeCheckChildIndependently
automatically pass down TypeCheckExprFlags::AllowUnresolvedTypeVariables 
IFF we have no contextual type.  This gives us UnresolvedTypes in more cases,
which improves diagnostics in various situations, and also simplifies
CSDiag.  The change to misc_diagnostics.swift is a particularly nice progression.



Swift SVN r31406
2015-08-22 05:41:28 +00:00
Chris Lattner
b6de061dd6 Rework assignment diagnostics to be built in terms of contextual types,
where we type check the destination first, then apply its type to the source.

This allows us to get diagnostics for assignments that are as good as PBD
initializers and other cases.


Swift SVN r31404
2015-08-22 05:16:07 +00:00
Chris Lattner
b660cb3c8e Enhance the general parameter list matching logic in CSDiags to know about
argument list mismatches, and diagnose them with a very specific error when
they occur in member lookups.  This fixes
<rdar://problem/22356434> QoI: Missing diagnostic for invalid arguments passed to enum case constructor

where before we'd produce:

ee.swift:5:16: error: type of expression is ambiguous without more context
let list: E = .C(wrongLabel: 0)
              ~^~~~~~~~~~~~~~~~

now we produce:

ee.swift:1:17: error: incorrect argument label in call (have 'wrongLabel:', expected 'label:')
let list: E = .C(wrongLabel: 0)
                ^~~~~~~~~~~
                 label

I think that unresolved member exprs now get good diagnostics in all cases that they have
a contextual type, but of course there are lots more cases where we're not getting a 
contextual type.



Swift SVN r31402
2015-08-22 04:46:49 +00:00
Chris Lattner
cd7d638a0b refactor "evaluateCloseness" and related machinery to be defined in terms of
an array of TupleTypeElt's instead of an array of Type's, exposing information
about varargs and parameter labels to it.  NFC.


Swift SVN r31385
2015-08-21 18:19:23 +00:00
Chris Lattner
8f0d0d325d build out the infrastructure for diagnosing calls that have contextual type
information and use this to improve the UnresolvedMemberExpr errors.

The notable problem remaining is that we don't handle problems involving
argument labels.


Swift SVN r31378
2015-08-21 05:24:00 +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 Lattner
61de97b063 Switch string literal type checking to work more like other literals: if
it already has a type, don't re-type-check the literal.  This improves a
diagnostic involved with 20491794.


Swift SVN r31330
2015-08-19 04:58:36 +00:00
Chris Lattner
458534f9d7 fix <rdar://problem/22320758> QoI: collection literals don't handle substitutions well in csdiag
Now we can propagate contextual types through collection literals even when they are generic, producing
specific diagnostics for elements within them.


Swift SVN r31327
2015-08-19 04:24:27 +00:00
Chris Lattner
7dcb4e7b71 remove some redundant code Slava noticed
Swift SVN r31324
2015-08-19 00:50:56 +00:00
Chris Lattner
f72b2136c4 simplify and rebrand diag::invalid_relation now that it can only produce
two messages.


Swift SVN r31320
2015-08-18 23:58:49 +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
b9e97ed600 Fix the constraint ranking predicate to produce a correct partial ordering w.r.t to
favored bit.  Also, ignore disjunction constraints now that we drop them on the floor
here.

Also, move the in_cast_expr_types blob down further, to make sure we emit the note for
a diagnostic after the error.

NFC on the testsuite.


Swift SVN r31312
2015-08-18 22:02:47 +00:00
Chris Lattner
4f15bce57a refactor a bit of the code filtering UnresolvedTypes, NFC.
Swift SVN r31306
2015-08-18 19:21:59 +00:00
Chris Lattner
880f06aedf Enhance FailureDiagnosis::diagnoseGeneralOverloadFailure to more correctly
handle try/try? exprs between the overload set and the CallExpr (previously
we'd blow by the CallExpr walking the parent map) and make some other 
diagnostic generation stuff more careful about handling type variables.

NFC since type vars aren't getting into here yet.



Swift SVN r31301
2015-08-18 18:21:58 +00:00
Chris Lattner
045c4533c1 remove a now-redundant flag, which is implicit in typeCheckArbitrarySubExprIndependently.
Swift SVN r31300
2015-08-18 18:02:29 +00:00
Chris Lattner
505ff58cdb Make conversion diagnostics between as? and as! expression consistent by allowing
diagnoseGeneralConversionFailure() to handle them (instead of it handling as? but
special code handling as!).

As part of this, enhance things so we get error messages about both the problem, 
and the overall type involved (when they're different) e.g.:

  if let s = setD as? Set<BridgedToObjC> { }

error: 'ObjC' is not a subtype of 'DerivesObjC'
note: in cast from type 'Set<DerivesObjC>' to 'Set<BridgedToObjC>'

This also finally fixes the case in test/Generics/existential_restrictions.swift



Swift SVN r31299
2015-08-18 18:00:37 +00:00
Chris Lattner
fa6dcfee50 rework how diagnoseConstraintFailure sorts through constraints that it
is considering, making it a bit stronger at handling disjunction constraints
as well as fixing it to stop sending non-conversion constraints down into
diagnoseGeneralConversionFailure.



Swift SVN r31294
2015-08-18 17:12:46 +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 Lattner
c34fb19a92 fix a regression on Swift.compiler_crashers_fixed.26134-swift-constraints-constraintsystem-solverstate-solverstate.swift
Swift SVN r31291
2015-08-18 05:06:26 +00:00
Chris Lattner
1ad24fdfc2 rework how 'as' casts are diagnosed, removing a special case from CSApply and
allowing these failures to hook into other diagnostic goodies (e.g. the
"did you mean to use '!' or '?'?" cases showing in the testsuite).  That said,
by itself this doesn't have a huge impact, but avoids regressions with other
pending changes.


Swift SVN r31289
2015-08-18 04:35:41 +00:00
Chris Willmore
8c57b7cde5 Improve diagnosis of implicit match expressions
When diagnosing failure to typecheck a binary '~=' expression that was
synthesized when typechecking an expression pattern, offer a message
that describes the failure more helpfully.

<rdar://problem/21995744> QoI: Binary operator '~=' cannot be applied to operands of type 'String' and 'String?'

Swift SVN r31286
2015-08-18 02:33:10 +00:00
Chris Lattner
cc2fad1149 remove some unnecessary code that produces unfortunate effects when other changes go in.
The fallthrough path that this enables does ambiguity checking and simplifies active 
constraints that are left into the system, before jumping into diagnoseFailureForExpr.
By jumping into diagnoseFailureForExpr directly, CSDiags can produce the wrong
diagnostic because the constraints are still on the active list.

NFC for now because other changes are needed to provoke this.



Swift SVN r31272
2015-08-17 20:56:01 +00:00
Chris Lattner
402dcbc782 rename forEachChildExpr to forEachImmediateChildExpr, since that is
what it does, and add a more general forEachChildExpr that walks the
entire expr tree.  Allow both of these to mutate the expr in question
by allowing the lambda to return a new expr.

NFC, this is needed by subsequent work.



Swift SVN r31267
2015-08-17 17:38:16 +00:00
Chris Lattner
6c3976061c If a contextual type is available when type checking collection literals, use it to
produce specific diagnostics about individual elements being incorrect instead of
complaining about the whole thing in aggregate.

This improves diagnostics immediately, but is also important to unblock future progress.



Swift SVN r31264
2015-08-17 16:41:55 +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
a7be48c0d3 Use getValueProvidingExpr instead of getSemanticsProvidingExpr in some
cases and use KnownProtocolKind::BooleanType instead of a string compare
against BooleanType, as suggested by Jordan.


Swift SVN r31214
2015-08-13 16:32:27 +00:00
Chris Lattner
7f7b78e051 Fix <rdar://problem/22263468> QoI: Not producing specific argument conversion diagnostic for tuple init
Swift SVN r31211
2015-08-13 06:05:03 +00:00
Chris Lattner
5731646716 fix <rdar://problem/21601687> QoI: Using "=" instead of "==" in if statement leads to incorrect error message
Swift SVN r31210
2015-08-13 05:43:02 +00:00
Chris Lattner
616dbb2a5e fix <rdar://problem/22255907> QoI: bad diagnostic if spurious & in argument list
Swift SVN r31206
2015-08-13 05:22:20 +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
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
5237e5f279 adjust two places to deal with the ParamDecls in closureexprs that have
no type set on them.  This is the minimal fix to address the regressions
on two validation tests, but another approach (not involving null types)
seems like it would be a good idea to investigate on mainline.


Swift SVN r31099
2015-08-08 20:26:57 +00:00
Chris Lattner
27a65e0013 Now that the ambiguity diagnostics are better, we can remove a bunch of complexity
handlng CC_ExactMatch overloads, resolving a poor diagnostic in the testsuite.



Swift SVN r31087
2015-08-07 21:14:29 +00:00
Chris Lattner
bb35cbcd38 Teach CSDiag how to resolve SubscriptMember locators, fixing
<rdar://problem/21364448> QoI: Poor error message for ambiguous subscript call

and improving several other subscript-related diagnostics.



Swift SVN r31082
2015-08-07 20:00:32 +00:00
Chris Lattner
45a88b0aca Now that the top level "diagnoseConstraintFailure" engine can handle multiple
constraints of each kind, enhance FailureDiagnosis::diagnoseGeneralConversionFailure to 
ignore conversion constraints that are either trivially resolvable (like Int conforming
to IntegerLiteralConvertible) or constraints that cannot be resolved because a type 
variable is ambiguous.

This eliminates the last (known to me at least!) source of diagnostics that end up
complaining about obviously incorrect issues.


Swift SVN r31065
2015-08-07 04:18:11 +00:00
Dave Abrahams
493ae5294f Kill some dead code
Swift SVN r31056
2015-08-06 20:04:17 +00:00