Commit Graph

425 Commits

Author SHA1 Message Date
Chris Lattner
26b51bfaee Fix rdar://19710848 QoI: Friendlier error message for "[] as Set"
This makes diagnoseGeneralConversionFailure more conservative: it
now never diagnoses a failed conversion when it involves a type that
has unresolved type in it.  These types could not be resolved, so it
is better to let ambiguity resolution handle the problem.

On "[] as Set", we would previously get:
  error: 'Set<_>' is not convertible to 'Set<Element>'
now we get:
  error: generic parameter 'Element' could not be inferred
2016-01-11 19:19:06 -08:00
Chris Lattner
b5500b8600 Generalize the conditions in which we'll accept an ambiguous solution to
a constraint system in "allowFreeTypeVariables" mode.  Previously, we
only allowed a few specific constraints, now we allow any relational and
member constraints.  The later one is a big deal because it means that we
can allow ".Foo" expressions as ambiguous solutions, which CSDiags can
handle well.

This unblocks solving 23942743 and enables some minor improvements across
the board, including diagnosing things like this better:
  Optional(.none)  // now: generic parameter 'T' could not be inferred

That said, it also just permutes some non-awesome diagnostics.
2016-01-11 17:04:46 -08:00
Chris Lattner
d9b2fe601b fix a compiler crash in error recovery that @practicalswift just reported. 2016-01-09 20:17:56 -08:00
Chris Lattner
675ca03b12 fix a compiler crasher that practicalswift recently found. 2016-01-08 23:03:18 -08:00
Chris Lattner
3f19714479 James Campbell pointed out on swift-evolution that the [:] literal syntax
for dictionaries is non-discoverable.  Fix that by having the compiler
suggest a fixit hint to rewrite [] in a dictionary context to [:].
2016-01-05 10:57:39 -08:00
Mark Lacey
3c614922e5 Revert "Rework the interface to coerceParameterListToType to make it obvious"
This reverts commit 420bedaae1 because it
appears to have unintentionally made some previously accepted code
involving casts of variadic parameters to closures no longer compile.
2016-01-04 19:24:06 -08:00
Chris Lattner
420bedaae1 Rework the interface to coerceParameterListToType to make it obvious
that it is specific to ClosureExprs.  Also, consolidate some logic
in CSDiags into the now shared coerceParameterListToType, which
makes a bit more sense and simplifies things a lot.  NFC.

There are still unanswered questions.  It isn't clear to me why
we support API names on closures, when we don't implement proper
semantic analysis for them. This seems like an accidentally supported
feature that should be removed.
2016-01-03 19:24:25 -08:00
Chris Lattner
3c1965ef4c Merge the AST walker for "ExprTypeSaver" and "TypeNullifier" into a single
AST walk that saves the types, then nukes them.  This saves a pass over the
AST and makes sure the two walks stay in sync.  NFC.
2016-01-03 16:18:50 -08:00
Chris Lattner
6afe77d597 Eliminate the Parameter type completely - now ParameterList is just
an overblown array of ParamDecl*'s that also keeps track of parenlocs
and has helper methods.
2016-01-03 14:45:38 -08:00
Chris Lattner
2af78aede1 forward declare ASTWalker in ASTNode.h instead of including its header, NFC. 2015-12-31 21:05:13 -08:00
Chris Lattner
a30ae2bf55 Merge pull request #836 from zachpanz88/new-year
Update copyright date
2015-12-31 19:36:14 -08:00
Chris Lattner
7daaa22d93 Completely reimplement/redesign the AST representation of parameters.
Parameters (to methods, initializers, accessors, subscripts, etc) have always been represented
as Pattern's (of a particular sort), stemming from an early design direction that was abandoned.
Being built on top of patterns leads to patterns being overly complicated (e.g. tuple patterns
have to have varargs and default parameters) and make working on parameter lists complicated
and error prone.  This might have been ok in 2015, but there is no way we can live like this in
2016.

Instead of using Patterns, carve out a new ParameterList and Parameter type to represent all the
parameter specific stuff.  This simplifies many things and allows a lot of simplifications.
Unfortunately, I wasn't able to do this very incrementally, so this is a huge patch.  The good
news is that it erases a ton of code, and the technical debt that went with it.  Ignoring test
suite changes, we have:
   77 files changed, 2359 insertions(+), 3221 deletions(-)

This patch also makes a bunch of wierd things dead, but I'll sweep those out in follow-on
patches.

