Commit Graph

173 Commits

Author SHA1 Message Date
Pavel Yaskevich
cb3252e055 [ConstraintSystem] Extend use of missing explicit call fix
Now covers following new areas (alongside simple assignments):

- Contextual type coercions:
  - In assignment e.g. `let _: X = foo`
  - In return type positions e.g. `func foo() -> A { return bar }`

- Argument-to-parameter applications (including @autoclosure)
2019-05-07 16:41:19 -07:00
Slava Pestov
f3bb3d92f0 Sema: Don't allow closure return type to contain noescape types 2019-04-16 23:01:39 -04:00
Slava Pestov
39a22f3d6a AST: Remove ParameterTypeFlags::Escaping
Escapingness is a property of the type of a value, not a property of a function
parameter. Having it as a separate parameter flag just meant one more piece of
state that could get out of sync and cause weird problems.

Instead, always look at the noescape bit in a function type as the canonical
source of truth.

This does mean that '@escaping' is now printed in a few diagnostics where it was
not printed before; we can investigate these as separate issues, but it is
correct to print it there because the function types in question are, in fact,
escaping.

Fixes <https://bugs.swift.org/browse/SR-10256>, <rdar://problem/49522774>.
2019-04-15 00:25:03 -04:00
Doug Gregor
2c744b4d50 [Constraint solver] Remove a dubious hack introduced with SR-2505 2019-03-06 08:58:56 -08:00
Pavel Yaskevich
846ae44a1f [CSSimplify] Implode parameters into a tuple through a type variable
There are cases where SE-0110 allows tuple splatting behavior,
so to aid diagnostics let's use a new type variable to represent
a tuple type formed from existing arguments instead of imploding
them directly.
2019-02-28 01:41:01 -08:00
Pavel Yaskevich
502e88deff [CSSimplify] Tuple splat should account for single dependent member parameters
12a65fffee restricted tuple splat
to a single tuple or type variable parameter, but it has to
support dependent member types as well because they could be
resolved to `Void` (or empty tuple).

Resolves: rdar://problem/48443263
2019-02-27 12:20:11 -08:00
Pavel Yaskevich
aeaa26d926 [CSDiagnostics] Add missing arguments failure
Currently only supports closures, but could be easily expanded
to other types of situations e.g. function/member calls.
2019-02-26 13:29:35 -08:00
Pavel Yaskevich
6791ef3624 Merge pull request #21756 from xedin/rdar-36989788
[Diagnostics] Diagnose missing members via fixes
2019-01-10 10:36:17 -08:00
Pavel Yaskevich
74a8ee177e [Diagnostics] Diagnose missing members via fixes
Try to fix constraint system in a way where member
reference is going to be defined in terms of its use,
which makes it seem like parameters match arguments
exactly. Such helps to produce solutions and diagnose
failures related to missing members precisely.

These changes would be further extended to diagnose use
of unavailable members and other structural member failures.

Resolves: rdar://problem/34583132
Resolves: rdar://problem/36989788
Resolved: rdar://problem/39586166
Resolves: rdar://problem/40537782
Resolves: rdar://problem/46211109
2019-01-09 17:29:49 -08:00
Slava Pestov
b05e52b579 Sema: Add a couple of inout-vs-closures regression tests 2019-01-09 15:49:08 -05:00
Brent Royal-Gordon
a72be0fb7d Sort overloads in suggestPotentialOverloads()
When the compiler fails to find an overload with suitable parameter or return types, it often attaches a note listing the available overloads so that users can find the one they meant to use. The overloads are currently ordered in a way that depends on the order they were declared, so swift-evolve would sometimes cause tests involving these diagnostics to fail.

This change emits the list in a textually-sorted order instead. The names were already being sorted as they were inserted into a std::set, so this shouldn’t significantly slow down the diagnostic.
2018-12-12 11:58:57 -08:00
Pavel Yaskevich
694c89c090 [ConstraintSystem] Handle InOut base introduced by diagnostic re-typecheck
While trying to lookup member reference on some base type, handle
base being an `InOutType`, which could be a result of previous
sub-expression re-typechecks made by diagnostics.

