Commit Graph

377 Commits

Author SHA1 Message Date
Pavel Yaskevich
613fdca3a8 [CSDiagnostics] Fix trailing closure ambiguity note to not expect that anchor is expression
`TrailingClosureAmbiguityFailure::diagnoseAsNote()` is used by
`diagnoseAmbiguity` opportunistically, which means that anchor
could be a pattern or a statement condition element.
2022-03-17 12:20:23 -07:00
Pavel Yaskevich
2dd97748f3 [CSGen] Use correct locator for member references in enum element patterns
Referencing a member in pattern context is not a regular member reference,
it should use only enum element declarations, just like leading-dot syntax
does, so both locators should end with `pattern matching` element to indicate
that to the member lookup.

Resolves: rdar://90347159
2022-03-17 11:54:40 -07:00
Pavel Yaskevich
5331e276d5 Merge pull request #41730 from xedin/se-0326-solve-pattern-bindings-via-conjunctions
[SE-0326] Re-enable multi-statement closure inference by default
2022-03-15 13:21:03 -07:00
Pavel Yaskevich
894c932ad0 [CSApply] Use decl context of target when applying solution to it
Solution application target can have its declaration context differ
from one used for constraint system, since target could be e.g. a
pattern associated with pattern binding declaration, a statement or
a sub-element of one (e.g. where clause) used in a closure etc.
2022-03-14 10:09:35 -07:00
Pavel Yaskevich
5c3fb222e1 [CSClosure] Explode pattern binding declarations into conjunctions
`one-way` constraints disable some optimizations related to component
selection because they imply strict ordering. This is a problem for
multi-statement closures because variable declarations could involve
complex operator expressions that rely on aforementioned optimizations.

In order to fix that, let's move away from solving whole pattern binding
declaration into scheme that explodes such declarations into indvidual
elements and inlines them into a conjunction.

For example:

```
let x = 42, y = x + 1, z = (x, test())
```

Would result in a conjunction of three elements:

```
x = 42
y = x + 1
z = (x, test())
```

Each element is solved indepedently, which eliminates the need for
`one-way` constraints and re-enables component selection optimizations.
2022-03-08 21:37:40 -08:00
Pavel Yaskevich
966f58f044 [Tests] NFC: Adjust all the test-cases improved by multi-statement inference 2022-03-08 01:13:44 -08:00
Pavel Yaskevich
e9f7a6a8f5 [CSClosure] Delay type-checking of local functions until body is rewritten
A local function can capture a variable that has been declared after it,
which means that type-checking such declaration in-order would trigger
sub-typecheck that would corrupt AST underness the solution application
walker.
2022-03-02 16:37:12 -08:00
Pavel Yaskevich
59154d6d71 [ConstraintSystem] Support solving expression patterns via injecting call to ~= operator
Augment the constraint solver to fallback to implicit `~=` application
when member couldn't be found for `EnumElement` patterns because
`case` statement should be able to match enum member directly, as well
as through an implicit `~=` operator application.
2022-02-28 17:02:28 -08:00
Cal Stephens
369e20f64c Put back unused capture warnings 2021-12-26 14:29:38 -08:00
Cal Stephens
ccdf807211 Move weak self errors to their own file 2021-12-26 14:14:31 -08:00
Cal Stephens
20ed27a5c6 Tests pass, except for 'capture 'y' was never used' issue 2021-12-26 12:34:27 -08:00
Cal Stephens
c6b4a200e1 Make more tests pass 2021-12-25 20:29:36 -08:00
Cal Stephens
66c89f1ab1 More test fixes 2021-12-25 10:49:29 -08:00
Cal Stephens
a5b3312da5 Fix more cases, write more test 2021-12-24 20:51:57 -08:00
Cal Stephens
d909365e2e Update tests 2021-12-24 08:16:56 -08:00
Pavel Yaskevich
f287f7e6e4 [CSClosure] Support empty return when closure result is optional Void
Source compatibility workaround.

func test<T>(_: () -> T?) {
  ...
}

A multi-statement closure passed to `test` that has an optional
`Void` result type inferred from the body allows:
  - empty `return`(s);
  - to skip `return nil` or `return ()` at the end.

Implicit `return ()` has to be inserted as the last element
of the body if there is none. This wasn't needed before SE-0326
because result type was (incorrectly) inferred as `Void` due to
the body being skipped.

Resolves: rdar://85840941
2021-12-08 13:27:21 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Hamish Knight
ede9302704 [Sema] Fix spurious trailing closure warning
With the argument list refactoring, it's no
longer sufficient to stop walking when we encounter
an explicit tuple or paren. Add an additional
check to stop walking when we encounter an explicit
argument list.

