Commit Graph

510 Commits

Author SHA1 Message Date
Chris Lattner
0e3143c244 fix <rdar://problem/21447318> dot'ing through a partially applied member produces poor diagnostic
Swift SVN r30017
2015-07-09 05:54:29 +00:00
Chris Lattner
7e5282ce6c reapply part of r29978, wherein we simplify CleanupIllFormedExpression,
and stop it from nuking types on decls inside of closures, which needlessly
pessimizes some IDE tests.



Swift SVN r29979
2015-07-08 18:18:46 +00:00
Chris Lattner
31facf7204 revert r29932, which is triggering a problem on the ASAN bot that I need to investigate.
Swift SVN r29978
2015-07-08 17:14:43 +00:00
Chris Lattner
acecd187a3 fix <rdar://problem/21553065> Spurious diagnostic: '_' can only appear in a pattern or on the left side of an assignment
... by reimplementing the DiscardAssignmentExpr checker in MiscDiagnostics 
instead of being in CSApply (which gets run on partial expression ASTs).

Also, when type checking and salvaging an expression fails, do not generate 
structural diagnostics in addition to the type checker diagnostics, they are 
just noise.



Swift SVN r29937
2015-07-07 05:52:17 +00:00
Chris Lattner
e8877e9555 Several changes:
- Fix a diagnostic to not include redundant ''s around a type name.
 - Rework CleanupIllFormedExpression to be simpler and to not 
   unconditionally destroy data when it doesn't.  This makes a code 
   completion test a bit more precise.
 - Completely revamp getTypeOfIndependentSubExpression, to return the 
   subexpression produced by type checking instead of just a type.  This
   is important for cases when type checking changes the root of the AST
   (e.g. resolving an unresolved_dot_expr) and allows us to eliminate
   grungy and unsafe recovery code that was in place to work around this.

The last point makes the examples in Constraints/lvalues.swift better (giving
a somewhat generic error instead of an specific-but-incorrect error that 'z'
is immutable), but more importantly, it fixes a class of crashers like
<rdar://problem/21369926> Malformed Swift Enums crash playground service

where we'd end up with a LiteralExpr typed as Int instead of a Builtin integer
type of some sort.



Swift SVN r29932
2015-07-07 04:35:31 +00:00
Chris Lattner
183c279889 change visitAssignExpr to only get retypecheck one side of its subexpr at a time,
and change it to get the source before the destination.  This is the right thing to
do and also conveniently works around a bogus:
  '_' can only appear in a pattern or on the left side of an assignment

diagnostic.

This needs to be fixed in a more systematic way to cover other cases, but this change
is the right thing to do independently of that.



Swift SVN r29929
2015-07-07 01:05:15 +00:00
Chris Lattner
47a1a2cf38 fix <rdar://problem/20905802> Swift using incorrect diagnostic sometimes on String().asdf
when we're diagnosing a value member constraint but have resolved the base, then the
member must not exist.  Diagnose this with a specific message.



Swift SVN r29908
2015-07-03 05:03:46 +00:00
Chris Lattner
612a41278a Teach TypeMember constraints to suggest force unwrapping optionals, the same way
value member constraints do.

This fixes:
<rdar://problem/21662365> QoI: diagnostic for for-each over an optional sequence isn't great

