Commit Graph

1092 Commits

Author SHA1 Message Date
Jordan Rose
6bc61fe8ce Fix a fix-it to cast to the destination type, not the source type.
Copy/paste issue, most likely.
2016-09-21 16:40:40 -07:00
Slava Pestov
ced715cb46 Sema: Fix crash when attempting to diagnose a contextual conversion of an ambiguous expression
The fixits call back into the type checker via typeCheckCheckedCast(),
which sets up a new constraint system. As a result we would hit
assertions by introducing type variables from a previous "generation".

It seems that if we bail out of this code path altogether, we get a
better diagnostic -- in the provided test, it complains about an
ambiguous member to '.value', rather than not being able to convert
_? to V?.

Fixes <https://bugs.swift.org/browse/SR-2592>.
2016-09-19 17:53:05 -07:00
Jordan Rose
e6d6e0e92f Offer fix-its to disambiguate based on a trailing closure's label.
(by making it a normal argument with a label and not a trailing
closure)

Diagnostic part of rdar://problem/25607552. A later commit will keep
us from getting in this situation quite so much when default arguments
are involved.
2016-09-15 11:05:02 -07:00
Jordan Rose
a14e3291ce Say "ambiguous use of 'foo'" instead of "'foo(bar:)'".
...unless the argument labels are the same for every possible
overload.

