Commit Graph

1556 Commits

Author SHA1 Message Date
Slava Pestov
1c3ac86796 AST: Banish OptionalTypeKind to ClangImporter.h
The only place this was used in Decl.h was the failability kind of a
constructor.

I decided to replace this with a boolean isFailable() bit. Now that
we have isImplicitlyUnwrappedOptional(), it seems to make more sense
to not have ConstructorDecl represent redundant information which
might not be internally consistent.

Most callers of getFailability() actually only care if the result is
failable or not; the few callers that care about it being IUO can
check isImplicitlyUnwrappedOptional() as well.
2019-08-15 18:41:42 -04: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
omochimetaru
47a254bfee [Sema] fix typevar attr for subscript 2019-08-13 12:32:06 +09:00
gregomni
a2cd53d802 Handle simplification of optional-chaining key path literals, distinguish function type and keypath BGT type without explicit disjunctions. 2019-08-06 07:52:56 -07:00
Brent Royal-Gordon
33c68e01d1 Distinguish subscript member type from result type 2019-08-06 07:52:56 -07:00
Brent Royal-Gordon
1a9be789e3 Allow optional-chaining key paths to match function types 2019-08-06 07:52:56 -07:00
gregomni
5e98f3e8e5 Implicit conversion of KeyPath<Root,Value> to (Root) -> Value.
Fix autoclosure param interface type when it involves archetypes.

Had some repeated locals from moving this block of code around - cleaned up.

Alternate implementation where KeyPathExpr is essentially a literal type and can be either a KeyPath(R,V) or (R)->V.

Some unneccessary code now.