before we'd produce:
t.swift:3:10: error: '[Int]?' does not have a member named 'Generator'
for x in array {
         ^

now we produce:

t.swift:3:10: error: value of optional type '[Int]?' not unwrapped; did you mean to use '!' or '?'?
for x in array {
         ^



Swift SVN r29902
2015-07-02 23:25:08 +00:00
Chris Lattner
0f80934c48 Fix <rdar://problem/19774755> Incorrect diagnostic for unwrapping non-optional bridged types
Swift SVN r29872
2015-07-02 00:13:59 +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
762cb3fa62 start loosening up getTypeOfIndependentSubExpression() to walk into more kinds of
expressions.  Broadening from callexpr to apply expr (picking up operators) improves
several diagnostics in the testsuite, and is important to avoid regressions from an 
upcoming patch.


Swift SVN r29821
2015-06-30 23:25:47 +00:00
Chris Lattner
79563ff700 Several conflated changes:
- Enhance subscript diagnostics to chase into the constraint system to find
   overload candidates that failed to match so we can rank and diagnose 
   subscript ambiguities using the same mechanics we have for operators.
 - Implement a copy of suggestPotentialOverloads based on the new mechanics
   for overload set resolution.  This allows us to diagnose these in a more
   detailed way, but for now we're keeping it as similar to the old system as
   possible.  The old version to be removed once the last client moves off it.
 - Add a bunch of testcases to decl/subscript/subscripting.swift where we are
   doing unfortunate things still.



Swift SVN r29810
2015-06-30 21:50:36 +00:00
Chris Lattner
1a3d3b0bae teach resolveImmutableBase about BindOptionalExpr, making some mutability diagnostics more precise.
Swift SVN r29799
2015-06-30 03:46:29 +00:00
Chris Lattner
3c7cb837e8 Now that diagnoseGeneralOverloadFailure() has a correct notion of the
ApplyExpr involved in the overload failure, wire it up to some of the
mechanics we have for more specific situations, including diagnosing
the last (known to me) mutation issues that weren't being specifically
diagnosed, as well as printing the candidate set in these cases.



Swift SVN r29797
2015-06-29 23:52:57 +00:00
Chris Lattner
d5aaf13e04 fix <rdar://problem/21459429> QoI: Poor compilation error calling assert
This teaches overload constraint diagnosis to look at the resolved anchor 
expression that fails (instead of assuming that it is the expr itself) and
walks up the AST to find the applyexpr in question.  This allows us to give
much more specific diagnostics for overload resolution failures, and to give
much more specific location information.

Where before my recent patches we used to produce:

t.swift:2:3: error: cannot invoke 'assert' with an argument list of type '(Bool, String)'
  assert(a != nil, "ASSERT COMPILATION ERROR")
  ^
t.swift:2:9: note: expected an argument list of type '(@autoclosure () -> Bool, @autoclosure () -> String, file: StaticString, line: UWord)'
  assert(a != nil, "ASSERT COMPILATION ERROR")
        ^

with this and the other recent patches, we now produce:

t.swift:2:12: error: cannot invoke '!=' with an argument list of type '(Int, nil)'
  assert(a != nil, "ASSERT COMPILATION ERROR")
         ~~^~~~~~




Swift SVN r29792
2015-06-29 22:00:09 +00:00
Chris Lattner
be906493ac Teach CSDiag how to recursively walk into an arbitrary expression, to produce
its diagnostics in post-order.  Notably, this picks up support for if-expr, which
gives up much better diagnostics in ternary operators.  For example, rdar://17224804
used to produce:

error: could not find an overload for '<=' that accepts the supplied arguments
var monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
                  ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

now we produce:

 error: binary operator '+' cannot be applied to operands of type 'String' and 'Int'
var monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
                                        ~~~ ^ ~~~~~~~~~~~
 note: overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (UnsafeMutablePointer<Memory>, Int), (UnsafePointer<Memory>, Int)
var monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
                                        ^

which is the correct diagnostic.  While I'm at it, improve the location info for this
binary operator diagnostic to point to the right spot (the operator) and highlight
the LHS/RHS of the operator.



Swift SVN r29774
2015-06-28 17:53:36 +00:00
Chris Lattner
5b31f94d5e Enhance diagnoseGeneralConversionFailure to understand that constraints have a
path associated with them, and to dig the expression the constraint refers to out
of the locator.  Also teach simplifyLocator how to simplify closureexpr results out.

This eliminates a class of completely bogus diagnostics where the types reported
don't make any sense, resolving a class of radars like 19821875, where we now 
produce excellent diagnostics.

That said, we still pick constraints to report that are unfortunate in some cases, 
such as the example in expr/closure/closures.swift.


Swift SVN r29757
2015-06-27 04:33:10 +00:00
Chris Lattner
bff16e6d92 remove some special case code that is no longer needed, resolving the
@noescape QoI regression in the last patch.



Swift SVN r29738
2015-06-26 07:35:00 +00:00
Slava Pestov
9a65927f40 Sema: Another fix for protocol self-conformance
It looks like we were checking in the wrong place, as a result we didn't
catch stuff like

class G<T : AnyObject> {}
_ = G<P>()

This would crash later in IRGen.

Make the conformsToProtocol() check do the right thing, and remove some
other miscellaneous diagnostics in the process. Also, make the
"type 'T' does not conform to protocol 'P'" diagnostic a bit more
detailed.

Unfortunately in a few instances we lose a more descriptive diagnostic to
a general 'cannot invoke 'foo' with argument list of type 'T'' error. The
argument matching diagnostics need to be addressed anyway though.

Fixes <rdar://problem/20311619>.

Swift SVN r29737
2015-06-26 07:29:56 +00:00
Slava Pestov
8636b00cb0 Sema: Formatting fix, NFC
Swift SVN r29736
2015-06-26 07:29:54 +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
bd915d3625 two changes here:
- When diagnosing an error passing a noescape function to an escapeing function pointer,
   return 'true' to avoid follow-on bogus error messages and notes being produced.
 - Start classifying the closeness of overload matches, to allow future diagnostics to be
   improved.

Note that the second regresses the testcase in test/Constraints/generics.swift because the
decomposeArgumentType function doesn't know to look through the archetype for the protocol
present on the operator, and thus thinks the function only takes one argument.  Advice on
how to best detect this situation is appreciated.



Swift SVN r29731
2015-06-26 05:27:46 +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
553c2d8e8d refactor some common code out to a collectCalleeCandidateInfo helper, NFC.
Swift SVN r29712
2015-06-25 23:18:21 +00:00
Doug Gregor
25fc2d0073 Improve dumping of solutions to constraint systems.
Swift SVN r29707
2015-06-25 22:42:44 +00:00
Chris Lattner
fde613daae move the FailureDiagnosis class into CSDiag.cpp, since it is an
implementation detail of that file.


Swift SVN r29680
2015-06-25 18:39:26 +00:00
Chris Lattner
630a637948 move diagnoseAssignmentFailure from FailureDiagnosis to ConstraintSystem, NFC.
Swift SVN r29675
2015-06-25 17:47:00 +00:00
Chris Lattner
719996ee0f merge FailureDiagnosis into GeneralFailureDiagnosis.
Swift SVN r29672
2015-06-25 16:43:23 +00:00
Chris Lattner
dbedb88995 Switch FailureDiagnosis to use the standard CRTP ASTVisitor instead of
rolling its own thing.  NFC.


Swift SVN r29671
2015-06-25 16:40:36 +00:00
Chris Lattner
add4deef92 add special diagnostics handling for ParenExpr: without this, we would handle
them with diagnoseGeneralFailure() which would miss out on the common cases
where the subexpr of the ParenExpr is the issue.

For example, before we would produce:

t.swift:8:8: error: could not find an overload for '&' that accepts the supplied arguments
if !(x & 4.0) {}
    ~~~^~~~~~

now we produce:

t.swift:8:6: error: binary operator '&' cannot be applied to operands of type 'Int' and 'Double'
if !(x & 4.0) {}
     ^
t.swift:8:6: note: overloads for '&' exist with these partially matching parameter lists: (Int, Int)
if !(x & 4.0) {}
     ^

also, remove some special handling for lvalues and inout from overload
diagnostics, which can't matter anymore.


Swift SVN r29661
2015-06-25 06:30:27 +00:00
Chris Lattner
48551a1814 more tidying up, mark a bunch of helpers "static". NFC.
Swift SVN r29660
2015-06-25 05:16:27 +00:00
Chris Lattner
9891d1172f simplify some code by inverting nesting etc, NFC.
Swift SVN r29657
2015-06-25 04:50:03 +00:00
Chris Lattner
3e3ff0bba7 add improved diagnostics for another bad return path. CSDiag is a maze to me,
this really fixes the case in rdar://14096697.


Swift SVN r29566
2015-06-23 06:09:07 +00:00
Chris Lattner
f25e14ecc9 fix <rdar://problem/14096697> QoI: Diagnostics for trying to return values from void functions
by propagating the 'is return expr' bit more carefully in sequence folding, and by
adding another path for handling the return diagnostics better.

This probably improves a number of cases where we complain about "this argument list 
is invalid" when the call is in the context of a return.



Swift SVN r29565
2015-06-23 05:53:24 +00:00
Chris Lattner
886bc277b2 tidy up some code, NFC.
Swift SVN r29554
2015-06-22 21:57:36 +00:00
Chris Willmore
0e05470e5f Take care to preserve parentheses when opening generic types. Consider
lvalues when compiling list of partial-match overloads in diagnosis.
(This is a reapplication of commits r29462 and r29469.)

Also, fix the following tests:

    stdlib/FixedPointDiagnostics.swift.gyb
    stdlib/NumericDiagnostics.swift.gyb

<rdar://problem/17875634> can't append to array of tuples

Swift SVN r29493
2015-06-18 18:58:49 +00:00
Mark Lacey
f7ce9aab07 Revert "Take care to preserve parentheses when opening generic types. Consider lvalues when compiling list of partial-match overloads in diagnosis."
This reverts commit r29462 because it looks like it breaks the following
tests:
    Swift :: stdlib/FixedPointDiagnostics.swift.gyb
    Swift :: stdlib/NumericDiagnostics.swift.gyb

Swift SVN r29484
2015-06-18 06:51:34 +00:00
Chris Willmore
8d4f70580f Take care to preserve parentheses when opening generic types. Consider
lvalues when compiling list of partial-match overloads in diagnosis.

<rdar://problem/17875634> can't append to array of tuples

Swift SVN r29462
2015-06-17 22:59:35 +00:00
Slava Pestov
8504517e15 Sema: Fix opened existentials leaking out in diagnostics
getTypeOfIndependentSubExpression() might replace some values in the
given expression with OpaqueValueExprs, but if the type checker
decided to insert the corresponding OpenExistentialExpr at the top
level, the AST would now be in an inconsistent state, since
getTypeOfIndependentSubExpression() does not return a new expression
to the caller.

Ideally we would separate out type checking from expression rewriting
so that the latter is only performed when we know the expression
type checks, but that is a bigger project. For now, erase open
existentials after re-typechecking a sub-expression.

Fixes <rdar://problem/20598568>.

Swift SVN r29400
2015-06-16 17:25:46 +00:00
Slava Pestov
0bea0b8adc Sema: Remove an unused function parameter
NFC

Swift SVN r29381
2015-06-15 02:00:51 +00:00
Joe Groff
bebfa969bd Sema: Allow 'x.init' references on metatype expressions.
If 'x.init' appears as a member reference other than 'self.init' or 'super.init' within an initializer, treat it as a regular static member lookup for 'init' members. This allows a more explicit syntax for dynamic initializations; 'self.someMetatype()' looks too much like it's invoking a method. It also allows for partial applications of initializers using 'someMetatype.init' (though this needs some SILGen fixes, coming up next). While we're in the neighborhood, do some other correctness and QoI fixes:

- Only lookup initializers as members of metatypes, not instances, and add a fixit (instead of crashing) to insert '.dynamicType' if the initializer is found on an instance.
- Make it so that constructing a class-constrained archetype type correctly requires a 'required' or protocol initializer.
- Warn on unused initializer results. This seems to me like just the right thing to do, but is also a small guard against the fact that 'self.init' is now valid in a static method, but produces a newly-constructed value instead of delegating initialization (and evaluating to void).

Swift SVN r29344
2015-06-08 04:11:28 +00:00
Joe Groff
1479a56ab3 Sema: Move semantic constraints on super/self.init out of the parser.
Instead of forcing full application of '{super,self}.init' in the parser, and installing the RebindSelf semantic expr node early, make these constraints to Sema-time checks, and parse '<expr>.init' as a regular postfix production. This is a better separation of concerns, and also opens the door to supporting 'metatype.init()' in more general expression contexts (though that part still needs some follow-up sema work).

Swift SVN r29343
2015-06-08 04:11:16 +00:00
Slava Pestov
322f58d8b1 Sema: Tighten up existential vs generic type parameter distinction
Rename existentialConformsToSelf() to existentialTypeSupported(). This
predicate is the "protocol has no Self or associated type requirements"
check, which is a looser condition than self-conformance. This was being
tested to see if the user could refer to the protocol via an existential
type.

The new existentialConformsToSelf() now checks for protocol being @objc,
and for the absence of static methods. This is used as part of the
argument type matching logic in matchType() to determine if the
existential can be bound to a generic type parameter.

The latter condition is stricter, for two reasons:

1) We allow binding existentials to multiple type parameters all sharing
   the same generic type parameter T, so we don't want the user to be
   able to see any static methods on T.
2) There is an IRGen limitation whereby only existentials without witness
   tables can be passed in this manner.