rdar://85343171
2021-11-19 19:26:04 +00:00
Pavel Yaskevich
67d87e104f [Tests] NFC: Adjust all the test-cases improved by multi-statement inference 2021-11-15 16:42:06 -08:00
Pavel Yaskevich
42b1a3992e [TypeChecker] NFC: Add some test-cases for extended multi-statement closure inference 2021-10-14 12:00:31 -07:00
Rintaro Ishizaki
42bc8fbbb3 Merge pull request #38260 from JiarenWang/wjr
[Parse] give more useful errors for forget 'do' keyword.
2021-09-02 15:14:47 -07:00
jiaren wang
d8780d33e9 [Parse] give more useful errors for forget 'do' keyword. 2021-09-01 21:18:56 +08:00
John McCall
1711df4ce4 Fix the condition for warning about implicit capture of self captures.
We've always emitted an error if we saw an implicit use of a self
parameter of class type from an escaping closure.  In PR #35898, I fixed
this to also emit an error if the reference was to an explicit capture
of self that wasn't made in the current closure.  That was causing
some source incompatibilities that we decided were too severe, so in
PR #38947 I weakened that to a warning when the diagnostic walk was
within multiple levels of closures, because I have always thought of
this as a fix to nested closures.  However, this was the wrong condition
in two ways.

First, the diagnostic walk does not always start from the outermost
function declaration; it can also start from a multi-statement closure.
In that case, we'll still end up emitting an error when we see uses
of explicit captures from the closure when we walk it, and so we still
have a source incompatibility.  That is rdar://82545600.

Second, the old diagnostic did actually fire correctly in nested
closures as long as the code was directly referring to the original
self parameter and not any intervening captures.  Therefore, #38947
actually turned some things into warnings that had always been errors.

The fix is to produce a warning exactly when the referenced declaration
was an explicit capture.
2021-09-01 02:29:51 -04:00
Hamish Knight
3281cf9e0c [test] closures_swift6 requires asserts
`-swift-version 6` is currently only permitted
for asserts builds.

rdar://82126678
2021-08-19 16:35:29 +01:00
John McCall
5eecc6ac07 Only warn about self capture in nested closures until Swift 6.
Swift has diagnosed implicit uses of class-reference `self` in
escaping closures for a long time as potentially leading to reference
cycles.  PR #35898 fixed a bug where we failed to diagnose this
condition in nested closures.  Unfortunately, treating this as an
error has proven problematic because there's a lot of code doing
it.  Requiring that code to be thoroughly stamped out before we
ship a compiler is just too much to ask.  Stage the fix in by
treating it as a warning in Swift versions prior to 6.

As with the non-nested case, this warning can be suppressed by
explicitly either capturing `self` or spelling out `self.` in the
access.

Fixes rdar://80847025.
2021-08-18 21:44:23 -04:00
Hamish Knight
3a28a525cb [AST] Enforce that ParenExpr is typed as ParenType
Make sure we don't accidentally drop the sugar of
the type.
2021-08-02 20:40:50 +01:00
jiaren wang
c5de1f0800 [SR-14824] Improve diagnostic for multi-statement closures instead of saying "too complex closure return type" 2021-06-30 14:36:38 +08:00
Luciano Almeida
0e8b7ceb18 [SR-13239] Adjust error message to split inferrence source into notes 2021-06-18 10:56:04 -03:00
Luciano Almeida
2e4ea3b94c [Sema] Adjusments from review in SR-13239 and for variadic closure case 2021-06-18 10:56:04 -03:00
Luciano Almeida
5401ed3f30 [tests] Add regression test cases for SR-13239 2021-06-18 10:56:03 -03:00
Luciano Almeida
308d104f70 [Sema] [Diagnostics] Consider function result locator when get structural contextual type for function arg/param 2021-05-31 00:07:47 -03:00
Holly Borla
c297070106 [Diagnostics] Always use the parameter name for closure parameter diagnostics,
because the $ prefix does not indicate that the parameter is anonymous.
2021-05-11 18:30:27 -07:00
John McCall
f29074d8b6 Diagnose the implicit use of self in nested closures.
Fixes SE-14120.
2021-02-10 18:44:42 -05:00
Slava Pestov
5808d9beb9 Parse: Remove parse-time name lookup 2020-11-16 22:39:44 -05:00
Pavel Yaskevich
51bd8d93d6 Merge pull request #34510 from maustinstar/sr-11711
[SR-11711]  [Parse] Single-expression implicit returns within #if declarations
2020-11-05 21:14:44 -08:00
Pavel Yaskevich
1c65a55633 [CSGen] Turn invalid decls into holes
Instead of failing constraint generation upon encountering
an invalid declaration, let's turn that declaration into a
potential hole and keep going. Doing so enables the solver
to reach a solution and diagnose any other issue with
expression.
2020-11-04 16:19:38 -08:00
maustinstar
3913c31688 [SR-11711] Single-expression returns for #if declarations within closures 2020-11-03 12:48:05 -05:00
Slava Pestov
bd36100cb3 Update tests in preparation for disabling parser lookup
I created a second copy of each test where the output changes
after disabling parser lookup. The primary copy now explicitly
calls the frontend with -disable-parser-lookup and expects the
new diagnostics; the *_parser_lookup.swift version calls the
frontend with -enable-parser-lookup and has the old expectations.