Implicit closure pieces need valid sourceLocs in case a coerce expr needs to refer to the beginning of the closure in `\.foo as (A) -> B`.
Removed explicit noescape from function type so `let a: (A) -> B = \.foo` is valid.
Remove optimization that optional path is always read-only KP type in CSGen, since it can also now be of function type.
2019-08-06 07:52:56 -07:00
Pavel Yaskevich
45aec3c6ad [ConstraintSystem] NFC: Extract logic related to forming locator for argument info retrieval 2019-07-31 00:52:00 -07:00
Pavel Yaskevich
29d1f6a705 [ConstraintSystem] Don't allow overwrites while collecting argument info
Currently, because argument info has been collected based solely
on anchor, it would be possible to overwrite labels for expressions
like `foo[0](x)` since `ApplyExpr` uses its function expression as
a key for argument information cache, which leads to errors while
attempting optimizations based on that information.
2019-07-31 00:00:21 -07:00
Pavel Yaskevich
e2dd9da5e3 [ConstraintSystem] NFC: Move argument info handling into constraint system 2019-07-31 00:00:21 -07:00
Pavel Yaskevich
b7cafd0e82 [ConstraintSystem] NFC: Collect argument labels while generating constraints
Instead of making a separate pass over AST to collect labels for
potential performance optimizations, let's do that while generating
constraints.
2019-07-29 13:57:56 -07:00
Doug Gregor
8355f3d270 [Constraint graph] Move constraint uniquing into gatherConstraints().
Simplify the interface to gatherConstraints() by performing the
uniquing within the function itself and returning only the resulting
(uniqued) vector of constraints.
2019-07-25 02:26:49 -04:00
Xi Ge
911b52f69c Merge pull request #26333 from nkcsgexi/convert-type-relation
IDE+Evaluator: converge multiple type relation checks into a single sema request, NFC
2019-07-24 14:25:58 -07:00
Xi Ge
b6e6f69e80 IDE+Evaluator: converge multiple type relation checks into a single sema request, NFC
From libIDE, the utility functions will invoke the request in the implementation.
2019-07-24 12:53:10 -07:00
Slava Pestov
d94baea444 Merge pull request #26328 from slavapestov/to-const-or-not-to-const
Don't pass SubstitutionMap by const reference
2019-07-24 14:14:18 -04:00
Rintaro Ishizaki
32854b3d93 [AST] Use LookUpConformanceInModule to check if extension applied
rdar://problem/53401609
2019-07-24 09:48:46 -07:00
Slava Pestov
bdaeaf1e5f Don't pass SubstitutionMap by const reference 2019-07-24 12:43:36 -04:00
Slava Pestov
a532a325e1 AST: Move a few methods from VarDecl down to ParamDecl 2019-07-22 20:19:09 -04:00
Xi Ge
77ba3a21b4 IDE+Evaluator: refactor the implementation of two type checker utilities to evaluator requests. NFC
IDE functionality needs some internal type checking logics, e.g. checking
whether an extension is applicable to a concrete type. We used to directly
expose an header from sema called IDETypeChecking.h so that IDE functionalities
could invoke these APIs. The goal of the commit and following commits is to
expose evaluator requests instead of directly exposing function entry points from
sema so that we could later move IDETypeChecking.h to libIDE and implement these functions
by internally evaluating these requests.
2019-07-22 12:49:36 -07:00
pschuh
26c4cccb4b Convert InterpolatedStringLiteralExpr to not use tc.callWitness(). (#26076)
For reference, all other callers of callWitness have been migrated.
2019-07-16 10:24:45 -07:00
Slava Pestov
366f758d3c AST: Remove ObjectLiteralExpr::{get,set}SemanticExpr() 2019-07-12 14:10:47 -04:00
Rintaro Ishizaki
4d076e85c7 [CodeCompletion] Enable 'openArchetypes' when checking if convertible
```swift
protocol Proto {}
struct ConcreteProto {}
struct MyStruct<T> {}

extension MyStruct where T: Proto {
  static var option: MyStruct<ConcreteProto> { get }
}
func foo<T: Proto>(arg: MyStruct<T>) {}
func test() {
  foo(arg: .#^HERE^#)
}
```
In this case, the type of `MyStruct.option` is `MyStruct<ConcreteProto>`
whereas the context type is `MyStruct<T> where T: Proto`.
When checking the convertibility of them , we need to "open archetype types".

rdar://problem/24570603
rdar://problem/51723460
2019-06-28 15:25:52 -07:00
Pavel Yaskevich
dae19710f5 [ConstraintSystem] NFC: Remove duplicate code from LinkedExprAnalyzer 2019-06-21 09:21:26 -07:00
Rintaro Ishizaki
032c6e647e [CodeCompleiton] Don't hide members for unresolved base types
Don't filter out members if the base type has unresolved types.

Previously, initializers used to be hidden if the type has 'where'
requirements on the generic parameters.

This patch enables initializer completion for `SwiftUI.ForEach`.

rdar://problem/49480808
2019-06-18 12:18:27 -07:00
Slava Pestov
8ea650043d Merge pull request #25408 from pschuh/s-8
Convert DictionaryExpr to not use tc.callWitness() or generate a SemanticExpr.
2019-06-17 18:01:54 -04:00
Doug Gregor
82ed5e9a02 [SE-0258] Implement basic support for property wrapper composition.
When multiple property wrapper attributes are provided on a declaration,
compose them outside-in to form a composite property wrapper type. For
example,

  @A @B @C var foo = 17

will produce

  var $foo = A(initialValue: B(initialValue: C(initialValue: 17)))

and foo's getter/setter will access "foo.value.value.value".
2019-06-13 18:26:29 -07:00
Parker Schuh
0b03721ce8 Convert DictionaryExpr to not use tc.callWitness() or generate a SemanticExpr.
For reference, everything else except string interpolation has been
migrated to this new form.
2019-06-13 15:58:03 -07:00
Doug Gregor
e08f1fbe18 [DSL] Clean up assignment of types to synthesized TypeExprs.
The implicit TypeExprs for function builders were getting inconsistently set
types, which would sometimes be the metatype and sometimes not be the
metatype, leading to a crash in the new test code.
2019-06-11 17:34:45 -07:00
Doug Gregor
b7bbf4ca1a [Function builders] Allow uses of generic function builders.
Use the opened type from the callee declaration to open up references to
generic function builders that contain type parameters. This allows general
use of generic function builders.
2019-06-11 17:34:45 -07:00
Doug Gregor
6a3739ac65 Ensure that we use the right closure context for constraint generation. 2019-06-11 17:34:44 -07:00
Slava Pestov
c84aad8bf1 Sema: TypeChecker::conformsToProtocol() is static 2019-06-11 02:47:32 -07:00
Doug Gregor
c02ecf9859 [SE-0258] Rename to Property Wrappers 2019-05-29 22:17:50 -07:00
Slava Pestov
c947eda18f AST: Use ProtocolDecl::getAssociatedTypeMembers() where possible 2019-05-28 22:08:31 -04:00
Hamish Knight
8aa7401ba5 [Sema] Add getDeclOrNull to OverloadChoice
This helps clean up a bunch of call sites.
2019-05-16 12:07:41 +01:00
Doug Gregor
165133e3dd Merge pull request #24799 from DougGregor/disfavored-overload-attr
Add @_disfavoredOverload attribute to affect overload resolution.
2019-05-15 16:03:28 -07:00
Doug Gregor
5aea1315cd Add @_disfavoredOverload attribute to affect overload resolution.
Introduce an attribute @_disfavoredOverload that can be used to state
that a particular declaration should be avoided if there is a
successful type-check for a non-@_disfavoredOverload. It's a way to
nudge overload resolution away from particular solutions.
2019-05-14 23:07:26 -07:00
Slava Pestov
e65161ea2e Sema: Build type checked bodies for lazy getters 2019-05-14 19:18:58 -04:00
Slava Pestov
37412c2de2 AST: Add ParamDecl::toFunctionParam() 2019-05-10 15:00:49 -04:00
Slava Pestov
1dedba1f12 Sema: Pass argument match kind down to matchCallArguments() 2019-05-07 23:10:48 -04:00
swift-ci
d24bc38797 Merge pull request #23701 from DougGregor/property-delegates-as-custom-attributes 2019-04-24 22:51:18 -07:00
Pavel Yaskevich
a42bf065df [ConstraintSystem] Add a special locator for type of the key path expression 2019-04-23 15:17:24 -07:00
Doug Gregor
cc68b12d1a [SILGen] Initialization of instance properties with property delegates
The initialization of an instance property that has an attached
property delegate involves the initial value written on the property
declaration, the implicit memberwise initializer, and the default
arguments to the implicit memberwise initializer. Implement SILGen
support for each of these cases.

There is a small semantic change to the creation of the implicit
memberwise initializer due to SE-0242 (default arguments for the
memberwise initializer). Specifically, the memberwise initializer will
use the original property type for the parameter to memberwise
initializer when either of the following is true:

  - The corresponding property has an initial value specified with the
    `=` syntax, e.g., `@Lazy var i = 17`, or
  - The corresponding property has no initial value, but the property
    delegate type has an `init(initialValue:)`.

The specific case that changed is when a property has an initial value
specified as a direct initialization of the delegate *and* the
property delegate type has an `init(initialValue:)`, e.g.,

```swift
struct X {
  @Lazy(closure: { ... })
  var i: Int
}
```

Previously, this would have synthesized an initializer:

```swift
init(i: Int = ???) { ... }
```

However, there is no way for the initialization specified within the
declaration of i to be expressed via the default argument. Now, it
synthesizes an initializer:

```swift
init(i: Lazy<Int> = Lazy(closure: { ... }))
```
2019-04-23 11:31:59 -07:00
Doug Gregor
53b9e25502 Implement initialization of properties with attached delegates.
A property with an attached delegate can be initialized in one of two ways:

* By directly specifying initialization arguments on the attribute,
  e.g., "@SomeDelegate(x: 17, y: 42) var a".
* By initializing the original property itself, which goes through the
  delegate type's init(initialValue:) initializer, e.g.,
  "@Lazy var x = 17".

Implement support for both forms of initialization, including type
inference and checking for inconsistencies (e.g., if the annotation on
the property type doesn't match what the delegate type would
provide).
2019-04-23 11:31:58 -07:00
Brent Royal-Gordon
760d4be5e0 [ConstraintSystem] Add a KeyPathComponentResult locator path element
We’ll use this instead of the function result, except for subscript components, which are already structured similarly to subscript expressions.
2019-04-17 21:15:16 -07:00
Brent Royal-Gordon
44a472766d [ConstraintSystem] Add KeyPathComponentTypes map
Avoids digging through the long list of type vars to find the ones related to key path components when dumping an expression.
2019-04-17 21:15:16 -07:00
Brent Royal-Gordon
9cb896b828 [ConstraintSolver] Give type vars for keypath components useful locators
Previously, nearly all of the type vars for a key path component were dumped under the KeyPathExpr itself, whcih made them difficult to differentiate when debugging. Attach each one to “key path component #N -> function result” instead.

# Conflicts:
#	lib/Sema/CSGen.cpp
2019-04-17 14:34:45 -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
eed84abda9 Sema: Add TVO_CanBindToNoEscape
We have a systemic class of issues where noescape types end up bound to
type variables in places that should not. The existing diagnostic for
this is ad-hoc and duplicated in several places but it doesn't actually
address the root cause of the problem.

For now, I've changed all call sites of createTypeVariable() to set the
new flag. I plan on removing enough occurrences of the flag to replicate
the old diagnostics. Then we can continue to refine this over time.
2019-04-16 23:01:33 -04:00
Suyash Srijan
072e84acd6 [CSSimplify] Reject key path if root type is AnyObject (#23820)
Detect situations where `AnyObject` is attempted to be used as a root type of the key path
early and diagnose via new diagnostics framework.
2019-04-14 12:18:35 -07:00
Pavel Yaskevich
16b65018b4 Merge pull request #23436 from xedin/keypath-dynamic-lookup
[SE-0252][TypeChecker] Keypath Dynamic Member Lookup
2019-04-05 00:10:53 -07:00