Resolves: rdar://problem/45771997
2018-12-06 16:02:28 -08:00
Ben Cohen
fc7830a4ed Remove Swift 3-only Compatibility Shims (#21019) 2018-12-04 18:22:27 -08:00
Pavel Yaskevich
fa1e2d33aa [CSBindings] Detect situations when type variable bindings could be incomplete
When `bind param` constraint is associated with a given
type variable a set of bindings collected for it is
potentially incomplete, because binding gathering doesn't
look through related type variables. Which means that
such type variable has to be de-prioritized until
`bind param` constraint is resolved or there is just
nothing else to try, otherwise there is a risk that
solver would skip some of the valid solutions.

This only affects type variable that appears one the
right-hand side of the `bind param` constraint and
represents result type of the closure body, because
left-hand side gets types from overload choices.

Resolves: rdar://problem/45659733
2018-11-09 15:27:27 -08:00
Pavel Yaskevich
c4c9cc32fb Revert "[CSBindings] Detect situations when type variable bindings could be incomplete"
This reverts commit 4f708890c1.
2018-11-09 11:50:19 -08:00
Pavel Yaskevich
66a79301b4 [CSDiagnostics] Diagnose contextual closure result mismatches via fixes
Let's keep track of type mismatch between type deduced
for the body of the closure vs. what is requested
contextually, it makes it much easier to diagnose
problems like:

```swift
func foo(_: () -> Int) {}
foo { "hello" }
```

Because we can pin-point problematic area of the source
when the rest of the system is consistent.

Resolves: rdar://problem/40537960
2018-11-07 14:28:50 -08:00
Pavel Yaskevich
4f708890c1 [CSBindings] Detect situations when type variable bindings could be incomplete
When `bind param` constraint is associated with a given type
variable a set of bindings collected for it is potentially
incomplete, because binding gathering doesn't look through related
type variables. Which means that such type variable has to be
de-prioritized until `bind param` constraint is resolved
or there is just nothing else to try, otherwise there is a
risk that solver would skip some of the valid solutions.

Resolves: rdar://problem/45659733
2018-10-31 17:58:54 -07:00
Pavel Yaskevich
baceb68f85 [ConstraintSystem] Rename solveIteratively to solve and start using it
Remove solution filtering from new iterative `solve` method, and
replace all usages of `solveRec` with it.
2018-09-15 20:56:47 -07:00
Mark Lacey
352e4a2de4 [ConstraintSystem] Infer empty closures as returning () more eagerly.
We previously allowed these closures to default to (), but be inferred
as other types as well, which means that we will find some expressions
to be ambiguous because we end up finding multiple viable solutions
where there is really only one reasonable solution.

Fixes: rdar://problem/42337247
2018-09-12 13:30:35 -07:00
Mark Lacey
6b96054159 Merge closures_swift4.swift back into closures.swift.
This should have happened when closures_swift3.swift was removed.
2018-09-07 11:44:24 -07:00
gregomni
df34cdc539 Test including multiple generic args with missing unwraps. 2018-08-14 20:36:04 -07:00
Slava Pestov
f316d46a26 SILGen: Remove support for anonymous closure arguments that bind to tuples containing InOutType
The constraint solver no longer produces such ASTs.

Also remove a comment and some other dead code referencing the
long-gone 'deshadowing' pass.
2018-08-10 18:42:06 -07:00
Pavel Yaskevich
c2bf3d5ba9 [TypeChecker] NFC: Fix all of the diagnostics improved by conformance tracking 2018-08-02 21:55:16 -07:00
Doug Gregor
5db1901d57 [Type checker] Emit coalesce-or-force-unwrap Fix-Its for necessary unwraps more consistently.
Replace the last (and most obscure) use of the poor “use ‘?’ or ‘!’” diagnostic with the
new, more explanatory version that provides separate notes with Fix-Its for coalescing or
force-unwrapping the value.

Finishes rdar://problem/42081852.
2018-07-13 16:48:15 -07:00
Slava Pestov
31ab93b82c Remove Swift 3-specific tests 2018-07-02 21:14:22 -07:00
Pavel Yaskevich
aa2b25b3dd Merge pull request #15868 from xedin/solver-get-params
[Sema] Attempt to replace usages of `getInput()` with `getParams()`
2018-04-26 23:32:15 -07:00
Pavel Yaskevich
1ee03bc9c4 [TypeChecker] Miscellaneous changes to switch to FunctionType::getParams() 2018-04-26 17:33:08 -07:00
Slava Pestov
ff61b9e039 Sema: Fix another type variable leak with closures
When we type check a closure expression with inferred parameter
types, we assign type variables to them. If type checking fails,
we end up in FailureDiagnosis::diagnoseClosureExpr().

If there was no contextual type, we would skip the code path
which nukes type variables in the closure's ParamDecls before
proceeding to type check the body. Type checking of the body
creates a new constraint system which would pick up the outer
constraint system's type variables and blow up.

Fixes <rdar://problem/39489003>.
2018-04-25 20:52:34 -07:00
Pavel Yaskevich
40b305e183 [Diagnostics] Switch to using FunctionType::getParams instead of input type
Convert all of the usages of `FunctionType->getInput()` to use
`Function->getParams()` instead which is a new preferred API.
2018-04-09 22:35:48 -07:00
Huon Wilson
a5268f8d19 Merge pull request #15189 from huonw/one-unnamed-owned-argument
[AST] ParenType needs enough bits to store ValueOwnership::Owned.
2018-03-13 13:56:22 +11:00
Huon Wilson
e7002b6413 [AST] ParenType needs enough bits to store ValueOwnership::Owned.
The truncation here meant func foo(_: __owned X) {} was the same as
foo(_: X), so @guaranteed when that's the default.

Fixes rdar://problem/38390524.
2018-03-13 10:18:40 +11:00
Pavel Yaskevich
18d2c1d8b4 [CSBindings] Try Void as a binding for closure result type variable
If we could find a binding for type variable representing closure result
let's also add implicit `Void` type as another pontential binding to attempt
because there is an implicit conversion from `() -> T` to `() -> Void`
so finding `Void` early is going to help avoid function conversions down
the line.

Resolves: rdar://problem/37790062
2018-03-10 23:59:11 -08:00
Pavel Yaskevich
d8b355dbae Revert "[CSSolver] Use correct locator when matching function result types re…" 2018-03-07 23:19:30 -08:00
Pavel Yaskevich
264625898e [CSSolver] NFC: Add few more tests to verify () -> T to () -> Void conversions 2018-03-01 12:50:59 -08:00
Pavel Yaskevich
20019be9d3 [CSSolver] Use correct locator when matching function result types related to closures
Currently we always use 'FunctionResult' as a path element when matching
function result types, but closure result type is allowed to be implicitly
converted to Void, which means we need to be careful when to use
'FunctionResult' and 'ClosureResult'.

Resolves: rdar://problem/37790062
2018-03-01 11:41:36 -08:00
Maxim Moiseev
128092a7d6 Rename filterMap to compactMap 2017-12-18 09:22:41 -08:00
Max Moiseev
96d3439669 Update tests after deprecating flatMap variant 2017-12-18 09:16:37 -08:00
Pavel Yaskevich
4aad7de6e5 [TypeChecker] NFC: Add test case for rdar://problem/36054961 2017-12-14 12:32:37 -08:00
Pavel Yaskevich
3b06f2e897 Merge pull request #12072 from xedin/rdar-33429010
[ConstraintGraph] Don't try to contract edge of parameter bindings with `inout` attribute
2017-09-26 11:51:09 -07:00
Pavel Yaskevich
80e4a2226b [ConstraintGraph] Don't try to contract edge of parameter bindings with inout attribute
Currently edge related to the parameter bindings is contracted
without properly checking if newly created equivalence class has
the same inout & l-value requirements. This patch improves the
situation by disallowing contraction of the edges related to parameter
binding constraint where left-hand side has `inout` attribute set.

Such guarantees that parameter can get `inout` type assigned when
argument gets `l-value` type.

Resolves: rdar://problem/33429010
2017-09-22 17:23:12 -07:00
David Zarzycki
6dfcec2204 [tests] Ensure that functions do not subtype autoclosures 2017-09-21 21:25:28 -04:00
Pavel Yaskevich
d5fb892515 [ConstraintSolver] NFC: Cleanup dead-code related to contraction of subtype constraints in matchTypes
Since we no longer allow constraction of subtype constraints, workaround
which exists in `matchTypes`, for situations where left-hand side of such
constraint is going to be wrapped in `inout` type, is no longer required.

Resolves: rdar://problem/34137342, rdar://problem/34136625
2017-09-13 15:43:50 -07:00
Mark Lacey
6c7af2578f Add a regression test for function argument conversions.
Add a test to ensure that for closures that take an argument of type ()
can be written with or without an explicit argument in Swift 3 and Swift
4.
2017-08-29 11:44:32 -07:00
Mark Lacey
9ff4bd51e8 Make closures.swift test compile in both Swift 3 and 4 modes.
Split out the things that are really specific to Swift 3 into a new
closures_swift3.swift.

In the process I found that two things that should compile without error
under -swift-version 4 do not, so those tests are currently in
closures_swift3.swift and I have opened new bugs for the issues:

https://bugs.swift.org/browse/SR-5791
https://bugs.swift.org/browse/SR-5792
2017-08-29 11:42:56 -07:00
Slava Pestov
ce12e643b7 Sema: Fix crash when we try to generate constraints for invalid code
Perhaps we shouldn't visit AST nodes for which pre-checking failed
at all, but that would be a bigger change.

Fixes <rdar://problem/34077439>.
2017-08-28 18:09:51 -07:00
Mark Lacey
a41fa9482f Try to ensure we try to bind the elements of BindParam constraints left-to-right.
If we allow the right-hand type to be bound first, it can artificially
limit the options for the left-hand side, so attempt to delay binding of
the right-hand side types until after we've chosen something for the
left-hand side.

The test here will not fail without this change, but will fail without
this change if the ConstraintGraph.cpp changes from @xedin's edge
contraction patch (https://github.com/apple/swift/pull/11118) are
applied.
2017-08-25 18:00:30 -07:00
Mark Lacey
e726ad47fe Merge pull request #11556 from rudkx/fix-rdar33296619
[Type checker] Guard against recursing into non-single-expression clo…
2017-08-23 21:25:02 -07:00
Mark Lacey
b6dd52a531 [Type checker] Guard against recursing into non-single-expression closures.
In diagnoseUnintendedOptionalBehavior, guard against recursing into
non-single-expression closures. If we recurse into these, we can end
up hitting malformed ASTs that we do not expect. These happen when we
successfully type-check the code outside of the closure, but the
closure body has errors.

There are potentially other similar issues in other miscellaneous
diagnostics walks.

I opened https://bugs.swift.org/browse/SR-5758 to remind us to review
these and ensure they do not have similar issues.
2017-08-23 17:12:04 -07:00
Robert Widmann
5458fe15a0 Walk into the closure of a CaptureListExpr
Previously, the failure diagnosis visitor would
default to the generic expr walk which would wind
up type checking the closure body and calling it a day.
Walk into the closure of a capture list expression to
emit better diagnostics.

A regression test for SR-5747 is added.
2017-08-23 01:28:43 -07:00
Robert Widmann
3ae852ce8c Lower __shared to @guaranteed
Using the existing logic to lower 'self' parameters, we inherit
the benefits of the existing infrastructure for @guaranteed
argument values.
2017-07-25 18:09:17 -07:00