Fixes <rdar://problem/22846558> No code completions in Foo( when Foo has error type
Fixes <rdar://problem/24026538> Slight regression in generated header, which I filed to go with 3a23d75.

Fixes an overloading bug involving default arguments and curried functions (see the diff to
Constraints/diagnostics.swift, which we now correctly accept).

Fixes cases where problems with parameters would get emitted multiple times, e.g. in the
test/Parse/subscripting.swift testcase.

The source range for ParamDecl now includes its type, which permutes some of the IDE / SourceModel tests
(for the better, I think).

Eliminates the bogus "type annotation missing in pattern" error message when a type isn't
specified for a parameter (see test/decl/func/functions.swift).

This now consistently parenthesizes argument lists in function types, which leads to many diffs in the
SILGen tests among others.

This does break the "sibling indentation" test in SourceKit/CodeFormat/indent-sibling.swift, and
I haven't been able to figure it out.  Given that this is experimental functionality anyway,
I'm just XFAILing the test for now.  i'll look at it separately from this mongo diff.
2015-12-31 19:24:46 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
practicalswift
021a186c23 Fix typos. 2015-12-28 18:30:15 +01:00
practicalswift
fd70b26033 Fix typos in comments. 2015-12-28 02:15:34 +01:00
practicalswift
fa0b339a21 Fix typos. 2015-12-26 17:51:59 +01:00
Chris Lattner
5d6c20b0b6 fix 058-swift-constraints-constraintsystem-diagnosefailureforexpr.swift
CSDiag's wasn't considering the case when an operator was invalid and had ErrorType.
2015-12-23 17:28:08 -08:00
Chris Lattner
2bfb788ef2 remove the logic for upgrading Swift 1's print that used "appendNewline"
to Swift 2 syntax that uses "terminator".  It is legacy, and removing it
allows simplifications.
2015-12-22 14:42:18 -08:00
practicalswift
36d7072013 Remove immediately adjacent repeated words ("the the", "for for", "an an", etc.). 2015-12-21 22:16:04 +01:00
Chris Lattner
eefc5df1bb Fix the major case of <rdar://problem/23641896> QoI: Strings in Swift cannot be indexed directly with integer offsets
This adds special tuned diagnostics to handle the case when someone attempts to index
a string with an integer value, which is pretty common to do for people new to Swift.o
That said, this only handles one special case, if there are other cases that are
common, I'd love to hear about them.  Subscripts are already handled with a different
approach, I'm not sure which is better, but I added a testcase for both of them.
2015-12-17 23:10:46 -08:00
Chris Willmore
49a7e5d5ae Improved diagnostic when using object literal without AppKit/UIKit
If you use a color literal without first having imported AppKit or
UIKit, the diagnostic is poor. Instead, recommend importing one of those
two modules as appropriate.
2015-12-16 18:10:17 -08:00
Dmitri Gribenko
126a018c4e Merge pull request #509 from practicalswift/fix-typos-13
Fix typos (13 of 30)
2015-12-14 01:06:53 -08:00
practicalswift
9d056438c5 Fix typo: expresion → expression 2015-12-14 00:11:46 +01:00
practicalswift
3ae7818141 Fix typo: convertable → convertible 2015-12-14 00:11:29 +01:00
Slava Pestov
2a34a62ce1 Fix some compiler_crashers that regressed recently 2015-12-11 09:51:20 -08:00
Slava Pestov
f6e4355b69 Sema: Fix monthly TypeNullifier bug
This time, the issue is that TypeNullifier skips bodies of
multi-statement closures. However, ExprRewriter will type
happily pass them on to typeCheckClosureBody(). This could
trigger assertions. Fix this by skipping type checking of
multi-statement closures when diagnosing.

There seems to be a minor QoI regression in some test cases
that already looked pretty dodgy and/or had FIXMEs. However
I think its worth fixing a crash.
2015-12-11 08:58:52 -08:00
Chris Lattner
a2d9b10b64 Fix <rdar://problem/22108559> QoI: Confusing error message when calling an instance method as a class method 2015-12-10 23:07:44 -08:00
Chris Willmore
c99c02b5a6 Transform EditorPlaceholderExpr into trap if executed in playground
mode (take 2)

Allow untyped placeholder to take arbitrary type, but default to Void.
Add _undefined<T>() function, which is like fatalError() but has
arbitrary return type. In playground mode, merely warn about outstanding
placeholders instead of erroring out, and transform placeholders into
calls to _undefined(). This way, code with outstanding placeholders will
only crash when it attempts to evaluate such placeholders.

