Commit Graph

183 Commits

Author SHA1 Message Date
Max Moiseev
d610fa0d1c Merge remote-tracking branch 'origin' into swift-3-api-guidelines 2015-12-10 10:29:52 -08:00
Maxim Moiseev
844b81c46b SequenceType => Sequence 2015-12-09 17:16:56 -08:00
Dmitri Gribenko
df17ddbc9b init(count: Int, repeatedValue: Element) => init(repeating:count:)
Affected types: _ArrayType, Array, ArraySlice, ContiguousArray, Repeat,
String (initializers from Character and UnicodeScalar)
2015-12-09 17:14:37 -08:00
Maxim Moiseev
7e2466c14e CollectionType => Collection 2015-12-09 17:12:48 -08:00
Dmitri Gribenko
2cf172160c Rename SequenceType.Generator associated type to SequenceType.Iterator 2015-12-09 17:11:05 -08:00
Chris Lattner
5dded3f352 Fix <rdar://problem/23718816> QoI: "Extra argument" error when accidentally currying a method
It is a somewhat common case where folks are accidentally referring to an
instance member with the Type as the base.  This forms a curried member,
which then produced head scratching error messages downstream.

Now that the prep work has gone in, the first part of this is now
straight-forward to fix: simply check for this case and diagnose it
with a custom error, which makes it more clear what the mistake was.

The other half of this problem (tracked by 22108559) affects cases where
the method you're calling takes a single argument.  This isn't fixed
yet, but I'm adding a testcase for it anyway.
2015-12-08 23:01:44 -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
96a1e96dea Improve printing of "too few" or "too many" arguments in generic or
overloaded argument list mismatches.  We printed them in simple cases
due to "Failure" detecting them in trivial situations.  Instead of
doing that, let CSDiags do it, which allows us to pick things out of
overload sets and handle the more complex cases well.

This is a progression across the board except for a couple of cases
where we now produce "cannot convert value of type 'whatever' to
expected argument type '(arglist)'", this is a known issue that I'll
fix in a subsequent commit.
2015-12-07 23:09:08 -08:00
Max Moiseev
d0e1cb3a0e Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2015-12-07 16:43:20 -08:00
Dmitri Gribenko
174d475833 stdlib: Remove unavavailable APIs that were left as migration aids
These APIs are from the Swift 1.2 => Swift 2.0 transition, and are not
relevant anymore.

Removing them reduces the surface area of the library that needs to be
reviewed.
2015-12-02 01:19:50 -08:00
Chris Lattner
6636bafadf Fix <rdar://problem/23272739> Poor diagnostic due to contextual constraint
Previously we erroneously complained:
  error: cannot invoke 'contains' with an argument list of type '(String)'
now we correctly complain:
  error: unexpected non-void return value in void function

This enhances CSDiags to use "getTypeOfMember" when analyzing method
candidates that are applied to a known base type.  Using it allows us to
substitute information about the base, resolving archetypes that exist in
subsequent argument positions.  In the testcase, this means that we use
information about Set<String> to know that the argument to "contains" is a
String.

This allows us to generate much better diagnostics in some cases, and works
around some limitations in the existing stuff for handling unresolved
archetypes.  One unfortunate change is the notes in Misc/misc_diagnostics.swift.
Because we don't track argument lists very well, we are flattening an argument
list that is actually ((Int,Int)) into (Int, Int) so we get a bogus looking
diagnostic.  This was possible before this patch though, it is just one
more case that triggers the issue.
2015-12-01 21:22:24 -08:00
Chris Lattner
0958f67c19 Fix <rdar://problem/22058555> QoI: unhelpful type error "cannot invoke 'withCString' with an argument list of type '((_) -> _)'"
The original issue has long since been fixed, but we were now producing:
error: cannot subscript a value of type 'UnsafePointer<Int8>'

which is pretty obviously wrong.  The problem is that when ranking subscript
decl candidates, CSDiags was using TC.isConvertibleTo to evaluate whether the
actual base type is compatible with the base type of a subscript decl.  This
was failing when the base was generic, because the logic isn't opening
archetypes.  Instead of incorrectly deciding that they are incompatible, just
decide we don't know if an archetype is present.  This allows us to generate
good errors in situation like this.
2015-11-29 22:31:08 -08:00
Chris Lattner
1c592a750f The fix for 23433271 also fixed the crash on 22058555, add its testcase so
we don't regress on it in the future.  Yes, the diagnostic is lousy, I'll
repurpose the second radar to improve it.
2015-11-29 22:15:27 -08:00
Chris Lattner
f4fc002fe2 Fix <rdar://problem/23433271> Swift compiler segfault in failure diagnosis
A crash in CSDiag that happened when we were unconditionally looking at the
getter of a subscript.  This failed on UnsafeMutablePointer because it only
has addressors, not getter/setters.
2015-11-29 22:09:58 -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
Chris Lattner
4b8ccacf63 Fix <rdar://problem/22243469> QoI: Poor error message with throws, default arguments, & overloads
and probably others.

