Commit Graph

1391 Commits

Author SHA1 Message Date
Holly Borla
fce8738ea8 [ConstraintSystem] Replace dependent member types with holes when the base type
doesn't conform to the associatedtype's protocol (only in diagnostic mode).

This allows the solver to find solutions for more cases involving requirement
failures for dependent member types without special cases across the solver
that check for dependent member types with no type variables.
2020-03-03 15:45:24 -08:00
Suyash Srijan
956e918476 [CSSimplify] Move property wrapper fix check after check for conversion restrictions (#30129) 2020-02-28 23:16:32 +00:00
Pavel Yaskevich
389e84fc55 Merge pull request #30115 from LucianoPAlmeida/nfc-abstract-is-stdlib
[NFC] Abstracting isStdlibType and isStdLibDecl logic into Type and Decl
2020-02-28 13:32:32 -08:00
Luciano Almeida
f4b530fb95 [NFC] Abstracting isStdlibType and isStdLibDecl logic into Type and Decl 2020-02-28 07:40:08 -03:00
Pavel Yaskevich
c0b9394c42 Merge pull request #30113 from xedin/rdar-54580247
[CSBindings] Open collection before binding parameter only if origina…
2020-02-28 00:01:27 -08:00
Pavel Yaskevich
20fc51d4f4 [CSBindings] Open collection before binding parameter only if original argument type failed
Instead of always opening argument type represented by a collection
without type variables (to support subtyping when element is a labeled tuple),
let's try original type first and if that fails use a slower path with
indirection which attempts `array upcast`. Doing it this way helps to
propagate contextual information faster which fixes a performance regression.

Resolves: rdar://problem/54580247
2020-02-27 16:26:13 -08:00
Doug Gregor
30e8d1791e [Constraint system] Handle implicit "some" patterns implicitly, better.
We have two similar code paths here that should probably be unified. For
now, make sure the more-specific one for pattern matching kicks in first.

Fixes rdar://problem/59838566.
2020-02-27 16:25:02 -08:00
omochimetaru
79a8a596e4 [ConstraintSystem] remove unused computation in claimNextNamed 2020-02-27 10:49:42 +09:00
Pavel Yaskevich
9ae68133ff Merge pull request #30040 from xedin/sr-11743
[ConstraintSystem] Look through l-value while checking whether dynami…
2020-02-25 17:09:45 -08:00
Doug Gregor
fe5c068ffb Merge pull request #29879 from DougGregor/constraint-system-patterns
[Constraint system] Generate constraints from patterns
2020-02-24 20:41:14 -08:00
Pavel Yaskevich
2cdd78501c [ConstraintSystem] Look through l-value while checking whether dynamic key path is recursive
Fix a crash in dynamic member lookup attempting to recursively
lookup a member on the same base type.

The problem is related to `isSelfRecursiveKeyPathDynamicMemberLookup`
which failed to look through l-value wrapping base type on each side of
the comparison, that's important because it's possible to have l-value
mismatch due to the fact that implicit call always gets l-value base
type but actual subscript which triggered dynamic lookup might be r-value.

Resolves: [SR-11743](https://bugs.swift.org/browse/SR-11743)
Resolves: rdar://problem/57091169
2020-02-24 19:38:20 -08:00
Doug Gregor
644ed76a01 [Constraint system] Prefer enum cases to static members when pattern matching. 2020-02-24 15:00:39 -08:00
Doug Gregor
6cfa0b047d [Constraint system] Don't dereference a null member locator. 2020-02-24 14:46:25 -08:00
Doug Gregor
f6f80befe8 [Constraint system] Handle implicit tupling during pattern matching.
SE-0110 strikes again!
2020-02-24 11:45:23 -08:00
Doug Gregor
d607d3a068 [Constraint system] Custom diagnostics for failed pattern matches.
Extend the constraint system’s diagnostics with specific handling for
matching an enum element pattern that has a subpattern (i.e., to capture
associated values) against an enum case that does not have any associated
value. This brings diagnostics for the new code path on par with the existing
diagnostics of coercePatternToType.
2020-02-24 00:48:15 -08:00
Doug Gregor
dcf7ddeb3a [Constraint system] Generate constraints for EnumElement patterns.
Generate a complete set of constraints for EnumElement patterns, e.g.,

    case let .something(x, y)

Most of the complication here comes from the implicit injection of optionals,
e.g., this case can be matched to an optional of the enum type of which
`something` is a member. To effect this change, introduce a locator for
pattern matching and use it to permit implicit unwrapping during member
lookup without triggering an error.

Note also labels are dropped completely when performing the match,
because labels can be added or removed when pattern matching. Label
conflict are currently diagnosed as part of coercePatternToType, which
suffices so long as overloading cases based on argument labels is not
permitted.

The primary observable change from this commit is in diagnostics: rather
than diagnostics being triggered by `TypeChecker::coercePatternToType`,
diagnostics for matching failures here go through the diagnostics machinery
of the constraint solver. This is currently a regression, because
there are no custom diagnostics for pattern match failures within the
constraint system. This regression will be addressed in a subsequent
commit; for now, leave those tests failing.
2020-02-24 00:48:15 -08:00
Pavel Yaskevich
ed255596a6 [ConstraintSystem] Increase fix impact when get-only dynamic key path is used as a mutable one 2020-02-21 17:28:10 -08:00
Pavel Yaskevich
46d945fdcf [ConstraintSystem] Avoid exploring too much search space when call arguments are holes
Detect that disjunction is going to be applied to arguments which
don't provide any additional contextual information and allow only
a single choice to be attempted in such case to avoid triggering
exponential behavior in the solver.

The problem is most visible with operators e.g.

```swift
.foo == .bar || 1 == .baz
```

If neither member could be contextually determined and solver was
allowed to attempt all of the overloads for `==` and `||` that
would lead to exponential behavior (because each has 30+ overloads)
and generation of hundreds of partial solutions.

Resolves: rdar://problem/56400265
2020-02-20 16:52:30 -08:00
Pavel Yaskevich
71753f3ca6 [ConstraintSystem] Rank contextually unavailable overloads lower than other choices (#29921)
Currently constraint solver is only capable of detecting universally unavailable
overloads but that's insufficient because it's still possible to pick a contextually
unavailable overload choice which could be better than e.g. generic overload, or
one with defaulted arguments, marked as disfavored etc.

Let's introduce `ConstraintSystem::isDeclUnavailable` which supports both universal
and contextual unavailability and allow constraint solver to rank all unavailable
overload choices lower than any other possible choice(s).

Resolves: rdar://problem/59056638
2020-02-19 13:13:53 -05:00
Pavel Yaskevich
d2953bb0c0 [ConstraintSystem] Let simplifyRestrictedConstraintImpl fix any failures related to arguments with restrictions 2020-02-18 13:22:22 -08:00
Pavel Yaskevich
15036bcead [ConstraintSystem] Fix unwrap optional base fix to preserve l-valueness of a member type 2020-02-17 16:09:11 -08:00
Pavel Yaskevich
ca91a1bee0 [ConstraintSystem] Detect and diagnose OoO arguments in call to binary functions/operators
Identify problems like:

```swift
func foo(_ x: Int, _ y: String) {}

func bar(a: Int, b: String) {
  foo(b, a) // Instead of `foo(a, b)`
}
```

Where arguments are out-of-order and repair it by using OoO fix on the
parent locator.
2020-02-17 16:09:11 -08:00
Pavel Yaskevich
df21cbf85c [ConstraintSystem] Ignore attempt to bind type var to dependent member with incorrect base
Just like in cases where both sides are dependent member types
with resolved base that can't be simplified to a concrete type
let's ignore this mismatch and mark affected type variable as a hole
because something else has to be fixed already for this to happen.
2020-02-17 16:09:11 -08:00
Pavel Yaskevich
79b757d3cd [ConstraintSystem] Detect and diagnose mismatches in single parameter function conversions
When there is a conversion from e.g. `(A) -> Void` to `(B) -> Void`
matching between `A` and `B` is going to have a special locator which
doesn't end in `TupleElement`, so `repairFailures` has to account
for that and fix it just like regular argument mismatch.

Resolves: rdar://problem/59066040
2020-02-17 16:09:11 -08:00
Pavel Yaskevich
f6b7df161c [ConstraintSystem] Increase impact of a missing conformance related to stdlib type
Prioritize type mismatches over conformance failures when stdlib
types are involved because it wouldn't be appropriate to suggest
to add such a conformance, so the problem is most likely related
to something else e.g. other overload choice has a better fix.

Consider following example:

```swift
struct S {
  init(_: Double) {}
  init<T: BinaryInteger>(_: T) {}
}

_ = S(Double("0"))
```

In cases like that it's better to prefer failable initializer
which takes a `String` and returns `Double?` and diagnose a
problem related to missing optional unwrap instead of missing
conformances related to a `String` argument of other `Double`
initializer just because it returns a concrete type.
2020-02-17 16:09:11 -08:00
Pavel Yaskevich
802e141be9 [Diagnostics] Diagnose mismatches related to use of === and !== via a tailored fix 2020-02-17 16:09:11 -08:00
Pavel Yaskevich
0a8de8bda8 [ConstraintSystem] Allow simplifyRestrictedConstraintImpl to diagnose contextual failures with optionals
Since `simplifyRestrictedConstraintImpl` has both parent types and
does nested type matching it's a good place to diagnose top-level
contextual problems like mismatches in underlying types of optionals.
2020-02-17 16:09:11 -08:00
Suyash Srijan
d62411535b [CSSimplify] Guard against null locator in isSelfRecursiveKeyPathDynamicMemberLookup (#29886) 2020-02-17 21:28:44 +00:00
Pavel Yaskevich
4b2b436033 Merge pull request #29845 from omochi/trailing-closure-position
[ConstraintSystem] Accept trailing closure if multiple defaulted parameters after last function parameter
2020-02-15 19:50:08 -08:00
omochimetaru
252e82ff7b [ConstrainySystem] Accept trailing closure if multiple...
defaulted parameters are after last function parameter
2020-02-16 07:48:39 +09:00
Vuk Radosavljevic
a570e9330f Removed using desugard type and changed impact from 1 to 10 2020-02-12 17:29:22 -06:00
Vuk Radosavljevic
41e2411752 [CSSimplify] Increment impact by 1 for DefineMemberBasedOnUse when base type is any function type 2020-02-12 14:49:19 -06:00
Luciano Almeida
5572832368 [CSSimplify] Remove diagnostics obsolete diagnostics checks involving CSDiag logic 2020-02-08 12:11:11 -03:00
Pavel Yaskevich
c5ae6c96a9 [ConstraintSystem] Detect missing/extraneous arguments even if there is optional injection required
If parameter type is optional let's ignore that since argument could
be either optional itself or be injected into optional implicitly.

```swift
func foo(_: ((Int, Int) -> Void)!) {}

foo { _ in } // Missing second closure parameter
```
2020-02-05 12:11:01 -08:00
Pavel Yaskevich
506cd7d858 [ConstraintSystem] Let optional chaining infer a type from sub-expression even with contextual mismatch
Just like in case of `try?` it makes it easier to distinguish between
missing optional unwrap and contextual type mismatch.
2020-02-05 12:11:01 -08:00
Pavel Yaskevich
fb0a484a08 [ConstraintSystem] Let try? infer type from sub-expression even with contextual mismatch
This helps to diagnose contextual mismatches like `Int? vs. Bool`
instead of suggesting to unwrap the optional which would still
produce an incorrect type.
2020-02-05 12:11:01 -08:00
Pavel Yaskevich
0103041a30 [ConstraintSystem] Tidy up restriction handling in contextual failures
Delay "fixing" contextual conversion failures until restriction is applied
this helps to tidy up logic for superclass and existential conversions.

Too bad we have to "fix" in `simplifyRestrictedConstraintImpl` now but
we can't really do much about that because diagnostics need both top-level
types to be useful.
2020-02-05 12:11:01 -08:00
Pavel Yaskevich
99856ed42c Merge pull request #29655 from xedin/rdar-41416758
[ConstraintSystem] Make variadics work with anonymous closure parameters
2020-02-05 12:10:25 -08:00
Pavel Yaskevich
07a69c425f [ConstraintSystem] Make variadics work with anonymous closure parameters
Since opening closure body is now delayed until contextual type becomes
available it's possible to infer anonymous parameters as being variadic
based on context and propagate that information down to the closure body.

Resolves: rdar://problem/41416758
2020-02-05 09:35:49 -08:00
swift-ci
08dca4666e Merge pull request #29635 from DougGregor/simplify-solution-target 2020-02-04 02:36:15 -08:00
Doug Gregor
bbc0a95723 [Constraint system] Simplify creation of contextual conversion constraint.
We were storing more state than necessary in the constraint system.
2020-02-03 22:38:24 -08:00
Doug Gregor
f8eee50310 [Constraint solver] Fix function builders with single-expression closures
Fix a few related issues involving the interaction with
single-expression closures:

* A single-expression closure can have a "return" in it; in such
cases, disable the function-builder transform.
* Have the function builder constraint generator look through the
"return" statement in a single-expression closure the same way as
solution application does

Fixes rdar://problem/59045763, where we rejected some well-formed code
involving a single-expression closure with a "return" keyword.
2020-02-03 17:52:44 -08:00
swift-ci
8c8f5b1eb1 Merge pull request #29588 from DougGregor/silence-the-listeners 2020-01-31 20:02:13 -08:00
Doug Gregor
4b0e7b2780 [Type checker] Sink logic for @autoclosure default parameters into the solver
Rather than use an ExprTypeCheckListener subclass to introduce the
autoclosure expression, do it at the end of solving.
2020-01-31 18:20:46 -08:00
Pavel Yaskevich
d24537f032 Merge pull request #29024 from xedin/diagnose-shadowing
[Diagnostics] Port name shadowing diagnostics
2020-01-31 10:39:58 -08:00
Pavel Yaskevich
6debe3d3b5 [ConstraintSystem] Don't produce conformance fixes for mismatches associated with function result type
If there are any requirement failures associated with result types
which are part of a function type conversion, let's record general
conversion mismatch in order for it to capture and display complete
function types.
2020-01-30 09:54:33 -08:00
Pavel Yaskevich
2d10a8a9a0 [ConstraintSystem] Extend function type conversion mismatch coverage
Originally type mismatches associated with conversions between
function types were only covered for coercions and assignments
but fix coverage grew since then and now it makes sense
to remove special case and let `repairFailures` take care of it.
2020-01-30 09:45:36 -08:00
Pavel Yaskevich
3fff5dd064 [ConstraintSystem] Extend metatype instance type mismatch coverage
Originally type mismatches associated with metatypes were only covered
for coercions but fix coverage grew since then and now it makes sense
to remove special case and let `repairFailures` take care of it.
2020-01-29 09:40:38 -08:00
Pavel Yaskevich
78fda9ed98 [ConstraintSystem] Use new fix/diagnostic for name shadowing
Stop filtering outer overload choices while trying to pre-check
expression, instead have it always fetch those and use new
fix to only attempt them in diagnostic mode (unless it's min/max
situation with conditional conformances).
2020-01-29 09:14:24 -08:00
Pavel Yaskevich
dd094018de Merge pull request #29528 from xedin/anyobject-conversion-diagnostics
[Diagnostics] Port invalid conversion to AnyObject diagnostic
2020-01-29 08:03:50 -08:00