Using the above, the representsNonTrivialGenericParameter() function
has been renamed to canBindGenericParamToExistential(). It now allows
an existential type to be bound to a generic type parameter only under
the following circumstances:

A) If the generic type parameter has no conformances, the match is allowed.

B) If the generic type parameter has at least one conformance, then all
   of the conformances on the generic type parameter must be
   existentialConformsToSelf() (condition 1 above), and all conformances
   on the existential must be @objc (condition 2 above).

Fixes <rdar://problem/18378390> and <rdar://problem/18683843>, and lays
the groundwork for fixing a few other related issues.

Swift SVN r29337
2015-06-07 10:16:21 +00:00
Joe Pamer
784fe6d601 Normalize some diagnostics by walking into tuple expressions. Doing so can
result in slightly more descriptive diagnostics in some cases. (Specifically,
for diagnostics involving binary operators.)

(rdar://problem/21080030)

Swift SVN r29020
2015-05-26 03:46:15 +00:00
Chris Lattner
cf1cb14205 recommit 28958/28959, there was a testcase that didn't get updated correctly
but it was between the two revisions.


Swift SVN r28964
2015-05-23 15:50:16 +00:00
Ted Kremenek
b4b81ff657 Revert "merge assignments to UnresolvedDotExpr onto the common path,"
Backing out to fix the build.

Swift SVN r28959
2015-05-23 07:55:07 +00:00
Ted Kremenek
381eb52207 Revert "Further consolidate mutation code, this time for DeclRefExprs. With that"
Backing out to fix th build.

Swift SVN r28958
2015-05-23 07:55:04 +00:00
Chris Lattner
291d7eaad7 Further consolidate mutation code, this time for DeclRefExprs. With that
done, the rest of the infrastructure is all common and can be simplified.  This
leaves us with a quite small and maintainable subsystem for diagnosing these
kinds of problems.

 include/swift/AST/DiagnosticsSema.def |   28 ++-----
 lib/Sema/CSDiag.cpp                   |  132 ++++++++++------------------------
 2 files changed, 48 insertions(+), 112 deletions(-)




Swift SVN r28957
2015-05-23 05:49:19 +00:00
Chris Lattner
3b64718b89 merge assignments to UnresolvedDotExpr onto the common path,
this is neutral w.r.t. diagnostics quality, but deletes a ton
of code:

 include/swift/AST/DiagnosticsSema.def |   21 ++---------
 lib/Sema/CSDiag.cpp                   |   64 ++--------------------------------
 2 files changed, 9 insertions(+), 76 deletions(-)



Swift SVN r28956
2015-05-23 05:18:59 +00:00
Chris Lattner
d5f68b478f Teach the recursive part of the diagnostics to handle the various things
that make vardecls and subscripts immutable.  This makes the indirect cases
a lot more specific ("this is a get-only property" instead of "this is 
immutable") and allows us to consolidate a bunch of code:

 2 files changed, 45 insertions(+), 119 deletions(-)




Swift SVN r28954
2015-05-23 04:53:08 +00:00