When we're type-checking a failed ApplyExpr that has an overload set that
prevents getting a specific type to feed into the initial typechecking of
the argument list, ranking can often narrow down the list of candidates
further, to the point where there is only one candidate left or where all
candidates agree that one argument is wrong.

In this case, re-type-check the subexpr with the expected type.  In the case of
rdar://problem/22243469 we now produce:

t.swift:6:11: error: invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> Void'
  process {
          ^

instead of:

t.swift:6:3: error: cannot invoke 'process' with an argument list of type '(() throws -> ())'
  process {
  ^
t.swift:6:3: note: overloads for 'process' exist with these partially matching parameter lists: (UInt, fn: () -> Void)
  process {
  ^

Which is a heck of a lot less specific.  Similarly, in the testcase from rdar://23550816, instead
of producing:

  takeTwoFuncsWithDefaults { $0 + 1 }
error: cannot invoke 'takeTwoFuncsWithDefaults' with an argument list of type '((Int -> Int)?)'
note: expected an argument list of type '(f1: (Int -> Int)?, f2: (String -> String)?)'

we now produce:
error: cannot convert value of type '_ -> Int' to expected argument type '(String -> String)?'

which is a lot closer to what we want to complain about.
2015-11-17 20:27:47 -08:00
Chris Lattner
5a23bfde33 add a testcase for a diagnostic not tested elsewhere, NFC. 2015-11-16 22:22:07 -08:00
Chris Lattner
178e6f62d6 6370e40c65 fixed rdar://21890157 as well, add a testcase from it. 2015-11-16 21:35:51 -08:00
Chris Lattner
6370e40c65 Fix <rdar://problem/23560128> QoI: trying to mutate an optional dictionary result produces bogus diagnostic
This case attempts to diagnose assignment into an invalid lvalue which only had
a computable type due to a fixit that the constraint solver was assuming.  In this
situation, don't diagnose the invalid lvalue at all, diagnose the required fix.
2015-11-16 18:01:34 -08:00
Chris Lattner
2f04cb13f1 Fix <rdar://problem/23550816> QoI: Poor diagnostic in argument list of "print" (varargs related)
When passing a contextual type to a call, if we have a scalar element
initializing a varargs parameter list, we need to use the varargs element type
contextually.  Fixing this improves some confusing diagnostics.
2015-11-15 15:13:21 -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
30af42fda9 Add warning that curried function decl syntax is going away.
<rdar://problem/23111018>
2015-11-02 15:45:11 -08:00
Chris Lattner
e47065b68c Dial back the specificity of these diagnostics a bit to not mention Float80 to avoid breaking test on targets that lack it.
Swift SVN r31892
2015-09-11 06:15:35 +00:00
Chris Lattner
96eaa14f3b Fix an arbitrary restriction where we would not propagate the result type of
call expression onto a callee when it was a binary expression.  Doing this
requires improving the diagnostics for when the contextual result type is
incompatible with all candidates, but that is general goodness all around.

This fixes:
<rdar://problem/22333090> QoI: Propagate contextual information in a call to operands

and improves a number of diagnostics where the problem is that an operator
is used in a context that expects a type that it cannot produce.



Swift SVN r31891
2015-09-11 06:12:05 +00:00
Chris Lattner
72c5c3e4fe Two changes:
- Enhance the branch new argument label overload diagnostic to just
   print the argument labels that are the problem, instead of printing
   the types inferred at the argument context.  This can lead to confusion
   particularly when an argument label is missing.  For example before:

error: argument labels '(Int)' do not match any available overloads
note: overloads for 'TestOverloadSets.init' exist with these partially matching parameter lists: (a: Z0), (value: Int), (value: Double)

after:

error: argument labels '(_:)' do not match any available overloads
note: overloads for 'TestOverloadSets.init' exist with these partially matching parameter lists: (a: Z0), (value: Int), (value: Double)


Second, fix <rdar://problem/22451001> QoI: incorrect diagnostic when argument to print has the wrong type
by specifically diagnosing the problem when you pass in an argument to a nullary function.  Before:

error: cannot convert value of type 'Int' to expected argument type '()'

after:
error: argument passed to call that takes no arguments
print(r22451001(5))
                ^




Swift SVN r31795
2015-09-09 00:26:37 +00:00
Chris Lattner
adccd0a1cd Fix <rdar://problem/19962010> QoI: argument label mismatches produce not-great diagnostic
by wiring visitApplyExpr up to diagnose argument label mismatches with the existing
diagnoseArgumentLabelError mechanics.




Swift SVN r31791
2015-09-08 23:57:27 +00:00
Chris Lattner
c9b6ece16c add a somewhat gross hack to CSDiag's to tell it to not retypecheck
OverloadedDeclRefExpr or OverloadedMemberRefExpr when there is no
contextual type information available.  The problem is that CSRanking
will take a look at the various solutions formed by picking each member
of the set, and will arbitrarily rank them against each other based on
how specific the candidates are.  The problem with this is that the
constraints on the candidates are being resolved by UnresolvedType, which
means that we end up accidentally pruning the overload set too early.

This can lead to incorrect diagnostics that *should* have been ambiguity
diagnostics, such as the example in TypeCoercion/overload_noncall.swift.
It also is causing me other grief as I'm trying to make the call analysis
diagnostics more specific and the lack of the proper candidates is 
triggering badness.

The actual change to the testsuite here is minor, but not all good.  It will
be re-won by later changes.



Swift SVN r31744
2015-09-07 22:41:10 +00:00
Chris Lattner
9ab54bfaf3 r31728 fixed 17557899 (and several other) radars as well, add a testcase so we
don't regress on it in the future.



Swift SVN r31730
2015-09-06 20:59:23 +00:00
Chris Lattner
bab9b62c87 reimplement the "function produces expected type 'XYZ'; did you mean to call it with '()'?"
fixit hint in CSDiags instead of being a FixKind.  This resolves a number of issues with
it, particularly that it didn't actually check to see if the function in question takes
a () argument or not.  

This fixes:
<rdar://problem/21692808> QoI: Incorrect 'add ()' fixit with trailing closure

among other issues.



Swift SVN r31728
2015-09-06 20:41:21 +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
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
1034a94929 reword a diagnostic to be shorter and hopefully more useful as suggested by DaveA.
Swift SVN r31638
2015-09-02 21:13:48 +00:00
Chris Lattner
16a51639ef Fix <rdar://problem/22519983> QoI: Weird error when failing to infer archetype
Introduce a new "OpenedGeneric" locator for when openGeneric opens a generic
decl into a plethora of constraints, and use this in CSDiags to distinguish 
whether a constraint refers to an Expr as a whole or an "aspect" of the constraint.

Use that information in FailureDiagnosis::diagnoseGeneralConversionFailure
to know whether (as a fallback) we can correctly re-typecheck an entire expr 
to obtain a missing type.  If we are talking about an aspect of the expr, then
this clearly won't work.

The upshot of this is that where we previously compiled the testcase in 22519983
to:

y.swift:31:9: error: type '(inout _) -> Bool' does not conform to protocol 'RawRepresentable'
let a = safeAssign
        ^

we now produce the somewhat more useful:
y.swift:31:9: error: argument for generic parameter 'T' could not be inferred
let a = safeAssign
        ^
y.swift:27:6: note: in call to function 'safeAssign'
func safeAssign<T: RawRepresentable>(inout lhs: T) -> Bool {
     ^



Swift SVN r31620
2015-09-02 05:15:22 +00:00
Chris Lattner
bdb2edb198 With yaks properly groomed and infrastructure in place, we can now improve
diagnostics around invalid references to unavailable declarations, resolving
<rdar://problem/22491394> References to unavailable decls sometimes diagnosed as ambiguous

and a complex case exposed working through rdar://21928143.



Swift SVN r31587
2015-08-30 22:45: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
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
725cd9745a add some more sub-casses of the rdar://20491794 testcase.
Swift SVN r31479
2015-08-26 04:24:26 +00:00
Chris Lattner
ec600e245d fix <rdar://problem/22322266> Swift let late initialization in top level control flow statements
Swift SVN r31464
2015-08-26 00:39:20 +00:00
Chris Lattner
712d3447ce improve some diagnostics around inout expr.
Swift SVN r31415
2015-08-22 22:29:14 +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
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
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
e9b49f1b1f add test static factory method diagnostic, NFC.
Swift SVN r31361
2015-08-20 16:34:11 +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
0bdf7d1018 two pretty unhelpful diagnostics relating to sub-problems of rdar://20491794.
Swift SVN r31329
2015-08-19 04:43:12 +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