Commit Graph

517 Commits

Author SHA1 Message Date
Pavel Yaskevich
427c6478bc [ConstraintSystem] ConstraintSystem cannot be const when inferring new bindings
It's possible that bindings would need to mutate constraint system
e.g. by allocating new locators.
2020-09-29 12:46:26 -07:00
Pavel Yaskevich
225d87a9bd [CSBindings] NFC: Simplify TypeVariableBinding::attempt but extracting fix computation
Add a separate method `fixForHole` on `TypeVariableBinding`
responsible for determining whether fix is required and if so,
what kind of fix to apply when a particular type variable is
resolved to a hole.
2020-09-28 15:40:19 -07:00
Pavel Yaskevich
e30bdacd57 [CSBindings] Adjust impact of an event when nil is bound to a hole 2020-09-25 15:00:47 -07:00
Pavel Yaskevich
1b5ce2b88f [CSBindings] Start recording SpecifyContextualTypeForNil fix when nil is bound to a hole 2020-09-25 15:00:32 -07:00
Nathan Hawes
a1ef6e4dac Merge pull request #33749 from nathawes/new-member-completion
[CodeCompletion] Update member completion to handle ambiguous and invalid base expressions
2020-09-09 18:51:22 -07:00
Pavel Yaskevich
b7d5ea844a Merge pull request #33740 from xedin/rdar-68094328
[CSBindings] Delay binding chain result if type is an optional of a…
2020-09-01 10:17:36 -07:00
Pavel Yaskevich
6a99fde6a6 [CSBindings] Delay binding chain result if type is an optional of a type variable
If subtyping is allowed for a result type of an implicit member chain,
let's delay binding it to an optional until its object type resolved too or
it has been determined that there is no possibility to resolve it.
Otherwise we might end up missing solutions since it's allowed to
implicitly unwrap base type but it can't be done early - type variable
representing chain's result type has a different l-valueness comparing
to generic parameter of an optional.

