Commit Graph

664 Commits

Author SHA1 Message Date
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
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
1fe2883fe8 another NFC refactoring: CSDiags is now split into 3 really separate
pieces: Failure diagnosis (which should shrink over time), Expr diagnosis
(which should grow over time), and constraint diagnosis (which should shrink
over time but be the ultimate backstop).  This sets up the layers so that
all but the last can "return false" with impunity if they have no bright
ideas based on the context they know about.  


Swift SVN r31043
2015-08-06 04:57:10 +00:00
Chris Lattner
daa24e507d now that the computation of conversion constraints is detangled, we can localize
the FailureDiagnosis::conversionConstraint state to be a local variable in
diagnoseGeneralConversionFailure(), unblocking other progress. NFC.



Swift SVN r31042
2015-08-06 04:40:17 +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
Jordan Rose
2801d47e59 Add Parse and Sema support for 'try?'.
rdar://problem/21692467

Swift SVN r31030
2015-08-05 22:17:25 +00:00
Chris Lattner
b0410ffc3b Remove dead code left around in previous commit.
Swift SVN r31029
2015-08-05 21:51:14 +00:00
Chris Lattner
7bfe2eb07d Pull the diagnostics for noescape and throws function type violations onto the
expr infrastructure, resolving rdar://22158167.


Swift SVN r31028
2015-08-05 21:45:10 +00:00
Chris Lattner
4a5be366c2 add a new ContinueAfterFailures default argument to ConstraintSystem::simplify,
and use it in the diagnostics path (only!) to revisit active constraints that
are left in the system after a failure is found.  This improves a number of 
otherwise sad diagnostics in the testsuite and resolves rdar://22083115.

The one QoI regression (in throwing_functions.swift) is now tracked by 22158167.



Swift SVN r31027
2015-08-05 20:57:39 +00:00
Chris Lattner
df60eccbf5 unbreak compiler_crashers_fixed/26558-swift-constraints-constraintsystem-gettypeofmemberreference.swift,
I'll reduce a testcase later.


Swift SVN r31020
2015-08-05 00:57:19 +00:00
Chris Lattner
8ef04bd42e Make callee analysis of UnresolvedDotExpr more specific by looking for candidates
have have been directly resolved.  This has very little impact on the testsuite
right now, but unblocks other progress.



Swift SVN r31019
2015-08-05 00:24:32 +00:00
Chris Lattner
33d44ea915 Rework diagnoseGeneralOverloadFailure a bit, making it more self contained
and to handle disjunction constraints by printing an overload set.


Swift SVN r31018
2015-08-04 23:35:08 +00:00