This allows us to turn parser lookup on and off by default
without disturbing tests. Once parser lookup is completely
removed we can remove the *_parser_lookup.swift variants.
2020-10-03 09:37:55 -04:00
Slava Pestov
d7f4b1a1bd AST: Capture list bindings now point back to their parent CaptureListExpr
We'll need this to get the right 'selfDC' when name lookup
finds a 'self' declaration in a capture list, eg

class C {
  func bar() {}
  func foo() {
    _ = { [self] in bar() }
  }
}
2020-09-18 02:59:15 -04:00
Pavel Yaskevich
9bc2819317 Merge pull request #33590 from xedin/fail-on-error-if-single-stmt
[CSGen] Abort constraint generation on error only if closure part…
2020-08-24 11:21:08 -07:00
Pavel Yaskevich
94bf392e0e [CSGen] Abort constraint generation on error only if closure participates in type-check
If reference collection discovered at least `ErrorExpr` in the body
of a closure, let's fail constraint generation only if it's a
single-statement closure, decision about multi-statement closures
should be delayed until body is opened.

This helps code completion because `ErrorExpr` could belong to
a statement unrelated to a completion, so it wouldn't affect
its correctness in any way.
2020-08-21 15:37:12 -07:00
Doug Gregor
471f1ee3af [Constraint solver] Disable pattern type "optimization" involving weak types.
Fix a regression introduced by moving the type checking of closure
captures into the constraint system. The pattern-type optimization for
initializations was causing inference of a double-optional where there
shouldn't be one, manifesting in a failure involving implicitly
unwrapped optionals and `weak self` captures.

Fixes rdar://problem/67351438.
2020-08-18 20:58:39 -07:00
Doug Gregor
73f07a6f0f Merge pull request #33092 from DougGregor/forward-trailing-closure-matching-sourcecompat-backward-bias
[SE-0286] Forward matching of trailing closure arguments
2020-07-28 14:11:15 -07:00
Doug Gregor
05c4cee9e2 [Constraint solver] Type check captures as part of the constraint system
Rather than type-checking captures as separate declarations during
pre-check, generate constraints and apply solutions to captures in
the same manner as other pattern bindings within a constraint
system.

Fixes SR-3186 / rdar://problem/64647232.
2020-07-25 00:20:05 -07:00
Doug Gregor
25d40125e9 [Trailing closures] Bias toward the backward scan for ambiguities.
This approach, suggested by Xiaodi Wu, provides better source
compatibility for existing Swift code, by breaking ties in favor of the
existing Swift semantics. Each time the backward-scan rule is needed
(and differs from the forward-scan result), we will produce a warning
+ Fix-It to prepare for Swift 6 where the backward rule can be
removed.
2020-07-24 08:47:51 -07:00
Doug Gregor
859b6388ce [Trailing closures] Warn about use of deprecated "backward" scan.
Whenever we form a call that relies on the deprecated "backward" scan,
produce a warning to note the deprecation along with a Fix-It to label
the parameter appropriately (and suppress the warning). For example:

    warning: backward matching of the unlabeled trailing closure is
        deprecated; label the argument with 'g' to suppress this warning
      trailingClosureEitherDirection { $0 * $1 }
                                     ^
                                    (g:         )
2020-07-24 08:11:25 -07:00
Doug Gregor
17669d7d5d [Trailing closures] Attempt both forward and backward scans.
To better preserve source compatibility, teach the constraint
solver to try both the new forward scanning rule as well as the
backward scanning rule when matching a single, unlabeled trailing
closure. In the extreme case, where the unlabeled trailing closure
matches different parameters with the different rules, and yet both
produce a potential match, introduce a disjunction to explore both
possibilities.

Prefer solutions that involve forward scans to those that involve
backward scans, so we only use the backward scan as a fallback.
2020-07-24 08:11:25 -07:00
Doug Gregor
4acb094677 Fix a NULL pointer dereference and update test cases. 2020-07-24 08:11:25 -07:00
Doug Gregor
2395225df7 [Type checker] Improve diagnostics for trailing closures.
The change to the forward-scanning rule regressed some diagnostics,
because we no longer generated the special "trailing closure mismatch"
diagnostic. Reinstate the special-case "trailing closure mismatch"
diagnostic, but this time do so as part of the normal argument
mismatch diagnostics so it is based on type information.

While here, clean up the handling of missing-argument diagnostics to
deal with (multiple) trailing closures properly, so that we can (e.g)
suggest adding a new labeled trailing closure at the end, rather than
producing nonsensical Fix-Its.

And, note that SR-12291 is broken (again) by the forward-scan matching
rules.
2020-07-24 08:10:54 -07:00
Luciano Almeida
384c3735e8 [tests] Add regression test for SR-12955 2020-06-11 21:43:01 -03:00