Commit Graph

781 Commits

Author SHA1 Message Date
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
Michael Gottesman
992f576d55 Merge pull request #3830 from rudkx/fix-25563498
Fix crash during type checker diagnostic emission.
2016-07-28 23:02:05 -07:00
Doug Gregor
202cf2e754 [SE-0111] Track function reference kinds in member references.
Extend the handling of function reference kinds to member references
(e.g., x.f), and therefore the logic for stripping argument labels. We
appear to be stripping argument labels from all of the places where it
is required.
2016-07-28 15:41:59 -07:00
Doug Gregor
8c7e75afa0 [AST] Eliminate OverloadedMemberRefExpr.
This expression kind was introduced in exactly one place, and only
with one member. That place can use MemberRefExpr instead.
2016-07-28 14:44:23 -07:00
Mark Lacey
8ed813080e Fix crash during type checker diagnostic emission.
We were crashing attempting to diagnose the case where we cannot assign
an array literal to a type conforming to _ArrayProtocol because we don't
know the element types of the conforming type.

We still don't give a great diagnostic for this, so I've
opened (rdar://problem/27594154) to track improving it.

Resolves rdar://problem/25563498.
2016-07-28 12:00:34 -07:00
John McCall
c8c41b385c Implement SE-0077: precedence group declarations.
What I've implemented here deviates from the current proposal text
in the following ways:

- I had to introduce a FunctionArrowPrecedence to capture the parsing
  of -> in expression contexts.

- I found it convenient to continue to model the assignment property
  explicitly.

- The comparison and casting operators have historically been
  non-associative; I have chosen to preserve that, since I don't
  think this proposal intended to change it.

- This uses the precedence group names and higherThan/lowerThan
  as agreed in discussion.
2016-07-26 14:04:57 -07:00
Doug Gregor
79c00ead96 [CSDiag] We have trailing closure info for subscripts now. Use it. 2016-07-26 11:23:57 -07:00
swift-ci
7974977828 Merge pull request #3659 from rintaro/args-order-diag 2016-07-26 09:30:10 -07:00
Doug Gregor
33d8b469d4 [SE-0111] Store argument labels directly on subscript expressions.
Factor out the trailing storage of call arguments, since we'll need it
for a few different kinds of expression nodes. Use it for both
CallExpr (which already had this storage, albeit with a specialized
implementation) and now SubscriptExpr.
2016-07-25 23:14:41 -07:00
swift-ci
3d1ff4dfba Merge pull request #3749 from nkcsgexi/coerce-fixit 2016-07-25 17:30:23 -07:00
Xi Ge
fc8a1d2274 [Fixit] Before applying forced cast, check if doing so leads to a warning. 2016-07-25 15:18:18 -07:00
Xi Ge
f6ef435141 [Fixit] Add a fixit to add forced cast to the contextual expected type.
This fixit helps migration. After we change some imported types to Any,
existing function calls to SDK APIs need cast to be compilable.
2016-07-25 13:40:50 -07:00