Only affects diagnostics.
2016-09-15 10:38:44 -07:00
Jacob Bandes-Storch
bc3a7d1241 [QoI] improve diagnostics for contextual members with argument mismatch 2016-08-28 15:59:41 -07:00
Andrew Trick
580372b98b [diagnostics] Emit a a note for optional conversion of raw pointers. (#4442)
In CSDiag.cpp I previously added a helper, isCastToTypedPointer(), to emit an
diagnostics explaining how to migrate UnsafeRawPointer conversion. This commit
broadens that diagnostic to handle Optional<UnsafeRawPointer>.

rdar:27894255 [3.0 migration] Emit an additional note for conversion
2016-08-20 21:11:50 -07:00
Slava Pestov
8a67d71de0 Sema: Look through TryExpr in FailureDiagnosis
This fixes a case where we would produce the generic 'ambiguous without
more context' error when a 'try' was present.

Fixes <rdar://problem/27891805>.
2016-08-19 16:01:38 -07:00
Xi Ge
52273554f4 [FixCode] Add diagnosis/fixit to help users deal with BooleanType's removal (#4392)
* [FixCode] Add diagnosis/fixit to help users deal with BooleanType's removal.

Due to migration reasons, types used to conform to BooleanType, which
must contain a member var 'boolValue', now does not convert to Bool. This patch
adds a specific diagnosis/fixit to explicitly invoke 'boolValue' to please the
context types.

* Address Jordan's code review comments.
2016-08-19 08:29:42 -07:00
Xi Ge
2ebab526bf [FixCode] Apply coercion fixits on return statement and initialization as well. rdar://27891589 rdar://27891426
We saw manual migration is necessary on these cases to add 'as Type'. This patch starts
to issue compiler fixits on return statement and initialization just like in other type-mismatch
cases.
2016-08-18 13:21:44 -07:00
Doug Gregor
65d8f03448 Merge pull request #4329 from rjmccall/collection-upcast-oves
Fix this to not die on the OVEs from collection upcasts.
2016-08-17 10:40:56 -07:00
John McCall
e360d7f2b0 Fix the open-existential remover to not die on the OVEs in collection upcasts. 2016-08-16 22:17:02 -07:00
Xi Ge
b620d3ebfe [FixCode] Apply coercion fixit for assignment types' mismatch too. rdar://27681428 2016-08-16 14:28:18 -07:00
Paul Meng
e172383e2f [Sema] Implement SE-0110
This commit built upon the work of Pull Request 3895. Apart from the
work to make the following work

```swift
let f: (Int, Int) -> Void = { x in  } // this is now an error
```

This patch also implement the part 2 mentioned in the #3895

```swift
let g: ((Int, Int)) -> Void = { y in  } // y should have type (Int, Int)
```
2016-08-14 12:02:27 +08:00
Michael Ilseman
97e4e2abe5 [noescape-by-default] Improve diagnostics.
This makes the special case diagnostics for detecting a missing
@escaping read through Optional and IUOs.
2016-08-12 17:06:12 -07:00
Mark Lacey
96f89384c4 Merge pull request #4245 from rudkx/fix-27449208
Special-case error for indexing into a nil literal.
2016-08-11 16:09:09 -07:00
Jordan Rose
148e25dc03 Provide the '@escaping' fix-it for any call-site escaping mismatch. (#4181)
...not just those where the functions match otherwise. This allows the
fix-it to be provided in cases where the closure being passed is a
subtype, or when there are generics involved. Yes, the '@escaping'
might not be the only issue, but it will need to get fixed, and
probably independently of anything else that's going on (except
perhaps calling the wrong function).

rdar://problem/27729540
2016-08-11 14:32:36 -07:00
Mark Lacey
753642e005 Special-case error for indexing into a nil literal.
We were previously crashing at some later point in diagnostic emission.

Resolves rdar://problem/27449208.
2016-08-11 14:32:21 -07:00
Slava Pestov
a485e525c5 Sema: Allow protocol typealiases to be accessed from expression context, as long as they don't depend on 'Self'
Suppose you have this protocol:

protocol P {
  typealias A = Int
  typealias B = Self
}

Clearly, 'P.B' does not make sense, because then the type parameter
would "leak out" of the existential container. However, 'P.A' is totally
fine, it just means 'Int'.

Previously, we would allow 'P.A' in type context, and diagnose on 'P.B'.

However, due to an oversight, neither one was allowed in expression
context, so for example you could not write 'P.A.self', even though
that should just mean 'Int.self'.

Fix this by generalizing performMemberLookup(), and fix up some
diagnostics to be more specific when something is wrong -- we want
to avoid talking about typealiases as 'static members', since that
doesn't really make much sense.

Fixes <https://bugs.swift.org/browse/SR-2314>.
2016-08-10 23:23:24 -07:00
Ted Kremenek
1d36ed9ed3 Merge pull request #4116 from rudkx/fix-27639935
Make fix for dynamic lookup on Any work for more general expressions.
2016-08-08 17:01:04 -07:00
Ted Kremenek
92f0d9ea30 Merge pull request #4114 from atrick/rawptr-diag
Improve diagnostics on invalid UnsafePointer conversion.
2016-08-08 14:23:49 -07:00
Mark Lacey
4f5b80dd03 Make fix for dynamic lookup on Any work for more general expressions.
Follow-up to 56a5ef00a4.

Thanks to Jordan for noticing and suggesting this!

rdar://problem/27639935
2016-08-08 12:59:56 -07:00
Andrew Trick
56f68c4cce Improve diagnostics on invalid UnsafePointer conversion.
The only remaning issue is that we should have a warning to rename
Unsafe[Mutable]Pointer<Void> to Unsafe[Mutable]RawPointer.
2016-08-08 12:54:21 -07:00
Xi Ge
a0c7f51e10 Addressing Jordan's code review feedbacks. NFC 2016-08-08 11:00:23 -07:00
Mark Lacey
62c814a86f Merge pull request #4078 from rudkx/fix-27639935
Add a fixit for attempting member lookup on Any.
2016-08-07 15:28:57 -07:00
Mark Lacey
56a5ef00a4 Add a fixit for attempting member lookup on Any.
Suggest casting to AnyObject or a more specific type.

rdar://problem/27639935
2016-08-07 12:19:53 -07:00
practicalswift
0661f61578 Use a-vs-an consistently. 2016-08-06 12:41:55 +02:00
Xi Ge
7afe4cc96d [FixCode] Add a fixit to help users migrate to Swift 3 name convention. (#4058)
* [FixCode] Add a fixit to help users migrate to Swift 3 name convention of enum cases. rdar://26887735

When users' referring to a enum case with a wrong name and we can find a correct enum case whose name
differs from the wrong name only in capitalization, we replace the wrong name with the correct one.

* Addressing Argyrios' code review comments. NFC

* [test] Update existing test.

* Grammatical polish suggested by @CodaFi.
2016-08-05 20:59:26 -07:00
Slava Pestov
22aa032285 Sema: Clean up SE-0036 diagnostic a bit
Suggest a fix-it for unqualified references to all static members
from instance context, not just enum elements.

Also, fix a small problem with the fix-it for replacing protocol
names with 'Self' inside extension bodies -- we didn't handle nested
functions properly.
2016-08-05 14:27:16 -07:00
Jordan Rose
8d9535203b Give a proper error when a member type is inaccessible. (#4033)
This doesn't yet cover top-level types or top-level non-type values,
but it's a start.

Part of rdar://problem/27663403.
2016-08-05 12:41:45 -07:00
Xi Ge
0c8ace9413 [FixCode] Using coercion (as) instead of forced-coercion (as!) when the former is applicable. rdar://27683732 (#4008)
This fixit is migration-critical to help users call functions now take 'Any' or 'NSHashble' while used to take 'AnyObject'. We insert coercion to the original call sites.
2016-08-04 13:28:42 -07:00
Xi Ge
03d8017482 Addressing Chris' code review comments on 6324314. NFC (#3982) 2016-08-04 10:43:08 -07:00
Xi Ge
63243147af [FixCode] Help users construct mutable pointer from a pointer by inserting 'mutating'. rdar://27500578 (#3971) 2016-08-03 16:30:06 -07:00
Ted Kremenek
6f743dba7c Merge pull request #3939 from milseman/noescape_by_default
[noescape-by-default] Better diagnostics for parameters
2016-08-02 21:59:44 -07:00
Michael Ilseman
49394eb370 [noescape-by-default] incorporate Doug's feedback
Added sugared test case
2016-08-02 21:06:40 -07:00
Michael Ilseman
4ca39fd284 [noescape-by-default] Expand useful diagnostics to more cases
Extends the more useful diagnostics for non-escaping function
parameters used in @escaping contexts to apply to all functions that
are equivalent modulo ExtInfo.
2016-08-02 14:52:43 -07:00
Doug Gregor
31edf710bf [Sema] Improve diagnostics for attempt to throw an error *code*
Attempting to throw an error code value, e.g.,

  throw CocoaError.fileNoSuchFileError

is now ill-formed, although it was well-formed prior to the
introduction of NSError bridging (SE-0112). Provide a specialized
diagnostic with a Fix-It to add the appropriate parentheses:

  throw CocoaError(.fileNoSuchFileError)

Fixes rdar://problem/27543121.
2016-08-02 14:47:18 -07:00
Michael Ilseman
ce1565a110 [noescape-by-default] Better diagnostics for parameters
Issue better diagnostics, along with notes and fixits, for the common
case of using an implicitly non-escaping parameter of function type in
a context expecting an @escaping closure. Provides even more specific
diagnostics for common scenarios such as passing to another function
or assignment.
2016-08-02 14:20:11 -07:00
Jordan Rose
f42158b12e Revert "[Sema] ban multi-arguments to tuple coercion" (#3922)
It breaks cases where there really is a single unlabeled argument of tuple type, like this:

  let pairs = [(1, "A"), (2, "B")]
  print(pairs.map { $0.0 })
2016-08-01 19:22:19 -07:00
Doug Gregor
f1706303a0 Merge pull request #3895 from dduan/se0110_a-pr
[Sema] ban multi-arguments to tuple coercion
2016-07-31 23:00:32 -07:00
Chris Lattner
3f827c0ca1 When diagnosing a missing parameter, point to the decl that is being called.
This resolves:
<rdar://problem/24106465> QoI: missing argument to memberwise initializer doesn't tell me its signature
2016-07-31 16:34:52 -07:00
Daniel Duan
c9b73dacc2 [Sema] ban multi-arguments to tuple coercion
Implements part of SE-0110. Single argument in closures will not be accepted if
there exists explicit type with a number of arguments that's not 1.

```swift
let f: (Int, Int) -> Void = { x in } // this is now an error
```

Note there's a second part of SE-0110 which could be considered additive,
which says one must add an extra pair of parens to specify a single arugment
type that is a tuple:

```swift
let g ((Int, Int)) -> Void = { y in } // y should have type (Int, Int)
```

This patch does not implement that part.
2016-07-31 16:22:57 -07:00
Chris Lattner
fb7a4e9ded Generalize the closure work from yesterday, enough to fix:
rdar://17687826 - QoI: error message when reducing to an untyped dictionary isn't helpful
2016-07-31 15:25:17 -07:00
Chris Lattner
f1ea135f8c fix <rdar://problem/27384685> QoI: Poor diagnostic when assigning a value to a method
We previously said:
  x.method = 1 // error: cannot assign to property: 'x' is immutable

we now say:
error: cannot assign to property: 'method' is a method
2016-07-31 15:03:38 -07:00
Chris Lattner
807345a909 When we get an abiguity problem with a multi-statement closure return type, it is
almost always the case that the user didn't know what the rules are between
single expression and multistatement closures, and they often don't know how to
fix the problem.

Address this by doing some heroics when we detect this situation.  We now go dive
into the closure body, type check the explicit returns within it, and can usually
divine the right answer.  When we do that, generate a fixit hint that generates a
modification to the existing signature, or synthesizes the entire signature from
scratch.  This addresses:
<rdar://problem/22123191> QoI: multi-line closure with failure to infer result type should add a fixit
2016-07-30 14:36:47 -07:00
Chris Lattner
4e3ec30f89 refactor some static functions to be methods on FailureDiagnosis, NFC. 2016-07-30 14:36:47 -07:00
Chris Lattner
84d27f8528 fix <rdar://problem/21675896> QoI: [Closure return type inference] Swift cannot find members for the result of inlined lambdas with branches
We previously produced the unhelpful error message:

x.swift:11:7: error: type of expression is ambiguous without more context

we now produce:

error: unable to infer closure return type in current context

which is going in the right direction.
2016-07-29 21:22:01 -07:00
Robert Widmann
b79fa44da5 Merge pull request #3878 from CodaFi/decltype
[SE-0096] Implement type(of:)
2016-07-29 18:05:01 -07:00
Chris Lattner
764d0fc371 improve the diagnostics for when a multi-statement closure has no inferred result type.
Previously:

error: generic parameter 'T' could not be inferred
now:
error: unable to infer closure return type in current context

There is still more to do, but this fixes:
<rdar://problem/23570873> QoI: Poor error calling map without being able to infer "U" (closure result inference)
2016-07-29 17:49:23 -07:00
Robert Widmann
14dc86cf15 Polish off uses of dynamicType in codebase 2016-07-29 16:58:40 -07:00
Chris Lattner
773d521bfa fix rdar://25271859 QoI: Misleading error message when expression result can't be inferred from closure
We previously produced the error message:

rdar25271859.swift:14:11: error: value of tuple type '(Float, Int)' has no member '0'
  a.map { $0.0 }
          ^~ ~

We now produce:

rdar25271859.swift:15:5: error: generic parameter 'U' could not be inferred
   .andThen { dataResult in
    ^

which is at least is correct, if not yet helpful.
2016-07-29 16:45:17 -07:00