When generating constraints for an iterated sequence of type T, emit

    T convertible to $T1
    $T1 conforms to SequenceType

instead of

    T convertible to SequenceType

This ensures that an untyped placeholder in for-each sequence position
doesn't get inferred to have type SequenceType. (The conversion is still
necessary because the sequence may have IUO type.) The new constraint
system precipitates changes in CSSimplify and CSDiag, and ends up fixing
18741539 along the way.

(NOTE: There is a small regression in diagnosis of issues like the
following:

    class C {}
    class D: C {}
    func f(a: [C]!) { for _: D in a {} }

It complains that [C]! doesn't conform to SequenceType when it should be
complaining that C is not convertible to D.)

<rdar://problem/21167372>

(Originally Swift SVN r31481)
2015-12-10 22:05:16 -08:00
Justas Brazauskas
043c518b27 Fix typos 2015-12-09 17:54:54 +02: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
Nick
953d895f23 Fixed Typo. "a" to "an". 2015-12-08 10:33:51 -05: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
15862749a3 Completely eliminate the MissingArgument/ExtraArguments Failure kinds, as they
are now unused.  Failure is down to just 4 kinds, so it is closer to going away.
NFC.
2015-12-07 23:14:19 -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
Chris Lattner
4ebb461d63 Fix: <rdar://problem/23670252> QoI: Misleading error message when assigning a value from [String : AnyObject]
This teaches SubscriptExpr diagnostics to substitute the base type of a
subscript expr through the generic argument types, resolving achetypes in
common situations to concrete types.  We previously complained:

  error: cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'

which is completely wrong, we now produce the correct error:

  error: cannot assign value of type 'AnyObject?' to type 'String?'
2015-12-07 17:28:16 -08:00
Leo Shimonaka
70e66973ee Fix spelling error in documentation / comments / method name 2015-12-05 00:28:08 -05:00
Chris Lattner
5b1f42cab9 Generalize some code to be based on SelfApplyExpr instead of its two
derived classes, NFC.
2015-12-01 21:40:35 -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
b8c8844850 Improve UncurriedCandidate::dump to include the type information for a candidate,
NFC since this is just a debugging aid.
2015-12-01 16:19:05 -08:00
Chris Lattner
c7c180f8d8 Fix <rdar://problem/23700031> QoI: Terrible diagnostic in tuple assignment
When inferring a contextual type for the input expression to an assignment,
use getRValueType() to strip lvalues from tuple types in recursive positions,
this allows us to produce a sensible diagnostic of:

error: binary operator '%' cannot be applied to two 'T' operands

instead of:

error: cannot assign value of type 'T' to type '@lvalue _'

because we don't have a weird lvalue type in the way.
2015-11-30 20:54:14 -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
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
Nadav Rotem
97a793b04f Fix a warning in CSDiag.
The function return value is bool but we were returning a nullptr.
2015-11-29 08:10:45 -08:00
Chris Lattner
714dcd5ba5 Fix <rdar://problem/23202128> QoI: Poor diagnostic with let vs. var passed to C function
This improves the error message when attempting an array to UnsafeMutablePointer
conversion but where the element type of the array is incorrect or where the array itself
is immutable.

As a bonus fix, this dramatically improves the diagnostic when you pass "&array" to
a function that takes an UnsafePointer.  We decided to not require & in this case, so
we can just provide a nice fixit to rip it off when this common error happens.
2015-11-28 22:13:49 -08:00
Chris Lattner
568ed0420e fix <rdar://problem/23271868> QoI: Non-descriptive error message in Swift when passing immutable unsafe pointer instead of mutable
Making diagnostics more specific when using & in a call argument as part of an
Unsafe*Pointer conversion.
2015-11-28 21:38:56 -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
6bbcdd0322 Start keeping track of the argument that failed when filtering a candidate
produces a list that differs in a consistent argument.  NFC since this isn't
used yet.
2015-11-16 23:09:24 -08:00
Chris Lattner
3cbd67dbb3 Refactor handling of 'hasTrailingClosure' in CalleeCandidateInfo. This is a
statically determinable property of a given call site, so pass it into the
ctor for CalleeCandidateInfo instead of computing it when filtering.  NFC.
2015-11-16 22:22:08 -08:00
Ben Langmuir
50cc79c7f2 Fix crash code-completing after 1 + [0]
A literal in a sub-expression of the right-most expression in a sequence
could accidentally still have an error-type when going through CSApply.

rdar://problem/23488528
2015-11-16 11:35:13 -08:00