This used to work before due to a hack in constraint generator where
unresolved member with arguments would return base type (which
doesn't allow l-value) instead of a result type.

Resolves: rdar://problem/68094328
2020-09-01 00:51:09 -07:00
Pavel Yaskevich
20f8a37357 [ConstraintSystem] Allow code completion type to be a hole
Code completion expression type could be a hole if and only
if there is obosolutely no contextual information available
e.g. `let _ = .#^MEMBER^#`
2020-08-31 16:56:05 -07:00
Pavel Yaskevich
ebc03a1805 [CSBindings] NFC: Remove dead condition leftover from Swift version 3 support 2020-08-28 17:39:21 -07:00
Pavel Yaskevich
c03d76291c [ConstraintSystem] NFC: Clarify why upward propagation of literal conformances is ncessary at the moment
This is NFC because only literal protocols are tracked at the moment.

Forward propagate (subtype -> supertype) only literal conformance
requirements since that helps solver to infer more types at
parameter positions.

```swift
func foo<T: ExpressibleByStringLiteral>(_: String, _: T) -> T {
  fatalError()
}

func bar(_: Any?) {}

func test() {
  bar(foo("", ""))
}
```

If one of the literal arguments doesn't propagate its
`ExpressibleByStringLiteral` conformance, we'd end up picking
`T` with only one type `Any?` which is incorrect.

This is not going to be necessary once bindings are filtered based
of requirements placed on a type variable.
2020-08-28 17:30:19 -07:00
Pavel Yaskevich
9665091121 [CSBindings] Don't attempt to join types that contain holes 2020-08-27 11:51:00 -07:00
Pavel Yaskevich
d039107bdb [ConstraintSystem] Replace use of UnresolvedType with specialized HoleType 2020-08-26 18:43:10 -07:00
Pavel Yaskevich
56ef379fb8 [ConstraintSystem] Integrate IgnoreInvalidFunctionBuilderBody into the solver 2020-08-17 12:39:29 -07:00
Luciano Almeida
b1eccb594d [Sema] Do not diagnose contextual type mismatches for malformed key path expressions (#33230)
* [AST] Adding hasSingleInvalidComponent to key path expression

* [Sema] Adding a new fix and failure to diagnose missing key path component

* [Sema] Recording new fix for key path missing components and remove diagnose from pre-check

* [tests] Adjusting key path missing component contextual tests

* [Sema] Renaming missing component key path fix and failure

* [Sema] Correcting comments typos
2020-08-16 12:27:21 -03:00
Pavel Yaskevich
df4d89c58d Merge pull request #33164 from xedin/refactor-getpotentialbindings
[CSBindings] Refactor `getPotentialBindings` into `inferBindingsFor` + `infer`
2020-08-13 12:37:29 -07:00
Frederick Kellison-Linn
eb625f7b2a [Sema] Allow inference of binding to type var of differing lvalue-ness…
when we have an optional type. This uncovered an error with unresolved member lookup where we allowed an unresolved value member constraint to fail if lookup failed in an optional type wrapping a type variable.

This resolves SR-13357.
2020-08-08 08:53:16 -04:00
Pavel Yaskevich
d19c2ccc02 [CSBindings] Don't attempt to rank unviable bindings
If a type variable doesn't have any "direct" bindings let's not
consider it as viable to be attempted next. Such type variables
are helps purely to accommodate transitive binding inference
for other members of subtype chain.

Resolves: rdar://problem/66234725
2020-07-30 16:35:31 -07:00
Pavel Yaskevich
6dd859e92f [CSBindings] Remove remaining flags from inference 2020-07-28 12:48:33 -07:00
Pavel Yaskevich
dff73c9467 [CSBindings] NFC: Remove obsolete literalBindings vector 2020-07-27 16:33:30 -07:00
Pavel Yaskevich
99e6cd42f6 [CSBindings] Modernize and consolidate locator checking for binding adjustment 2020-07-27 16:33:30 -07:00
Pavel Yaskevich
de390584cd [CSBindings] Refactor getPotentialBindings into inferBindingsFor + infer
Split `getPotentialBindings` in two:

- `inferBindingsFor` should take responsibility for binding constraints
  to be evaluated as potential binding sources;
- `PotentialBindings::infer` would process constraints provided by
  `inferBindingsFor` and "extract" bindings from them.

This is done to separate "sourcing" logic from "inference" and
make the former pluggable, so bindings could be computed based
on changes in constraint graph in the future.
2020-07-27 16:33:30 -07:00
Pavel Yaskevich
fa55358730 [CSBindings] const qualify ConstraintSystem passed to inference methods 2020-07-27 16:33:30 -07:00
Pavel Yaskevich
03a9caac0f [CSBindings] Make sure that transitive bindings aren't inferred on itself
It's possible to find current type variable while trying to infer
transitive bindings (because sources are gathered from multiple
different type variables and current type variable could be a
representative of an equivalence class), let's make sure we don't
attempt to use constraints which refer to current type variable
on the left-hand side.

Resolves: rdar://problem/65724310
2020-07-17 18:06:26 -07:00
Pavel Yaskevich
7f65620140 [CSBindings] Fix std::rotate to move only Any
It used to move everything up to `Any`, now it's going to move
only `Any` itself to the end of the list.
2020-07-16 00:21:09 -07:00
Pavel Yaskevich
d726c90326 [CSBindings] Limit optionality hack only to direct protocol requirements
In situations like:

$T0 subtype $T1
$T1 literal conforms to ExpressibleByArrayLiteral
$T0 conv [<Type>]?

We have to ensure that $T0 gets to maintain its optionality
because $T1 then could strip optionality later based on a
typing rule where `T <: T?`
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
c3cc96829c [CSBindings] Coalesce integer/float literal protocols to produce a single default
Let's try to coalesce integer and floating point literal protocols
if they appear together because the only possible default type that
could satisfy both requirements is `Double`.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
7622993083 [ConstraintSystem] Add a new endpoint to infer bindings for a given type variable
Since bindings now require finalization we need a new endpoint
which perform all of the required actions before returning complete
`PotentialBindings` object when they are requested for a particular
type variable without any other context.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
aae6e0c27a [CSBindings] Let's always consider Any to be a last resort binding
If `Any` has been inferred as a non-default binding (e.g.
if parameter has `Any` type) let's give it the lowest
priority and attempt after any other bindings (including
literal ones) because it's better to infer some concrete
type and then erase it to `Any` if required by the
constraint system.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
1b75bd0e55 [CSBindings] Perform more checking before accepting default types
Some literal protocols default to unbound types, we need to make
sure that it's okay to include them to avoid duplicate solutions.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
ea5eb89114 [CSBindings] Determine literal coverage and default types during finalization
Based on collected direct and transitive information about protocol
requirements let's determine literal protocol coverage by existing
bindings as well as any default types which have to be introduced
to the set as part of finalization phase.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
e5e54e703c [CSBindings] Infer transitive defaults and add them at finalization 2020-07-15 20:50:50 -07:00
Pavel Yaskevich
91823fd00e [CSBindings] Record holes only after all binding inference steps are done
If there are no direct, transitive or default bindings only then
consider binding the given type variable to a "hole".
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
248341a4f0 [CSBindings] Adjust optionality of the compute bindings as a final step
If type variable is expected to conform to `ExpressibleByNilLiteral`
adjust optionality of the inferred bindings only after all of the
bindings have been collected otherwise transitive supertype bindings
are going to stay non-optional which is incorrect.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
3e74a443f9 [CSBindings] Infer transitive protocol requirements through all conversions 2020-07-15 20:50:50 -07:00
Pavel Yaskevich
4568e6e27b [CSBindings] Add finalize method to PotentialBindings
This is used to infer transitive information and adjust existing
bindings before solver can consider them.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
8df19d2c87 [ConstraintSystem] NFC: Associate transitive binding inference with PotentialBindings 2020-07-15 20:50:50 -07:00
Pavel Yaskevich
fa553ab83d [CSBindings] Record conformance requirements associated with type variable
Include direct conformnace requirements as well as transitive ones.
2020-07-15 20:50:50 -07:00
Luciano Almeida
6d29e75787 [CSBindings] Improving comment explaning marking projected value as fully bounded 2020-07-12 15:22:33 -03:00
Luciano Almeida
37fa67afa4 [CSBindings] Delay key path application projected value binding until the whole constraint is considered fully bounded 2020-07-12 13:45:03 -03:00
Pavel Yaskevich
739ba4bb39 [CSBindings] Open collection binding associated with @autoclosure argument
To preserve subtype relationship between element types of a collection
passed as an argument to a parameter represented by another collection
type let's extend opening of the collection types to be done for arguments
to @autoclosure parameters as well because implicit closure is transparent
to the caller.

Resolves: [SR-13135](https://bugs.swift.org/browse/SR-13135)
Resolves: rdar://problem/65088975
2020-07-06 11:54:39 -07:00
Luciano Almeida
28fb66cbaf [SR-13088] Fix false positive downcast unrelated of types that cannot be statically known (#32592)
* [TypeCheckConstraints] Adjusting cases where checked casts that cannot be determined statically were producing misleading warnings

* [tests] Adding regression tests for SR-13088

* [TypeCheckConstraints] Adjusting comment and adding an extra test case for SR13035

* [TypeCheckConstraints] Fixing typos in comments

* [AST] Moving implementation of isCollection from ConstraintSystem to AST TypeBase

* [TypeCheckConstraints] Adjusting logic to verify specific conformance to stdlib collection type before emit an downcast warning

* [TypeCheckConstraints] Creating new CheckedCastContextKind::CollectionElement to be able to verify special cases within typeCheckCheckedCast for collection elements

* [TypeCheckConstraints] Adjusting logic around generic substitution to check both subtype and supertype

* [Sema] Adding isKnownStdlibCollectionType and replacing all usages contraint system method

* [TypeChecker] Reverting fixes around array element types

* [TypeChecker] Abstract logic of check for conditional requirements on TypeChecker::couldDynamicallyConformToProtocol

* [TypeChecker] Ajdustinc can conformDynamically conform and adjust review comments

* [TypeChecker] Ajusting comments and fixing typos

* [TypeChecker] Adjusting existential and archetype logic to check inside couldDynamicConform

* [TypeChecker] Adjusting minor and adding existential check into couldDynamically conform.

* [TypeChecker] Adjusting comments
2020-07-02 22:06:29 -03:00
Pavel Yaskevich
92271e9ce5 [ConstraintSystem] Rework handling of object literal expressions
Instead of special casing argument-to-parameter matching for
object literal expressions, let's allow constraint system to
lookup a witness initializer and apply it to the given set
of arguments.

This also simplifies constraint application because
`coerceCallArguments` could be used to form type-checked
argument expression.
2020-06-22 10:15:08 -07:00
Pavel Yaskevich
22832080e2 [ConstraintSystem] Don't propagate holes through supertype inference
Disallow holes to be inferred as supertype bindings  while inferring
bindings from other type variables (based on transitivity of subtype
conversions), otherwise it would be possible to record a hole without
recording associated fix.

Resolves: rdar://problem/64368545
2020-06-18 10:53:50 -07:00
Anthony Latsis
f4f2101e9c [NFC] Sema: Internalize the use of UnboundGenericType in applyUnboundGenericArguments 2020-06-12 18:04:55 +03:00
Anthony Latsis
86b7e989e7 [NFC] CS: Give the type-transforming openUnboundGenericType method a more accurate name 2020-06-11 23:58:01 +03:00
Doug Gregor
64f903fe2a [Type checker] Experimental support for one-way parameter constraints.
Introduce an experimental mode (behind the flag
`experimental-one-way-closure-params`) that places one-way
constraints between closure parameter types and references to those
parameters within the body of the closure. The intent here is to
break up constraint systems further, potentially improving type
checking performance and making way for larger closure bodies to be
supported.

This is a source-breaking change when the body of a single-expression
closure is used to determine the parameter types. One obvious example
is when there is no contextual type, e.g.,

    let _ = { $0 + 1 }

this type-checks today because `1` becomes `Int`, which matches the
`+` overload with the type `(Int, Int) -> Int`, determining the
parameter type `Int` for the closure. Such code would not type-check
with one-way constraints.
2020-06-05 22:47:21 -07:00
Pavel Yaskevich
28fa3ab9e9 Merge pull request #31848 from LucianoPAlmeida/SR-12827-keypath-hole
[SR-12827] [Diagnostics] Improve diagnostics keypath hole involving generic argument
2020-05-21 17:19:02 -07:00
Luciano Almeida
50803b8fca [CSBindings] Emit specific key path root arg if generic param type var being bound to hole has a key path root representatee 2020-05-21 17:14:50 -03:00
Robert Widmann
afe8f2b63f Drop TypeCheckerDebugConsumer 2020-05-18 22:49:55 -07:00
Pavel Yaskevich
d111f119d8 [ConstraintSystem] Detect and diagnose inability to infer type of closure parameter(s)
Detect situation when it's impossible to determine types for
closure parameters used in the body from the context. E.g.
when a call closure is associated with refers to a missing
member.

```swift
struct S {
}

S.foo { a, b in } // `S` doesn't have static member `foo`

let _ = { v in } // not enough context to infer type of `v`

_ = .foo { v in } // base type for `.foo` couldn't be determined
```

Resolves: [SR-12815](https://bugs.swift.org/browse/SR-12815)
Resolves: rdar://problem/63230293
2020-05-15 01:14:30 -07:00