Commit Graph

264 Commits

Author SHA1 Message Date
Pavel Yaskevich
ceb78c091c [Constraint] NFC: Improve debug output of pattern binding element constraints 2022-03-08 21:37:40 -08:00
Robert Widmann
d44d8ec043 Allow Converting Pack Types to Tuples
Insert an implicit conversion from pack types to tuples with equivalent parallel structure. That means

1) The tuple must have the same arity
2) The tuple may not have any argument labels
3) The tuple may not have any variadic or inout components
4) The tuple must have the same element types as the pack
2021-12-16 08:51:38 -08:00
Pavel Yaskevich
46ff410a23 [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-12-03 10:53:50 -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
237338b504 Merge pull request #40224 from hamishknight/super-tuple-shuffle 2021-11-18 09:50:09 +00:00
Hamish Knight
da36a2cb88 [CS] Restore a type variable for compatibility with rdar://85263844
Despite being otherwise disconnected from the
constraint system, it's possible for it to affect
how we type-check tuple matches in certain cases.

This is due to the fact that:
- It can have a lower type variable ID than an
opened generic parameter type, so becomes the
representative when merged with it. And because it
has a different locator, this can influence
binding prioritization.
- Tuple subtyping is broken, as it's currently a
*weaker* relationship than conversion.

Therefore, temporarily restore this bit of logic
for language versions < 6. If possible, we should
try and fix tuple subtying in Swift 6 mode to not
accept label mismatches, so that it's not more
permissive than tuple conversion.

rdar://85263844
2021-11-17 17:06:19 +00:00
Pavel Yaskevich
3927f56dbd [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-11-15 16:42:04 -08:00
Hamish Knight
b8e4c676c6 [CS] Remove function component constraints
FunctionInput relies on being able to represent
parameter lists as tuples, which won't be possible
once parameter flags are stripped from tuple types.
FunctionResult is reasonable, but is currently
unused.
2021-10-12 09:51:45 +01:00
Pavel Yaskevich
67a721485f [ConstraintSystem] Compute variables referenced by conjunction elements incrementally
Attempting to pre-compute a set of referenced type variables
upfront is incorrect because parameter(s) and/or result type
could be bound before conjunction is attempted. Let's compute
a set of referenced variables before each element gets attempted.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
55bae6150d [Constraint] Allow closure body elements to carry contextual information 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
528c6968d4 [Constraint] Don't propagate variables from elements to conjunction
Elements have to be solved in isolation, so if conjunction were
to reference all of the variables referenced by its elements then
it would have to bring unrelated type variables into its scope.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
698ea82a6d [Constraint] Implement gatherReferencedVariables for closure body element 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
9073d00613 [ConstraintSystem] Allow conjunctions marking conjunctions as isolated from the rest of the system 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
ad01027a0d [ConstraintSystem] Allow closure body element constraint to reference type variables
This would be useful when once it's used as part of a conjunction to
be able to solve the closure body element component in isolation.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
73dffb3125 [ConstraintSystem] Don't require a type variable per closure body element
Since each of the body elements is already going to have a type associated
with it there is no need to create yet another type variable.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
6aaee599bd [ConstraintSystem] Allow conjunction constraints to reference other variables 2021-10-08 10:08:00 -07:00
Pavel Yaskevich
2b4691048e [ConstraintSystem] Add a skeleton of Conjunction constraint
It's similar to disjunction constraint but represents an "and"
relationship between its elements instead of "or", so all of the
elements have to produce a solution for conjunction constraint
to be considered solved successfully.
2021-10-08 10:08:00 -07:00
Pavel Yaskevich
eb8eabfba7 [ConstraintSystem] Add a skeleton of ClosureBodyElement constraint 2021-10-08 10:08:00 -07:00
Pavel Yaskevich
3a41f7af5f [ConstraintSystem] Add a new conversion - Swift to C pointers
Following pointer conversions are supported in argument positions (when referencing C/ObjC functions):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
2021-09-20 17:22:25 -07:00
willtunnels
319b3e64aa Add support for opaque result types in structural positions (#38392)
* [TypeResolver][TypeChecker] Add support for structural opaque result types

* [TypeResolver][TypeChecker] Clean up changes that add structural opaque result types
2021-08-03 23:45:02 -04:00
Pavel Yaskevich
d7204bfe8d [ConstraintSystem] Fix handling of whether choice should be remembered while cloning disjunctions 2021-07-09 15:59:14 -07:00
Pavel Yaskevich
957e05cfb1 [Constraint] Allow constraints to be disabled only in "performance" mode 2021-04-27 16:11:47 -07:00
Pavel Yaskevich
985843a21f [ConstraintSystem] Add a new transitive conformance constraint
Conformance constraints could be transferred through conversions,
but that would also require checking implicit conversions
such as optional and pointer promotions for conformance is the
type itself doesn't conform, for that let's add a special constraint
`TransitivelyConformsTo`.
2021-04-07 17:55:11 -07:00
Pavel Yaskevich
f00c578761 Merge pull request #34401 from xedin/implicit-cgfloat-conversion
[DNM][TypeChecker] Implement Double <-> CGFloat implicit conversion
2021-03-31 10:20:28 -07:00
Holly Borla
7cd8add885 [ConstraintSystem] Add a property wrapper constraint to delay checking
if an inferred wrapper type is a property wrapper until that type is
resolved.
2021-03-24 11:24:21 -07:00
Pavel Yaskevich
8c6017687a [ConstraintSystem] Limit new implicit conversion to only Double <-> CGFloat 2021-03-17 00:18:10 -07:00
Pavel Yaskevich
2b935bab7c [ConstraintSystem] Add conversion restriction to cover implicit conversion from/to CGFloat 2021-03-17 00:18:08 -07:00
Pavel Yaskevich
497d42d90e [ConstraintSystem] Add a new constraint which connects base with result of the member chain
The first type represents a result of an unresolved member chain,
and the second type is its base type. This constraint acts almost
like `Equal` but also enforces following semantics:

- It's possible to infer a base from a result type by looking through
  this constraint, but it's only solved when both types are bound.

- If base is a protocol metatype, this constraint becomes a conformance
  check instead of an equality.
2021-02-23 11:33:10 -08:00
Holly Borla
752cf7f2ac [ConstraintSystem] Consider the number of resolved argument types
for applied overloads when selecting a disjunction to attempt.
2021-02-03 13:26:57 -08:00
Pavel Yaskevich
b03dc63634 [AST] Adjust TypeBase::getTypeVariables to accept a set
Currently the pattern is to collect the type variables and then unique
them. Instead of asking clients to do uniquing, let's just accept a set
as an argument.
2021-01-26 18:13:34 -08:00
Pavel Yaskevich
86b1449bc0 [ConstraintSystem] NFC: Print out favored attribute associated with disjunction choice 2021-01-14 15:37:12 -08:00
gregomni
91291c7748 Dump disjunction possibilities on multiple aligned lines for better readability. 2020-10-15 18:27:38 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Pavel Yaskevich
6ba7ecb7c2 [ConstraintSystem] NFC: Move Constraint.h to include/swift/Sema 2020-10-08 10:45:32 -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
Anthony Latsis
42b19b75c7 [NFC] Remove a redundant location print after dumping a decl reference 2020-07-09 20:33:56 +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
Joe Groff
b4c7a7e85d Merge pull request #31224 from AnthonyLatsis/rename-getfullname-2
AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl
2020-04-24 12:51:28 -07:00
Pavel Yaskevich
4b1aa29149 [ConstraintSystem] NFC: Adjust all uses of locator anchors to work with TypedNode 2020-04-23 01:13:13 -07:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Hamish Knight
c74a7512f7 [CS] NFC: Remove OverloadChoiceKind::BaseType
This doesn't appear to be used any more.
2020-03-22 22:51:58 -07:00
Pavel Yaskevich
9257b29465 [ConstraintSystem] Clarify DefaultClosureType constraint and add its own simplify method 2020-01-14 00:09:32 -08:00
Pavel Yaskevich
66db166922 [ConstraintSystem] Introduce new DefaultClosureType constraint
This constraint connects type variable representing a closure
expression to its inferred type and behaves just like regular
`Defaultable` constraint expect for type inference where it's
used only if there are no contextual bindings available.
2020-01-14 00:09:32 -08:00
Doug Gregor
bbcaf8c669 [Type checker] Introduce value witness constraints.
Introduce a new kind of constraint, the "value witness" constraint,
which captures a reference to a witness for a specific protocol
conformance. It otherwise acts like a more restricted form of a "value
member" constraint, where the specific member is known (as a
ValueDecl*) in advance.

The constraint is effectively dependent on the protocol
conformance itself; if that conformance fails, mark the type variables
in the resolved member type as "holes", so that the conformance
failure does not cascade.

Note that the resolved overload for this constraint always refers to
the requirement, rather than the witness, so we will end up recording
witness-method references in the AST rather than concrete references,
and leave it up to the optimizers to perform devirtualization. This is
demonstrated by the SIL changes needed in tests, and is part of the
wider resilience issue with conformances described by
rdar://problem/22708391.
2020-01-02 12:06:23 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Robert Widmann
d890b8ad41 Remove some save-and-restores
An awful pattern we use throughout the compiler is to save and restore global flags just for little things.  In this case, it was just to turn on some extra options in AST printing for type variables. The kicker is that the ASTDumper doesn't even respect this flag. Add this as a PrintOption and remove the offending save-and-restores.

This doesn't quite get them all: we appear to have productized this pattern in the REPL.
2019-11-13 07:37:12 -08:00
Doug Gregor
562ed4d999 [Constraint solver] Tie together all of the type variables in a key path.
This is a workaround for rdar://problem/54322807 that should limit the
expansion in the number of constraint scopes visited.
2019-08-20 11:11:07 -07:00
Doug Gregor
17ea39accd [Constraint solver] Simplify one-way constraints to Equal, not Bind.
One-way constraint expressions, which are the only things that
introduce one-way constraints at this point, want to look through
lvalue types to produce values. Rename OneWayBind to OneWayEqual, map
it down to an Equal constraint when it is simplified (to drop
lvalue-ness), and apply that coercion during constraint application.

Part of rdar://problem/50150793.
2019-08-16 14:13:21 -07:00
Doug Gregor
3c69f6a305 [Constraint solver] Introduce one-way constraints.
Introduce the notion of "one-way" binding constraints of the form

  $T0 one-way bind to $T1

which treats the type variables $T0 and $T1 as independent up until
the point where $T1 simplifies down to a concrete type, at which point
$T0 will be bound to that concrete type. $T0 won't be bound in any
other way, so type information ends up being propagated right-to-left,
only. This allows a constraint system to be broken up in more
components that are solved independently. Specifically, the connected
components algorithm now proceeds as follows:

1. Compute connected components, excluding one-way constraints from
consideration.
2. Compute a directed graph amongst the components using only the
one-way constraints, where an edge A -> B indicates that the type
variables in component A need to be solved before those in component
B.
3. Using the directed graph, compute the set of components that need
to be solved before a given component.

To utilize this, implement a new kind of solver step that handles the
propagation of partial solutions across one-way constraints. This
introduces a new kind of "split" within a connected component, where
we collect each combination of partial solutions for the input
components and (separately) try to solve the constraints in this
component. Any correct solution from any of these attempts will then
be recorded as a (partial) solution for this component.

For example, consider:

  let _: Int8 = b ? Builtin.one_way(int8Or16(17)) :
  Builtin.one_way(int8Or16(42\
))

where int8Or16 is overloaded with types `(Int8) -> Int8` and
`(Int16) -> Int16`. There are two one-way components (`int8Or16(17)`)
and (`int8Or16(42)`), each of which can produce a value of type `Int8`
or `Int16`. Those two components will be solved independently, and the
partial solutions for each will be fed into the component that
evaluates the ternary operator. There are four ways to attempt that
evaluation:

```
  [Int8, Int8]
  [Int8, Int16]
  [Int16, Int8]
  [Int16, Int16]

To test this, introduce a new expression builtin `Builtin.one_way(x)` that
introduces a one-way expression constraint binding the result of the
expression 'x'. The builtin is meant to be used for testing purposes,
and the one-way constraint expression itself can be synthesized by the
type checker to introduce one-way constraints later on.

Of these two, there are only two (partial) solutions that can work at
all, because the types in the ternary operator need a common
supertype:

  [Int8, Int8]
  [Int16, Int16]

Therefore, these are the partial solutions that will be considered the
results of the component containing the ternary expression. Note that
only one of them meets the final constraint (convertibility to
`Int8`), so the expression is well-formed.

Part of rdar://problem/50150793.
2019-08-13 11:48:42 -07:00
Joe Groff
f008019bda Sema: Infer the underlying type for opaque return types from function bodies. 2019-04-17 14:43:32 -07:00